Skip to content

Commit 31d9903

Browse files
committed
Use a ServiceLocator to access makers in tests, instead of guessing the service name
1 parent eba3045 commit 31d9903

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/Test/MakerTestCase.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Composer\Semver\Semver;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Bundle\MakerBundle\MakerInterface;
17-
use Symfony\Bundle\MakerBundle\Str;
1817
use Symfony\Component\HttpKernel\KernelInterface;
1918
use Symfony\Component\Process\Process;
2019

@@ -100,10 +99,7 @@ private function getMakerInstance(string $makerClass): MakerInterface
10099
$this->kernel->boot();
101100
}
102101

103-
// a cheap way to guess the service id
104-
$serviceId ??= \sprintf('maker.maker.%s', Str::asSnakeCase((new \ReflectionClass($makerClass))->getShortName()));
105-
106-
return $this->kernel->getContainer()->get($serviceId);
102+
return $this->kernel->getContainer()->get('maker_locator_for_tests')->get($makerClass);
107103
}
108104

109105
protected function createKernel(): KernelInterface

src/Test/MakerTestKernel.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\Config\Loader\LoaderInterface;
1919
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Reference;
22+
use Symfony\Component\DependencyInjection\ServiceLocator;
2123
use Symfony\Component\HttpKernel\Kernel;
2224
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
2325

@@ -77,10 +79,15 @@ public function getRootDir(): string
7779

7880
public function process(ContainerBuilder $container): void
7981
{
80-
// makes all makers public to help the tests
82+
// Add a service locator to find makers by class name
83+
$makers = [];
8184
foreach ($container->findTaggedServiceIds(MakeCommandRegistrationPass::MAKER_TAG) as $id => $tags) {
82-
$defn = $container->getDefinition($id);
83-
$defn->setPublic(true);
85+
$makers[$container->getDefinition($id)->getClass()] = new Reference($id);
8486
}
87+
88+
$container->register('maker_locator_for_tests', ServiceLocator::class)
89+
->setArguments([$makers])
90+
->addTag('container.service_locator')
91+
->setPublic(true);
8592
}
8693
}

0 commit comments

Comments
 (0)