KernelTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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();
  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. public function testLocateResourceReturnsTheFirstThatMatches()
  273. {
  274. $kernel = $this->getKernel();
  275. $kernel
  276. ->expects($this->once())
  277. ->method('getBundle')
  278. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  279. ;
  280. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
  281. }
  282. public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
  283. {
  284. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
  285. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
  286. $kernel = $this->getKernel();
  287. $kernel
  288. ->expects($this->exactly(2))
  289. ->method('getBundle')
  290. ->will($this->returnValue(array($child, $parent)))
  291. ;
  292. $this->assertEquals(__DIR__.'/Fixtures/Bundle2Bundle/foo.txt', $kernel->locateResource('@ParentAABundle/foo.txt'));
  293. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
  294. }
  295. public function testLocateResourceReturnsAllMatches()
  296. {
  297. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
  298. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
  299. $kernel = $this->getKernel();
  300. $kernel
  301. ->expects($this->once())
  302. ->method('getBundle')
  303. ->will($this->returnValue(array($child, $parent)))
  304. ;
  305. $this->assertEquals(array(
  306. __DIR__.'/Fixtures/Bundle2Bundle/foo.txt',
  307. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
  308. $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
  309. }
  310. public function testLocateResourceReturnsAllMatchesBis()
  311. {
  312. $kernel = $this->getKernel();
  313. $kernel
  314. ->expects($this->once())
  315. ->method('getBundle')
  316. ->will($this->returnValue(array(
  317. $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'),
  318. $this->getBundle(__DIR__.'/Foobar')
  319. )))
  320. ;
  321. $this->assertEquals(
  322. array(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
  323. $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false)
  324. );
  325. }
  326. public function testLocateResourceIgnoresDirOnNonResource()
  327. {
  328. $kernel = $this->getKernel();
  329. $kernel
  330. ->expects($this->once())
  331. ->method('getBundle')
  332. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  333. ;
  334. $this->assertEquals(
  335. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
  336. $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
  337. );
  338. }
  339. public function testLocateResourceReturnsTheDirOneForResources()
  340. {
  341. $kernel = $this->getKernel();
  342. $kernel
  343. ->expects($this->once())
  344. ->method('getBundle')
  345. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
  346. ;
  347. $this->assertEquals(
  348. __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
  349. $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  350. );
  351. }
  352. public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
  353. {
  354. $kernel = $this->getKernel();
  355. $kernel
  356. ->expects($this->once())
  357. ->method('getBundle')
  358. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
  359. ;
  360. $this->assertEquals(array(
  361. __DIR__.'/Fixtures/Resources/Bundle1Bundle/foo.txt',
  362. __DIR__.'/Fixtures/Bundle1Bundle/Resources/foo.txt'),
  363. $kernel->locateResource('@Bundle1Bundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  364. );
  365. }
  366. public function testLocateResourceOverrideBundleAndResourcesFolders()
  367. {
  368. $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
  369. $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'ParentBundle', 'ChildBundle', 'ChildBundle');
  370. $kernel = $this->getKernel();
  371. $kernel
  372. ->expects($this->exactly(4))
  373. ->method('getBundle')
  374. ->will($this->returnValue(array($child, $parent)))
  375. ;
  376. $this->assertEquals(array(
  377. __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
  378. __DIR__.'/Fixtures/ChildBundle/Resources/foo.txt',
  379. __DIR__.'/Fixtures/BaseBundle/Resources/foo.txt',
  380. ),
  381. $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  382. );
  383. $this->assertEquals(
  384. __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
  385. $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  386. );
  387. try {
  388. $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', false);
  389. $this->fail('Hidden resources should raise an exception when returning an array of matching paths');
  390. } catch (\RuntimeException $e) {
  391. }
  392. try {
  393. $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', true);
  394. $this->fail('Hidden resources should raise an exception when returning the first matching path');
  395. } catch (\RuntimeException $e) {
  396. }
  397. }
  398. public function testLocateResourceOnDirectories()
  399. {
  400. $kernel = $this->getKernel();
  401. $kernel
  402. ->expects($this->exactly(2))
  403. ->method('getBundle')
  404. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
  405. ;
  406. $this->assertEquals(
  407. __DIR__.'/Fixtures/Resources/FooBundle/',
  408. $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
  409. );
  410. $this->assertEquals(
  411. __DIR__.'/Fixtures/Resources/FooBundle',
  412. $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
  413. );
  414. $kernel = $this->getKernel();
  415. $kernel
  416. ->expects($this->exactly(2))
  417. ->method('getBundle')
  418. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
  419. ;
  420. $this->assertEquals(
  421. __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
  422. $kernel->locateResource('@Bundle1Bundle/Resources/')
  423. );
  424. $this->assertEquals(
  425. __DIR__.'/Fixtures/Bundle1Bundle/Resources',
  426. $kernel->locateResource('@Bundle1Bundle/Resources')
  427. );
  428. }
  429. public function testInitializeBundles()
  430. {
  431. $parent = $this->getBundle(null, null, 'ParentABundle');
  432. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  433. $kernel = $this->getKernel();
  434. $kernel
  435. ->expects($this->once())
  436. ->method('registerBundles')
  437. ->will($this->returnValue(array($parent, $child)))
  438. ;
  439. $kernel->initializeBundles();
  440. $map = $kernel->getBundleMap();
  441. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  442. }
  443. public function testInitializeBundlesSupportInheritanceCascade()
  444. {
  445. $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
  446. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  447. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  448. $kernel = $this->getKernel();
  449. $kernel
  450. ->expects($this->once())
  451. ->method('registerBundles')
  452. ->will($this->returnValue(array($grandparent, $parent, $child)))
  453. ;
  454. $kernel->initializeBundles();
  455. $map = $kernel->getBundleMap();
  456. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  457. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  458. $this->assertEquals(array($child), $map['ChildBBundle']);
  459. }
  460. /**
  461. * @expectedException \LogicException
  462. */
  463. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  464. {
  465. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  466. $kernel = $this->getKernel();
  467. $kernel
  468. ->expects($this->once())
  469. ->method('registerBundles')
  470. ->will($this->returnValue(array($child)))
  471. ;
  472. $kernel->initializeBundles();
  473. }
  474. public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
  475. {
  476. $grandparent = $this->getBundle(null, null, 'GrandParentCCundle');
  477. $parent = $this->getBundle(null, 'GrandParentCCundle', 'ParentCCundle');
  478. $child = $this->getBundle(null, 'ParentCCundle', 'ChildCCundle');
  479. $kernel = $this->getKernel();
  480. $kernel
  481. ->expects($this->once())
  482. ->method('registerBundles')
  483. ->will($this->returnValue(array($parent, $grandparent, $child)))
  484. ;
  485. $kernel->initializeBundles();
  486. $map = $kernel->getBundleMap();
  487. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCCundle']);
  488. $this->assertEquals(array($child, $parent), $map['ParentCCundle']);
  489. $this->assertEquals(array($child), $map['ChildCCundle']);
  490. }
  491. /**
  492. * @expectedException \LogicException
  493. */
  494. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  495. {
  496. $parent = $this->getBundle(null, null, 'ParentCBundle');
  497. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  498. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  499. $kernel = $this->getKernel();
  500. $kernel
  501. ->expects($this->once())
  502. ->method('registerBundles')
  503. ->will($this->returnValue(array($parent, $child1, $child2)))
  504. ;
  505. $kernel->initializeBundles();
  506. }
  507. /**
  508. * @expectedException \LogicException
  509. */
  510. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  511. {
  512. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  513. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  514. $kernel = $this->getKernel();
  515. $kernel
  516. ->expects($this->once())
  517. ->method('registerBundles')
  518. ->will($this->returnValue(array($fooBundle, $barBundle)))
  519. ;
  520. $kernel->initializeBundles();
  521. }
  522. /**
  523. * @expectedException \LogicException
  524. */
  525. public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
  526. {
  527. $circularRef = $this->getBundle(null, 'CircularRefBundle', 'CircularRefBundle');
  528. $kernel = $this->getKernel();
  529. $kernel
  530. ->expects($this->once())
  531. ->method('registerBundles')
  532. ->will($this->returnValue(array($circularRef)))
  533. ;
  534. $kernel->initializeBundles();
  535. }
  536. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  537. {
  538. $bundle = $this
  539. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\BundleForTest')
  540. ->setMethods(array('getPath', 'getParent', 'getName'))
  541. ->disableOriginalConstructor()
  542. ;
  543. if ($className) {
  544. $bundle->setMockClassName($className);
  545. }
  546. $bundle = $bundle->getMockForAbstractClass();
  547. $bundle
  548. ->expects($this->any())
  549. ->method('getName')
  550. ->will($this->returnValue(is_null($bundleName) ? get_class($bundle) : $bundleName))
  551. ;
  552. $bundle
  553. ->expects($this->any())
  554. ->method('getPath')
  555. ->will($this->returnValue($dir))
  556. ;
  557. $bundle
  558. ->expects($this->any())
  559. ->method('getParent')
  560. ->will($this->returnValue($parent))
  561. ;
  562. return $bundle;
  563. }
  564. protected function getKernel()
  565. {
  566. return $this
  567. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  568. ->setMethods(array('getBundle', 'registerBundles'))
  569. ->disableOriginalConstructor()
  570. ->getMock()
  571. ;
  572. }
  573. protected function getKernelForInvalidLocateResource()
  574. {
  575. return $this
  576. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  577. ->disableOriginalConstructor()
  578. ->getMockForAbstractClass()
  579. ;
  580. }
  581. }
  582. class KernelForTest extends Kernel
  583. {
  584. public function getBundleMap()
  585. {
  586. return $this->bundleMap;
  587. }
  588. public function registerBundles()
  589. {
  590. }
  591. public function registerBundleDirs()
  592. {
  593. }
  594. public function registerContainerConfiguration(LoaderInterface $loader)
  595. {
  596. }
  597. public function initializeBundles()
  598. {
  599. parent::initializeBundles();
  600. }
  601. public function isBooted()
  602. {
  603. return $this->booted;
  604. }
  605. public function setIsBooted($value)
  606. {
  607. $this->booted = (bool) $value;
  608. }
  609. }
  610. abstract class BundleForTest implements BundleInterface
  611. {
  612. // We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final
  613. }
  614. class FooBarBundle extends Bundle
  615. {
  616. // We need a full namespaced bundle instance to test isClassInActiveBundle
  617. }