KernelTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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\Component\HttpKernel\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  14. use Symfony\Component\HttpKernel\Config\EnvParametersResource;
  15. use Symfony\Component\HttpKernel\Kernel;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
  20. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
  21. class KernelTest extends TestCase
  22. {
  23. public function testConstructor()
  24. {
  25. $env = 'test_env';
  26. $debug = true;
  27. $kernel = new KernelForTest($env, $debug);
  28. $this->assertEquals($env, $kernel->getEnvironment());
  29. $this->assertEquals($debug, $kernel->isDebug());
  30. $this->assertFalse($kernel->isBooted());
  31. $this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
  32. $this->assertNull($kernel->getContainer());
  33. }
  34. public function testClone()
  35. {
  36. $env = 'test_env';
  37. $debug = true;
  38. $kernel = new KernelForTest($env, $debug);
  39. $clone = clone $kernel;
  40. $this->assertEquals($env, $clone->getEnvironment());
  41. $this->assertEquals($debug, $clone->isDebug());
  42. $this->assertFalse($clone->isBooted());
  43. $this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
  44. $this->assertNull($clone->getContainer());
  45. }
  46. public function testBootInitializesBundlesAndContainer()
  47. {
  48. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
  49. $kernel->expects($this->once())
  50. ->method('initializeBundles');
  51. $kernel->expects($this->once())
  52. ->method('initializeContainer');
  53. $kernel->boot();
  54. }
  55. public function testBootSetsTheContainerToTheBundles()
  56. {
  57. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  58. $bundle->expects($this->once())
  59. ->method('setContainer');
  60. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'getBundles'));
  61. $kernel->expects($this->once())
  62. ->method('getBundles')
  63. ->will($this->returnValue(array($bundle)));
  64. $kernel->boot();
  65. }
  66. public function testBootSetsTheBootedFlagToTrue()
  67. {
  68. // use test kernel to access isBooted()
  69. $kernel = $this->getKernelForTest(array('initializeBundles', 'initializeContainer'));
  70. $kernel->boot();
  71. $this->assertTrue($kernel->isBooted());
  72. }
  73. public function testClassCacheIsLoaded()
  74. {
  75. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  76. $kernel->loadClassCache('name', '.extension');
  77. $kernel->expects($this->once())
  78. ->method('doLoadClassCache')
  79. ->with('name', '.extension');
  80. $kernel->boot();
  81. }
  82. public function testClassCacheIsNotLoadedByDefault()
  83. {
  84. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  85. $kernel->expects($this->never())
  86. ->method('doLoadClassCache');
  87. $kernel->boot();
  88. }
  89. public function testClassCacheIsNotLoadedWhenKernelIsNotBooted()
  90. {
  91. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  92. $kernel->loadClassCache();
  93. $kernel->expects($this->never())
  94. ->method('doLoadClassCache');
  95. }
  96. public function testEnvParametersResourceIsAdded()
  97. {
  98. $container = new ContainerBuilder();
  99. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
  100. ->disableOriginalConstructor()
  101. ->setMethods(array('getContainerBuilder', 'prepareContainer', 'getCacheDir', 'getLogDir'))
  102. ->getMock();
  103. $kernel->expects($this->any())
  104. ->method('getContainerBuilder')
  105. ->will($this->returnValue($container));
  106. $kernel->expects($this->any())
  107. ->method('prepareContainer')
  108. ->will($this->returnValue(null));
  109. $kernel->expects($this->any())
  110. ->method('getCacheDir')
  111. ->will($this->returnValue(sys_get_temp_dir()));
  112. $kernel->expects($this->any())
  113. ->method('getLogDir')
  114. ->will($this->returnValue(sys_get_temp_dir()));
  115. $reflection = new \ReflectionClass(get_class($kernel));
  116. $method = $reflection->getMethod('buildContainer');
  117. $method->setAccessible(true);
  118. $method->invoke($kernel);
  119. $found = false;
  120. foreach ($container->getResources() as $resource) {
  121. if ($resource instanceof EnvParametersResource) {
  122. $found = true;
  123. break;
  124. }
  125. }
  126. $this->assertTrue($found);
  127. }
  128. public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
  129. {
  130. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
  131. $kernel->expects($this->once())
  132. ->method('initializeBundles');
  133. $kernel->boot();
  134. $kernel->boot();
  135. }
  136. public function testShutdownCallsShutdownOnAllBundles()
  137. {
  138. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  139. $bundle->expects($this->once())
  140. ->method('shutdown');
  141. $kernel = $this->getKernel(array(), array($bundle));
  142. $kernel->boot();
  143. $kernel->shutdown();
  144. }
  145. public function testShutdownGivesNullContainerToAllBundles()
  146. {
  147. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  148. $bundle->expects($this->at(3))
  149. ->method('setContainer')
  150. ->with(null);
  151. $kernel = $this->getKernel(array('getBundles'));
  152. $kernel->expects($this->any())
  153. ->method('getBundles')
  154. ->will($this->returnValue(array($bundle)));
  155. $kernel->boot();
  156. $kernel->shutdown();
  157. }
  158. public function testHandleCallsHandleOnHttpKernel()
  159. {
  160. $type = HttpKernelInterface::MASTER_REQUEST;
  161. $catch = true;
  162. $request = new Request();
  163. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $httpKernelMock
  167. ->expects($this->once())
  168. ->method('handle')
  169. ->with($request, $type, $catch);
  170. $kernel = $this->getKernel(array('getHttpKernel'));
  171. $kernel->expects($this->once())
  172. ->method('getHttpKernel')
  173. ->will($this->returnValue($httpKernelMock));
  174. $kernel->handle($request, $type, $catch);
  175. }
  176. public function testHandleBootsTheKernel()
  177. {
  178. $type = HttpKernelInterface::MASTER_REQUEST;
  179. $catch = true;
  180. $request = new Request();
  181. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $kernel = $this->getKernel(array('getHttpKernel', 'boot'));
  185. $kernel->expects($this->once())
  186. ->method('getHttpKernel')
  187. ->will($this->returnValue($httpKernelMock));
  188. $kernel->expects($this->once())
  189. ->method('boot');
  190. $kernel->handle($request, $type, $catch);
  191. }
  192. public function testStripComments()
  193. {
  194. $source = <<<'EOF'
  195. <?php
  196. $string = 'string should not be modified';
  197. $string = 'string should not be
  198. modified';
  199. $heredoc = <<<HD
  200. Heredoc should not be modified {$a[1+$b]}
  201. HD;
  202. $nowdoc = <<<'ND'
  203. Nowdoc should not be modified
  204. ND;
  205. /**
  206. * some class comments to strip
  207. */
  208. class TestClass
  209. {
  210. /**
  211. * some method comments to strip
  212. */
  213. public function doStuff()
  214. {
  215. // inline comment
  216. }
  217. }
  218. EOF;
  219. $expected = <<<'EOF'
  220. <?php
  221. $string = 'string should not be modified';
  222. $string = 'string should not be
  223. modified';
  224. $heredoc = <<<HD
  225. Heredoc should not be modified {$a[1+$b]}
  226. HD;
  227. $nowdoc = <<<'ND'
  228. Nowdoc should not be modified
  229. ND;
  230. class TestClass
  231. {
  232. public function doStuff()
  233. {
  234. }
  235. }
  236. EOF;
  237. $output = Kernel::stripComments($source);
  238. // Heredocs are preserved, making the output mixing Unix and Windows line
  239. // endings, switching to "\n" everywhere on Windows to avoid failure.
  240. if ('\\' === DIRECTORY_SEPARATOR) {
  241. $expected = str_replace("\r\n", "\n", $expected);
  242. $output = str_replace("\r\n", "\n", $output);
  243. }
  244. $this->assertEquals($expected, $output);
  245. }
  246. public function testGetRootDir()
  247. {
  248. $kernel = new KernelForTest('test', true);
  249. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
  250. }
  251. public function testGetName()
  252. {
  253. $kernel = new KernelForTest('test', true);
  254. $this->assertEquals('Fixtures', $kernel->getName());
  255. }
  256. public function testOverrideGetName()
  257. {
  258. $kernel = new KernelForOverrideName('test', true);
  259. $this->assertEquals('overridden', $kernel->getName());
  260. }
  261. public function testSerialize()
  262. {
  263. $env = 'test_env';
  264. $debug = true;
  265. $kernel = new KernelForTest($env, $debug);
  266. $expected = serialize(array($env, $debug));
  267. $this->assertEquals($expected, $kernel->serialize());
  268. }
  269. /**
  270. * @expectedException \InvalidArgumentException
  271. */
  272. public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
  273. {
  274. $this->getKernel()->locateResource('Foo');
  275. }
  276. /**
  277. * @expectedException \RuntimeException
  278. */
  279. public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
  280. {
  281. $this->getKernel()->locateResource('@FooBundle/../bar');
  282. }
  283. /**
  284. * @expectedException \InvalidArgumentException
  285. */
  286. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  287. {
  288. $this->getKernel()->locateResource('@FooBundle/config/routing.xml');
  289. }
  290. /**
  291. * @expectedException \InvalidArgumentException
  292. */
  293. public function testLocateResourceThrowsExceptionWhenResourceDoesNotExist()
  294. {
  295. $kernel = $this->getKernel(array('getBundle'));
  296. $kernel
  297. ->expects($this->once())
  298. ->method('getBundle')
  299. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  300. ;
  301. $kernel->locateResource('@Bundle1Bundle/config/routing.xml');
  302. }
  303. public function testLocateResourceReturnsTheFirstThatMatches()
  304. {
  305. $kernel = $this->getKernel(array('getBundle'));
  306. $kernel
  307. ->expects($this->once())
  308. ->method('getBundle')
  309. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  310. ;
  311. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
  312. }
  313. public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
  314. {
  315. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
  316. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
  317. $kernel = $this->getKernel(array('getBundle'));
  318. $kernel
  319. ->expects($this->exactly(2))
  320. ->method('getBundle')
  321. ->will($this->returnValue(array($child, $parent)))
  322. ;
  323. $this->assertEquals(__DIR__.'/Fixtures/Bundle2Bundle/foo.txt', $kernel->locateResource('@ParentAABundle/foo.txt'));
  324. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
  325. }
  326. public function testLocateResourceReturnsAllMatches()
  327. {
  328. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
  329. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
  330. $kernel = $this->getKernel(array('getBundle'));
  331. $kernel
  332. ->expects($this->once())
  333. ->method('getBundle')
  334. ->will($this->returnValue(array($child, $parent)))
  335. ;
  336. $this->assertEquals(array(
  337. __DIR__.'/Fixtures/Bundle2Bundle/foo.txt',
  338. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt', ),
  339. $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
  340. }
  341. public function testLocateResourceReturnsAllMatchesBis()
  342. {
  343. $kernel = $this->getKernel(array('getBundle'));
  344. $kernel
  345. ->expects($this->once())
  346. ->method('getBundle')
  347. ->will($this->returnValue(array(
  348. $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'),
  349. $this->getBundle(__DIR__.'/Foobar'),
  350. )))
  351. ;
  352. $this->assertEquals(
  353. array(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
  354. $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false)
  355. );
  356. }
  357. public function testLocateResourceIgnoresDirOnNonResource()
  358. {
  359. $kernel = $this->getKernel(array('getBundle'));
  360. $kernel
  361. ->expects($this->once())
  362. ->method('getBundle')
  363. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  364. ;
  365. $this->assertEquals(
  366. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
  367. $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
  368. );
  369. }
  370. public function testLocateResourceReturnsTheDirOneForResources()
  371. {
  372. $kernel = $this->getKernel(array('getBundle'));
  373. $kernel
  374. ->expects($this->once())
  375. ->method('getBundle')
  376. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
  377. ;
  378. $this->assertEquals(
  379. __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
  380. $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  381. );
  382. }
  383. public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
  384. {
  385. $kernel = $this->getKernel(array('getBundle'));
  386. $kernel
  387. ->expects($this->once())
  388. ->method('getBundle')
  389. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
  390. ;
  391. $this->assertEquals(array(
  392. __DIR__.'/Fixtures/Resources/Bundle1Bundle/foo.txt',
  393. __DIR__.'/Fixtures/Bundle1Bundle/Resources/foo.txt', ),
  394. $kernel->locateResource('@Bundle1Bundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  395. );
  396. }
  397. public function testLocateResourceOverrideBundleAndResourcesFolders()
  398. {
  399. $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
  400. $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'ParentBundle', 'ChildBundle', 'ChildBundle');
  401. $kernel = $this->getKernel(array('getBundle'));
  402. $kernel
  403. ->expects($this->exactly(4))
  404. ->method('getBundle')
  405. ->will($this->returnValue(array($child, $parent)))
  406. ;
  407. $this->assertEquals(array(
  408. __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
  409. __DIR__.'/Fixtures/ChildBundle/Resources/foo.txt',
  410. __DIR__.'/Fixtures/BaseBundle/Resources/foo.txt',
  411. ),
  412. $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  413. );
  414. $this->assertEquals(
  415. __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
  416. $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  417. );
  418. try {
  419. $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', false);
  420. $this->fail('Hidden resources should raise an exception when returning an array of matching paths');
  421. } catch (\RuntimeException $e) {
  422. }
  423. try {
  424. $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', true);
  425. $this->fail('Hidden resources should raise an exception when returning the first matching path');
  426. } catch (\RuntimeException $e) {
  427. }
  428. }
  429. public function testLocateResourceOnDirectories()
  430. {
  431. $kernel = $this->getKernel(array('getBundle'));
  432. $kernel
  433. ->expects($this->exactly(2))
  434. ->method('getBundle')
  435. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
  436. ;
  437. $this->assertEquals(
  438. __DIR__.'/Fixtures/Resources/FooBundle/',
  439. $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
  440. );
  441. $this->assertEquals(
  442. __DIR__.'/Fixtures/Resources/FooBundle',
  443. $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
  444. );
  445. $kernel = $this->getKernel(array('getBundle'));
  446. $kernel
  447. ->expects($this->exactly(2))
  448. ->method('getBundle')
  449. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
  450. ;
  451. $this->assertEquals(
  452. __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
  453. $kernel->locateResource('@Bundle1Bundle/Resources/')
  454. );
  455. $this->assertEquals(
  456. __DIR__.'/Fixtures/Bundle1Bundle/Resources',
  457. $kernel->locateResource('@Bundle1Bundle/Resources')
  458. );
  459. }
  460. public function testInitializeBundles()
  461. {
  462. $parent = $this->getBundle(null, null, 'ParentABundle');
  463. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  464. // use test kernel so we can access getBundleMap()
  465. $kernel = $this->getKernelForTest(array('registerBundles'));
  466. $kernel
  467. ->expects($this->once())
  468. ->method('registerBundles')
  469. ->will($this->returnValue(array($parent, $child)))
  470. ;
  471. $kernel->boot();
  472. $map = $kernel->getBundleMap();
  473. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  474. }
  475. public function testInitializeBundlesSupportInheritanceCascade()
  476. {
  477. $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
  478. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  479. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  480. // use test kernel so we can access getBundleMap()
  481. $kernel = $this->getKernelForTest(array('registerBundles'));
  482. $kernel
  483. ->expects($this->once())
  484. ->method('registerBundles')
  485. ->will($this->returnValue(array($grandparent, $parent, $child)))
  486. ;
  487. $kernel->boot();
  488. $map = $kernel->getBundleMap();
  489. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  490. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  491. $this->assertEquals(array($child), $map['ChildBBundle']);
  492. }
  493. /**
  494. * @expectedException \LogicException
  495. * @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered.
  496. */
  497. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  498. {
  499. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  500. $kernel = $this->getKernel(array(), array($child));
  501. $kernel->boot();
  502. }
  503. public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
  504. {
  505. $grandparent = $this->getBundle(null, null, 'GrandParentCBundle');
  506. $parent = $this->getBundle(null, 'GrandParentCBundle', 'ParentCBundle');
  507. $child = $this->getBundle(null, 'ParentCBundle', 'ChildCBundle');
  508. // use test kernel so we can access getBundleMap()
  509. $kernel = $this->getKernelForTest(array('registerBundles'));
  510. $kernel
  511. ->expects($this->once())
  512. ->method('registerBundles')
  513. ->will($this->returnValue(array($parent, $grandparent, $child)))
  514. ;
  515. $kernel->boot();
  516. $map = $kernel->getBundleMap();
  517. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCBundle']);
  518. $this->assertEquals(array($child, $parent), $map['ParentCBundle']);
  519. $this->assertEquals(array($child), $map['ChildCBundle']);
  520. }
  521. /**
  522. * @expectedException \LogicException
  523. * @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle".
  524. */
  525. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  526. {
  527. $parent = $this->getBundle(null, null, 'ParentCBundle');
  528. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  529. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  530. $kernel = $this->getKernel(array(), array($parent, $child1, $child2));
  531. $kernel->boot();
  532. }
  533. /**
  534. * @expectedException \LogicException
  535. * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
  536. */
  537. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  538. {
  539. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  540. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  541. $kernel = $this->getKernel(array(), array($fooBundle, $barBundle));
  542. $kernel->boot();
  543. }
  544. /**
  545. * @expectedException \LogicException
  546. * @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself.
  547. */
  548. public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
  549. {
  550. $circularRef = $this->getBundle(null, 'CircularRefBundle', 'CircularRefBundle');
  551. $kernel = $this->getKernel(array(), array($circularRef));
  552. $kernel->boot();
  553. }
  554. public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
  555. {
  556. $kernel = $this->getKernel(array('getHttpKernel'));
  557. $kernel->expects($this->never())
  558. ->method('getHttpKernel');
  559. $kernel->terminate(Request::create('/'), new Response());
  560. }
  561. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  562. {
  563. // does not implement TerminableInterface
  564. $httpKernel = new TestKernel();
  565. $kernel = $this->getKernel(array('getHttpKernel'));
  566. $kernel->expects($this->once())
  567. ->method('getHttpKernel')
  568. ->willReturn($httpKernel);
  569. $kernel->boot();
  570. $kernel->terminate(Request::create('/'), new Response());
  571. $this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
  572. // implements TerminableInterface
  573. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  574. ->disableOriginalConstructor()
  575. ->setMethods(array('terminate'))
  576. ->getMock();
  577. $httpKernelMock
  578. ->expects($this->once())
  579. ->method('terminate');
  580. $kernel = $this->getKernel(array('getHttpKernel'));
  581. $kernel->expects($this->exactly(2))
  582. ->method('getHttpKernel')
  583. ->will($this->returnValue($httpKernelMock));
  584. $kernel->boot();
  585. $kernel->terminate(Request::create('/'), new Response());
  586. }
  587. public function testKernelRootDirNameStartingWithANumber()
  588. {
  589. $dir = __DIR__.'/Fixtures/123';
  590. require_once $dir.'/Kernel123.php';
  591. $kernel = new \Symfony\Component\HttpKernel\Tests\Fixtures\_123\Kernel123('dev', true);
  592. $this->assertEquals('_123', $kernel->getName());
  593. }
  594. /**
  595. * Returns a mock for the BundleInterface.
  596. *
  597. * @return BundleInterface
  598. */
  599. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  600. {
  601. $bundle = $this
  602. ->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')
  603. ->setMethods(array('getPath', 'getParent', 'getName'))
  604. ->disableOriginalConstructor()
  605. ;
  606. if ($className) {
  607. $bundle->setMockClassName($className);
  608. }
  609. $bundle = $bundle->getMockForAbstractClass();
  610. $bundle
  611. ->expects($this->any())
  612. ->method('getName')
  613. ->will($this->returnValue(null === $bundleName ? get_class($bundle) : $bundleName))
  614. ;
  615. $bundle
  616. ->expects($this->any())
  617. ->method('getPath')
  618. ->will($this->returnValue($dir))
  619. ;
  620. $bundle
  621. ->expects($this->any())
  622. ->method('getParent')
  623. ->will($this->returnValue($parent))
  624. ;
  625. return $bundle;
  626. }
  627. /**
  628. * Returns a mock for the abstract kernel.
  629. *
  630. * @param array $methods Additional methods to mock (besides the abstract ones)
  631. * @param array $bundles Bundles to register
  632. *
  633. * @return Kernel
  634. */
  635. protected function getKernel(array $methods = array(), array $bundles = array())
  636. {
  637. $methods[] = 'registerBundles';
  638. $kernel = $this
  639. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  640. ->setMethods($methods)
  641. ->setConstructorArgs(array('test', false))
  642. ->getMockForAbstractClass()
  643. ;
  644. $kernel->expects($this->any())
  645. ->method('registerBundles')
  646. ->will($this->returnValue($bundles))
  647. ;
  648. $p = new \ReflectionProperty($kernel, 'rootDir');
  649. $p->setAccessible(true);
  650. $p->setValue($kernel, __DIR__.'/Fixtures');
  651. return $kernel;
  652. }
  653. protected function getKernelForTest(array $methods = array())
  654. {
  655. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
  656. ->setConstructorArgs(array('test', false))
  657. ->setMethods($methods)
  658. ->getMock();
  659. $p = new \ReflectionProperty($kernel, 'rootDir');
  660. $p->setAccessible(true);
  661. $p->setValue($kernel, __DIR__.'/Fixtures');
  662. return $kernel;
  663. }
  664. }
  665. class TestKernel implements HttpKernelInterface
  666. {
  667. public $terminateCalled = false;
  668. public function terminate()
  669. {
  670. $this->terminateCalled = true;
  671. }
  672. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  673. {
  674. }
  675. }