KernelTest.php 24KB

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