|
7 | 7 | use Symfony\Component\HttpKernel\Kernel;
|
8 | 8 | use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
9 | 9 |
|
| 10 | +/** |
| 11 | + * TestKernel base class for Symfony CMF Bundle |
| 12 | + * integration tests. |
| 13 | + * |
| 14 | + * @author Daniel Leech <[email protected]> |
| 15 | + */ |
10 | 16 | abstract class TestKernel extends Kernel
|
11 | 17 | {
|
| 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 | + */ |
12 | 144 | public function getKernelDir()
|
13 | 145 | {
|
14 | 146 | $refl = new \ReflectionClass($this);
|
|
0 commit comments