Skip to content

Commit 9a62b3e

Browse files
authored
Merge pull request #13 from SymfonyTest/pass
Adding compiler pass support
2 parents 3955a8b + 6c81903 commit 9a62b3e

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

Readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ class BundleInitializationTest extends BaseBundleTestCase
7272

7373
```
7474

75+
## Private services in Symfony 4
76+
77+
In Symfony 4 services are private by default. This is a good thing, but in order to test them properly we need to make
78+
them public when we are running the tests. This can easily be done with a compiler pass.
79+
80+
```php
81+
class BundleInitializationTest extends BaseBundleTestCase
82+
{
83+
protected function setUp()
84+
{
85+
parent::setUp();
86+
87+
// Make all services public
88+
$this->addCompilerPass(new PublicServicePass());
89+
90+
// Make services public that have an idea that matches a regex
91+
$this->addCompilerPass(new PublicServicePass('|my_bundle.*|'));
92+
}
93+
94+
// ...
95+
}
96+
```
97+
7598
## Configure Travis
7699

77100
You want Travis to run against each currently supported LTS version of Symfony (since there would be only one per major version), plus the current if it's not an LTS too. There is no need for testing against version in between because Symfony follows [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

src/AppKernel.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
66
use Symfony\Component\Config\Loader\LoaderInterface;
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\HttpKernel\Kernel;
910
use Symfony\Component\Routing\RouteCollectionBuilder;
@@ -33,6 +34,11 @@ class AppKernel extends Kernel
3334
*/
3435
private $fakedProjectDir;
3536

37+
/**
38+
* @var CompilerPassInterface[]
39+
*/
40+
private $compilerPasses = [];
41+
3642
/**
3743
* @param string $cachePrefix
3844
*/
@@ -141,4 +147,26 @@ public function loadRoutes(LoaderInterface $loader)
141147

142148
return $routes->build();
143149
}
150+
151+
/**
152+
* {@inheritdoc}
153+
*/
154+
protected function buildContainer()
155+
{
156+
$container = parent::buildContainer();
157+
158+
foreach ($this->compilerPasses as $pass) {
159+
$container->addCompilerPass($pass);
160+
}
161+
162+
return $container;
163+
}
164+
165+
/**
166+
* @param CompilerPassInterface[] $compilerPasses
167+
*/
168+
public function addCompilerPasses(array $compilerPasses)
169+
{
170+
$this->compilerPasses = $compilerPasses;
171+
}
144172
}

src/BaseBundleTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Nyholm\BundleTest;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
67
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
78
use Symfony\Component\HttpKernel\Kernel;
89

@@ -16,6 +17,11 @@ abstract class BaseBundleTestCase extends TestCase
1617
*/
1718
private $kernel;
1819

20+
/**
21+
* @var CompilerPassInterface[]
22+
*/
23+
private $compilerPasses = [];
24+
1925
/**
2026
* @return string
2127
*/
@@ -61,6 +67,7 @@ protected function createKernel()
6167

6268
$this->kernel = new $class(uniqid('cache'));
6369
$this->kernel->addBundle($this->getBundleClass());
70+
$this->kernel->addCompilerPasses($this->compilerPasses);
6471

6572
return $this->kernel;
6673
}
@@ -86,4 +93,12 @@ protected function tearDown()
8693
{
8794
$this->ensureKernelShutdown();
8895
}
96+
97+
/**
98+
* @param CompilerPassInterface $compilerPass
99+
*/
100+
protected function addCompilerPass(CompilerPassInterface $compilerPass)
101+
{
102+
$this->compilerPasses[] = $compilerPass;
103+
}
89104
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\CompilerPass;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
/**
9+
* @author Tobias Nyholm <[email protected]>
10+
*/
11+
class PublicServicePass implements CompilerPassInterface
12+
{
13+
/**
14+
* A regex to match the services that should be public
15+
* @var string
16+
*/
17+
private $regex;
18+
19+
/**
20+
* @param string $regex
21+
*/
22+
public function __construct($regex = '|.*|')
23+
{
24+
$this->regex = $regex;
25+
}
26+
27+
/**
28+
* @param ContainerBuilder $container
29+
*/
30+
public function process(ContainerBuilder $container)
31+
{
32+
foreach ($container->getDefinitions() as $id => $definition) {
33+
if (preg_match($this->regex, $id)) {
34+
$definition->setPublic(true);
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)