KernelTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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('@Foo/../bar');
  264. }
  265. /**
  266. * @expectedException \InvalidArgumentException
  267. */
  268. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  269. {
  270. $this->getKernelForInvalidLocateResource()->locateResource('@Foo/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('@Bundle1/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('@ParentAA/foo.txt'));
  293. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAA/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('@Bundle1/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('@Bundle1/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('@Bundle1/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, 'Foo'))))
  346. ;
  347. $this->assertEquals(
  348. __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
  349. $kernel->locateResource('@Foo/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, 'Bundle1'))))
  359. ;
  360. $this->assertEquals(array(
  361. __DIR__.'/Fixtures/Resources/Bundle1Bundle/foo.txt',
  362. __DIR__.'/Fixtures/Bundle1Bundle/Resources/foo.txt'),
  363. $kernel->locateResource('@Bundle1/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  364. );
  365. }
  366. public function testLocateResourceOverrideBundleAndResourcesFolders()
  367. {
  368. $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'Base', 'Base');
  369. $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'Parent', 'Child', 'Child');
  370. $kernel = $this->getKernel();
  371. $kernel
  372. ->expects($this->once())
  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('@Child/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  382. );
  383. }
  384. public function testLocateResourceOnDirectories()
  385. {
  386. $kernel = $this->getKernel();
  387. $kernel
  388. ->expects($this->exactly(2))
  389. ->method('getBundle')
  390. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'Foo'))))
  391. ;
  392. $this->assertEquals(
  393. __DIR__.'/Fixtures/Resources/FooBundle/',
  394. $kernel->locateResource('@Foo/Resources/', __DIR__.'/Fixtures/Resources')
  395. );
  396. $this->assertEquals(
  397. __DIR__.'/Fixtures/Resources/FooBundle',
  398. $kernel->locateResource('@Foo/Resources', __DIR__.'/Fixtures/Resources')
  399. );
  400. $kernel = $this->getKernel();
  401. $kernel
  402. ->expects($this->exactly(2))
  403. ->method('getBundle')
  404. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1'))))
  405. ;
  406. $this->assertEquals(
  407. __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
  408. $kernel->locateResource('@Bundle1/Resources/')
  409. );
  410. $this->assertEquals(
  411. __DIR__.'/Fixtures/Bundle1Bundle/Resources',
  412. $kernel->locateResource('@Bundle1/Resources')
  413. );
  414. }
  415. public function testInitializeBundles()
  416. {
  417. $parent = $this->getBundle(null, null, 'ParentABundle');
  418. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  419. $kernel = $this->getKernel();
  420. $kernel
  421. ->expects($this->once())
  422. ->method('registerBundles')
  423. ->will($this->returnValue(array($parent, $child)))
  424. ;
  425. $kernel->initializeBundles();
  426. $map = $kernel->getBundleMap();
  427. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  428. }
  429. public function testInitializeBundlesSupportInheritanceCascade()
  430. {
  431. $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
  432. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  433. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  434. $kernel = $this->getKernel();
  435. $kernel
  436. ->expects($this->once())
  437. ->method('registerBundles')
  438. ->will($this->returnValue(array($grandparent, $parent, $child)))
  439. ;
  440. $kernel->initializeBundles();
  441. $map = $kernel->getBundleMap();
  442. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  443. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  444. $this->assertEquals(array($child), $map['ChildBBundle']);
  445. }
  446. /**
  447. * @expectedException \LogicException
  448. */
  449. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  450. {
  451. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  452. $kernel = $this->getKernel();
  453. $kernel
  454. ->expects($this->once())
  455. ->method('registerBundles')
  456. ->will($this->returnValue(array($child)))
  457. ;
  458. $kernel->initializeBundles();
  459. }
  460. public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
  461. {
  462. $grandparent = $this->getBundle(null, null, 'GrandParentCCundle');
  463. $parent = $this->getBundle(null, 'GrandParentCCundle', 'ParentCCundle');
  464. $child = $this->getBundle(null, 'ParentCCundle', 'ChildCCundle');
  465. $kernel = $this->getKernel();
  466. $kernel
  467. ->expects($this->once())
  468. ->method('registerBundles')
  469. ->will($this->returnValue(array($parent, $grandparent, $child)))
  470. ;
  471. $kernel->initializeBundles();
  472. $map = $kernel->getBundleMap();
  473. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCCundle']);
  474. $this->assertEquals(array($child, $parent), $map['ParentCCundle']);
  475. $this->assertEquals(array($child), $map['ChildCCundle']);
  476. }
  477. /**
  478. * @expectedException \LogicException
  479. */
  480. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  481. {
  482. $parent = $this->getBundle(null, null, 'ParentCBundle');
  483. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  484. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  485. $kernel = $this->getKernel();
  486. $kernel
  487. ->expects($this->once())
  488. ->method('registerBundles')
  489. ->will($this->returnValue(array($parent, $child1, $child2)))
  490. ;
  491. $kernel->initializeBundles();
  492. }
  493. /**
  494. * @expectedException \LogicException
  495. */
  496. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  497. {
  498. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  499. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  500. $kernel = $this->getKernel();
  501. $kernel
  502. ->expects($this->once())
  503. ->method('registerBundles')
  504. ->will($this->returnValue(array($fooBundle, $barBundle)))
  505. ;
  506. $kernel->initializeBundles();
  507. }
  508. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  509. {
  510. $bundle = $this
  511. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\BundleForTest')
  512. ->setMethods(array('getPath', 'getParent', 'getName'))
  513. ->disableOriginalConstructor()
  514. ;
  515. if ($className) {
  516. $bundle->setMockClassName($className);
  517. }
  518. $bundle = $bundle->getMockForAbstractClass();
  519. $bundle
  520. ->expects($this->any())
  521. ->method('getName')
  522. ->will($this->returnValue(is_null($bundleName) ? get_class($bundle) : $bundleName))
  523. ;
  524. $bundle
  525. ->expects($this->any())
  526. ->method('getPath')
  527. ->will($this->returnValue(strtr($dir, '\\', '/')))
  528. ;
  529. $bundle
  530. ->expects($this->any())
  531. ->method('getParent')
  532. ->will($this->returnValue($parent))
  533. ;
  534. return $bundle;
  535. }
  536. protected function getKernel()
  537. {
  538. return $this
  539. ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  540. ->setMethods(array('getBundle', 'registerBundles'))
  541. ->disableOriginalConstructor()
  542. ->getMock()
  543. ;
  544. }
  545. protected function getKernelForInvalidLocateResource()
  546. {
  547. return $this
  548. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  549. ->disableOriginalConstructor()
  550. ->getMockForAbstractClass()
  551. ;
  552. }
  553. }
  554. class KernelForTest extends Kernel
  555. {
  556. public function getBundleMap()
  557. {
  558. return $this->bundleMap;
  559. }
  560. public function registerBundles()
  561. {
  562. }
  563. public function registerBundleDirs()
  564. {
  565. }
  566. public function registerContainerConfiguration(LoaderInterface $loader)
  567. {
  568. }
  569. public function initializeBundles()
  570. {
  571. parent::initializeBundles();
  572. }
  573. public function isBooted()
  574. {
  575. return $this->booted;
  576. }
  577. public function setIsBooted($value)
  578. {
  579. $this->booted = (bool) $value;
  580. }
  581. }
  582. abstract class BundleForTest implements BundleInterface
  583. {
  584. // We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final
  585. }
  586. class FooBarBundle extends Bundle
  587. {
  588. // We need a full namespaced bundle instance to test isClassInActiveBundle
  589. }