diff --git a/Makefile b/Makefile index 341211af..f78a6bbe 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: diff --git a/composer.json b/composer.json index a0c6adcd..bebe203b 100644 --- a/composer.json +++ b/composer.json @@ -1,33 +1,26 @@ { "name": "lcobucci/di-builder", - "type": "library", "description": "Dependency Injection Builder for PHP applications", - "keywords": [ - "dependency-injection" - ], - "homepage": "https://github.com/lcobucci/di-builder", "license": [ "BSD-3-Clause" ], + "type": "library", + "keywords": [ + "dependency-injection" + ], "authors": [ { "name": "Luís Cobucci", "email": "lcobucci@gmail.com" } ], + "homepage": "https://github.com/lcobucci/di-builder", "require": { "php": "^8.0", "symfony/config": "^5.3", "symfony/dependency-injection": "^5.3", "symfony/expression-language": "^5.3" }, - "replace": { - "symfony/polyfill-php71": "*", - "symfony/polyfill-php72": "*", - "symfony/polyfill-php73": "*", - "symfony/polyfill-php74": "*", - "symfony/polyfill-php80": "*" - }, "require-dev": { "infection/infection": "^0.25", "lcobucci/coding-standard": "^8.0", @@ -42,13 +35,16 @@ "symfony/proxy-manager-bridge": "^5.3", "symfony/yaml": "^5.3" }, + "replace": { + "symfony/polyfill-php71": "*", + "symfony/polyfill-php72": "*", + "symfony/polyfill-php73": "*", + "symfony/polyfill-php74": "*", + "symfony/polyfill-php80": "*" + }, "suggest": { "symfony/proxy-manager-bridge": "Allows the creation of lazy services" }, - "config": { - "preferred-install": "dist", - "sort-packages": true - }, "autoload": { "psr-4": { "Lcobucci\\DependencyInjection\\": "src" @@ -58,5 +54,15 @@ "psr-4": { "Lcobucci\\DependencyInjection\\": "test" } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "infection/extension-installer": true, + "phpstan/extension-installer": true, + "ocramius/package-versions": true + }, + "preferred-install": "dist", + "sort-packages": true } } diff --git a/composer.lock b/composer.lock index 4a6c51dd..e9fc5c13 100644 --- a/composer.lock +++ b/composer.lock @@ -5468,5 +5468,5 @@ "php": "^8.0" }, "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" } 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..7e834831 --- /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/CompilerTest.php b/test/CompilerTest.php index 4cf329ee..f4e44b0f 100644 --- a/test/CompilerTest.php +++ b/test/CompilerTest.php @@ -20,7 +20,6 @@ use function file_get_contents; use function file_put_contents; use function iterator_to_array; -use function mkdir; /** * @covers \Lcobucci\DependencyInjection\Compiler @@ -33,6 +32,8 @@ */ final class CompilerTest extends TestCase { + use GeneratesDumpDirectory; + private const EXPECTED_FILES = [ 'getTestingService.php', 'AppContainer.php', @@ -42,7 +43,6 @@ final class CompilerTest extends TestCase private ContainerConfiguration $config; private ConfigCache $dump; - private string $dumpDir; private ParameterBag $parameters; /** @before */ @@ -59,8 +59,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', @@ -71,15 +70,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); } /** @test */ @@ -125,7 +116,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'), ); } @@ -149,7 +140,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__)); @@ -169,7 +160,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')); @@ -179,7 +170,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 14cc86d2..58b99e22 100644 --- a/test/Config/ContainerConfigurationTest.php +++ b/test/Config/ContainerConfigurationTest.php @@ -341,7 +341,23 @@ public function setBaseClassShouldChangeTheAttribute(): void $config = new ContainerConfiguration('Me\\MyApp'); $config->setBaseClass('Test'); - self::assertSame('Test', $config->getBaseClass()); + self::assertSame('\\Test', $config->getBaseClass()); + } + + /** + * @test + * + * @covers ::setBaseClass + * @covers ::getBaseClass + * + * @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::__construct + */ + public function setBaseClassShouldTrimTheLeadingSlash(): void + { + $config = new ContainerConfiguration('Me\\MyApp'); + $config->setBaseClass('\\Test'); + + self::assertSame('\\Test', $config->getBaseClass()); } /** @@ -426,7 +442,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 6374c16d..ce03a97c 100644 --- a/test/ContainerBuilderTest.php +++ b/test/ContainerBuilderTest.php @@ -250,7 +250,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 @@ +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 2fb93f41..f18a01d1 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()); } @@ -85,34 +80,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'); } }