KernelTest.php 10 KB

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