<?php

/*
 * This file is part of the Symfony package.
 *
 * (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\Component\DependencyInjection\Tests\Dumper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\GraphvizDumper;
use Symfony\Component\DependencyInjection\Reference;

class GraphvizDumperTest extends TestCase
{
    protected static string $fixturesPath;

    public static function setUpBeforeClass(): void
    {
        self::$fixturesPath = realpath(__DIR__.'/../Fixtures');
    }

    public function testDump()
    {
        $dumper = new GraphvizDumper($container = new ContainerBuilder());

        $this->assertStringEqualsGeneratedFile('services1.dot', $dumper->dump(), '->dump() dumps an empty container as an empty dot file');

        $container = include self::$fixturesPath.'/containers/container9.php';
        $dumper = new GraphvizDumper($container);
        $this->assertStringEqualsGeneratedFile('services9.dot', $dumper->dump(), '->dump() dumps services');

        $container = include self::$fixturesPath.'/containers/container10.php';
        $dumper = new GraphvizDumper($container);
        $this->assertStringEqualsGeneratedFile('services10.dot', $dumper->dump(), '->dump() dumps services');

        $container = include self::$fixturesPath.'/containers/container10.php';
        $dumper = new GraphvizDumper($container);
        $this->assertEquals($dumper->dump([
            'graph' => ['ratio' => 'normal'],
            'node' => ['fontsize' => 13, 'fontname' => 'Verdana', 'shape' => 'square'],
            'edge' => ['fontsize' => 12, 'fontname' => 'Verdana', 'color' => 'white', 'arrowhead' => 'closed', 'arrowsize' => 1],
            'node.instance' => ['fillcolor' => 'green', 'style' => 'empty'],
            'node.definition' => ['fillcolor' => 'grey'],
            'node.missing' => ['fillcolor' => 'red', 'style' => 'empty'],
        ]), file_get_contents(self::$fixturesPath.'/graphviz/services10-1.dot'), '->dump() dumps services');
    }

    public function testDumpWithFrozenContainer()
    {
        $container = include self::$fixturesPath.'/containers/container13.php';
        $dumper = new GraphvizDumper($container);
        $this->assertStringEqualsGeneratedFile('services13.dot', $dumper->dump(), '->dump() dumps services');
    }

    public function testDumpWithFrozenCustomClassContainer()
    {
        $container = include self::$fixturesPath.'/containers/container14.php';
        $dumper = new GraphvizDumper($container);
        $this->assertStringEqualsGeneratedFile('services14.dot', $dumper->dump(), '->dump() dumps services');
    }

    public function testDumpWithUnresolvedParameter()
    {
        $container = include self::$fixturesPath.'/containers/container17.php';
        $dumper = new GraphvizDumper($container);

        $this->assertStringEqualsGeneratedFile('services17.dot', $dumper->dump(), '->dump() dumps services');
    }

    public function testDumpWithInlineDefinition()
    {
        $container = new ContainerBuilder();
        $container->register('foo', 'stdClass')->addArgument(
            (new Definition('stdClass'))->addArgument(new Reference('bar'))
        );
        $container->register('bar', 'stdClass');
        $dumper = new GraphvizDumper($container);

        $this->assertStringEqualsGeneratedFile('services_inline.dot', $dumper->dump(), '->dump() dumps nested references');
    }

    private static function assertStringEqualsGeneratedFile(string $expectedFile, string $dumpedCode): void
    {
        $expectedFile = self::$fixturesPath.'/graphviz/'.$expectedFile;

        if ($_ENV['TEST_GENERATE_FIXTURES'] ?? false) {
            file_put_contents($expectedFile, $dumpedCode);
            self::markTestIncomplete('TEST_GENERATE_FIXTURES is set');
        }

        self::assertStringEqualsFile($expectedFile, $dumpedCode);
    }
}
