PHPackages                             innmind/black-box-symfony - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. innmind/black-box-symfony

ActiveLibrary[Testing &amp; Quality](/categories/testing)

innmind/black-box-symfony
=========================

Extension to test Symfony apps via BlackBox

1.4.0(3mo ago)11.9k↓50%[1 issues](https://github.com/Innmind/black-box-symfony/issues)MITPHPPHP ~8.2CI passing

Since Jul 9Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Innmind/black-box-symfony)[ Packagist](https://packagist.org/packages/innmind/black-box-symfony)[ Docs](http://github.com/innmind/black-box-symfony)[ RSS](/packages/innmind-black-box-symfony/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (5)Dependencies (8)Versions (12)Used By (0)

black-box-symfony
=================

[](#black-box-symfony)

[![Build Status](https://github.com/innmind/black-box-symfony/workflows/CI/badge.svg?branch=main)](https://github.com/innmind/black-box-symfony/actions?query=workflow%3ACI)[![Type Coverage](https://camo.githubusercontent.com/f47e94694a053a6e90aa2e632357ad44c2c991650f62396264776e4a9fefc208/68747470733a2f2f73686570686572642e6465762f6769746875622f696e6e6d696e642f626c61636b2d626f782d73796d666f6e792f636f7665726167652e737667)](https://shepherd.dev/github/innmind/black-box-symfony)

This package is an extension of [`innmind/black-box`](https://packagist.org/packages/innmind/black-box) to help test [Symfony](https://symfony.com) applications.

Installation
------------

[](#installation)

```
composer require innmind/black-box-symfony
```

Usage
-----

[](#usage)

```
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);
        },
    );
};
```

### Model Based Testing

[](#model-based-testing)

By representing the application as a standalone object we can consider it as a system to test and so test it via properties.

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

/**
 * @implements Property
 */
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;
    }
}
```

And you would run it via:

```
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 emulation

[](#phpunit-emulation)

BlackBox is able to run PHPUnit tests and this extension allows to run functional tests. For this to work you only need to prefix the `Symfony\Bundle\FrameworkBundle\Test\WebTestCase` by `Innmind\BlackBox\`.

And the file to run BlackBox would look like:

```
