KernelTest.php 28 KB

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