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