Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Tests/Dumper/MermaidDumperTest.php (PHP)

Code before:
        $this->assertStringContainsString('ROLE_MANAGER --> ROLE_USER', $output);
        $this->assertStringContainsString('ROLE_EDITOR --> ROLE_USER', $output);
    }

    public function testInvalidDirection()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Direction "INVALID" is not valid');

        new MermaidDumper('INVALID');
    }

    public function testValidDirections()
    {
        $validDirections = [
            MermaidDumper::DIRECTION_TOP_TO_BOTTOM,
            MermaidDumper::DIRECTION_TOP_DOWN,
            MermaidDumper::DIRECTION_BOTTOM_TO_TOP,
            MermaidDumper::DIRECTION_RIGHT_TO_LEFT,
            MermaidDumper::DIRECTION_LEFT_TO_RIGHT,
        ];

        foreach ($validDirections as $direction) {
            $dumper = new MermaidDumper($direction);
            $this->assertInstanceOf(MermaidDumper::class, $dumper);
        }
    }

    public function testRoleNameEscaping()
    {
        $hierarchy = [
            'ROLE_ADMIN-TEST' => ['ROLE_USER.SPECIAL'],
        ];

        $roleHierarchy = new RoleHierarchy($hierarchy);
        $dumper = new MermaidDumper();
        $output = $dumper->dump($roleHierarchy);

        $this->assertStringContainsString('ROLE_ADMIN_TEST', $output);
        $this->assertStringContainsString('ROLE_USER_SPECIAL', $output);
        $this->assertStringContainsString('ROLE_ADMIN_TEST --> ROLE_USER_SPECIAL', $output);
    }
}


Review comment (suggestion):
There's no point to instantiate an object then assert its type. If you want to test valid directions, I'd suggest instead to use `@testWith`, instantiate the dumper and `$this->expectNoAssertion()`. Also, `testWith` will force you to use the raw values of constants, which is nice because tests will break (and warn) if someone updates one of the values

Code after:
        $this->assertStringContainsString('ROLE_EDITOR --> ROLE_USER', $output);
    }

    public function testInvalidDirection()
    {
        $this->expectException(\TypeError::class);

        $dumper = new MermaidDumper();
        $dumper->dump(new RoleHierarchy([]), 'INVALID');
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderValidDirection')]
    public function testValidDirections(MermaidDirectionEnum $direction)
    {
        $this->expectNotToPerformAssertions();
        $dumper = new MermaidDumper();
        $dumper->dump(new RoleHierarchy([]), $direction);
    }

    public static function dataProviderValidDirection()
    {
        return [
            [MermaidDirectionEnum::TOP_TO_BOTTOM],
            [MermaidDirectionEnum::TOP_DOWN],
            [MermaidDirectionEnum::BOTTOM_TO_TOP],
            [MermaidDirectionEnum::RIGHT_TO_LEFT],
            [MermaidDirectionEnum::LEFT_TO_RIGHT],
        ];
    }

    public function testRoleNameEscaping()
    {
        $hierarchy = [
            'ROLE_ADMIN-TEST' => ['ROLE_USER.SPECIAL'],
        ];

        $roleHierarchy = new RoleHierarchy($hierarchy);
        $dumper = new MermaidDumper();
        $output = $dumper->dump($roleHierarchy);

        $this->assertStringContainsString('ROLE_ADMIN_TEST', $output);
        $this->assertStringContainsString('ROLE_USER_SPECIAL', $output);
        $this->assertStringContainsString('ROLE_ADMIN_TEST --> ROLE_USER_SPECIAL', $output);
    }
}