eerison / pest-plugin-api-platform
此软件包已废弃,不再维护。未建议替代包。
Pest 插件用于 Api 平台
v1.1.0
2021-11-14 13:23 UTC
Requires
- php: ^7.4 || ^8.0
- api-platform/core: ^2.6
- pestphp/pest: ^1.0
- pestphp/pest-plugin: ^1.0
Requires (Dev)
- pestphp/pest-dev-tools: dev-master
- sensio/framework-extra-bundle: ^6.1
- spatie/pest-plugin-snapshots: ^1.1
- symfony/browser-kit: ^5.3
- symfony/framework-bundle: ^5.3
- symfony/http-client: ^5.3
- symfony/yaml: ^5.3
This package is auto-updated.
Last update: 2022-08-26 15:52:56 UTC
README
此软件包为 Pest 添加了 Api platform 测试功能。
安装
您可以通过 composer 安装此软件包
composer require eerison/pest-plugin-api-platform --dev
在您的 tests/Pest.php
中添加 uses(ApiTestCase::class)
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; uses(ApiTestCase::class)->beforeEach(fn() => static::bootKernel())->in('Feature');
使用方法
it('is checking the body structure') ->group('response') ->get('/foo/response/200') ->expectResponseContent() ->json() ->toHaveKey('name', 'Erison') ->toHaveKey('company.name', 'Fake company');
或者您可以通过导入函数使用
use function Eerison\PestPluginApiPlatform\get; it('is checking the body structure using context.', function () { $responseContent = get('/foo/response/200')->getContent(); expect($responseContent) ->json() ->toHaveKey('company.address') ; });
使用 findIriBy
use function Eerison\PestPluginApiPlatform\{get, findIriBy}; it('can use findIriBy', function () { $iri = findIriBy(YourEntity::class, ['yourField' => 'yourValue']); $responseContent = get($iri)->getContent(); expect($responseContent) ->json() ->toHaveKey('company.address') ; });
使用快照(请安装 pest-plugin-snapshots)
it('can be used with snapshot') ->group('response') ->get('/foo/response/200') ->expectResponseContent() ->json() ->toMatchJsonSnapshot();
将 Api platform 测试转换为 pest
之前
<?php // api/tests/BooksTest.php namespace App\Tests; use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; use App\Entity\Book; class BooksTest extends ApiTestCase { // This trait provided by AliceBundle will take care of refreshing the database content to a known state before each test use RefreshDatabaseTrait; public function testGetCollection(): void { // The client implements Symfony HttpClient's `HttpClientInterface`, and the response `ResponseInterface` $response = static::createClient()->request('GET', '/books'); $this->assertResponseIsSuccessful(); // Asserts that the returned JSON is a superset of this one $this->assertJsonContains([ '@context' => '/contexts/Book', '@id' => '/books', '@type' => 'hydra:Collection', 'hydra:totalItems' => 100, 'hydra:view' => [ '@id' => '/books?page=1', '@type' => 'hydra:PartialCollectionView', 'hydra:first' => '/books?page=1', 'hydra:last' => '/books?page=4', 'hydra:next' => '/books?page=2', ], ]); // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform // This generated JSON Schema is also used in the OpenAPI spec! $this->assertMatchesResourceCollectionJsonSchema(Book::class); }
之后
use App\Entity\Book; use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait; uses(RefreshDatabaseTrait::class); it('can get a collection') ->get('/books') ->assertResponseIsSuccessful() ->expectResponseContent() ->json() ->toHaveKey('@context', '/contexts/Book') ->toHaveKey('@id', '/books') ->toHaveKey('@type', 'hydra:Collection') ->toHaveKey('hydra:totalItems', 100) ->toHaveKey('hydra:view.@id', '/books?page=1') ->toHaveKey('hydra:view.@type', 'hydra:PartialCollectionView') ->toHaveKey('hydra:first', '/books?page=1') ->toHaveKey('hydra:last', '/books?page=4') ->toHaveKey('hydra:next', '/books?page=2') ->toMatchesResourceCollectionJsonSchema(Book::class) ;
预期
toMatchesResourceCollectionJsonSchema(Your::class)
toMatchesResourceItemJsonSchema(Your::class)
函数
apiClient()
get()
post()
put()
delete()
findIriBy()
assertResponseIsSuccessful()
assertResourceIsBadRequest()
assertResourceIsNotFound()
assertResourceIsUnauthorized()
assertResourceIsForbidden()
assertMatchesResourceItemJsonSchema(Your::class)
assertMatchesResourceCollectionJsonSchema(Your::class)
assertResourceIsBadRequest()
assertResourceIsUnprocessableEntity()
expectResponseContent()
如果您想测试
expectResponseContent
而不抛出异常,请将参数传递为false
,例如:expectResponseContent(false)
测试
composer test
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。