KernelTest.php 29 KB

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