KernelTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpKernel;
  11. use Symfony\Component\HttpKernel\Kernel;
  12. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  13. use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
  14. class KernelTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @expectedException \InvalidArgumentException
  18. */
  19. public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
  20. {
  21. $this->getKernelForInvalidLocateResource()->locateResource('foo');
  22. }
  23. /**
  24. * @expectedException \RuntimeException
  25. */
  26. public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
  27. {
  28. $this->getKernelForInvalidLocateResource()->locateResource('@foo/../bar');
  29. }
  30. /**
  31. * @expectedException \InvalidArgumentException
  32. */
  33. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  34. {
  35. $this->getKernelForInvalidLocateResource()->locateResource('@foo/config/routing.xml');
  36. }
  37. public function testLocateResourceReturnsTheFirstThatMatches()
  38. {
  39. $kernel = $this->getKernel();
  40. $kernel
  41. ->expects($this->once())
  42. ->method('getBundle')
  43. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  44. ;
  45. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/foo.txt', $kernel->locateResource('@foo/foo.txt'));
  46. }
  47. public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
  48. {
  49. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1', null, 'ParentAABundle');
  50. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2', 'ParentAABundle', 'ChildAABundle');
  51. $kernel = $this->getKernel();
  52. $kernel
  53. ->expects($this->any())
  54. ->method('getBundle')
  55. ->will($this->returnValue(array($child, $parent)))
  56. ;
  57. $this->assertEquals(__DIR__.'/Fixtures/Bundle2/foo.txt', $kernel->locateResource('@foo/foo.txt'));
  58. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/bar.txt', $kernel->locateResource('@foo/bar.txt'));
  59. }
  60. public function testLocateResourceReturnsTheAllMatches()
  61. {
  62. $kernel = $this->getKernel();
  63. $kernel
  64. ->expects($this->once())
  65. ->method('getBundle')
  66. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'), $this->getBundle(__DIR__.'/Fixtures/Bundle2'))))
  67. ;
  68. $this->assertEquals(array(__DIR__.'/Fixtures/Bundle1/foo.txt', __DIR__.'/Fixtures/Bundle2/foo.txt'), $kernel->locateResource('@foo/foo.txt', null, false));
  69. }
  70. public function testLocateResourceReturnsAllMatchesBis()
  71. {
  72. $kernel = $this->getKernel();
  73. $kernel
  74. ->expects($this->once())
  75. ->method('getBundle')
  76. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'), $this->getBundle(__DIR__.'/foobar'))))
  77. ;
  78. $this->assertEquals(array(__DIR__.'/Fixtures/Bundle1/foo.txt'), $kernel->locateResource('@foo/foo.txt', null, false));
  79. }
  80. public function testLocateResourceIgnoresDirOnNonResource()
  81. {
  82. $kernel = $this->getKernel();
  83. $kernel
  84. ->expects($this->once())
  85. ->method('getBundle')
  86. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  87. ;
  88. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/foo.txt', $kernel->locateResource('@foo/foo.txt', __DIR__.'/Fixtures'));
  89. }
  90. public function testLocateResourceReturnsTheDirOneForResources()
  91. {
  92. $kernel = $this->getKernel();
  93. $this->assertEquals(__DIR__.'/Fixtures/foo/foo.txt', $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures'));
  94. }
  95. public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
  96. {
  97. $kernel = $this->getKernel();
  98. $kernel
  99. ->expects($this->once())
  100. ->method('getBundle')
  101. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  102. ;
  103. $this->assertEquals(array(__DIR__.'/Fixtures/foo/foo.txt', __DIR__.'/Fixtures/Bundle1/Resources/foo.txt'), $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures', false));
  104. }
  105. public function testLocateResourceOnDirectories()
  106. {
  107. $kernel = $this->getKernel();
  108. $this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources/', __DIR__.'/Fixtures'));
  109. $this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources', __DIR__.'/Fixtures'));
  110. $kernel
  111. ->expects($this->any())
  112. ->method('getBundle')
  113. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  114. ;
  115. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
  116. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
  117. }
  118. public function testInitializeBundles()
  119. {
  120. $parent = $this->getBundle(null, null, 'ParentABundle');
  121. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  122. $kernel = $this->getKernel();
  123. $kernel
  124. ->expects($this->once())
  125. ->method('registerBundles')
  126. ->will($this->returnValue(array($parent, $child)))
  127. ;
  128. $kernel->initializeBundles();
  129. $map = $kernel->getBundleMap();
  130. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  131. }
  132. public function testInitializeBundlesSupportInheritanceCascade()
  133. {
  134. $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
  135. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  136. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  137. $kernel = $this->getKernel();
  138. $kernel
  139. ->expects($this->once())
  140. ->method('registerBundles')
  141. ->will($this->returnValue(array($grandparent, $parent, $child)))
  142. ;
  143. $kernel->initializeBundles();
  144. $map = $kernel->getBundleMap();
  145. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  146. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  147. $this->assertEquals(array($child), $map['ChildBBundle']);
  148. }
  149. /**
  150. * @expectedException \LogicException
  151. */
  152. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  153. {
  154. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  155. $kernel = $this->getKernel();
  156. $kernel
  157. ->expects($this->once())
  158. ->method('registerBundles')
  159. ->will($this->returnValue(array($child)))
  160. ;
  161. $kernel->initializeBundles();
  162. }
  163. public function testInitializeBundlesSupportsArbitaryBundleRegistrationOrder()
  164. {
  165. $grandparent = $this->getBundle(null, null, 'GrandParentCCundle');
  166. $parent = $this->getBundle(null, 'GrandParentCCundle', 'ParentCCundle');
  167. $child = $this->getBundle(null, 'ParentCCundle', 'ChildCCundle');
  168. $kernel = $this->getKernel();
  169. $kernel
  170. ->expects($this->once())
  171. ->method('registerBundles')
  172. ->will($this->returnValue(array($parent, $grandparent, $child)))
  173. ;
  174. $kernel->initializeBundles();
  175. $map = $kernel->getBundleMap();
  176. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCCundle']);
  177. $this->assertEquals(array($child, $parent), $map['ParentCCundle']);
  178. $this->assertEquals(array($child), $map['ChildCCundle']);
  179. }
  180. /**
  181. * @expectedException \LogicException
  182. */
  183. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  184. {
  185. $parent = $this->getBundle(null, null, 'ParentCBundle');
  186. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  187. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  188. $kernel = $this->getKernel();
  189. $kernel
  190. ->expects($this->once())
  191. ->method('registerBundles')
  192. ->will($this->returnValue(array($parent, $child1, $child2)))
  193. ;
  194. $kernel->initializeBundles();
  195. }
  196. /**
  197. * @expectedException \LogicException
  198. */
  199. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  200. {
  201. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  202. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  203. $kernel = $this->getKernel();
  204. $kernel
  205. ->expects($this->once())
  206. ->method('registerBundles')
  207. ->will($this->returnValue(array($fooBundle, $barBundle)))
  208. ;
  209. $kernel->initializeBundles();
  210. }
  211. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  212. {
  213. $bundle = $this
  214. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\BundleForTest')
  215. ->setMethods(array('getPath', 'getParent', 'getName'))
  216. ->disableOriginalConstructor()
  217. ;
  218. if ($className) {
  219. $bundle->setMockClassName($className);
  220. }
  221. $bundle = $bundle->getMockForAbstractClass();
  222. $bundle
  223. ->expects($this->any())
  224. ->method('getName')
  225. ->will($this->returnValue(is_null($bundleName) ? get_class($bundle) : $bundleName))
  226. ;
  227. $bundle
  228. ->expects($this->any())
  229. ->method('getPath')
  230. ->will($this->returnValue($dir))
  231. ;
  232. $bundle
  233. ->expects($this->any())
  234. ->method('getParent')
  235. ->will($this->returnValue($parent))
  236. ;
  237. return $bundle;
  238. }
  239. protected function getKernel()
  240. {
  241. return $this
  242. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  243. ->setMethods(array('getBundle', 'registerBundles'))
  244. ->disableOriginalConstructor()
  245. ->getMock()
  246. ;
  247. }
  248. protected function getKernelForInvalidLocateResource()
  249. {
  250. return $this
  251. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  252. ->disableOriginalConstructor()
  253. ->getMockForAbstractClass()
  254. ;
  255. }
  256. }
  257. class KernelForTest extends Kernel
  258. {
  259. public function getBundleMap()
  260. {
  261. return $this->bundleMap;
  262. }
  263. public function registerRootDir()
  264. {
  265. }
  266. public function registerBundles()
  267. {
  268. }
  269. public function registerBundleDirs()
  270. {
  271. }
  272. public function registerContainerConfiguration(LoaderInterface $loader)
  273. {
  274. }
  275. public function initializeBundles()
  276. {
  277. parent::initializeBundles();
  278. }
  279. }
  280. abstract class BundleForTest implements BundleInterface
  281. {
  282. // We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final
  283. }