Skip to content

Commit

Permalink
Fix generation of containers using a base class
Browse files Browse the repository at this point in the history
Since the introduction of the container namespace, we must be explicitly
set the FQCN of the base class (with its leading slash).

Without that, Symfony generates a dump pointing to the wrong class.

Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Apr 3, 2023
1 parent c21ce06 commit fe6f52e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
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);
}
}
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
{
}

0 comments on commit fe6f52e

Please sign in to comment.