From 61861270620475ac03aa39ea518061e9c2a8f52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Mon, 3 Apr 2023 21:41:15 +0200 Subject: [PATCH 1/4] Allow composer plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luís Cobucci --- composer.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6515b114..ba2dff40 100644 --- a/composer.json +++ b/composer.json @@ -50,6 +50,12 @@ }, "config": { "preferred-install": "dist", - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "infection/extension-installer": true, + "phpstan/extension-installer": true, + "ocramius/package-versions": true + } } } From 1ab52bbf9a3e3ef96dee52c62a164ab53e198cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Mon, 3 Apr 2023 22:08:04 +0200 Subject: [PATCH 2/4] Continue on PHPCBF issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luís Cobucci --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ed6b5ff5..3e629203 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ infection: .PHONY: phpcbf phpcbf: - @vendor/bin/phpcbf --parallel=$(PARALLELISM) + @vendor/bin/phpcbf --parallel=$(PARALLELISM) || true .PHONY: phpcs phpcs: From c21ce0668a0abae4dc7c93e3d60293f77cf776da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Mon, 3 Apr 2023 22:09:28 +0200 Subject: [PATCH 3/4] Isolate generation of dumped containers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This refactors tests a bit to ensure each test uses a different dump directory, accepting the usage of filesystem during tests. Signed-off-by: Luís Cobucci --- test/CompilerTest.php | 33 ++++++--------------------- test/GeneratesDumpDirectory.php | 35 +++++++++++++++++++++++++++++ test/GeneratorTest.php | 40 ++++++++++----------------------- 3 files changed, 54 insertions(+), 54 deletions(-) create mode 100644 test/GeneratesDumpDirectory.php diff --git a/test/CompilerTest.php b/test/CompilerTest.php index bfc33270..809afe5c 100644 --- a/test/CompilerTest.php +++ b/test/CompilerTest.php @@ -15,15 +15,10 @@ use Symfony\Component\Config\ConfigCache; use Symfony\Component\DependencyInjection\Compiler\PassConfig; -use function bin2hex; use function count; -use function exec; use function file_get_contents; use function file_put_contents; use function iterator_to_array; -use function mkdir; -use function random_bytes; -use function realpath; /** * @covers \Lcobucci\DependencyInjection\Compiler @@ -36,6 +31,8 @@ */ final class CompilerTest extends TestCase { + use GeneratesDumpDirectory; + private const EXPECTED_FILES = [ 'getTestingService.php', 'AppContainer.php', @@ -45,7 +42,6 @@ final class CompilerTest extends TestCase private ContainerConfiguration $config; private ConfigCache $dump; - private string $dumpDir; /** @before */ public function configureDependencies(): void @@ -61,8 +57,7 @@ public function configureDependencies(): void $parameterBag->set('container.dumper.inline_factories', false); $parameterBag->set('container.dumper.inline_class_loader', true); - $this->dumpDir = $this->createDumpDirectory(); - $this->dump = new ConfigCache($this->dumpDir . '/AppContainer.php', false); + $this->dump = new ConfigCache($this->dumpDirectory . '/AppContainer.php', false); $this->config = new ContainerConfiguration( 'Me\\MyApp', @@ -73,21 +68,7 @@ public function configureDependencies(): void ] ); - $this->config->setDumpDir($this->dumpDir); - } - - private function createDumpDirectory(): string - { - $dir = __DIR__ . '/../tmp/' . bin2hex(random_bytes(5)) . '/me_myapp'; - mkdir($dir, 0777, true); - - return $dir; - } - - /** @after */ - public function cleanUpDumpDirectory(): void - { - exec('rm -rf ' . realpath($this->dumpDir . '/../../')); + $this->config->setDumpDir($this->dumpDirectory); } /** @test */ @@ -114,7 +95,7 @@ public function compileShouldTrackChangesOnTheConfigurationFile(): void self::assertStringContainsString( __FILE__, - (string) file_get_contents($this->dumpDir . '/AppContainer.php.meta') + (string) file_get_contents($this->dumpDirectory . '/AppContainer.php.meta') ); } @@ -138,7 +119,7 @@ public function compileShouldAllowForLazyServices(): void /** @test */ public function compilationShouldBeSkippedWhenFileAlreadyExists(): void { - file_put_contents($this->dumpDir . '/AppContainer.php', 'testing'); + file_put_contents($this->dumpDirectory . '/AppContainer.php', 'testing'); $compiler = new Compiler(); $compiler->compile($this->config, $this->dump, new Yaml(__FILE__)); @@ -151,7 +132,7 @@ public function compilationShouldBeSkippedWhenFileAlreadyExists(): void /** @return PHPGenerator */ private function getGeneratedFiles(?string $dir = null): PHPGenerator { - $dir ??= $this->dumpDir; + $dir ??= $this->dumpDirectory; foreach (new DirectoryIterator($dir) as $fileInfo) { if ($fileInfo->isDot()) { diff --git a/test/GeneratesDumpDirectory.php b/test/GeneratesDumpDirectory.php new file mode 100644 index 00000000..877ee40a --- /dev/null +++ b/test/GeneratesDumpDirectory.php @@ -0,0 +1,35 @@ +dumpDirectory = $tempName; + unlink($this->dumpDirectory); + mkdir($this->dumpDirectory); + } + + /** @after */ + public function removeDumpDir(): void + { + exec('rm -rf ' . __DIR__ . '/../tmp'); + } +} diff --git a/test/GeneratorTest.php b/test/GeneratorTest.php index be21ae9a..4594b5f1 100644 --- a/test/GeneratorTest.php +++ b/test/GeneratorTest.php @@ -6,16 +6,13 @@ use Lcobucci\DependencyInjection\Compiler\DumpXmlContainer; use Lcobucci\DependencyInjection\Compiler\ParameterBag; use Lcobucci\DependencyInjection\Config\ContainerConfiguration; +use Lcobucci\DependencyInjection\Generators\Yaml; use org\bovigo\vfs\vfsStream; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use stdClass; use Symfony\Component\Config\ConfigCache; -use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\Compiler\PassConfig; -use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyBuilder; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; /** * @coversDefaultClass \Lcobucci\DependencyInjection\Generator @@ -24,17 +21,13 @@ * @uses \Lcobucci\DependencyInjection\Compiler * @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag * @uses \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer + * @uses \Lcobucci\DependencyInjection\Generators\Yaml */ final class GeneratorTest extends TestCase { - /** @var Generator&MockObject */ - private Generator $generator; + use GeneratesDumpDirectory; - /** @before */ - public function configureDependencies(): void - { - $this->generator = $this->getMockForAbstractClass(Generator::class, [__FILE__]); - } + private const DI_NAMESPACE = 'Lcobucci\\DiTests\\Generator'; /** * @test @@ -44,7 +37,9 @@ public function configureDependencies(): void */ public function initializeContainerShouldAddTheConfigurationFileAsAResource(): void { - $container = $this->generator->initializeContainer(new ContainerConfiguration('Me\\MyApp')); + $container = (new Yaml(__FILE__))->initializeContainer( + new ContainerConfiguration(self::DI_NAMESPACE) + ); self::assertEquals([new FileResource(__FILE__)], $container->getResources()); } @@ -66,34 +61,23 @@ public function generateShouldCompileAndLoadTheContainer(): void ); $config = new ContainerConfiguration( - 'Me\\MyApp', + self::DI_NAMESPACE, [vfsStream::url('tests/services.yml')], [ [new ParameterBag(['app.devmode' => true]), PassConfig::TYPE_BEFORE_OPTIMIZATION], [ - new DumpXmlContainer( - new ConfigCache(vfsStream::url('tests/dump.xml'), true) - ), + new DumpXmlContainer(new ConfigCache($this->dumpDirectory . '/dump.xml', true)), PassConfig::TYPE_AFTER_REMOVING, -255, ], ] ); - $dump = new ConfigCache(vfsStream::url('tests/container.php'), false); - - $this->generator->method('getLoader')->willReturnCallback( - static function (SymfonyBuilder $container, array $paths): YamlFileLoader { - return new YamlFileLoader( - $container, - new FileLocator($paths) - ); - } - ); + $dump = new ConfigCache($this->dumpDirectory . '/container.php', false); - $container = $this->generator->generate($config, $dump); + $container = (new Yaml(__FILE__))->generate($config, $dump); self::assertInstanceOf(stdClass::class, $container->get('testing')); - self::assertFileExists(vfsStream::url('tests/dump.xml')); + self::assertFileExists($this->dumpDirectory . '/dump.xml'); } } From fe6f52e297af59de24d226dfba2c2574fc7f7458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Mon, 3 Apr 2023 22:26:55 +0200 Subject: [PATCH 4/4] Fix generation of containers using a base class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since the introduction of the container namespace, we must be explicitly set the FQCN of the base class (with its leading slash). Without that, Symfony generates a dump pointing to the wrong class. Signed-off-by: Luís Cobucci --- src/Config/ContainerConfiguration.php | 2 +- test/CompilationWithBaseClassTest.php | 33 ++++++++++++++++++++++ test/Config/ContainerConfigurationTest.php | 4 +-- test/ContainerBuilderTest.php | 2 +- test/ContainerForTests.php | 10 +++++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/CompilationWithBaseClassTest.php create mode 100644 test/ContainerForTests.php diff --git a/src/Config/ContainerConfiguration.php b/src/Config/ContainerConfiguration.php index 6926c4ca..3f1b057c 100644 --- a/src/Config/ContainerConfiguration.php +++ b/src/Config/ContainerConfiguration.php @@ -176,7 +176,7 @@ public function getBaseClass(): ?string public function setBaseClass(string $baseClass): void { - $this->baseClass = $baseClass; + $this->baseClass = '\\' . ltrim($baseClass, '\\'); } public function withSubNamespace(string $namespace): self diff --git a/test/CompilationWithBaseClassTest.php b/test/CompilationWithBaseClassTest.php new file mode 100644 index 00000000..74ae548a --- /dev/null +++ b/test/CompilationWithBaseClassTest.php @@ -0,0 +1,33 @@ +setBaseClass(ContainerForTests::class) + ->setDumpDir($this->dumpDirectory) + ->getContainer(); + + self::assertInstanceOf(ContainerForTests::class, $container); + } +} diff --git a/test/Config/ContainerConfigurationTest.php b/test/Config/ContainerConfigurationTest.php index b5ab692c..113a8724 100644 --- a/test/Config/ContainerConfigurationTest.php +++ b/test/Config/ContainerConfigurationTest.php @@ -342,7 +342,7 @@ public function setBaseClassShouldChangeTheAttribute(): void $config = new ContainerConfiguration('Me\\MyApp'); $config->setBaseClass('Test'); - self::assertSame('Test', $config->getBaseClass()); + self::assertSame('\\Test', $config->getBaseClass()); } /** @@ -427,7 +427,7 @@ public function getDumpOptionsShouldIncludeBaseWhenWasConfigured(): void $options = [ 'class' => ContainerConfiguration::CLASS_NAME, 'namespace' => 'Me\\MyApp', - 'base_class' => 'Test', + 'base_class' => '\\Test', 'hot_path_tag' => 'container.hot_path', ]; diff --git a/test/ContainerBuilderTest.php b/test/ContainerBuilderTest.php index dcbcf5ca..868e6293 100644 --- a/test/ContainerBuilderTest.php +++ b/test/ContainerBuilderTest.php @@ -208,7 +208,7 @@ public function setBaseClassShouldConfigureTheBaseClassAndReturnSelf(): void $builder = new ContainerBuilder($this->config, $this->generator, $this->parameterBag); self::assertSame($builder, $builder->setBaseClass('Test')); - self::assertEquals('Test', $this->config->getBaseClass()); + self::assertEquals('\\Test', $this->config->getBaseClass()); } /** diff --git a/test/ContainerForTests.php b/test/ContainerForTests.php new file mode 100644 index 00000000..bdb119f5 --- /dev/null +++ b/test/ContainerForTests.php @@ -0,0 +1,10 @@ +