菜谱项目

KernelTest.php 29KB

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