KernelTest.php 22 KB

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\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\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  19. use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
  20. use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
  21. use Symfony\Component\HttpKernel\HttpKernelInterface;
  22. use Symfony\Component\HttpKernel\Kernel;
  23. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
  24. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
  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/var');
  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/var/cache/custom/ContainerA123456';
  61. $fs->mkdir($legacyContainerDir);
  62. touch($legacyContainerDir.'.legacy');
  63. $kernel = new CustomProjectDirKernel();
  64. $kernel->boot();
  65. $containerDir = __DIR__.'/Fixtures/var/cache/custom/'.substr(\get_class($kernel->getContainer()), 0, 16);
  66. $this->assertTrue(unlink(__DIR__.'/Fixtures/var/cache/custom/TestsSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.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(['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(['initializeBundles', 'initializeContainer', 'getBundles']);
  91. $kernel->expects($this->once())
  92. ->method('getBundles')
  93. ->will($this->returnValue([$bundle]));
  94. $kernel->boot();
  95. }
  96. public function testBootSetsTheBootedFlagToTrue()
  97. {
  98. // use test kernel to access isBooted()
  99. $kernel = $this->getKernelForTest(['initializeBundles', 'initializeContainer']);
  100. $kernel->boot();
  101. $this->assertTrue($kernel->isBooted());
  102. }
  103. public function testClassCacheIsNotLoadedByDefault()
  104. {
  105. $kernel = $this->getKernel(['initializeBundles', 'initializeContainer', 'doLoadClassCache']);
  106. $kernel->expects($this->never())
  107. ->method('doLoadClassCache');
  108. $kernel->boot();
  109. }
  110. public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
  111. {
  112. $kernel = $this->getKernel(['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([], [$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(['getBundles']);
  134. $kernel->expects($this->any())
  135. ->method('getBundles')
  136. ->will($this->returnValue([$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(['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(['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. /**
  229. * @group legacy
  230. */
  231. public function testGetRootDir()
  232. {
  233. $kernel = new KernelForTest('test', true);
  234. $this->assertEquals(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
  235. }
  236. /**
  237. * @group legacy
  238. */
  239. public function testGetName()
  240. {
  241. $kernel = new KernelForTest('test', true);
  242. $this->assertEquals('Fixtures', $kernel->getName());
  243. }
  244. /**
  245. * @group legacy
  246. */
  247. public function testOverrideGetName()
  248. {
  249. $kernel = new KernelForOverrideName('test', true);
  250. $this->assertEquals('overridden', $kernel->getName());
  251. }
  252. public function testSerialize()
  253. {
  254. $env = 'test_env';
  255. $debug = true;
  256. $kernel = new KernelForTest($env, $debug);
  257. $expected = serialize([$env, $debug]);
  258. $this->assertEquals($expected, $kernel->serialize());
  259. }
  260. /**
  261. * @expectedException \InvalidArgumentException
  262. */
  263. public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
  264. {
  265. $this->getKernel()->locateResource('Foo');
  266. }
  267. /**
  268. * @expectedException \RuntimeException
  269. */
  270. public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
  271. {
  272. $this->getKernel()->locateResource('@FooBundle/../bar');
  273. }
  274. /**
  275. * @expectedException \InvalidArgumentException
  276. */
  277. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  278. {
  279. $this->getKernel()->locateResource('@FooBundle/config/routing.xml');
  280. }
  281. /**
  282. * @expectedException \InvalidArgumentException
  283. */
  284. public function testLocateResourceThrowsExceptionWhenResourceDoesNotExist()
  285. {
  286. $kernel = $this->getKernel(['getBundle']);
  287. $kernel
  288. ->expects($this->once())
  289. ->method('getBundle')
  290. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle')))
  291. ;
  292. $kernel->locateResource('@Bundle1Bundle/config/routing.xml');
  293. }
  294. public function testLocateResourceReturnsTheFirstThatMatches()
  295. {
  296. $kernel = $this->getKernel(['getBundle']);
  297. $kernel
  298. ->expects($this->once())
  299. ->method('getBundle')
  300. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle')))
  301. ;
  302. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
  303. }
  304. public function testLocateResourceIgnoresDirOnNonResource()
  305. {
  306. $kernel = $this->getKernel(['getBundle']);
  307. $kernel
  308. ->expects($this->once())
  309. ->method('getBundle')
  310. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle')))
  311. ;
  312. $this->assertEquals(
  313. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
  314. $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
  315. );
  316. }
  317. public function testLocateResourceReturnsTheDirOneForResources()
  318. {
  319. $kernel = $this->getKernel(['getBundle']);
  320. $kernel
  321. ->expects($this->once())
  322. ->method('getBundle')
  323. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle')))
  324. ;
  325. $this->assertEquals(
  326. __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
  327. $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  328. );
  329. }
  330. public function testLocateResourceOnDirectories()
  331. {
  332. $kernel = $this->getKernel(['getBundle']);
  333. $kernel
  334. ->expects($this->exactly(2))
  335. ->method('getBundle')
  336. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle')))
  337. ;
  338. $this->assertEquals(
  339. __DIR__.'/Fixtures/Resources/FooBundle/',
  340. $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
  341. );
  342. $this->assertEquals(
  343. __DIR__.'/Fixtures/Resources/FooBundle',
  344. $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
  345. );
  346. $kernel = $this->getKernel(['getBundle']);
  347. $kernel
  348. ->expects($this->exactly(2))
  349. ->method('getBundle')
  350. ->will($this->returnValue($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle')))
  351. ;
  352. $this->assertEquals(
  353. __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
  354. $kernel->locateResource('@Bundle1Bundle/Resources/')
  355. );
  356. $this->assertEquals(
  357. __DIR__.'/Fixtures/Bundle1Bundle/Resources',
  358. $kernel->locateResource('@Bundle1Bundle/Resources')
  359. );
  360. }
  361. /**
  362. * @expectedException \LogicException
  363. * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
  364. */
  365. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  366. {
  367. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  368. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  369. $kernel = $this->getKernel([], [$fooBundle, $barBundle]);
  370. $kernel->boot();
  371. }
  372. public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
  373. {
  374. $kernel = $this->getKernel(['getHttpKernel']);
  375. $kernel->expects($this->never())
  376. ->method('getHttpKernel');
  377. $kernel->terminate(Request::create('/'), new Response());
  378. }
  379. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  380. {
  381. // does not implement TerminableInterface
  382. $httpKernel = new TestKernel();
  383. $kernel = $this->getKernel(['getHttpKernel']);
  384. $kernel->expects($this->once())
  385. ->method('getHttpKernel')
  386. ->willReturn($httpKernel);
  387. $kernel->boot();
  388. $kernel->terminate(Request::create('/'), new Response());
  389. $this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
  390. // implements TerminableInterface
  391. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  392. ->disableOriginalConstructor()
  393. ->setMethods(['terminate'])
  394. ->getMock();
  395. $httpKernelMock
  396. ->expects($this->once())
  397. ->method('terminate');
  398. $kernel = $this->getKernel(['getHttpKernel']);
  399. $kernel->expects($this->exactly(2))
  400. ->method('getHttpKernel')
  401. ->will($this->returnValue($httpKernelMock));
  402. $kernel->boot();
  403. $kernel->terminate(Request::create('/'), new Response());
  404. }
  405. public function testKernelWithoutBundles()
  406. {
  407. $kernel = new KernelWithoutBundles('test', true);
  408. $kernel->boot();
  409. $this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
  410. }
  411. public function testProjectDirExtension()
  412. {
  413. $kernel = new CustomProjectDirKernel();
  414. $kernel->boot();
  415. $this->assertSame(__DIR__.'/Fixtures', $kernel->getProjectDir());
  416. $this->assertSame(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures', $kernel->getContainer()->getParameter('kernel.project_dir'));
  417. }
  418. public function testKernelReset()
  419. {
  420. (new Filesystem())->remove(__DIR__.'/Fixtures/var/cache');
  421. $kernel = new CustomProjectDirKernel();
  422. $kernel->boot();
  423. $containerClass = \get_class($kernel->getContainer());
  424. $containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
  425. unlink(__DIR__.'/Fixtures/var/cache/custom/TestsSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');
  426. $kernel = new CustomProjectDirKernel();
  427. $kernel->boot();
  428. $this->assertInstanceOf($containerClass, $kernel->getContainer());
  429. $this->assertFileExists($containerFile);
  430. unlink(__DIR__.'/Fixtures/var/cache/custom/TestsSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');
  431. $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
  432. $kernel->boot();
  433. $this->assertNotInstanceOf($containerClass, $kernel->getContainer());
  434. $this->assertFileExists($containerFile);
  435. $this->assertFileExists(\dirname($containerFile).'.legacy');
  436. }
  437. public function testKernelPass()
  438. {
  439. $kernel = new PassKernel();
  440. $kernel->boot();
  441. $this->assertTrue($kernel->getContainer()->getParameter('test.processed'));
  442. }
  443. public function testServicesResetter()
  444. {
  445. $httpKernelMock = $this->getMockBuilder(HttpKernelInterface::class)
  446. ->disableOriginalConstructor()
  447. ->getMock();
  448. $httpKernelMock
  449. ->expects($this->exactly(2))
  450. ->method('handle');
  451. $kernel = new CustomProjectDirKernel(function ($container) {
  452. $container->addCompilerPass(new ResettableServicePass());
  453. $container->register('one', ResettableService::class)
  454. ->setPublic(true)
  455. ->addTag('kernel.reset', ['method' => 'reset']);
  456. $container->register('services_resetter', ServicesResetter::class)->setPublic(true);
  457. }, $httpKernelMock, 'resetting');
  458. ResettableService::$counter = 0;
  459. $request = new Request();
  460. $kernel->handle($request);
  461. $kernel->getContainer()->get('one');
  462. $this->assertEquals(0, ResettableService::$counter);
  463. $this->assertFalse($kernel->getContainer()->initialized('services_resetter'));
  464. $kernel->handle($request);
  465. $this->assertEquals(1, ResettableService::$counter);
  466. }
  467. /**
  468. * @group time-sensitive
  469. */
  470. public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel()
  471. {
  472. $kernel = $this->getKernelForTest(['initializeBundles'], true);
  473. $kernel->boot();
  474. $preReBoot = $kernel->getStartTime();
  475. sleep(3600); //Intentionally large value to detect if ClockMock ever breaks
  476. $kernel->reboot(null);
  477. $this->assertGreaterThan($preReBoot, $kernel->getStartTime());
  478. }
  479. /**
  480. * Returns a mock for the BundleInterface.
  481. *
  482. * @return BundleInterface
  483. */
  484. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  485. {
  486. $bundle = $this
  487. ->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')
  488. ->setMethods(['getPath', 'getParent', 'getName'])
  489. ->disableOriginalConstructor()
  490. ;
  491. if ($className) {
  492. $bundle->setMockClassName($className);
  493. }
  494. $bundle = $bundle->getMockForAbstractClass();
  495. $bundle
  496. ->expects($this->any())
  497. ->method('getName')
  498. ->will($this->returnValue(null === $bundleName ? \get_class($bundle) : $bundleName))
  499. ;
  500. $bundle
  501. ->expects($this->any())
  502. ->method('getPath')
  503. ->will($this->returnValue($dir))
  504. ;
  505. $bundle
  506. ->expects($this->any())
  507. ->method('getParent')
  508. ->will($this->returnValue($parent))
  509. ;
  510. return $bundle;
  511. }
  512. /**
  513. * Returns a mock for the abstract kernel.
  514. *
  515. * @param array $methods Additional methods to mock (besides the abstract ones)
  516. * @param array $bundles Bundles to register
  517. *
  518. * @return Kernel
  519. */
  520. protected function getKernel(array $methods = [], array $bundles = [])
  521. {
  522. $methods[] = 'registerBundles';
  523. $kernel = $this
  524. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  525. ->setMethods($methods)
  526. ->setConstructorArgs(['test', false])
  527. ->getMockForAbstractClass()
  528. ;
  529. $kernel->expects($this->any())
  530. ->method('registerBundles')
  531. ->will($this->returnValue($bundles))
  532. ;
  533. $p = new \ReflectionProperty($kernel, 'rootDir');
  534. $p->setAccessible(true);
  535. $p->setValue($kernel, __DIR__.'/Fixtures');
  536. return $kernel;
  537. }
  538. protected function getKernelForTest(array $methods = [], $debug = false)
  539. {
  540. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
  541. ->setConstructorArgs(['test', $debug])
  542. ->setMethods($methods)
  543. ->getMock();
  544. $p = new \ReflectionProperty($kernel, 'rootDir');
  545. $p->setAccessible(true);
  546. $p->setValue($kernel, __DIR__.'/Fixtures');
  547. return $kernel;
  548. }
  549. }
  550. class TestKernel implements HttpKernelInterface
  551. {
  552. public $terminateCalled = false;
  553. public function terminate()
  554. {
  555. $this->terminateCalled = true;
  556. }
  557. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  558. {
  559. }
  560. }
  561. class CustomProjectDirKernel extends Kernel
  562. {
  563. private $baseDir;
  564. private $buildContainer;
  565. private $httpKernel;
  566. public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $env = 'custom')
  567. {
  568. parent::__construct($env, true);
  569. $this->buildContainer = $buildContainer;
  570. $this->httpKernel = $httpKernel;
  571. }
  572. public function registerBundles()
  573. {
  574. return [];
  575. }
  576. public function registerContainerConfiguration(LoaderInterface $loader)
  577. {
  578. }
  579. public function getProjectDir()
  580. {
  581. return __DIR__.'/Fixtures';
  582. }
  583. protected function build(ContainerBuilder $container)
  584. {
  585. if ($build = $this->buildContainer) {
  586. $build($container);
  587. }
  588. }
  589. protected function getHttpKernel()
  590. {
  591. return $this->httpKernel;
  592. }
  593. }
  594. class PassKernel extends CustomProjectDirKernel implements CompilerPassInterface
  595. {
  596. public function __construct()
  597. {
  598. parent::__construct();
  599. Kernel::__construct('pass', true);
  600. }
  601. public function process(ContainerBuilder $container)
  602. {
  603. $container->setParameter('test.processed', true);
  604. }
  605. }