8000 implement a test compiler · symfony-cmf/Testing@b381ccb · GitHub
[go: up one dir, main page]

Skip to content

Commit b381ccb

Browse files
implement a test compiler
* build a test compiler, which makes services public i want want to fetch in a test scenario only * add licence header * add a changelog entry
1 parent 4271a31 commit b381ccb

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changelog
44
2.1.0 (unreleased)
55
------------------
66

7+
* **2017-01-18**: Introduce a `TestCompilePass` to make services public, which are needed
8+
in i.e. a WebTest.
79
* **2017-11-08**: Removed php 5.6 and 7.0 support, removed Symfony 3.0.* and 3.1.* support
810
introduce KERNEL_CLASS handling to avoid deprecated KERNEL_DIR, removed usage of `ProcessBuilder`
911

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Component\Testing\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
19+
*/
20+
class TestContainerPass implements CompilerPassInterface
21+
{
22+
/**
23+
* An array of service id's which should be public in a test scenario.
24+
*
25+
* @var array
26+
*/
27+
private $services = [];
28+
29+
public function __construct(array $services = [])
30+
{
31+
$this->services = $services;
32+
}
33+
34+
public function process(ContainerBuilder $container)
35+
{
36+
foreach ($container->getDefinitions() as $id => $definition) {
37+
if (in_array($id, $this->services, true)) {
38+
$definition->setPublic(true);
39+
}
40+
}
41+
}
42+
}

src/HttpKernel/TestKernel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1919
use Symfony\Bundle\TwigBundle\TwigBundle;
2020
use Symfony\Bundle\WebServerBundle\WebServerBundle;
21+
use Symfony\Cmf\Component\Testing\DependencyInjection\Compiler\TestContainerPass;
22+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
23+
use Symfony\Component\DependencyInjection\ContainerBuilder;
2124
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2225
use Symfony\Component\HttpKernel\Kernel;
2326

@@ -205,4 +208,13 @@ protected function registerConfiguredBundles()
205208
}
206209
}
207210
}
211+
212+
protected function build(ContainerBuilder $container)
213+
{
214+
parent::build($container);
215+
if (in_array($this->getEnvironment(), ['test', 'phpcr']) && file_exists($this->getKernelDir().'/config/public_services.php')) {
216+
$services = require $this->getKernelDir().'/config/public_services.php';
217+
$container->addCompilerPass(new TestContainerPass($services), PassConfig::TYPE_OPTIMIZE);
218+
}
219+
}
208220
}

tests/HttpKernel/TestKernelTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1717
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1818
use Symfony\Bundle\TwigBundle\TwigBundle;
19+
use Symfony\Component\HttpKernel\Kernel;
1920

2021
class TestKernelTest extends \PHPUnit_Framework_TestCase
2122
{
23+
/**
24+
* @var Kernel
25+
*/
2226
private $kernel;
2327

2428
private $mockBundle;

0 commit comments

Comments
 (0)
0