Skip to content

Commit

Permalink
Merge pull request #508 from lcobucci/fix-base-class-configuration
Browse files Browse the repository at this point in the history
Fix generation of containers using a base class
  • Loading branch information
lcobucci committed Apr 3, 2023
2 parents 096acbb + fe6f52e commit 2307bda
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ infection:

.PHONY: phpcbf
phpcbf:
@vendor/bin/phpcbf --parallel=$(PARALLELISM)
@vendor/bin/phpcbf --parallel=$(PARALLELISM) || true

.PHONY: phpcs
phpcs:
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
2 changes: 1 addition & 1 deletion src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/CompilationWithBaseClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use PHPUnit\Framework\TestCase;

/**
* @covers \Lcobucci\DependencyInjection\ContainerBuilder
* @covers \Lcobucci\DependencyInjection\Compiler
* @covers \Lcobucci\DependencyInjection\Compiler\ParameterBag
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration
* @covers \Lcobucci\DependencyInjection\Generator
* @covers \Lcobucci\DependencyInjection\Generators\Xml
* @covers \Lcobucci\DependencyInjection\Testing\MakeServicesPublic
*/
final class CompilationWithBaseClassTest extends TestCase
{
use GeneratesDumpDirectory;

private const DI_NAMESPACE = 'Lcobucci\\DiTests\\BaseClass';

/** @test */
public function containerCanHaveACustomBaseClass(): void
{
$container = ContainerBuilder::default(__FILE__, self::DI_NAMESPACE)
->setBaseClass(ContainerForTests::class)
->setDumpDir($this->dumpDirectory)
->getContainer();

self::assertInstanceOf(ContainerForTests::class, $container);
}
}
33 changes: 7 additions & 26 deletions test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +31,8 @@
*/
final class CompilerTest extends TestCase
{
use GeneratesDumpDirectory;

private const EXPECTED_FILES = [
'getTestingService.php',
'AppContainer.php',
Expand All @@ -45,7 +42,6 @@ final class CompilerTest extends TestCase

private ContainerConfiguration $config;
private ConfigCache $dump;
private string $dumpDir;

/** @before */
public function configureDependencies(): void
Expand All @@ -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',
Expand All @@ -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 */
Expand All @@ -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')
);
}

Expand All @@ -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__));
Expand All @@ -151,7 +132,7 @@ public function compilationShouldBeSkippedWhenFileAlreadyExists(): void
/** @return PHPGenerator<string, SplFileInfo> */
private function getGeneratedFiles(?string $dir = null): PHPGenerator
{
$dir ??= $this->dumpDir;
$dir ??= $this->dumpDirectory;

foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isDot()) {
Expand Down
4 changes: 2 additions & 2 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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',
];

Expand Down
2 changes: 1 addition & 1 deletion test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
10 changes: 10 additions & 0 deletions test/ContainerForTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use Symfony\Component\DependencyInjection\Container;

abstract class ContainerForTests extends Container
{
}
35 changes: 35 additions & 0 deletions test/GeneratesDumpDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use function assert;
use function exec;
use function is_string;
use function mkdir;
use function tempnam;
use function unlink;

trait GeneratesDumpDirectory
{
private string $dumpDirectory;

/** @before */
public function createDumpDir(): void
{
mkdir(__DIR__ . '/../tmp');

$tempName = tempnam(__DIR__ . '/../tmp', 'lcobucci-di-builder');
assert(is_string($tempName));

$this->dumpDirectory = $tempName;
unlink($this->dumpDirectory);
mkdir($this->dumpDirectory);
}

/** @after */
public function removeDumpDir(): void
{
exec('rm -rf ' . __DIR__ . '/../tmp');
}
}
40 changes: 12 additions & 28 deletions test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -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');
}
}

0 comments on commit 2307bda

Please sign in to comment.