KernelTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class KernelTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testConstructor()
  19. {
  20. $env = 'test_env';
  21. $debug = true;
  22. $kernel = new KernelForTest($env, $debug);
  23. $this->assertEquals($env, $kernel->getEnvironment());
  24. $this->assertEquals($debug, $kernel->isDebug());
  25. $this->assertFalse($kernel->isBooted());
  26. $this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
  27. $this->assertNull($kernel->getContainer());
  28. }
  29. public function testClone()
  30. {
  31. $env = 'test_env';
  32. $debug = true;
  33. $kernel = new KernelForTest($env, $debug);
  34. $clone = clone $kernel;
  35. $this->assertEquals($env, $clone->getEnvironment());
  36. $this->assertEquals($debug, $clone->isDebug());
  37. $this->assertFalse($clone->isBooted());
  38. $this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
  39. $this->assertNull($clone->getContainer());
  40. }
  41. public function testBootInitializesBundlesAndContainer()
  42. {
  43. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  44. ->disableOriginalConstructor()
  45. ->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles'))
  46. ->getMock();
  47. $kernel->expects($this->once())
  48. ->method('initializeBundles');
  49. $kernel->expects($this->once())
  50. ->method('initializeContainer');
  51. $kernel->expects($this->once())
  52. ->method('getBundles')
  53. ->will($this->returnValue(array()));
  54. $kernel->boot();
  55. }
  56. public function testBootSetsTheContainerToTheBundles()
  57. {
  58. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $bundle->expects($this->once())
  62. ->method('setContainer');
  63. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  64. ->disableOriginalConstructor()
  65. ->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles'))
  66. ->getMock();
  67. $kernel->expects($this->once())
  68. ->method('getBundles')
  69. ->will($this->returnValue(array($bundle)));
  70. $kernel->boot();
  71. }
  72. public function testBootSetsTheBootedFlagToTrue()
  73. {
  74. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  75. ->disableOriginalConstructor()
  76. ->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles'))
  77. ->getMock();
  78. $kernel->expects($this->once())
  79. ->method('getBundles')
  80. ->will($this->returnValue(array()));
  81. $kernel->boot();
  82. $this->assertTrue($kernel->isBooted());
  83. }
  84. public function testShutdownCallsShutdownOnAllBundles()
  85. {
  86. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $bundle->expects($this->once())
  90. ->method('shutdown');
  91. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  92. ->disableOriginalConstructor()
  93. ->setMethods(array('getBundles'))
  94. ->getMock();
  95. $kernel->expects($this->once())
  96. ->method('getBundles')
  97. ->will($this->returnValue(array($bundle)));
  98. $kernel->shutdown();
  99. }
  100. public function testShutdownGivesNullContainerToAllBundles()
  101. {
  102. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $bundle->expects($this->once())
  106. ->method('setContainer')
  107. ->with(null);
  108. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  109. ->disableOriginalConstructor()
  110. ->setMethods(array('getBundles'))
  111. ->getMock();
  112. $kernel->expects($this->once())
  113. ->method('getBundles')
  114. ->will($this->returnValue(array($bundle)));
  115. $kernel->shutdown();
  116. }
  117. public function testHandleCallsHandleOnHttpKernel()
  118. {
  119. $type = HttpKernelInterface::MASTER_REQUEST;
  120. $catch = true;
  121. $request = new Request();
  122. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $httpKernelMock
  126. ->expects($this->once())
  127. ->method('handle')
  128. ->with($request, $type, $catch);
  129. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  130. ->disableOriginalConstructor()
  131. ->setMethods(array('getHttpKernel'))
  132. ->getMock();
  133. $kernel->expects($this->once())
  134. ->method('getHttpKernel')
  135. ->will($this->returnValue($httpKernelMock));
  136. $kernel->handle($request, $type, $catch);
  137. }
  138. public function testStripComments()
  139. {
  140. if (!function_exists('token_get_all')) {
  141. $this->markTestSkipped();
  142. return;
  143. }
  144. $source = <<<EOF
  145. <?php
  146. /**
  147. * some class comments to strip
  148. */
  149. class TestClass
  150. {
  151. /**
  152. * some method comments to strip
  153. */
  154. public function doStuff()
  155. {
  156. // inline comment
  157. }
  158. }
  159. EOF;
  160. $expected = <<<EOF
  161. <?php
  162. class TestClass
  163. {
  164. public function doStuff()
  165. {
  166. }
  167. }
  168. EOF;
  169. $this->assertEquals($expected, Kernel::stripComments($source));
  170. }
  171. /**
  172. * @expectedException \InvalidArgumentException
  173. */
  174. public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
  175. {
  176. $this->getKernelForInvalidLocateResource()->locateResource('foo');
  177. }
  178. /**
  179. * @expectedException \RuntimeException
  180. */
  181. public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
  182. {
  183. $this->getKernelForInvalidLocateResource()->locateResource('@foo/../bar');
  184. }
  185. /**
  186. * @expectedException \InvalidArgumentException
  187. */
  188. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  189. {
  190. $this->getKernelForInvalidLocateResource()->locateResource('@foo/config/routing.xml');
  191. }
  192. public function testLocateResourceReturnsTheFirstThatMatches()
  193. {
  194. $kernel = $this->getKernel();
  195. $kernel
  196. ->expects($this->once())
  197. ->method('getBundle')
  198. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  199. ;
  200. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/foo.txt', $kernel->locateResource('@foo/foo.txt'));
  201. }
  202. public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
  203. {
  204. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1', null, 'ParentAABundle');
  205. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2', 'ParentAABundle', 'ChildAABundle');
  206. $kernel = $this->getKernel();
  207. $kernel
  208. ->expects($this->any())
  209. ->method('getBundle')
  210. ->will($this->returnValue(array($child, $parent)))
  211. ;
  212. $this->assertEquals(__DIR__.'/Fixtures/Bundle2/foo.txt', $kernel->locateResource('@foo/foo.txt'));
  213. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/bar.txt', $kernel->locateResource('@foo/bar.txt'));
  214. }
  215. public function testLocateResourceReturnsTheAllMatches()
  216. {
  217. $kernel = $this->getKernel();
  218. $kernel
  219. ->expects($this->once())
  220. ->method('getBundle')
  221. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'), $this->getBundle(__DIR__.'/Fixtures/Bundle2'))))
  222. ;
  223. $this->assertEquals(array(__DIR__.'/Fixtures/Bundle1/foo.txt', __DIR__.'/Fixtures/Bundle2/foo.txt'), $kernel->locateResource('@foo/foo.txt', null, false));
  224. }
  225. public function testLocateResourceReturnsAllMatchesBis()
  226. {
  227. $kernel = $this->getKernel();
  228. $kernel
  229. ->expects($this->once())
  230. ->method('getBundle')
  231. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'), $this->getBundle(__DIR__.'/foobar'))))
  232. ;
  233. $this->assertEquals(array(__DIR__.'/Fixtures/Bundle1/foo.txt'), $kernel->locateResource('@foo/foo.txt', null, false));
  234. }
  235. public function testLocateResourceIgnoresDirOnNonResource()
  236. {
  237. $kernel = $this->getKernel();
  238. $kernel
  239. ->expects($this->once())
  240. ->method('getBundle')
  241. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  242. ;
  243. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/foo.txt', $kernel->locateResource('@foo/foo.txt', __DIR__.'/Fixtures'));
  244. }
  245. public function testLocateResourceReturnsTheDirOneForResources()
  246. {
  247. $kernel = $this->getKernel();
  248. $this->assertEquals(__DIR__.'/Fixtures/foo/foo.txt', $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures'));
  249. }
  250. public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
  251. {
  252. $kernel = $this->getKernel();
  253. $kernel
  254. ->expects($this->once())
  255. ->method('getBundle')
  256. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  257. ;
  258. $this->assertEquals(array(__DIR__.'/Fixtures/foo/foo.txt', __DIR__.'/Fixtures/Bundle1/Resources/foo.txt'), $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures', false));
  259. }
  260. public function testLocateResourceOnDirectories()
  261. {
  262. $kernel = $this->getKernel();
  263. $this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources/', __DIR__.'/Fixtures'));
  264. $this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources', __DIR__.'/Fixtures'));
  265. $kernel
  266. ->expects($this->any())
  267. ->method('getBundle')
  268. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
  269. ;
  270. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
  271. $this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
  272. }
  273. public function testInitializeBundles()
  274. {
  275. $parent = $this->getBundle(null, null, 'ParentABundle');
  276. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  277. $kernel = $this->getKernel();
  278. $kernel
  279. ->expects($this->once())
  280. ->method('registerBundles')
  281. ->will($this->returnValue(array($parent, $child)))
  282. ;
  283. $kernel->initializeBundles();
  284. $map = $kernel->getBundleMap();
  285. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  286. }
  287. public function testInitializeBundlesSupportInheritanceCascade()
  288. {
  289. $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
  290. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  291. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  292. $kernel = $this->getKernel();
  293. $kernel
  294. ->expects($this->once())
  295. ->method('registerBundles')
  296. ->will($this->returnValue(array($grandparent, $parent, $child)))
  297. ;
  298. $kernel->initializeBundles();
  299. $map = $kernel->getBundleMap();
  300. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  301. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  302. $this->assertEquals(array($child), $map['ChildBBundle']);
  303. }
  304. /**
  305. * @expectedException \LogicException
  306. */
  307. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  308. {
  309. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  310. $kernel = $this->getKernel();
  311. $kernel
  312. ->expects($this->once())
  313. ->method('registerBundles')
  314. ->will($this->returnValue(array($child)))
  315. ;
  316. $kernel->initializeBundles();
  317. }
  318. public function testInitializeBundlesSupportsArbitaryBundleRegistrationOrder()
  319. {
  320. $grandparent = $this->getBundle(null, null, 'GrandParentCCundle');
  321. $parent = $this->getBundle(null, 'GrandParentCCundle', 'ParentCCundle');
  322. $child = $this->getBundle(null, 'ParentCCundle', 'ChildCCundle');
  323. $kernel = $this->getKernel();
  324. $kernel
  325. ->expects($this->once())
  326. ->method('registerBundles')
  327. ->will($this->returnValue(array($parent, $grandparent, $child)))
  328. ;
  329. $kernel->initializeBundles();
  330. $map = $kernel->getBundleMap();
  331. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCCundle']);
  332. $this->assertEquals(array($child, $parent), $map['ParentCCundle']);
  333. $this->assertEquals(array($child), $map['ChildCCundle']);
  334. }
  335. /**
  336. * @expectedException \LogicException
  337. */
  338. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  339. {
  340. $parent = $this->getBundle(null, null, 'ParentCBundle');
  341. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  342. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  343. $kernel = $this->getKernel();
  344. $kernel
  345. ->expects($this->once())
  346. ->method('registerBundles')
  347. ->will($this->returnValue(array($parent, $child1, $child2)))
  348. ;
  349. $kernel->initializeBundles();
  350. }
  351. /**
  352. * @expectedException \LogicException
  353. */
  354. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  355. {
  356. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  357. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  358. $kernel = $this->getKernel();
  359. $kernel
  360. ->expects($this->once())
  361. ->method('registerBundles')
  362. ->will($this->returnValue(array($fooBundle, $barBundle)))
  363. ;
  364. $kernel->initializeBundles();
  365. }
  366. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  367. {
  368. $bundle = $this
  369. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\BundleForTest')
  370. ->setMethods(array('getPath', 'getParent', 'getName'))
  371. ->disableOriginalConstructor()
  372. ;
  373. if ($className) {
  374. $bundle->setMockClassName($className);
  375. }
  376. $bundle = $bundle->getMockForAbstractClass();
  377. $bundle
  378. ->expects($this->any())
  379. ->method('getName')
  380. ->will($this->returnValue(is_null($bundleName) ? get_class($bundle) : $bundleName))
  381. ;
  382. $bundle
  383. ->expects($this->any())
  384. ->method('getPath')
  385. ->will($this->returnValue(strtr($dir, '\\', '/')))
  386. ;
  387. $bundle
  388. ->expects($this->any())
  389. ->method('getParent')
  390. ->will($this->returnValue($parent))
  391. ;
  392. return $bundle;
  393. }
  394. protected function getKernel()
  395. {
  396. return $this
  397. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  398. ->setMethods(array('getBundle', 'registerBundles'))
  399. ->disableOriginalConstructor()
  400. ->getMock()
  401. ;
  402. }
  403. protected function getKernelForInvalidLocateResource()
  404. {
  405. return $this
  406. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  407. ->disableOriginalConstructor()
  408. ->getMockForAbstractClass()
  409. ;
  410. }
  411. }
  412. class KernelForTest extends Kernel
  413. {
  414. public function getBundleMap()
  415. {
  416. return $this->bundleMap;
  417. }
  418. public function registerRootDir()
  419. {
  420. }
  421. public function registerBundles()
  422. {
  423. }
  424. public function registerBundleDirs()
  425. {
  426. }
  427. public function registerContainerConfiguration(LoaderInterface $loader)
  428. {
  429. }
  430. public function initializeBundles()
  431. {
  432. parent::initializeBundles();
  433. }
  434. public function isBooted()
  435. {
  436. return $this->booted;
  437. }
  438. }
  439. abstract class BundleForTest implements BundleInterface
  440. {
  441. // We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final
  442. }