Repository: symfony/symfony
File: src/Symfony/Bundle/SecurityBundle/Tests/Command/SecurityRoleHierarchyDumpCommandTest.php (PHP)

Code before:
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bundle\SecurityBundle\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Command\SecurityRoleHierarchyDumpCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Security\Core\Role\RoleHierarchy;

class SecurityRoleHierarchyDumpCommandTest extends TestCase
{
    public function testExecuteWithNoRoleHierarchy()
    {
        $command = new SecurityRoleHierarchyDumpCommand();
        $commandTester = new CommandTester($command);

        $exitCode = $commandTester->execute([]);

        $this->assertEquals(Command::SUCCESS, $exitCode);
        $this->assertStringContainsString('No role hierarchy is configured', $commandTester->getDisplay());
    }

    public function testExecuteWithRoleHierarchy()
    {
        $hierarchy = [
            'ROLE_ADMIN' => ['ROLE_USER'],
            'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN', 'ROLE_USER'],
        ];

        $roleHierarchy = new RoleHierarchy($hierarchy);
        $command = new SecurityRoleHierarchyDumpCommand($roleHierarchy);
        $commandTester = new CommandTester($command);

        $exitCode = $commandTester->execute([]);

        $this->assertEquals(Command::SUCCESS, $exitCode);
        $output = $commandTester->getDisplay();
        $expectedOutput = <<<EXPECTED
graph TB
    ROLE_ADMIN
    ROLE_USER
    ROLE_SUPER_ADMIN
    ROLE_ADMIN --> ROLE_USER
    ROLE_SUPER_ADMIN --> ROLE_ADMIN

Review comment (suggestion):
```suggestion
        $this->assertSame(Command::SUCCESS, $exitCode);
```

Code after:
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bundle\SecurityBundle\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Command\SecurityRoleHierarchyDumpCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Security\Core\Role\RoleHierarchy;

class SecurityRoleHierarchyDumpCommandTest extends TestCase
{
    public function testExecuteWithRoleHierarchy()
    {
        $hierarchy = [
            'ROLE_ADMIN' => ['ROLE_USER'],
            'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN', 'ROLE_USER'],
        ];

        $roleHierarchy = new RoleHierarchy($hierarchy);
        $command = new SecurityRoleHierarchyDumpCommand($roleHierarchy);
        $commandTester = new CommandTester($command);

        $exitCode = $commandTester->execute([]);

        $this->assertSame(Command::SUCCESS, $exitCode);
        $output = $commandTester->getDisplay();
        $expectedOutput = <<<EXPECTED
            graph TB
                ROLE_ADMIN
                ROLE_USER
                ROLE_SUPER_ADMIN
                ROLE_ADMIN --> ROLE_USER
                ROLE_SUPER_ADMIN --> ROLE_ADMIN
                ROLE_SUPER_ADMIN --> ROLE_USER

            EXPECTED;

        $this->assertSame($expectedOutput, $output);
    }

    public function testExecuteWithCustomDirection()
    {
        $hierarchy = [
            'ROLE_ADMIN' => ['ROLE_USER'],
        ];