Skip to content

Commit

Permalink
Merge pull request #511 from lcobucci/7.2.x-merge-up-into-7.3.x_5GRbHxfj
Browse files Browse the repository at this point in the history
Merge release 7.2.2 into 7.3.x
  • Loading branch information
lcobucci authored Apr 3, 2023
2 parents 71d06d5 + fb35021 commit e5899fc
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 49 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions test/CompilationWithBaseClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Lcobucci\DependencyInjection\Generators\Xml;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

#[PHPUnit\CoversClass(ContainerBuilder::class)]
#[PHPUnit\CoversClass(Compiler::class)]
#[PHPUnit\CoversClass(ParameterBag::class)]
#[PHPUnit\CoversClass(ContainerConfiguration::class)]
#[PHPUnit\CoversClass(Generator::class)]
#[PHPUnit\CoversClass(Xml::class)]
final class CompilationWithBaseClassTest extends TestCase
{
use GeneratesDumpDirectory;

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

#[PHPUnit\Test]
public function containerCanHaveACustomBaseClass(): void
{
$container = ContainerBuilder::xml(__FILE__, self::DI_NAMESPACE)
->setBaseClass(ContainerForTests::class)
->setDumpDir($this->dumpDirectory)
->getContainer();

self::assertInstanceOf(ContainerForTests::class, $container);
}
}
25 changes: 8 additions & 17 deletions test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -31,6 +30,8 @@
#[PHPUnit\UsesClass(MakeServicesPublic::class)]
final class CompilerTest extends TestCase
{
use GeneratesDumpDirectory;

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

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

#[PHPUnit\Before]
Expand All @@ -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',
Expand All @@ -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]
Expand Down Expand Up @@ -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'),
);
}

Expand All @@ -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__));
Expand All @@ -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'));
Expand All @@ -177,7 +168,7 @@ public function compileShouldUseCustomContainerBuilders(): 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
13 changes: 11 additions & 2 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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',
];

Expand Down
2 changes: 1 addition & 1 deletion test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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
{
}
37 changes: 37 additions & 0 deletions test/GeneratesDumpDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace Lcobucci\DependencyInjection;

use PHPUnit\Framework\Attributes as PHPUnit;

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

trait GeneratesDumpDirectory
{
private string $dumpDirectory;

#[PHPUnit\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);
}

#[PHPUnit\After]
public function removeDumpDir(): void
{
exec('rm -rf ' . __DIR__ . '/../tmp');
}
}
39 changes: 12 additions & 27 deletions test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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');
}
}

0 comments on commit e5899fc

Please sign in to comment.