l0wskilled / api-platform-test
用于简化ApiPlatform生成的端点测试的包。
v0.2.0
2022-07-19 10:51 UTC
Requires
- php: >=7.4
- ext-json: *
- api-platform/api-pack: ^1.2
- doctrine/orm: ^2.6
- hautelook/alice-bundle: ^2.1
- symfony/framework-bundle: ^4.3
- symfony/phpunit-bridge: ^4.3|^5|^6
- symfony/validator: ^4.3
Suggests
- doctrine/doctrine-bundle: to use the ORM extensions
- doctrine/mongodb-odm-bundle: to use the MongoDB ODM extensions
- dev-master
- v0.2.0
- v0.1.21
- v0.1.20
- 0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1
- dev-dependabot/composer/symfony/http-kernel-6.1.12
- dev-dependabot/composer/symfony/security-bundle-6.1.12
- dev-dependabot/composer/twig/twig-3.6.1
- dev-dependabot/composer/api-platform/core-3.1.12
This package is auto-updated.
Last update: 2024-09-21 16:48:53 UTC
README
用于简化ApiPlatform生成的端点测试的包。
你可能还想看看: https://github.com/epubli/api-platform-traits
使用方法
如果你想要为你的实体进行简单的CRUD测试,只需扩展ApiPlatformTestCase并重写端点和资源类
/** Endpoint to test (override in your testcase) */ protected const RESOURCE_URI = '/'; /** Entity class to test (override in your testcase) */ protected const RESOURCE_CLASS = '';
仅ORM
自v0.3.0以来,我们只支持ORM。
ORM示例测试类
class ExampleTest extends ApiPlatformTestCase { protected const RESOURCE_URI = '/api/example/'; protected const RESOURCE_CLASS = Example::class; protected function getTestEntity(): Example { $example = new Example(); $example->setLocale(static::$faker->countryCode()); $example->setStandard(false); $example->setTitle(static::$faker->country()); return $example; } }
使用此设置,将执行常见的CRUD测试。
如果你想调整某些测试用例的行为,只需重写默认测试用例
例如。
public function testReadAResourceCollection(): void { parent::testReadAResourceCollection(); $this->assertCollectionCount(3); } public function testReadAResource(): void { parent::testReadAResource(); $this->assertResourcePropertyCount(10); }
在测试过程中,实体将被symfony序列化器序列化。这可能会序列化你不想比较的属性,尤其是当你有循环引用时。
对于这种情况,你可以提供一个要忽略的属性列表。这些属性在序列化过程中将被跳过。
protected function getIgnoredAttributes(): array { return ['children']; }
如果你没有所有CRUD操作都可用,你可以通过重写不可用路由的测试用例来确保这一点。
public function testCreateAResource(): void { $this->testThrowErrorWhenRouteIsForbidden(); parent::testCreateAResource(); }