benblub/ftg

Api Platform 的功能测试生成器

维护者

详细信息

github.com/benblub/ftg

源代码

问题

安装: 388

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.1-beta 2021-04-28 20:44 UTC

This package is auto-updated.

Last update: 2024-09-26 05:11:43 UTC


README

功能测试生成器。

需求

安装

composer require benblub/ftg "dev-main"

配置 Api Platform / Symfony / Foundry

目前还没有自动配置...

添加到 config/bundles.php

Benblub\Ftg\BenblubFtgBundle::class => ['dev' => true],

添加到 services.yaml

    Benblub\Ftg\Bundle\Maker\MakeFunctionalTest:
        tags: ['maker.command']

Foundry

此生成器使用 Foundry 工厂。对于每个我们生成的 Testclass,我们都需要一个工厂。创建您的工厂 php bin/console make:factory User --test 并设置默认值。默认值至少包括您实体中所有必需的字段。

将 myDefaults 方法添加到您的工厂中

    public static function myDefaults(): array
    {
        $class = new self();
        
        return $class->getDefaults();
    }

扩展 ApiTestCase / 实现 AuthHelperInterface

您的测试类可以扩展从 ApiPlatform 扩展的任何类。要使用 Auth,您需要像示例中那样实现 AuthHelperInterface。也需要设置 "custom_auth: true" 配置(配置尚未实现)

否则,您可以使用 AuthHelper 的默认值(使用 id 作为标识符)

<?php

namespace App\Test;

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;


class AuthHelper extends ApiTestCase implements AuthHelperInterface
{
    protected Client $client;
    protected array $identifier; // can be id, email or whatever is used as identifier

    public function setUp(): void
    {
        $this->client = self::createClient();
    }

    public function setIdentifier(array $identifier)
    {
        $this->identifier = $identifier;
    }

    /**
     * Set here whatever your config is from lexik_jwt_authentication.yaml <user_identity_field>
     * user_identity_field: email|username|id (your Provider must support it eg loadUserBy..)
     *
     * After Create a User in a test call this Method and make requests with this User authenticated
     */
    public function setAuthenticationHeader()
    {
        $arrayKey = array_key_first($this->identifier);
        $token = $this->getUserToken($this->client, $this->identifier[$arrayKey]);
        $this->client->setDefaultOptions([
            'headers' => [
                'Authorization' => 'Bearer ' . $token,
            ],
        ]);
    }

    /**
     * Generate our Bearer Token
     */
    public function getUserToken(Client $client, string $identifier): string
    {
        $data = $this->identifier;

        return $client
            ->getContainer()
            ->get('lexik_jwt_authentication.encoder')
            ->encode($data);
    }
}

使用

允许 CRUD 测试 php bin/console make:ftg
拒绝 CRUD 作为匿名测试 php bin/console make:ftg --deny=deny
拒绝 CRUD 作为 %role% 对其他用户 php bin/console make:ftg --deny=deny --other=Other

交互式问题
问题:认证用户的角色,例如 user、admin 或其他
输入用于创建此测试的 ROLE 类型。user 用于 ROLE_USER,admin 用于 ROLE_ADMIN 或所有其他角色。any 表示不会设置认证头。

问题:创建功能测试的实体类
选择您想要测试的实体

为什么使用此扩展包

创建功能 CRUD 测试对于所有实体通常是相同的,并且跨不同的项目。使用生成器有多种好处。

  • 测试看起来相同
  • 不再无聊地重复编写相同的代码
  • 加速编写测试,并专注于测试应用程序的各个部分
  • 轻松替换测试,如果新版本/改进可用