KernelTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 testInitializeBundles()
  110. {
  111. $parent = $this->getBundle(null, '', 'ParentABundle');
  112. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  113. $kernel = $this->getKernel();
  114. $kernel
  115. ->expects($this->once())
  116. ->method('registerBundles')
  117. ->will($this->returnValue(array($parent, $child)))
  118. ;
  119. $kernel->initializeBundles();
  120. $map = $kernel->getBundleMap();
  121. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  122. }
  123. public function testInitializeBundlesSupportInheritanceCascade()
  124. {
  125. $grandparent = $this->getBundle(null, '', 'GrandParentBBundle');
  126. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  127. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  128. $kernel = $this->getKernel();
  129. $kernel
  130. ->expects($this->once())
  131. ->method('registerBundles')
  132. ->will($this->returnValue(array($parent, $grandparent, $child)))
  133. ;
  134. $kernel->initializeBundles();
  135. $map = $kernel->getBundleMap();
  136. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  137. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  138. $this->assertEquals(array($child), $map['ChildBBundle']);
  139. }
  140. /**
  141. * @expectedException \LogicException
  142. */
  143. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  144. {
  145. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  146. $kernel = $this->getKernel();
  147. $kernel
  148. ->expects($this->once())
  149. ->method('registerBundles')
  150. ->will($this->returnValue(array($child)))
  151. ;
  152. $kernel->initializeBundles();
  153. }
  154. /**
  155. * @expectedException \LogicException
  156. */
  157. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  158. {
  159. $parent = $this->getBundle(null, '', 'ParentCBundle');
  160. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  161. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  162. $kernel = $this->getKernel();
  163. $kernel
  164. ->expects($this->once())
  165. ->method('registerBundles')
  166. ->will($this->returnValue(array($parent, $child1, $child2)))
  167. ;
  168. $kernel->initializeBundles();
  169. }
  170. protected function getBundle($dir = null, $parent = null, $className = null)
  171. {
  172. $bundle = $this
  173. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  174. ->setMethods(array('getPath', 'getParent', 'getName'))
  175. ->disableOriginalConstructor()
  176. ;
  177. if ($className) {
  178. $bundle->setMockClassName($className);
  179. }
  180. $bundle = $bundle->getMock();
  181. $bundle
  182. ->expects($this->any())
  183. ->method('getName')
  184. ->will($this->returnValue(get_class($bundle)))
  185. ;
  186. if (null !== $dir) {
  187. $bundle
  188. ->expects($this->any())
  189. ->method('getPath')
  190. ->will($this->returnValue($dir))
  191. ;
  192. }
  193. if (null !== $parent) {
  194. $bundle
  195. ->expects($this->any())
  196. ->method('getParent')
  197. ->will($this->returnValue($parent))
  198. ;
  199. }
  200. return $bundle;
  201. }
  202. protected function getKernel()
  203. {
  204. return $this
  205. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  206. ->setMethods(array('getBundle', 'registerBundles'))
  207. ->disableOriginalConstructor()
  208. ->getMock()
  209. ;
  210. }
  211. protected function getKernelForInvalidLocateResource()
  212. {
  213. return $this
  214. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  215. ->disableOriginalConstructor()
  216. ->getMockForAbstractClass()
  217. ;
  218. }
  219. }
  220. class KernelForTest extends Kernel
  221. {
  222. public function __construct($environment, $debug, $name)
  223. {
  224. parent::__construct($environment, $debug);
  225. $this->name = $name;
  226. }
  227. public function getBundleMap()
  228. {
  229. return $this->bundleMap;
  230. }
  231. public function registerRootDir()
  232. {
  233. }
  234. public function registerBundles()
  235. {
  236. }
  237. public function registerBundleDirs()
  238. {
  239. }
  240. public function registerContainerConfiguration(LoaderInterface $loader)
  241. {
  242. }
  243. public function initializeBundles()
  244. {
  245. parent::initializeBundles();
  246. }
  247. }