PHP code example of xentral / laravel-testing

1. Go to this page and download the library: Download xentral/laravel-testing 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/ */

    

xentral / laravel-testing example snippets




#[SchemaFile('api-schema.yml')]
class MyEndpointTest extendsTestCase
{
    use \Xentral\LaravelTesting\OpenApi\ValidatesOpenApiSpec;
    
    public function test_api_returns_valid_response()
    {
        $response = $this->getJson('/api/users')->assertOk();
    }
}



uses(Xentral\LaravelTesting\OpenApi\ValidatesOpenApiSpec::class);

beforeEach(function () {
    $this->schemaFilePath('api-schema.yml');
});

test('API returns valid response', function () {
    $response = $this->getJson('/api/users')->assertOk(); // Automatically validates against OpenAPI schema
});

// tests/Feature/MyEndpointTest.php


class MyEndpointTest extends TestCase
{
    use HasBehatFeature;

    #[DataProvider('featureProvider')]
    public function test_behat_scenario($scenario, $feature)
    {
        $this->executeScenario($scenario, $feature);
    }
}

// tests/Feature/MyEndpointTest.php


#[FeatureFile(__DIR__.'/custom-feature.feature')]
class MyEndpointTest extends TestCase
{
    use HasBehatFeature;

    #[DataProvider('featureProvider')]
    public function test_behat_scenario($scenario, $feature)
    {
        $this->executeScenario($scenario, $feature);
    }
}



use Xentral\LaravelTesting\Behat\Attributes\Example;

class MyCustomMatchers
{
    #[Given('/^I send (a|an invalid|a non-API) ([^\s]+) request to path ([^\s]+)(?:\s+(.+))?$/i')]
    #[Example('I send a GET request to path /api/users', ['a', 'GET', '/api/users'])]
    #[Example('I send a POST request to path /api/users with payload', ['a', 'POST', '/api/users', 'with payload'])]
    public function iSendARequest($type, $method, $path, $modifiers = null)
    {
        // Implementation
    }
}



use Xentral\LaravelTesting\Behat\BehatMatcherChecker;
use Xentral\LaravelTesting\Behat\BehatMatcherFinder;
use Xentral\LaravelTesting\Behat\Dto\BehatMatcher;

test('all example attributes match their declared matchers', function (BehatMatcher $matcher) {
    BehatMatcherChecker::check($matcher);
})->with(
    fn () => array_map(
        fn (BehatMatcher $matcher) => [$matcher],
        BehatMatcherFinder::find('path/to/your/matchers'),
    )
);
bash
php artisan xentral:list-behat-matchers
bash
php artisan xentral:list-behat-matchers --non-interactive