l0wskilled/api-platform-test

用于简化ApiPlatform生成的端点测试的包。

v0.2.0 2022-07-19 10:51 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();
    }