KernelTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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($this->getBundle(__DIR__.'/Fixtures/Bundle1')))
  48. ;
  49. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/foo.txt', $kernel->locateResource('@foo/foo.txt'));
  50. }
  51. public function testLocateResourceReturnsTheAllMatches()
  52. {
  53. $kernel = $this->getKernel();
  54. $kernel
  55. ->expects($this->once())
  56. ->method('getBundle')
  57. ->with($this->anything(), $this->equalTo(false))
  58. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'), $this->getBundle(__DIR__.'/Fixtures/Bundle2'))))
  59. ;
  60. $this->assertEquals(array(__DIR__.'/Fixtures/Bundle1/foo.txt', __DIR__.'/Fixtures/Bundle2/foo.txt'), $kernel->locateResource('@foo/foo.txt', null, false));
  61. }
  62. public function testLocateResourceReturnsAllMatchesBis()
  63. {
  64. $kernel = $this->getKernel();
  65. $kernel
  66. ->expects($this->once())
  67. ->method('getBundle')
  68. ->with($this->anything(), $this->equalTo(false))
  69. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'), $this->getBundle(__DIR__.'/foobar'))))
  70. ;
  71. $this->assertEquals(array(__DIR__.'/Fixtures/Bundle1/foo.txt'), $kernel->locateResource('@foo/foo.txt', null, false));
  72. }
  73. public function testLocateResourceIgnoresDirOnNonResource()
  74. {
  75. $kernel = $this->getKernel();
  76. $kernel
  77. ->expects($this->once())
  78. ->method('getBundle')
  79. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1')))
  80. ;
  81. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/foo.txt', $kernel->locateResource('@foo/foo.txt', __DIR__.'/Fixtures'));
  82. }
  83. public function testLocateResourceReturnsTheDirOneForResources()
  84. {
  85. $kernel = $this->getKernel();
  86. $this->assertEquals(__DIR__.'/Fixtures/foo/foo.txt', $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures'));
  87. }
  88. public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
  89. {
  90. $kernel = $this->getKernel();
  91. $kernel
  92. ->expects($this->once())
  93. ->method('getBundle')
  94. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  95. ;
  96. $this->assertEquals(array(__DIR__.'/Fixtures/foo/foo.txt', __DIR__.'/Fixtures/Bundle1/Resources/foo.txt'), $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures', false));
  97. }
  98. public function testInitializeBundles()
  99. {
  100. $parent = $this->getBundle(null, '', 'ParentABundle');
  101. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  102. $kernel = $this->getKernel();
  103. $kernel
  104. ->expects($this->once())
  105. ->method('registerBundles')
  106. ->will($this->returnValue(array($parent, $child)))
  107. ;
  108. $kernel->initializeBundles();
  109. $map = $kernel->getBundleMap();
  110. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  111. }
  112. public function testInitializeBundlesSupportInheritanceCascade()
  113. {
  114. $grandparent = $this->getBundle(null, '', 'GrandParentBBundle');
  115. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  116. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  117. $kernel = $this->getKernel();
  118. $kernel
  119. ->expects($this->once())
  120. ->method('registerBundles')
  121. ->will($this->returnValue(array($parent, $grandparent, $child)))
  122. ;
  123. $kernel->initializeBundles();
  124. $map = $kernel->getBundleMap();
  125. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  126. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  127. $this->assertEquals(array($child), $map['ChildBBundle']);
  128. }
  129. /**
  130. * @expectedException \LogicException
  131. */
  132. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  133. {
  134. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  135. $kernel = $this->getKernel();
  136. $kernel
  137. ->expects($this->once())
  138. ->method('registerBundles')
  139. ->will($this->returnValue(array($child)))
  140. ;
  141. $kernel->initializeBundles();
  142. }
  143. /**
  144. * @expectedException \LogicException
  145. */
  146. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  147. {
  148. $parent = $this->getBundle(null, '', 'ParentCBundle');
  149. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  150. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  151. $kernel = $this->getKernel();
  152. $kernel
  153. ->expects($this->once())
  154. ->method('registerBundles')
  155. ->will($this->returnValue(array($parent, $child1, $child2)))
  156. ;
  157. $kernel->initializeBundles();
  158. }
  159. protected function getBundle($dir = null, $parent = null, $className = null)
  160. {
  161. $bundle = $this
  162. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  163. ->setMethods(array('getPath', 'getParent', 'getName'))
  164. ->disableOriginalConstructor()
  165. ;
  166. if ($className) {
  167. $bundle->setMockClassName($className);
  168. }
  169. $bundle = $bundle->getMock();
  170. $bundle
  171. ->expects($this->any())
  172. ->method('getName')
  173. ->will($this->returnValue(get_class($bundle)))
  174. ;
  175. if (null !== $dir) {
  176. $bundle
  177. ->expects($this->any())
  178. ->method('getPath')
  179. ->will($this->returnValue($dir))
  180. ;
  181. }
  182. if (null !== $parent) {
  183. $bundle
  184. ->expects($this->any())
  185. ->method('getParent')
  186. ->will($this->returnValue($parent))
  187. ;
  188. }
  189. return $bundle;
  190. }
  191. protected function getKernel()
  192. {
  193. return $this
  194. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  195. ->setMethods(array('getBundle', 'registerBundles'))
  196. ->disableOriginalConstructor()
  197. ->getMock()
  198. ;
  199. }
  200. protected function getKernelForInvalidLocateResource()
  201. {
  202. return $this
  203. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  204. ->disableOriginalConstructor()
  205. ->getMockForAbstractClass()
  206. ;
  207. }
  208. }
  209. class KernelForTest extends Kernel
  210. {
  211. public function __construct($environment, $debug, $name)
  212. {
  213. parent::__construct($environment, $debug);
  214. $this->name = $name;
  215. }
  216. public function getBundleMap()
  217. {
  218. return $this->bundleMap;
  219. }
  220. public function registerRootDir()
  221. {
  222. }
  223. public function registerBundles()
  224. {
  225. }
  226. public function registerBundleDirs()
  227. {
  228. }
  229. public function registerContainerConfiguration(LoaderInterface $loader)
  230. {
  231. }
  232. public function initializeBundles()
  233. {
  234. parent::initializeBundles();
  235. }
  236. }