Skip to content

Commit

Permalink
Merge pull request #509 from lcobucci/7.0.x-merge-up-into-7.1.x_RgT4Fg7p
Browse files Browse the repository at this point in the history
Merge release 7.0.1 into 7.1.x
  • Loading branch information
lcobucci authored Apr 3, 2023
2 parents 0fde70e + 2361afd commit ebec70f
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 67 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
38 changes: 22 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"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",
Expand All @@ -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"
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
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::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 @@ -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
Expand All @@ -33,6 +32,8 @@
*/
final class CompilerTest extends TestCase
{
use GeneratesDumpDirectory;

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

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

/** @before */
Expand All @@ -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',
Expand All @@ -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 */
Expand Down Expand Up @@ -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'),
);
}

Expand All @@ -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__));
Expand All @@ -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'));
Expand All @@ -179,7 +170,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
20 changes: 18 additions & 2 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

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

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

/**
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');
}
}
Loading

0 comments on commit ebec70f

Please sign in to comment.