xentral/laravel-testing
Composer 安装命令:
composer require xentral/laravel-testing
包简介
A laravel package which provides workflows and tooling around testing
关键字:
README 文档
README
This package provides comprehensive testing tools for Laravel applications, including OpenAPI validation, Behat feature testing, and Qase test reporting integration.
Installation
You can install the package via composer.
composer --dev require xentral/laravel-testing
Usage
OpenAPI Validation
Automatically validate your API responses against OpenAPI schemas in your tests.
The Schema file will be determined in the following order:
- Using the
schemaFilePathproperty (if notnull) - Using the
SchemaFileattribute - Using the configured
testing.openapi.default_schemaconfig value
Relative paths will be auto prefixed with the project base path (by default)
Basic example with PHPUnit:
<?php #[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(); } }
Basic example with Pest: Pest does not support attributes (at least i couldn't figure it out). You need to use beforeEach to set the schema file path.
<?php 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 });
Behat Feature Testing
Write behavior-driven tests using Gherkin syntax alongside your PHPUnit tests. (Does not work with Pest yet)
Basic Usage:
// tests/Feature/MyEndpointTest.php <?php class MyEndpointTest extends TestCase { use HasBehatFeature; #[DataProvider('featureProvider')] public function test_behat_scenario($scenario, $feature) { $this->executeScenario($scenario, $feature); } }
Feature File:
# tests/Feature/MyEndpointTest.feature Feature: Basic example feature Scenario: example endpoint scenario Given There are 5 testModels When I send a GET request to path /api/v1/test-models Then the response status code should be 200 And the response should contain the following properties | path | value | | data | ~count~5 |
The example above will automatically load the feature file with the same name as the test class with .feature
extension.
This is our preferred behavior, but you can go another way by specifying the via the FeatureFile attribute on the
class:
// tests/Feature/MyEndpointTest.php <?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); } }
Interactive Matcher Discovery
The package includes a powerful interactive command to explore and search available Behat matchers:
php artisan xentral:list-behat-matchers
This command provides an interactive search interface where you can:
- Type to search through matcher patterns and descriptions in real-time
- Browse available matchers with their examples
- View detailed information about each matcher including capture groups
For non-interactive usage (useful in CI/CD):
php artisan xentral:list-behat-matchers --non-interactive
Adding Examples to Custom Matchers
Document your custom Behat matchers with examples using the #[Example] attribute:
<?php 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 } }
The Example attribute accepts:
stepText: The exact step text that should match the patternmatches(optional): Array of expected capture groups from the regexdata(optional): Additional metadata for the example
Testing Your Matchers
Ensure your custom matchers work correctly by adding tests using the BehatMatcherChecker:
<?php 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'), ) );
This test will:
- Find all matchers in the specified directory
- Validate that each
#[Example]attribute matches its corresponding matcher pattern - Verify that capture groups are correctly extracted
- Ensure regex patterns are valid
Qase Test Run Reporter
Integrate with Qase.io for comprehensive test reporting and management.
Mark a PHPUnit test class with the \Qase\PHPUnitReporter\Attributes\Suite attribute, then it will be automatically
reported to Qase.io when it is executed in testops mode.
PHPUnit Configuration:
<!-- phpunit.xml --> <phpunit> <extensions> <bootstrap class="Xentral\LaravelTesting\Qase\XentralQaseExtension"/> </extensions> </phpunit>
More information can be found in the Qase PHPUnit Reporter qase/phpunit-reporter.
Testing
composer test
Contributing
Ideas/Roadmap
Here are some ideas for future development:
- Add support for Behat in Pest.
- Add Support for Qase Test Suite in Pest
- Add support for Outlines in the BehatDumper
- Extend Qase test cases with complete behat scenario information if given
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email engineering@xentral.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
xentral/laravel-testing 适用场景与选型建议
xentral/laravel-testing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44.13k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel-testing」 「xentral」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xentral/laravel-testing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xentral/laravel-testing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xentral/laravel-testing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically test your laravel application
A laravel package which provides a smooth OAS workflow
A laravel package which provides a documentation workflow leveraging phpdocs and mkdocs
A Laravel package to help you write expressive tests
Quickly refresh your large database in tests.
Automatically test your laravel application
统计信息
- 总下载量: 44.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-17