<commit_before><?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use Generator as PHPGenerator;
use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Lcobucci\DependencyInjection\Generators\Yaml;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use function assert;
use function count;
use function file_put_contents;
use function iterator_to_array;
use function umask;

final class CompilerTest extends TestCase
{
    /**
     * @var vfsStreamDirectory
     */
    private $root;

    /**
     * @var ContainerConfiguration
     */
    private $config;

    /**
     * @var ConfigCache
     */
    private $dump;

    /**
     * @before
     */
    public function configureDependencies(): void
    {
        $this->root = vfsStream::setup(
            'tests',
            null,
            ['services.yml' => 'services: { testing: { class: stdClass, public: true } }']
        );

        $this->config = new ContainerConfiguration(
            [vfsStream::url('tests/services.yml')],
            [
                [new ParameterBag(['app.devmode' => true]), PassConfig::TYPE_BEFORE_OPTIMIZATION],
            ]
        );

        $this->dump = new ConfigCache(vfsStream::url('tests/container.php'), false);
    }

    /**
     * @test
     *
     * @covers \Lcobucci\DependencyInjection\Compiler
     *
     * @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag
     * @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
     * @uses \Lcobucci\DependencyInjection\Generator
     * @uses \Lcobucci\DependencyInjection\Generators\Yaml
     */
    public function compileShouldCreateMultipleFiles(): void
    {
        $compiler = new Compiler();
        $compiler->compile($this->config, $this->dump, new Yaml());

        $expectedFiles = [
            'removed-ids.php',
            'getTestingService.php',
            $this->config->getClassName() . '.php',
            'container.php',
            'container.php.meta',
        ];

        $expectedPermissions = 0666 & ~umask();
        $generatedFiles      = iterator_to_array($this->getGeneratedFiles($this->root));

        self::assertCount(count($expectedFiles), $generatedFiles);

        foreach ($generatedFiles as $name => $file) {
            assert($file instanceof vfsStreamFile);

            self::assertContains($name, $expectedFiles);
            self::assertSame($expectedPermissions, $file->getPermissions());
        }
    }

    /**
     * @test
     *
     * @covers \Lcobucci\DependencyInjection\Compiler
     *
     * @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag
     * @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
     * @uses \Lcobucci\DependencyInjection\Generator
     */
    public function compilationShouldBeSkippedWhenFileAlreadyExists(): void
    {
        file_put_contents(vfsStream::url('tests/container.php'), 'testing');

        $compiler = new Compiler();
        $compiler->compile($this->config, $this->dump, new Yaml());

        $generatedFiles = iterator_to_array($this->getGeneratedFiles($this->root));

        self::assertCount(1, $generatedFiles);
    }

    private function getGeneratedFiles(vfsStreamDirectory $directory): PHPGenerator
    {
        foreach ($directory->getChildren() as $child) {
            if ($child instanceof vfsStreamDirectory) {
                yield from $this->getGeneratedFiles($child);
                continue;
            }

            if ($child->getName() !== 'services.yml') {
                yield $child->getName() => $child;
            }
        }
    }
}
<commit_msg>Make test cover delayed compiler passes as well<commit_after><?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use Generator as PHPGenerator;
use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Lcobucci\DependencyInjection\Generators\Yaml;
use Lcobucci\DependencyInjection\Testing\MakeServicesPublic;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use function assert;
use function count;
use function file_put_contents;
use function iterator_to_array;
use function umask;

final class CompilerTest extends TestCase
{
    /**
     * @var vfsStreamDirectory
     */
    private $root;

    /**
     * @var ContainerConfiguration
     */
    private $config;

    /**
     * @var ConfigCache
     */
    private $dump;

    /**
     * @before
     */
    public function configureDependencies(): void
    {
        $this->root = vfsStream::setup(
            'tests',
            null,
            ['services.yml' => 'services: { testing: { class: stdClass } }']
        );

        $this->config = new ContainerConfiguration(
            [vfsStream::url('tests/services.yml')],
            [
                [new ParameterBag(['app.devmode' => true]), PassConfig::TYPE_BEFORE_OPTIMIZATION],
                [[MakeServicesPublic::class, []], PassConfig::TYPE_BEFORE_OPTIMIZATION],
            ]
        );

        $this->dump = new ConfigCache(vfsStream::url('tests/container.php'), false);
    }

    /**
     * @test
     *
     * @covers \Lcobucci\DependencyInjection\Compiler
     *
     * @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag
     * @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
     * @uses \Lcobucci\DependencyInjection\Generator
     * @uses \Lcobucci\DependencyInjection\Generators\Yaml
     * @uses \Lcobucci\DependencyInjection\Testing\MakeServicesPublic
     */
    public function compileShouldCreateMultipleFiles(): void
    {
        $compiler = new Compiler();
        $compiler->compile($this->config, $this->dump, new Yaml());

        $expectedFiles = [
            'getTestingService.php',
            $this->config->getClassName() . '.php',
            'container.php',
            'container.php.meta',
        ];

        $expectedPermissions = 0666 & ~umask();
        $generatedFiles      = iterator_to_array($this->getGeneratedFiles($this->root));

        self::assertCount(count($expectedFiles), $generatedFiles);

        foreach ($generatedFiles as $name => $file) {
            assert($file instanceof vfsStreamFile);

            self::assertContains($name, $expectedFiles);
            self::assertSame($expectedPermissions, $file->getPermissions());
        }
    }

    /**
     * @test
     *
     * @covers \Lcobucci\DependencyInjection\Compiler
     *
     * @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag
     * @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
     * @uses \Lcobucci\DependencyInjection\Generator
     */
    public function compilationShouldBeSkippedWhenFileAlreadyExists(): void
    {
        file_put_contents(vfsStream::url('tests/container.php'), 'testing');

        $compiler = new Compiler();
        $compiler->compile($this->config, $this->dump, new Yaml());

        $generatedFiles = iterator_to_array($this->getGeneratedFiles($this->root));

        self::assertCount(1, $generatedFiles);
    }

    private function getGeneratedFiles(vfsStreamDirectory $directory): PHPGenerator
    {
        foreach ($directory->getChildren() as $child) {
            if ($child instanceof vfsStreamDirectory) {
                yield from $this->getGeneratedFiles($child);
                continue;
            }

            if ($child->getName() !== 'services.yml') {
                yield $child->getName() => $child;
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.12350044197499685, 'fraction_numerical': 0.0015153428463189798, 'mean_word_length': 2.7270588235294118, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6776469', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2194, 'n_words': 376}