l0wskilled / api-platform-test
Bundle to ease the test of Endpoints generated by ApiPlatform.
Installs: 1 597
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 4
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: 2025-02-21 17:50:50 UTC
README
Bundle to ease the test of Endpoints generated by ApiPlatform.
You may want to have a look at: https://github.com/epubli/api-platform-traits
Usage
If you want a simple CRUD Testing for your entity just extend the ApiPlatformTestCase and override the endpoint and the resource class
/** Endpoint to test (override in your testcase) */ protected const RESOURCE_URI = '/'; /** Entity class to test (override in your testcase) */ protected const RESOURCE_CLASS = '';
ORM only
Since v0.3.0 we only support ORM.
ORM Example Test class
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; } }
With this setup common CRUD-Tests will be executed.
If you want to adjust the behaviour of some Test-Cases just override the default Test-Case
For e.g.
public function testReadAResourceCollection(): void { parent::testReadAResourceCollection(); $this->assertCollectionCount(3); } public function testReadAResource(): void { parent::testReadAResource(); $this->assertResourcePropertyCount(10); }
During the testing the entities get serialized by the symfony serializer. This can serialize attributes you don't want to compare, especially if you have circular references.
For this case you can provide a list of ignored attributes. These will be skipped during serialization.
protected function getIgnoredAttributes(): array { return ['children']; }
If you do not have all CRUD Operations available you can override the unsupported test with a route not available test to ensure this.
public function testCreateAResource(): void { $this->testThrowErrorWhenRouteIsForbidden(); parent::testCreateAResource(); }