Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KernelTest.php 23KB

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