innmind/black-box-symfony

用于通过BlackBox测试Symfony应用的扩展

1.2.0 2023-12-02 10:53 UTC

This package is auto-updated.

Last update: 2024-08-31 00:52:08 UTC


README

Build Status Type Coverage

本包是 innmind/black-box 的扩展,旨在帮助测试 Symfony 应用程序。

安装

composer require innmind/black-box-symfony

使用方法

use Innmind\BlackBox\{
    Runner\Assert,
    Symfony\Application,
};

return static function() {
    yield test(
        'Login',
        function(Assert $assert) {
            $app = Application::new($assert); // This assumes the kernel class is 'App\Kernel'
            $response = $app
                ->json()
                ->post('/login', [
                    'username' => 'john',
                    'password' => 'doe',
                ]);
            $response
                ->statusCode()
                ->is(200);

            $content = $response->body()->json();
            $assert
                ->array($content)
                ->hasKey('token');
            $token = $content['token'];

            $app
                ->get('/me')
                ->statusCode()
                ->is(200);
        },
    );
};

基于模型的测试

通过将应用表示为一个独立对象,我们可以将其视为一个系统进行测试,并通过属性对其进行测试。

use Innmind\BlackBox\{
    Set,
    Property,
    Runner\Assert,
    Symfony\Application,
};

/**
 * @implements Property<Application>
 */
final class Login implements Property
{
    public static function any(): Set
    {
        return Set\Elements::of(new self);
    }

    public function applicableTo(object $app): bool
    {
        return true;
    }

    public function ensureHeldBy(Assert $assert, object $app): object
    {
        response = $app
            ->json()
            ->post('/login', [
                'username' => 'john',
                'password' => 'doe',
            ]);
        $response
            ->statusCode()
            ->is(200);

        $content = $response->body()->json();
        $assert
            ->array($content)
            ->hasKey('token');
        $token = $content['token'];

        $app
            ->get('/me')
            ->statusCode()
            ->is(200);

        return $app;
    }
}

您可以通过以下方式运行它:

use Innmind\BlackBox\{
    Set,
    Properties,
    Runner\Assert,
    Symfony\Application,
};

return static function() {
    yield proof(
        'Login',
        given(Login::any()),
        function(Assert $assert, Login $login) {
            $app = Application::new($assert);

            $login->ensureHeldBy($assert, $app);
        },
    );
    // and you can even test a sequence of properties to simulate a user actions
    yield proof(
        'No user interaction should crash the app',
        given(Set\Properties::any(
            Login::any(),
            AnotherProperty::any(),
            // etc...
        )),
        function(Assert $assert, Properties $properties) {
            $app = Application::new($assert);

            $properties->ensureHeldBy($assert, $app);
        },
    );
}

PHPUnit 模拟

BlackBox 能够运行PHPUnit测试,并且此扩展允许运行功能测试。为了实现这一点,您只需将 Symfony\Bundle\FrameworkBundle\Test\WebTestCase 前缀为 Innmind\BlackBox\

运行BlackBox的文件可能如下所示:

<?php
declare(strict_types = 1);

require 'vendor/autoload.php';

use Innmind\BlackBox\Application;
use Innmind\BlackBox\PHPUnit\Load;
use Symfony\Component\Dotenv\Dotenv;

(new Dotenv())->bootEnv('.env', 'test');

Application::new($argv)
    ->disableMemoryLimit()
    ->scenariiPerProof(1)
    ->tryToProve(Load::directory('tests/'))
    ->exit();

警告:不支持Symfony提供的自定义断言。