KernelTest.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Loader\LoaderInterface;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  17. use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
  18. use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
  19. use Symfony\Component\HttpKernel\Kernel;
  20. use Symfony\Component\HttpKernel\HttpKernelInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
  24. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
  25. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelWithoutBundles;
  26. use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
  27. class KernelTest extends TestCase
  28. {
  29. public static function tearDownAfterClass()
  30. {
  31. $fs = new Filesystem();
  32. $fs->remove(__DIR__.'/Fixtures/cache');
  33. }
  34. public function testConstructor()
  35. {
  36. $env = 'test_env';
  37. $debug = true;
  38. $kernel = new KernelForTest($env, $debug);
  39. $this->assertEquals($env, $kernel->getEnvironment());
  40. $this->assertEquals($debug, $kernel->isDebug());
  41. $this->assertFalse($kernel->isBooted());
  42. $this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
  43. $this->assertNull($kernel->getContainer());
  44. }
  45. public function testClone()
  46. {
  47. $env = 'test_env';
  48. $debug = true;
  49. $kernel = new KernelForTest($env, $debug);
  50. $clone = clone $kernel;
  51. $this->assertEquals($env, $clone->getEnvironment());
  52. $this->assertEquals($debug, $clone->isDebug());
  53. $this->assertFalse($clone->isBooted());
  54. $this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
  55. $this->assertNull($clone->getContainer());
  56. }
  57. public function testInitializeContainerClearsOldContainers()
  58. {
  59. $fs = new Filesystem();
  60. $legacyContainerDir = __DIR__.'/Fixtures/cache/custom/ContainerA123456';
  61. $fs->mkdir($legacyContainerDir);
  62. touch($legacyContainerDir.'.legacy');
  63. $kernel = new CustomProjectDirKernel();
  64. $kernel->boot();
  65. $containerDir = __DIR__.'/Fixtures/cache/custom/'.substr(get_class($kernel->getContainer()), 0, 16);
  66. $this->assertTrue(unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta'));
  67. $this->assertFileExists($containerDir);
  68. $this->assertFileNotExists($containerDir.'.legacy');
  69. $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
  70. $kernel->boot();
  71. $this->assertFileExists($containerDir);
  72. $this->assertFileExists($containerDir.'.legacy');
  73. $this->assertFileNotExists($legacyContainerDir);
  74. $this->assertFileNotExists($legacyContainerDir.'.legacy');
  75. }
  76. public function testBootInitializesBundlesAndContainer()
  77. {
  78. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
  79. $kernel->expects($this->once())
  80. ->method('initializeBundles');
  81. $kernel->expects($this->once())
  82. ->method('initializeContainer');
  83. $kernel->boot();
  84. }
  85. public function testBootSetsTheContainerToTheBundles()
  86. {
  87. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  88. $bundle->expects($this->once())
  89. ->method('setContainer');
  90. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'getBundles'));
  91. $kernel->expects($this->once())
  92. ->method('getBundles')
  93. ->will($this->returnValue(array($bundle)));
  94. $kernel->boot();
  95. }
  96. public function testBootSetsTheBootedFlagToTrue()
  97. {
  98. // use test kernel to access isBooted()
  99. $kernel = $this->getKernelForTest(array('initializeBundles', 'initializeContainer'));
  100. $kernel->boot();
  101. $this->assertTrue($kernel->isBooted());
  102. }
  103. public function testClassCacheIsNotLoadedByDefault()
  104. {
  105. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  106. $kernel->expects($this->never())
  107. ->method('doLoadClassCache');
  108. $kernel->boot();
  109. }
  110. public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
  111. {
  112. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
  113. $kernel->expects($this->once())
  114. ->method('initializeBundles');
  115. $kernel->boot();
  116. $kernel->boot();
  117. }
  118. public function testShutdownCallsShutdownOnAllBundles()
  119. {
  120. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  121. $bundle->expects($this->once())
  122. ->method('shutdown');
  123. $kernel = $this->getKernel(array(), array($bundle));
  124. $kernel->boot();
  125. $kernel->shutdown();
  126. }
  127. public function testShutdownGivesNullContainerToAllBundles()
  128. {
  129. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
  130. $bundle->expects($this->at(3))
  131. ->method('setContainer')
  132. ->with(null);
  133. $kernel = $this->getKernel(array('getBundles'));
  134. $kernel->expects($this->any())
  135. ->method('getBundles')
  136. ->will($this->returnValue(array($bundle)));
  137. $kernel->boot();
  138. $kernel->shutdown();
  139. }
  140. public function testHandleCallsHandleOnHttpKernel()
  141. {
  142. $type = HttpKernelInterface::MASTER_REQUEST;
  143. $catch = true;
  144. $request = new Request();
  145. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $httpKernelMock
  149. ->expects($this->once())
  150. ->method('handle')
  151. ->with($request, $type, $catch);
  152. $kernel = $this->getKernel(array('getHttpKernel'));
  153. $kernel->expects($this->once())
  154. ->method('getHttpKernel')
  155. ->will($this->returnValue($httpKernelMock));
  156. $kernel->handle($request, $type, $catch);
  157. }
  158. public function testHandleBootsTheKernel()
  159. {
  160. $type = HttpKernelInterface::MASTER_REQUEST;
  161. $catch = true;
  162. $request = new Request();
  163. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $kernel = $this->getKernel(array('getHttpKernel', 'boot'));
  167. $kernel->expects($this->once())
  168. ->method('getHttpKernel')
  169. ->will($this->returnValue($httpKernelMock));
  170. $kernel->expects($this->once())
  171. ->method('boot');
  172. $kernel->handle($request, $type, $catch);
  173. }
  174. public function testStripComments()
  175. {
  176. $source = <<<'EOF'
  177. <?php
  178. $string = 'string should not be modified';
  179. $string = 'string should not be
  180. modified';
  181. $heredoc = <<<HD
  182. Heredoc should not be modified {$a[1+$b]}
  183. HD;
  184. $nowdoc = <<<'ND'
  185. Nowdoc should not be modified
  186. ND;
  187. /**
  188. * some class comments to strip
  189. */
  190. class TestClass
  191. {
  192. /**
  193. * some method comments to strip
  194. */
  195. public function doStuff()
  196. {
  197. // inline comment
  198. }
  199. }
  200. EOF;
  201. $expected = <<<'EOF'
  202. <?php
  203. $string = 'string should not be modified';
  204. $string = 'string should not be
  205. modified';
  206. $heredoc = <<<HD
  207. Heredoc should not be modified {$a[1+$b]}
  208. HD;
  209. $nowdoc = <<<'ND'
  210. Nowdoc should not be modified
  211. ND;
  212. class TestClass
  213. {
  214. public function doStuff()
  215. {
  216. }
  217. }
  218. EOF;
  219. $output = Kernel::stripComments($source);
  220. // Heredocs are preserved, making the output mixing Unix and Windows line
  221. // endings, switching to "\n" everywhere on Windows to avoid failure.
  222. if ('\\' === DIRECTORY_SEPARATOR) {
  223. $expected = str_replace("\r\n", "\n", $expected);
  224. $output = str_replace("\r\n", "\n", $output);
  225. }
  226. $this->assertEquals($expected, $output);
  227. }
  228. public function testGetRootDir()
  229. {
  230. $kernel = new KernelForTest('test', true);
  231. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
  232. }
  233. public function testGetName()
  234. {
  235. $kernel = new KernelForTest('test', true);
  236. $this->assertEquals('Fixtures', $kernel->getName());
  237. }
  238. public function testOverrideGetName()
  239. {
  240. $kernel = new KernelForOverrideName('test', true);
  241. $this->assertEquals('overridden', $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->getKernel()->locateResource('Foo');
  257. }
  258. /**
  259. * @expectedException \RuntimeException
  260. */
  261. public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
  262. {
  263. $this->getKernel()->locateResource('@FooBundle/../bar');
  264. }
  265. /**
  266. * @expectedException \InvalidArgumentException
  267. */
  268. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  269. {
  270. $this->getKernel()->locateResource('@FooBundle/config/routing.xml');
  271. }
  272. /**
  273. * @expectedException \InvalidArgumentException
  274. */
  275. public function testLocateResourceThrowsExceptionWhenResourceDoesNotExist()
  276. {
  277. $kernel = $this->getKernel(array('getBundle'));
  278. $kernel
  279. ->expects($this->once())
  280. ->method('getBundle')
  281. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle')))
  282. ;
  283. $kernel->locateResource('@Bundle1Bundle/config/routing.xml');
  284. }
  285. public function testLocateResourceReturnsTheFirstThatMatches()
  286. {
  287. $kernel = $this->getKernel(array('getBundle'));
  288. $kernel
  289. ->expects($this->once())
  290. ->method('getBundle')
  291. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle')))
  292. ;
  293. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
  294. }
  295. public function testLocateResourceIgnoresDirOnNonResource()
  296. {
  297. $kernel = $this->getKernel(array('getBundle'));
  298. $kernel
  299. ->expects($this->once())
  300. ->method('getBundle')
  301. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle')))
  302. ;
  303. $this->assertEquals(
  304. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
  305. $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
  306. );
  307. }
  308. public function testLocateResourceReturnsTheDirOneForResources()
  309. {
  310. $kernel = $this->getKernel(array('getBundle'));
  311. $kernel
  312. ->expects($this->once())
  313. ->method('getBundle')
  314. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle')))
  315. ;
  316. $this->assertEquals(
  317. __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
  318. $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  319. );
  320. }
  321. public function testLocateResourceOnDirectories()
  322. {
  323. $kernel = $this->getKernel(array('getBundle'));
  324. $kernel
  325. ->expects($this->exactly(2))
  326. ->method('getBundle')
  327. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle')))
  328. ;
  329. $this->assertEquals(
  330. __DIR__.'/Fixtures/Resources/FooBundle/',
  331. $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
  332. );
  333. $this->assertEquals(
  334. __DIR__.'/Fixtures/Resources/FooBundle',
  335. $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
  336. );
  337. $kernel = $this->getKernel(array('getBundle'));
  338. $kernel
  339. ->expects($this->exactly(2))
  340. ->method('getBundle')
  341. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle')))
  342. ;
  343. $this->assertEquals(
  344. __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
  345. $kernel->locateResource('@Bundle1Bundle/Resources/')
  346. );
  347. $this->assertEquals(
  348. __DIR__.'/Fixtures/Bundle1Bundle/Resources',
  349. $kernel->locateResource('@Bundle1Bundle/Resources')
  350. );
  351. }
  352. /**
  353. * @expectedException \LogicException
  354. * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
  355. */
  356. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  357. {
  358. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  359. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  360. $kernel = $this->getKernel(array(), array($fooBundle, $barBundle));
  361. $kernel->boot();
  362. }
  363. public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
  364. {
  365. $kernel = $this->getKernel(array('getHttpKernel'));
  366. $kernel->expects($this->never())
  367. ->method('getHttpKernel');
  368. $kernel->terminate(Request::create('/'), new Response());
  369. }
  370. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  371. {
  372. // does not implement TerminableInterface
  373. $httpKernel = new TestKernel();
  374. $kernel = $this->getKernel(array('getHttpKernel'));
  375. $kernel->expects($this->once())
  376. ->method('getHttpKernel')
  377. ->willReturn($httpKernel);
  378. $kernel->boot();
  379. $kernel->terminate(Request::create('/'), new Response());
  380. $this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
  381. // implements TerminableInterface
  382. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  383. ->disableOriginalConstructor()
  384. ->setMethods(array('terminate'))
  385. ->getMock();
  386. $httpKernelMock
  387. ->expects($this->once())
  388. ->method('terminate');
  389. $kernel = $this->getKernel(array('getHttpKernel'));
  390. $kernel->expects($this->exactly(2))
  391. ->method('getHttpKernel')
  392. ->will($this->returnValue($httpKernelMock));
  393. $kernel->boot();
  394. $kernel->terminate(Request::create('/'), new Response());
  395. }
  396. public function testKernelWithoutBundles()
  397. {
  398. $kernel = new KernelWithoutBundles('test', true);
  399. $kernel->boot();
  400. $this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
  401. }
  402. public function testKernelRootDirNameStartingWithANumber()
  403. {
  404. $dir = __DIR__.'/Fixtures/123';
  405. require_once $dir.'/Kernel123.php';
  406. $kernel = new \Symfony\Component\HttpKernel\Tests\Fixtures\_123\Kernel123('dev', true);
  407. $this->assertEquals('_123', $kernel->getName());
  408. }
  409. public function testProjectDirExtension()
  410. {
  411. $kernel = new CustomProjectDirKernel();
  412. $kernel->boot();
  413. $this->assertSame('foo', $kernel->getProjectDir());
  414. $this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir'));
  415. }
  416. public function testKernelReset()
  417. {
  418. (new Filesystem())->remove(__DIR__.'/Fixtures/cache');
  419. $kernel = new CustomProjectDirKernel();
  420. $kernel->boot();
  421. $containerClass = get_class($kernel->getContainer());
  422. $containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
  423. unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
  424. $kernel = new CustomProjectDirKernel();
  425. $kernel->boot();
  426. $this->assertInstanceOf($containerClass, $kernel->getContainer());
  427. $this->assertFileExists($containerFile);
  428. unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
  429. $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
  430. $kernel->boot();
  431. $this->assertNotInstanceOf($containerClass, $kernel->getContainer());
  432. $this->assertFileExists($containerFile);
  433. $this->assertFileExists(dirname($containerFile).'.legacy');
  434. }
  435. public function testKernelPass()
  436. {
  437. $kernel = new PassKernel();
  438. $kernel->boot();
  439. $this->assertTrue($kernel->getContainer()->getParameter('test.processed'));
  440. }
  441. public function testServicesResetter()
  442. {
  443. $httpKernelMock = $this->getMockBuilder(HttpKernelInterface::class)
  444. ->disableOriginalConstructor()
  445. ->getMock();
  446. $httpKernelMock
  447. ->expects($this->exactly(2))
  448. ->method('handle');
  449. $kernel = new CustomProjectDirKernel(function ($container) {
  450. $container->addCompilerPass(new ResettableServicePass());
  451. $container->register('one', ResettableService::class)
  452. ->setPublic(true)
  453. ->addTag('kernel.reset', array('method' => 'reset'));
  454. $container->register('services_resetter', ServicesResetter::class)->setPublic(true);
  455. }, $httpKernelMock, 'resetting');
  456. ResettableService::$counter = 0;
  457. $request = new Request();
  458. $kernel->handle($request);
  459. $kernel->getContainer()->get('one');
  460. $this->assertEquals(0, ResettableService::$counter);
  461. $this->assertFalse($kernel->getContainer()->initialized('services_resetter'));
  462. $kernel->handle($request);
  463. $this->assertEquals(1, ResettableService::$counter);
  464. }
  465. /**
  466. * @group time-sensitive
  467. */
  468. public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel()
  469. {
  470. $kernel = $this->getKernelForTest(array('initializeBundles'), true);
  471. $kernel->boot();
  472. $preReBoot = $kernel->getStartTime();
  473. sleep(3600); //Intentionally large value to detect if ClockMock ever breaks
  474. $kernel->reboot(null);
  475. $this->assertGreaterThan($preReBoot, $kernel->getStartTime());
  476. }
  477. /**
  478. * Returns a mock for the BundleInterface.
  479. *
  480. * @return BundleInterface
  481. */
  482. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  483. {
  484. $bundle = $this
  485. ->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')
  486. ->setMethods(array('getPath', 'getParent', 'getName'))
  487. ->disableOriginalConstructor()
  488. ;
  489. if ($className) {
  490. $bundle->setMockClassName($className);
  491. }
  492. $bundle = $bundle->getMockForAbstractClass();
  493. $bundle
  494. ->expects($this->any())
  495. ->method('getName')
  496. ->will($this->returnValue(null === $bundleName ? get_class($bundle) : $bundleName))
  497. ;
  498. $bundle
  499. ->expects($this->any())
  500. ->method('getPath')
  501. ->will($this->returnValue($dir))
  502. ;
  503. $bundle
  504. ->expects($this->any())
  505. ->method('getParent')
  506. ->will($this->returnValue($parent))
  507. ;
  508. return $bundle;
  509. }
  510. /**
  511. * Returns a mock for the abstract kernel.
  512. *
  513. * @param array $methods Additional methods to mock (besides the abstract ones)
  514. * @param array $bundles Bundles to register
  515. *
  516. * @return Kernel
  517. */
  518. protected function getKernel(array $methods = array(), array $bundles = array())
  519. {
  520. $methods[] = 'registerBundles';
  521. $kernel = $this
  522. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  523. ->setMethods($methods)
  524. ->setConstructorArgs(array('test', false))
  525. ->getMockForAbstractClass()
  526. ;
  527. $kernel->expects($this->any())
  528. ->method('registerBundles')
  529. ->will($this->returnValue($bundles))
  530. ;
  531. $p = new \ReflectionProperty($kernel, 'rootDir');
  532. $p->setAccessible(true);
  533. $p->setValue($kernel, __DIR__.'/Fixtures');
  534. return $kernel;
  535. }
  536. protected function getKernelForTest(array $methods = array(), $debug = false)
  537. {
  538. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
  539. ->setConstructorArgs(array('test', $debug))
  540. ->setMethods($methods)
  541. ->getMock();
  542. $p = new \ReflectionProperty($kernel, 'rootDir');
  543. $p->setAccessible(true);
  544. $p->setValue($kernel, __DIR__.'/Fixtures');
  545. return $kernel;
  546. }
  547. }
  548. class TestKernel implements HttpKernelInterface
  549. {
  550. public $terminateCalled = false;
  551. public function terminate()
  552. {
  553. $this->terminateCalled = true;
  554. }
  555. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  556. {
  557. }
  558. }
  559. class CustomProjectDirKernel extends Kernel
  560. {
  561. private $baseDir;
  562. private $buildContainer;
  563. private $httpKernel;
  564. public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $name = 'custom')
  565. {
  566. parent::__construct($name, true);
  567. $this->baseDir = 'foo';
  568. $this->buildContainer = $buildContainer;
  569. $this->httpKernel = $httpKernel;
  570. }
  571. public function registerBundles()
  572. {
  573. return array();
  574. }
  575. public function registerContainerConfiguration(LoaderInterface $loader)
  576. {
  577. }
  578. public function getProjectDir()
  579. {
  580. return $this->baseDir;
  581. }
  582. public function getRootDir()
  583. {
  584. return __DIR__.'/Fixtures';
  585. }
  586. protected function build(ContainerBuilder $container)
  587. {
  588. if ($build = $this->buildContainer) {
  589. $build($container);
  590. }
  591. }
  592. protected function getHttpKernel()
  593. {
  594. return $this->httpKernel;
  595. }
  596. }
  597. class PassKernel extends CustomProjectDirKernel implements CompilerPassInterface
  598. {
  599. public function __construct()
  600. {
  601. parent::__construct();
  602. Kernel::__construct('pass', true);
  603. }
  604. public function process(ContainerBuilder $container)
  605. {
  606. $container->setParameter('test.processed', true);
  607. }
  608. }