KernelTest.php 9.8 KB

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