|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/symfony-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
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 Puli\SymfonyBundle\Tests; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit_Framework_TestCase; |
|
15
|
|
|
use Puli\SymfonyBundle\PuliBundle; |
|
16
|
|
|
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
|
17
|
|
|
use Symfony\Bundle\TwigBundle\TwigBundle; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
|
21
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
23
|
|
|
use Twig_Environment; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @since 1.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ContainerTest extends PHPUnit_Framework_TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
private $tempDir; |
|
33
|
|
|
|
|
34
|
|
|
private $rootDir; |
|
35
|
|
|
|
|
36
|
|
|
private $cacheDir; |
|
37
|
|
|
|
|
38
|
|
|
protected function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
while (false === @mkdir($this->tempDir = sys_get_temp_dir().'/puli-bundle/ContainerTest'.rand(10000, 99999), 0777, true)) { |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->rootDir = $this->tempDir.'/root'; |
|
44
|
|
|
$this->cacheDir = $this->tempDir.'/cache'; |
|
45
|
|
|
|
|
46
|
|
|
mkdir($this->rootDir); |
|
47
|
|
|
mkdir($this->cacheDir); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function tearDown() |
|
51
|
|
|
{ |
|
52
|
|
|
$filesystem = new Filesystem(); |
|
53
|
|
|
$filesystem->remove($this->tempDir); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testContainer() |
|
57
|
|
|
{ |
|
58
|
|
|
$container = $this->createContainer(false); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertInstanceOf('Puli\Repository\Api\ResourceRepository', $container->get('puli.repository')); |
|
61
|
|
|
$this->assertInstanceOf('Puli\Discovery\Api\Discovery', $container->get('puli.discovery')); |
|
62
|
|
|
$this->assertInstanceOf('Puli\UrlGenerator\Api\UrlGenerator', $container->get('puli.url_generator')); |
|
63
|
|
|
$this->assertInstanceOf('Puli\SymfonyBridge\Config\FileLocatorChain', $container->get('file_locator')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testTwigLoaded() |
|
67
|
|
|
{ |
|
68
|
|
|
$container = $this->createContainer(true); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertInstanceOf('Twig_Environment', $container->get('twig')); |
|
71
|
|
|
|
|
72
|
|
|
/** @var Twig_Environment $twig */ |
|
73
|
|
|
$twig = $container->get('twig'); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertInstanceOf('Puli\TwigExtension\PuliExtension', $twig->getExtension('puli')); |
|
76
|
|
|
$this->assertInstanceOf('Twig_Loader_Chain', $twig->getLoader()); |
|
77
|
|
|
|
|
78
|
|
|
$chainDefinition = $container->getDefinition('twig.loader'); |
|
79
|
|
|
$methodCalls = $chainDefinition->getMethodCalls(); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertCount(3, $methodCalls); |
|
82
|
|
|
|
|
83
|
|
|
// Puli loader is inserted first |
|
84
|
|
|
$this->assertSame('addLoader', $methodCalls[0][0]); |
|
85
|
|
|
$this->assertSame('Puli\TwigExtension\PuliTemplateLoader', $methodCalls[0][1][0]->getClass()); |
|
86
|
|
|
$this->assertSame('addLoader', $methodCalls[1][0]); |
|
87
|
|
|
$this->assertSame('Twig_Loader_Filesystem', $methodCalls[1][1][0]->getClass()); |
|
88
|
|
|
$this->assertSame('addLoader', $methodCalls[2][0]); |
|
89
|
|
|
$this->assertSame('Twig_Loader_Filesystem', $methodCalls[2][1][0]->getClass()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testTwigDisabled() |
|
93
|
|
|
{ |
|
94
|
|
|
$container = $this->createContainer(true, array('twig' => false)); |
|
95
|
|
|
|
|
96
|
|
|
/** @var Twig_Environment $twig */ |
|
97
|
|
|
$twig = $container->get('twig'); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertFalse($twig->hasExtension('puli')); |
|
100
|
|
|
|
|
101
|
|
|
$chainDefinition = $container->getDefinition('twig.loader'); |
|
102
|
|
|
$methodCalls = $chainDefinition->getMethodCalls(); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertCount(2, $methodCalls); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function createContainer($twig, array $config = array()) |
|
108
|
|
|
{ |
|
109
|
|
|
$bundles = array( |
|
110
|
|
|
'FrameworkBundle' => new FrameworkBundle(), |
|
111
|
|
|
'PuliBundle' => new PuliBundle(), |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
if ($twig) { |
|
115
|
|
|
$bundles['TwigBundle'] = new TwigBundle(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$container = new ContainerBuilder(new ParameterBag(array( |
|
119
|
|
|
'kernel.debug' => false, |
|
120
|
|
|
'kernel.bundles' => array_map(function ($bundle) { |
|
121
|
|
|
return get_class($bundle); |
|
122
|
|
|
}, $bundles), |
|
123
|
|
|
'kernel.cache_dir' => $this->rootDir, |
|
124
|
|
|
'kernel.root_dir' => $this->rootDir, |
|
125
|
|
|
'kernel.charset' => 'UTF-8', |
|
126
|
|
|
'kernel.secret' => '$ecret', |
|
127
|
|
|
'kernel.environment' => 'test', |
|
128
|
|
|
))); |
|
129
|
|
|
|
|
130
|
|
|
foreach ($bundles as $name => $bundle) { |
|
131
|
|
|
/* @var BundleInterface $bundle */ |
|
132
|
|
|
$extension = $bundle->getContainerExtension(); |
|
133
|
|
|
$container->registerExtension($extension); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
// Load bundle services |
|
136
|
|
|
$extension->load('PuliBundle' === $name ? array($config) : array(), $container); |
|
137
|
|
|
|
|
138
|
|
|
// Load compiler passes |
|
139
|
|
|
$bundle->build($container); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$container->addDefinitions(array( |
|
143
|
|
|
'kernel' => new Definition('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest', array( |
|
144
|
|
|
'test', // environment |
|
145
|
|
|
false, // debug |
|
146
|
|
|
)), |
|
147
|
|
|
)); |
|
148
|
|
|
|
|
149
|
|
|
$container->compile(); |
|
150
|
|
|
|
|
151
|
|
|
return $container; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: