Skip to content

Commit d9c5486

Browse files
committed
Added "bundle set" feature
- Enable predefined list of bundles to registered
1 parent f76bc1f commit d9c5486

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

src/Symfony/Cmf/Component/Testing/HttpKernel/TestKernel.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,140 @@
77
use Symfony\Component\HttpKernel\Kernel;
88
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
99

10+
/**
11+
* TestKernel base class for Symfony CMF Bundle
12+
* integration tests.
13+
*
14+
* @author Daniel Leech <[email protected]>
15+
*/
1016
abstract class TestKernel extends Kernel
1117
{
18+
protected $bundleSets = array();
19+
protected $requiredBundles = array();
20+
21+
/**
22+
* Register commonly needed bundle sets and then
23+
* after initializing the parent kernel, let the
24+
* concrete kernel configure itself using the abstracvt
25+
* configure() command.
26+
*/
27+
public function init()
28+
{
29+
$this->registerBundleSet('default', array(
30+
'\Symfony\Bundle\FrameworkBundle\FrameworkBundle',
31+
'\Symfony\Bundle\SecurityBundle\SecurityBundle',
32+
'\Symfony\Bundle\TwigBundle\TwigBundle',
33+
));
34+
35+
$this->registerBundleSet('phpcr_odm', array(
36+
'\Doctrine\Bundle\DoctrineBundle\DoctrineBundle',
37+
'\Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle',
38+
));
39+
40+
$this->registerBundleSet('sonata_admin', array(
41+
'\Sonata\BlockBundle\SonataBlockBundle',
42+
'\Sonata\AdminBundle\SonataAdminBundle',
43+
'\Sonata\DoctrinePHPCRAdminBundle\SonataDoctrinePHPCRAdminBundle',
44+
));
45+
46+
parent::init();
47+
$this->configure();
48+
}
49+
50+
/**
51+
* Use this method to declare which bundles are required
52+
* by the Kernel, e.g.
53+
*
54+
* $this->requireBundleSets('default', 'phpcr_odm');
55+
* $this->addBundle(new MyBundle);
56+
* $this->addBundles(array(new Bundle1, new Bundle2));
57+
*
58+
*/
59+
abstract protected function configure();
60+
61+
/**
62+
* Register a set of bundles with the given name
63+
*
64+
* This method does not add the bundles to the kernel,
65+
* it just makes a set available.
66+
*/
67+
public function registerBundleSet($name, $bundles)
68+
{
69+
$this->bundleSets[$name] = $bundles;
70+
}
71+
72+
/**
73+
* The bundles in the named sets will be added to the Kernel.
74+
*/
75+
public function requireBundleSets(array $names)
76+
{
77+
foreach ($names as $name) {
78+
$this->requireBundleSet($name);
79+
}
80+
}
81+
82+
/**
83+
* Require the bundles in the named bundle set.
84+
*
85+
* Note that we register the FQN's and not the concrete classes.
86+
* This enables us to declare pre-defined bundle sets without
87+
* worrying if the bundle is actually present or not.
88+
*/
89+
public function requireBundleSet($name)
90+
{
91+
if (!isset($this->bundleSets[$name])) {
92+
throw new \InvalidArgumentException(sprintf(
93+
'Bundle set %s has not been registered, available bundle sets: %s',
94+
$name,
95+
implode(',', array_keys($this->bundleSets))
96+
));
97+
}
98+
99+
foreach ($this->bundleSets[$name] as $bundle) {
100+
if (!class_exists($bundle)) {
101+
throw new \InvalidArgumentException(sprintf(
102+
'Bundle class "%s" does not exist.',
103+
$bundle
104+
));
105+
}
106+
107+
$this->requiredBundles[] = new $bundle;
108+
}
109+
}
110+
111+
/**
112+
* Add concrete bundles to the kernel
113+
*/
114+
public function addBundles(array $bundles)
115+
{
116+
foreach ($bundles as $bundle) {
117+
$this->addBundle($bundle);
118+
}
119+
}
120+
121+
/**
122+
* Add a concrete bundle to the kernel
123+
*/
124+
public function addBundle(BundleInterface $bundle)
125+
{
126+
$this->requiredBundles[] = $bundle;
127+
}
128+
129+
/**
130+
* {inheritDoc}
131+
*
132+
* Here we return our list of bundles
133+
*/
134+
public function registerBundles()
135+
{
136+
return $this->requiredBundles;
137+
}
138+
139+
/**
140+
* Returns the KernelDir of the CHILD class,
141+
* i.e. the concrete implementation in the bundles
142+
* src/ directory (or wherever).
143+
*/
12144
public function getKernelDir()
13145
{
14146
$refl = new \ReflectionClass($this);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\Tests\HttpKernel;
4+
5+
use Symfony\Cmf\Component\Testing\HttpKernel\TestKernel;
6+
use Symfony\Component\Config\Loader\LoaderInterface;
7+
8+
class TestKernelTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function setUp()
11+
{
12+
$this->kernel = $this->getMockBuilder(
13+
'Symfony\Cmf\Component\Testing\HttpKernel\TestKernel'
14+
)->disableOriginalConstructor()->getMockForAbstractClass();
15+
$this->mockBundle = $this->getMock(
16+
'Symfony\Component\HttpKernel\Bundle\BundleInterface'
17+
);
18+
}
19+
20+
public function testBundleSetRequire()
21+
{
22+
$this->kernel->init();
23+
$this->kernel->requireBundleSets(array(
24+
'default', 'phpcr_odm'
25+
));
26+
$bundles = $this->kernel->registerBundles();
27+
$this->assertCount(5, $bundles);
28+
}
29+
30+
public function testBundleAdd()
31+
{
32+
$this->kernel->addBundle($this->mockBundle);
33+
$this->kernel->addBundle($this->mockBundle);
34+
35+
$this->assertCount(2, $this->kernel->registerBundles());
36+
}
37+
38+
/**
39+
* @expectedException \InvalidArgumentException
40+
*/
41+
public function testRequireInvalidBundleSet()
42+
{
43+
$this->kernel->init();
44+
$this->kernel->requireBundleSet('foobar');
45+
}
46+
}

0 commit comments

Comments
 (0)