PHP code example of stromcom / http-smoke

1. Go to this page and download the library: Download stromcom/http-smoke library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stromcom / http-smoke example snippets




declare(strict_types=1);

use Stromcom\HttpSmoke\Definition\Suite;

return static function (Suite $suite): void {
    $suite->group('api.public', maxFailures: 3)
        ->baseUrl('{API_BASE_URL}')

        ->get('/ping')
            ->expectStatus(200)
            ->expectJsonPath('status', 'ok')

        ->get('/version')
            ->expectStatus(200)
            ->expectJsonHasKeys(['version', 'commit']);
};

$suite->header('Authorization', '{API_TOKEN}'); // suite-level default header
$suite->asJson();                                // suite-level: send all bodies as JSON

$suite->group('api.users', maxFailures: 3)
    ->baseUrl('{API_BASE_URL}')
    ->header('X-Tenant', 'smoke')
    ->defaultTimeout(5)
    ->defaultRetryOnFailure(10, 50)              // up to 10 retries, 50 ms apart
    // ->defaultRetries(10, 50)                  // shorter alias for the above
    ->defaultAsJson()

    // Sessions: shared cookie jar, sequential execution, fail-fast
    ->session('user-lifecycle')
        ->post('/users/', ['email' => '[email protected]'])
            ->expectStatus(201)
            ->captureJsonPath('userHash', 'data.hash')
        ->get('/users/{@userHash}/')
            ->expectStatus(200)
        ->delete('/users/{@userHash}/')
            ->expectStatus(204)
    ->endSession()

    // Independent (parallel) requests
    ->get('/users/')
        ->expectStatus(200)
        ->expectJson()
        ->expectJsonHasKeys(['data', 'meta.count'])
        ->expectJsonPath('status', 'success')

    ->put('{@externalUploadUrl}', file_get_contents('photo.jpg'))
        ->noGroupHeaders()                       // skip group Authorization header
        ->asJson(false)                          // raw body
        ->expectStatus(200)

    ->get('/dashboard')
        ->expectStatus(200)
        ->expectHtmlElement('h1', 'Dashboard')         // <h1>Dashboard</h1>
        ->expectHtmlElement('a', null, 'href', '/logout')
        ->expectHtmlElement('meta', null, 'name', 'viewport')

    ->get('/health')
        ->expectHeaderContains('Cache-Control', 'no-store')
        ->expect(fn (Stromcom\HttpSmoke\Http\Response $r) =>
            json_decode($r->body, true)['queue'] > 0 ? 'queue should be empty' : null
        );



declare(strict_types=1);

use Stromcom\HttpSmoke\Config\SmokeConfig;
use Stromcom\HttpSmoke\Container\Container;
use Stromcom\HttpSmoke\Variable\Source\ArraySource;

return static function (SmokeConfig $config): void {
    $config->configDir         = __DIR__ . '/tests/SmokeHttp';
    $config->concurrency       = 10;
    $config->jsonOutputPath    = __DIR__ . '/build/smoke.json';
    $config->markdownOutputPath = __DIR__ . '/build/smoke.md';

    // Plug in additional variable sources
    $config->extraVariableSources[] = new ArraySource([
        'CUSTOM_KEY' => 'value',
    ]);

    // Override services in the DI container
    $config->configureContainer = function (Container $container): void {
        // $container->set(HttpClientInterface::class, fn() => new MyClient());
    };
};

final class AwsSecretsSource implements VariableSourceInterface
{
    public function get(string $name): ?string { /* ... */ }
    public function all(): array { /* ... */ }
}

// In smoke.config.php:
$config->extraVariableSources[] = new AwsSecretsSource(...);