KernelTest.php 15 KB

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