kununu / testing-bundle
通过提供辅助类和固定加载,使测试变得简单
Requires
- php: >=8.3
- kununu/data-fixtures: ^12.0
- symfony/config: ^5.4|^6.4
- symfony/dependency-injection: ^5.4|^6.4
- symfony/framework-bundle: ^5.4|^6.4
- symfony/http-kernel: ^5.4|^6.4
Requires (Dev)
- ext-json: *
- ext-pdo: *
- doctrine/dbal: ^3.8
- doctrine/doctrine-bundle: ^2.7
- doctrine/doctrine-migrations-bundle: ^3.3
- doctrine/orm: ^3.0
- elasticsearch/elasticsearch: ^7.10
- kununu/scripts: >=5.0
- matthiasnoback/symfony-dependency-injection-test: ^6.0
- phpunit/phpunit: ^11.3
- psr/cache: ^1.0|^2.0
- symfony/browser-kit: ^6.4
- symfony/dotenv: ^6.4
- symfony/http-client: ^5.4|^6.4
- symfony/http-foundation: ^5.4|^6.4
Suggests
- ext-pdo_mysql: To run Integration Tests.
- doctrine/doctrine-bundle: To run Integration Tests.
- elasticsearch/elasticsearch: To run Integration Tests.
- psr/cache: To run Integration Tests.
- symfony/http-client: To run Integration Tests.
- symfony/http-foundation: To run Integration Tests.
- dev-master
- v19.0.0
- v18.0.0
- v17.4.1
- v17.4.0
- v17.3.0
- v17.2.0
- v17.1.0
- v17.0.0
- v16.1.0
- v16.0.1
- v16.0.0
- v15.0.0
- v14.0.2
- v14.0.1
- v14.0.0
- v13.0.1
- v13.0.0
- 12.0.0
- v11.0.0
- v10.0.0
- v9.0.0
- 8.0.2
- 8.0.1
- 8.0.0
- 7.0.1
- v7.0.0
- v6.0.0
- v5.0.0
- 4.0.0
- 3.0.0
- 2.1.0
- 2.0.0
- 1.0.0
- dev-support_php83_only
- dev-dependabot/composer/doctrine/dbal-tw-4.1
- dev-dependabot/composer/matthiasnoback/symfony-dependency-injection-test-tw-6.0
- dev-dependabot/composer/symfony/dotenv-tw-7.1
This package is auto-updated.
Last update: 2024-09-18 10:05:22 UTC
README
此捆绑包与 kununu/data-fixtures 包集成,允许您在测试中加载固定数据。它还提供了一些使测试更简单的实用工具,例如将测试控制器更具有表现力的 RequestBuilder
。如果您想看看这个捆绑包能为您做什么,请点击 这里。
安装
1. 将 kununu/testing-bundle 添加到您的项目中
请注意,此捆绑包不应在生产模式下使用!
composer require --dev kununu/testing-bundle
2. 启用捆绑包
在任何环境中启用捆绑包,请参阅 config/bundles.php
。
<?php return [ ... Kununu\TestingBundle\KununuTestingBundle::class => ['dev' => true, 'test' => true], ];
配置
在 config/packages/test/
内创建 kununu_testing.yaml
文件。捆绑包的配置选项高度依赖于固定数据类型。请查看 加载固定数据 部分,其中包含更多选项。
提示 如果您在多个环境中使用此捆绑包,例如 dev 和 test,并且配置选项完全相同,您可以通过以下方式导入 kununu_testing.yaml
以避免重复配置。
# config/packages/dev/kununu_testing.yaml kununu_testing: cache: pools: app.cache.first: load_command_fixtures_classes_namespace: - 'Kununu\TestingBundle\Tests\App\Fixtures\CachePool\CachePoolFixture1'
# config/packages/test/kununu_testing.yaml imports: - { resource: '../dev/kununu_testing.yaml' }
加载固定数据
此捆绑包与 kununu/data-fixtures 集成,允许您在测试中加载固定数据。目前,此捆绑包支持以下类型的固定数据
Schema Copier
此捆绑包还有从数据库复制数据库模式的方法。
查看更多
发送请求
请求构建器
此捆绑包提供请求构建器,它使调用端点更具表现力。
// Creates and returns a Builder that you can use to do a GET request public static function aGetRequest(): self; // Creates and returns a Builder that you can use to do a POST request public static function aPostRequest(): self; // Creates and returns a Builder that you can use to do a DELETE request public static function aDeleteRequest(): self; // Creates and returns a Builder that you can use to do a PUT request public static function aPutRequest(): self; // Creates and returns a Builder that you can use to do a PATCH request public static function aPatchRequest(): self; // Set The Request parameters public function withParameters(array $parameters): self; // Change The request method public function withMethod(string $method): self; // Set the URI to fetch public function withUri(string $uri): self; // Set the content of the request as an array that internally is transformed to a json and provided as the raw body data public function withContent(array $content): self; // Set the Raw body data public function withRawContent(string $content): self; // Sets an HTTP_AUTHORIZATION header with the value of "Bearer $token" public function withAuthorization(string $token): self; // Sets an header. // In converts any header name to uppercase and prepends "HTTP_" if the header name does not contains it public function withHeader(string $headerName, string $headerValue): self; // Sets a server parameter (HTTP headers are referenced with an HTTP_ prefix as PHP does) public function withServerParameter(string $parameterName, string $parameterValue): self;
WebTestCase
此捆绑包公开了一个可扩展的 WebTestCase,它公开了一个帮助您测试控制器而不必担心创建内核的方法。此类还允许您在测试中加载固定数据。
final protected function doRequest(RequestBuilder $builder): Symfony\Component\HttpFoundation\Response
内部,此方法通过以下方式调用 Symfony 客户端:
$client->request($builder->method, $builder->uri, $builder->parameters, $builder->files, $builder->server, $builder->content);
示例
让我们假设您有一个名为 company_create 的路由,它是受保护的(需要提供有效的访问令牌),并期望在请求体中提供一个 json,其中包含创建新公司所需的数据。
# routes.yaml company_create: path: /companies controller: App\Controller\CompaniesController::createAction methods: [POST]
使用此捆绑包提供的概念,例如 加载固定数据、请求构建器 和 WebTestCase,我们的测试可能如下所示:
<?php namespace App\Tests\Integration\Controller; use App\Tests\Integration\Controller\DataFixtures\MySQL\CreateCompanyDataFixtures; use Kununu\TestingBundle\Test\RequestBuilder; use Kununu\TestingBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Response; class CompaniesControllerTest extends WebTestCase { public function testCreateCompany(): void { $this->loadDbFixtures('your_doctrine_connection_name', [CreateCompanyDataFixtures::class]); $data = [ 'name' => 'kununu GmbH', 'location' => [ 'city' => 'Wien', 'country_code' => 'at', ], ]; $response = $this->doRequest( RequestBuilder::aPostRequest() ->withUri('/companies') ->withContent($data) ->withAuthorization('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjYyZDVkNzc5NmQxOTk') ->withServerParameter('REMOTE_ADDR', '127.0.0.1') ); $this->assertNotNull($response->getContent()); $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); $json = $response->getContent(); $this->assertJson($json); $company = json_decode($json, true); $this->assertSame($data['name'], $company['name']); $this->assertSame($data['location']['city'], $company['location']['city']); $this->assertSame($data['location']['country_code'], $company['location']['country_code']); } }
贡献
如果您有兴趣贡献,请阅读我们的 贡献指南。
测试
此存储库利用 GitHub actions 在向分支提交时运行测试。
如果您想在本地计算机上运行集成测试,您将需要
- pdo_mysql 扩展
- MySQL 服务器
- Elasticsearch 集群
为了在本地环境中为您准备好一切,请运行 ./tests/setupLocalTests.sh
并按照指示操作。然后您就可以运行测试了: vendor/bin/phpunit
。