diff --git a/composer.json b/composer.json index 6ec3125e..690d44b9 100644 --- a/composer.json +++ b/composer.json @@ -61,8 +61,9 @@ "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true, + "infection/extension-installer": true, "phpstan/extension-installer": true, - "infection/extension-installer": true + "ocramius/package-versions": true }, "preferred-install": "dist", "sort-packages": true diff --git a/src/Config/ContainerConfiguration.php b/src/Config/ContainerConfiguration.php index f96b31ef..1561a782 100644 --- a/src/Config/ContainerConfiguration.php +++ b/src/Config/ContainerConfiguration.php @@ -158,7 +158,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..78e70a41 --- /dev/null +++ b/test/CompilationWithBaseClassTest.php @@ -0,0 +1,34 @@ +setBaseClass(ContainerForTests::class) + ->setDumpDir($this->dumpDirectory) + ->getContainer(); + + self::assertInstanceOf(ContainerForTests::class, $container); + } +} diff --git a/test/CompilerTest.php b/test/CompilerTest.php index 495e5906..8dc7ecbb 100644 --- a/test/CompilerTest.php +++ b/test/CompilerTest.php @@ -21,7 +21,6 @@ use function file_get_contents; use function file_put_contents; use function iterator_to_array; -use function mkdir; #[PHPUnit\CoversClass(Compiler::class)] #[PHPUnit\UsesClass(ParameterBag::class)] @@ -31,6 +30,8 @@ #[PHPUnit\UsesClass(MakeServicesPublic::class)] final class CompilerTest extends TestCase { + use GeneratesDumpDirectory; + private const EXPECTED_FILES = [ 'getTestingService.php', 'AppContainer.php', @@ -40,7 +41,6 @@ final class CompilerTest extends TestCase private ContainerConfiguration $config; private ConfigCache $dump; - private string $dumpDir; private ParameterBag $parameters; #[PHPUnit\Before] @@ -57,8 +57,7 @@ public function configureDependencies(): void $this->parameters->set('container.dumper.inline_factories', false); $this->parameters->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\\CompilationTest', @@ -69,15 +68,7 @@ public function configureDependencies(): void ], ); - $this->config->setDumpDir($this->dumpDir); - } - - private function createDumpDirectory(): string - { - $dir = vfsStream::url('tests-compilation/tmp/me_myapp'); - mkdir($dir, 0777, true); - - return $dir; + $this->config->setDumpDir($this->dumpDirectory); } #[PHPUnit\Test] @@ -123,7 +114,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'), ); } @@ -147,7 +138,7 @@ public function compileShouldAllowForLazyServices(): void #[PHPUnit\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__)); @@ -167,7 +158,7 @@ public function compileShouldUseCustomContainerBuilders(): void new Yaml(__FILE__, CustomContainerBuilderForTests::class), ); - $container = include $this->dumpDir . '/AppContainer.php'; + $container = include $this->dumpDirectory . '/AppContainer.php'; self::assertInstanceOf(Container::class, $container); self::assertTrue($container->hasParameter('built-with-very-special-builder')); @@ -177,7 +168,7 @@ public function compileShouldUseCustomContainerBuilders(): 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/Config/ContainerConfigurationTest.php b/test/Config/ContainerConfigurationTest.php index b3cd8c89..61f24551 100644 --- a/test/Config/ContainerConfigurationTest.php +++ b/test/Config/ContainerConfigurationTest.php @@ -249,7 +249,16 @@ public function setBaseClassShouldChangeTheAttribute(): void $config = new ContainerConfiguration('Me\\MyApp'); $config->setBaseClass('Test'); - self::assertSame('Test', $config->getBaseClass()); + self::assertSame('\\Test', $config->getBaseClass()); + } + + #[PHPUnit\Test] + public function setBaseClassShouldTrimTheLeadingSlash(): void + { + $config = new ContainerConfiguration('Me\\MyApp'); + $config->setBaseClass('\\Test'); + + self::assertSame('\\Test', $config->getBaseClass()); } #[PHPUnit\Test] @@ -302,7 +311,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 4ecdf88d..18cc9d1f 100644 --- a/test/ContainerBuilderTest.php +++ b/test/ContainerBuilderTest.php @@ -176,7 +176,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()); } #[PHPUnit\Test] 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 @@ +dumpDirectory = $tempName; + unlink($this->dumpDirectory); + mkdir($this->dumpDirectory); + } + + #[PHPUnit\After] + public function removeDumpDir(): void + { + exec('rm -rf ' . __DIR__ . '/../tmp'); + } +} diff --git a/test/GeneratorTest.php b/test/GeneratorTest.php index 807470e0..4f3af354 100644 --- a/test/GeneratorTest.php +++ b/test/GeneratorTest.php @@ -6,37 +6,33 @@ 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\Attributes as PHPUnit; -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; #[PHPUnit\CoversClass(Generator::class)] #[PHPUnit\UsesClass(ParameterBag::class)] #[PHPUnit\UsesClass(ContainerConfiguration::class)] #[PHPUnit\UsesClass(DumpXmlContainer::class)] #[PHPUnit\UsesClass(Compiler::class)] +#[PHPUnit\UsesClass(Yaml::class)] final class GeneratorTest extends TestCase { - private Generator&MockObject $generator; + use GeneratesDumpDirectory; - #[PHPUnit\Before] - public function configureDependencies(): void - { - $this->generator = $this->getMockForAbstractClass(Generator::class, [__FILE__]); - } + private const DI_NAMESPACE = 'Lcobucci\\DiTests\\Generator'; #[PHPUnit\Test] 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()); } @@ -65,34 +61,23 @@ public function generateShouldCompileAndLoadTheContainer(): void ); $config = new ContainerConfiguration( - 'Me\\GenerationTest', + self::DI_NAMESPACE, [vfsStream::url('tests-generation/services.yml')], [ [new ParameterBag(['app.devmode' => true]), PassConfig::TYPE_BEFORE_OPTIMIZATION], [ - new DumpXmlContainer( - new ConfigCache(vfsStream::url('tests-generation/dump.xml'), true), - ), + new DumpXmlContainer(new ConfigCache($this->dumpDirectory . '/dump.xml', true)), PassConfig::TYPE_AFTER_REMOVING, -255, ], ], ); - $dump = new ConfigCache(vfsStream::url('tests-generation/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-generation/dump.xml')); + self::assertFileExists($this->dumpDirectory . '/dump.xml'); } }