PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/symfony/http-kernel/Tests/KernelTest.php

https://gitlab.com/ealexis.t/trends
PHP | 816 lines | 732 code | 66 blank | 18 comment | 3 complexity | 51849c267ee6a88265fff04f20b2d7e8 MD5 | raw file
  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 Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  13. use Symfony\Component\HttpKernel\Config\EnvParametersResource;
  14. use Symfony\Component\HttpKernel\Kernel;
  15. use Symfony\Component\HttpKernel\HttpKernelInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
  19. use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
  20. class KernelTest extends \PHPUnit_Framework_TestCase
  21. {
  22. public function testConstructor()
  23. {
  24. $env = 'test_env';
  25. $debug = true;
  26. $kernel = new KernelForTest($env, $debug);
  27. $this->assertEquals($env, $kernel->getEnvironment());
  28. $this->assertEquals($debug, $kernel->isDebug());
  29. $this->assertFalse($kernel->isBooted());
  30. $this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
  31. $this->assertNull($kernel->getContainer());
  32. }
  33. public function testClone()
  34. {
  35. $env = 'test_env';
  36. $debug = true;
  37. $kernel = new KernelForTest($env, $debug);
  38. $clone = clone $kernel;
  39. $this->assertEquals($env, $clone->getEnvironment());
  40. $this->assertEquals($debug, $clone->isDebug());
  41. $this->assertFalse($clone->isBooted());
  42. $this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
  43. $this->assertNull($clone->getContainer());
  44. }
  45. public function testBootInitializesBundlesAndContainer()
  46. {
  47. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
  48. $kernel->expects($this->once())
  49. ->method('initializeBundles');
  50. $kernel->expects($this->once())
  51. ->method('initializeContainer');
  52. $kernel->boot();
  53. }
  54. public function testBootSetsTheContainerToTheBundles()
  55. {
  56. $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
  57. $bundle->expects($this->once())
  58. ->method('setContainer');
  59. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'getBundles'));
  60. $kernel->expects($this->once())
  61. ->method('getBundles')
  62. ->will($this->returnValue(array($bundle)));
  63. $kernel->boot();
  64. }
  65. public function testBootSetsTheBootedFlagToTrue()
  66. {
  67. // use test kernel to access isBooted()
  68. $kernel = $this->getKernelForTest(array('initializeBundles', 'initializeContainer'));
  69. $kernel->boot();
  70. $this->assertTrue($kernel->isBooted());
  71. }
  72. public function testClassCacheIsLoaded()
  73. {
  74. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  75. $kernel->loadClassCache('name', '.extension');
  76. $kernel->expects($this->once())
  77. ->method('doLoadClassCache')
  78. ->with('name', '.extension');
  79. $kernel->boot();
  80. }
  81. public function testClassCacheIsNotLoadedByDefault()
  82. {
  83. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  84. $kernel->expects($this->never())
  85. ->method('doLoadClassCache');
  86. $kernel->boot();
  87. }
  88. public function testClassCacheIsNotLoadedWhenKernelIsNotBooted()
  89. {
  90. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
  91. $kernel->loadClassCache();
  92. $kernel->expects($this->never())
  93. ->method('doLoadClassCache');
  94. }
  95. public function testEnvParametersResourceIsAdded()
  96. {
  97. $container = new ContainerBuilder();
  98. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
  99. ->disableOriginalConstructor()
  100. ->setMethods(array('getContainerBuilder', 'prepareContainer', 'getCacheDir', 'getLogDir'))
  101. ->getMock();
  102. $kernel->expects($this->any())
  103. ->method('getContainerBuilder')
  104. ->will($this->returnValue($container));
  105. $kernel->expects($this->any())
  106. ->method('prepareContainer')
  107. ->will($this->returnValue(null));
  108. $kernel->expects($this->any())
  109. ->method('getCacheDir')
  110. ->will($this->returnValue(sys_get_temp_dir()));
  111. $kernel->expects($this->any())
  112. ->method('getLogDir')
  113. ->will($this->returnValue(sys_get_temp_dir()));
  114. $reflection = new \ReflectionClass(get_class($kernel));
  115. $method = $reflection->getMethod('buildContainer');
  116. $method->setAccessible(true);
  117. $method->invoke($kernel);
  118. $found = false;
  119. foreach ($container->getResources() as $resource) {
  120. if ($resource instanceof EnvParametersResource) {
  121. $found = true;
  122. break;
  123. }
  124. }
  125. $this->assertTrue($found);
  126. }
  127. public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
  128. {
  129. $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
  130. $kernel->expects($this->once())
  131. ->method('initializeBundles');
  132. $kernel->boot();
  133. $kernel->boot();
  134. }
  135. public function testShutdownCallsShutdownOnAllBundles()
  136. {
  137. $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
  138. $bundle->expects($this->once())
  139. ->method('shutdown');
  140. $kernel = $this->getKernel(array(), array($bundle));
  141. $kernel->boot();
  142. $kernel->shutdown();
  143. }
  144. public function testShutdownGivesNullContainerToAllBundles()
  145. {
  146. $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
  147. $bundle->expects($this->at(3))
  148. ->method('setContainer')
  149. ->with(null);
  150. $kernel = $this->getKernel(array('getBundles'));
  151. $kernel->expects($this->any())
  152. ->method('getBundles')
  153. ->will($this->returnValue(array($bundle)));
  154. $kernel->boot();
  155. $kernel->shutdown();
  156. }
  157. public function testHandleCallsHandleOnHttpKernel()
  158. {
  159. $type = HttpKernelInterface::MASTER_REQUEST;
  160. $catch = true;
  161. $request = new Request();
  162. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  163. ->disableOriginalConstructor()
  164. ->getMock();
  165. $httpKernelMock
  166. ->expects($this->once())
  167. ->method('handle')
  168. ->with($request, $type, $catch);
  169. $kernel = $this->getKernel(array('getHttpKernel'));
  170. $kernel->expects($this->once())
  171. ->method('getHttpKernel')
  172. ->will($this->returnValue($httpKernelMock));
  173. $kernel->handle($request, $type, $catch);
  174. }
  175. public function testHandleBootsTheKernel()
  176. {
  177. $type = HttpKernelInterface::MASTER_REQUEST;
  178. $catch = true;
  179. $request = new Request();
  180. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  181. ->disableOriginalConstructor()
  182. ->getMock();
  183. $kernel = $this->getKernel(array('getHttpKernel', 'boot'));
  184. $kernel->expects($this->once())
  185. ->method('getHttpKernel')
  186. ->will($this->returnValue($httpKernelMock));
  187. $kernel->expects($this->once())
  188. ->method('boot');
  189. $kernel->handle($request, $type, $catch);
  190. }
  191. public function testStripComments()
  192. {
  193. $source = <<<'EOF'
  194. <?php
  195. $string = 'string should not be modified';
  196. $string = 'string should not be
  197. modified';
  198. $heredoc = <<<HD
  199. Heredoc should not be modified {$a[1+$b]}
  200. HD;
  201. $nowdoc = <<<'ND'
  202. Nowdoc should not be modified
  203. ND;
  204. /**
  205. * some class comments to strip
  206. */
  207. class TestClass
  208. {
  209. /**
  210. * some method comments to strip
  211. */
  212. public function doStuff()
  213. {
  214. // inline comment
  215. }
  216. }
  217. EOF;
  218. $expected = <<<'EOF'
  219. <?php
  220. $string = 'string should not be modified';
  221. $string = 'string should not be
  222. modified';
  223. $heredoc = <<<HD
  224. Heredoc should not be modified {$a[1+$b]}
  225. HD;
  226. $nowdoc = <<<'ND'
  227. Nowdoc should not be modified
  228. ND;
  229. class TestClass
  230. {
  231. public function doStuff()
  232. {
  233. }
  234. }
  235. EOF;
  236. $output = Kernel::stripComments($source);
  237. // Heredocs are preserved, making the output mixing Unix and Windows line
  238. // endings, switching to "\n" everywhere on Windows to avoid failure.
  239. if ('\\' === DIRECTORY_SEPARATOR) {
  240. $expected = str_replace("\r\n", "\n", $expected);
  241. $output = str_replace("\r\n", "\n", $output);
  242. }
  243. $this->assertEquals($expected, $output);
  244. }
  245. public function testGetRootDir()
  246. {
  247. $kernel = new KernelForTest('test', true);
  248. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
  249. }
  250. public function testGetName()
  251. {
  252. $kernel = new KernelForTest('test', true);
  253. $this->assertEquals('Fixtures', $kernel->getName());
  254. }
  255. public function testOverrideGetName()
  256. {
  257. $kernel = new KernelForOverrideName('test', true);
  258. $this->assertEquals('overridden', $kernel->getName());
  259. }
  260. public function testSerialize()
  261. {
  262. $env = 'test_env';
  263. $debug = true;
  264. $kernel = new KernelForTest($env, $debug);
  265. $expected = serialize(array($env, $debug));
  266. $this->assertEquals($expected, $kernel->serialize());
  267. }
  268. /**
  269. * @expectedException \InvalidArgumentException
  270. */
  271. public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
  272. {
  273. $this->getKernel()->locateResource('Foo');
  274. }
  275. /**
  276. * @expectedException \RuntimeException
  277. */
  278. public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
  279. {
  280. $this->getKernel()->locateResource('@FooBundle/../bar');
  281. }
  282. /**
  283. * @expectedException \InvalidArgumentException
  284. */
  285. public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
  286. {
  287. $this->getKernel()->locateResource('@FooBundle/config/routing.xml');
  288. }
  289. /**
  290. * @expectedException \InvalidArgumentException
  291. */
  292. public function testLocateResourceThrowsExceptionWhenResourceDoesNotExist()
  293. {
  294. $kernel = $this->getKernel(array('getBundle'));
  295. $kernel
  296. ->expects($this->once())
  297. ->method('getBundle')
  298. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  299. ;
  300. $kernel->locateResource('@Bundle1Bundle/config/routing.xml');
  301. }
  302. public function testLocateResourceReturnsTheFirstThatMatches()
  303. {
  304. $kernel = $this->getKernel(array('getBundle'));
  305. $kernel
  306. ->expects($this->once())
  307. ->method('getBundle')
  308. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  309. ;
  310. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
  311. }
  312. public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
  313. {
  314. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
  315. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
  316. $kernel = $this->getKernel(array('getBundle'));
  317. $kernel
  318. ->expects($this->exactly(2))
  319. ->method('getBundle')
  320. ->will($this->returnValue(array($child, $parent)))
  321. ;
  322. $this->assertEquals(__DIR__.'/Fixtures/Bundle2Bundle/foo.txt', $kernel->locateResource('@ParentAABundle/foo.txt'));
  323. $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
  324. }
  325. public function testLocateResourceReturnsAllMatches()
  326. {
  327. $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
  328. $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
  329. $kernel = $this->getKernel(array('getBundle'));
  330. $kernel
  331. ->expects($this->once())
  332. ->method('getBundle')
  333. ->will($this->returnValue(array($child, $parent)))
  334. ;
  335. $this->assertEquals(array(
  336. __DIR__.'/Fixtures/Bundle2Bundle/foo.txt',
  337. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt', ),
  338. $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
  339. }
  340. public function testLocateResourceReturnsAllMatchesBis()
  341. {
  342. $kernel = $this->getKernel(array('getBundle'));
  343. $kernel
  344. ->expects($this->once())
  345. ->method('getBundle')
  346. ->will($this->returnValue(array(
  347. $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'),
  348. $this->getBundle(__DIR__.'/Foobar'),
  349. )))
  350. ;
  351. $this->assertEquals(
  352. array(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
  353. $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false)
  354. );
  355. }
  356. public function testLocateResourceIgnoresDirOnNonResource()
  357. {
  358. $kernel = $this->getKernel(array('getBundle'));
  359. $kernel
  360. ->expects($this->once())
  361. ->method('getBundle')
  362. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
  363. ;
  364. $this->assertEquals(
  365. __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
  366. $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
  367. );
  368. }
  369. public function testLocateResourceReturnsTheDirOneForResources()
  370. {
  371. $kernel = $this->getKernel(array('getBundle'));
  372. $kernel
  373. ->expects($this->once())
  374. ->method('getBundle')
  375. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
  376. ;
  377. $this->assertEquals(
  378. __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
  379. $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  380. );
  381. }
  382. public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
  383. {
  384. $kernel = $this->getKernel(array('getBundle'));
  385. $kernel
  386. ->expects($this->once())
  387. ->method('getBundle')
  388. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
  389. ;
  390. $this->assertEquals(array(
  391. __DIR__.'/Fixtures/Resources/Bundle1Bundle/foo.txt',
  392. __DIR__.'/Fixtures/Bundle1Bundle/Resources/foo.txt', ),
  393. $kernel->locateResource('@Bundle1Bundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  394. );
  395. }
  396. public function testLocateResourceOverrideBundleAndResourcesFolders()
  397. {
  398. $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
  399. $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'ParentBundle', 'ChildBundle', 'ChildBundle');
  400. $kernel = $this->getKernel(array('getBundle'));
  401. $kernel
  402. ->expects($this->exactly(4))
  403. ->method('getBundle')
  404. ->will($this->returnValue(array($child, $parent)))
  405. ;
  406. $this->assertEquals(array(
  407. __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
  408. __DIR__.'/Fixtures/ChildBundle/Resources/foo.txt',
  409. __DIR__.'/Fixtures/BaseBundle/Resources/foo.txt',
  410. ),
  411. $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
  412. );
  413. $this->assertEquals(
  414. __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
  415. $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
  416. );
  417. try {
  418. $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', false);
  419. $this->fail('Hidden resources should raise an exception when returning an array of matching paths');
  420. } catch (\RuntimeException $e) {
  421. }
  422. try {
  423. $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', true);
  424. $this->fail('Hidden resources should raise an exception when returning the first matching path');
  425. } catch (\RuntimeException $e) {
  426. }
  427. }
  428. public function testLocateResourceOnDirectories()
  429. {
  430. $kernel = $this->getKernel(array('getBundle'));
  431. $kernel
  432. ->expects($this->exactly(2))
  433. ->method('getBundle')
  434. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
  435. ;
  436. $this->assertEquals(
  437. __DIR__.'/Fixtures/Resources/FooBundle/',
  438. $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
  439. );
  440. $this->assertEquals(
  441. __DIR__.'/Fixtures/Resources/FooBundle',
  442. $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
  443. );
  444. $kernel = $this->getKernel(array('getBundle'));
  445. $kernel
  446. ->expects($this->exactly(2))
  447. ->method('getBundle')
  448. ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
  449. ;
  450. $this->assertEquals(
  451. __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
  452. $kernel->locateResource('@Bundle1Bundle/Resources/')
  453. );
  454. $this->assertEquals(
  455. __DIR__.'/Fixtures/Bundle1Bundle/Resources',
  456. $kernel->locateResource('@Bundle1Bundle/Resources')
  457. );
  458. }
  459. public function testInitializeBundles()
  460. {
  461. $parent = $this->getBundle(null, null, 'ParentABundle');
  462. $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
  463. // use test kernel so we can access getBundleMap()
  464. $kernel = $this->getKernelForTest(array('registerBundles'));
  465. $kernel
  466. ->expects($this->once())
  467. ->method('registerBundles')
  468. ->will($this->returnValue(array($parent, $child)))
  469. ;
  470. $kernel->boot();
  471. $map = $kernel->getBundleMap();
  472. $this->assertEquals(array($child, $parent), $map['ParentABundle']);
  473. }
  474. public function testInitializeBundlesSupportInheritanceCascade()
  475. {
  476. $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
  477. $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
  478. $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
  479. // use test kernel so we can access getBundleMap()
  480. $kernel = $this->getKernelForTest(array('registerBundles'));
  481. $kernel
  482. ->expects($this->once())
  483. ->method('registerBundles')
  484. ->will($this->returnValue(array($grandparent, $parent, $child)))
  485. ;
  486. $kernel->boot();
  487. $map = $kernel->getBundleMap();
  488. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
  489. $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
  490. $this->assertEquals(array($child), $map['ChildBBundle']);
  491. }
  492. /**
  493. * @expectedException \LogicException
  494. * @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered.
  495. */
  496. public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
  497. {
  498. $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
  499. $kernel = $this->getKernel(array(), array($child));
  500. $kernel->boot();
  501. }
  502. public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
  503. {
  504. $grandparent = $this->getBundle(null, null, 'GrandParentCBundle');
  505. $parent = $this->getBundle(null, 'GrandParentCBundle', 'ParentCBundle');
  506. $child = $this->getBundle(null, 'ParentCBundle', 'ChildCBundle');
  507. // use test kernel so we can access getBundleMap()
  508. $kernel = $this->getKernelForTest(array('registerBundles'));
  509. $kernel
  510. ->expects($this->once())
  511. ->method('registerBundles')
  512. ->will($this->returnValue(array($parent, $grandparent, $child)))
  513. ;
  514. $kernel->boot();
  515. $map = $kernel->getBundleMap();
  516. $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCBundle']);
  517. $this->assertEquals(array($child, $parent), $map['ParentCBundle']);
  518. $this->assertEquals(array($child), $map['ChildCBundle']);
  519. }
  520. /**
  521. * @expectedException \LogicException
  522. * @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle".
  523. */
  524. public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
  525. {
  526. $parent = $this->getBundle(null, null, 'ParentCBundle');
  527. $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
  528. $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
  529. $kernel = $this->getKernel(array(), array($parent, $child1, $child2));
  530. $kernel->boot();
  531. }
  532. /**
  533. * @expectedException \LogicException
  534. * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
  535. */
  536. public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
  537. {
  538. $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
  539. $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
  540. $kernel = $this->getKernel(array(), array($fooBundle, $barBundle));
  541. $kernel->boot();
  542. }
  543. /**
  544. * @expectedException \LogicException
  545. * @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself.
  546. */
  547. public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
  548. {
  549. $circularRef = $this->getBundle(null, 'CircularRefBundle', 'CircularRefBundle');
  550. $kernel = $this->getKernel(array(), array($circularRef));
  551. $kernel->boot();
  552. }
  553. public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
  554. {
  555. $kernel = $this->getKernel(array('getHttpKernel'));
  556. $kernel->expects($this->never())
  557. ->method('getHttpKernel');
  558. $kernel->terminate(Request::create('/'), new Response());
  559. }
  560. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  561. {
  562. // does not implement TerminableInterface
  563. $httpKernel = new TestKernel();
  564. $kernel = $this->getKernel(array('getHttpKernel'));
  565. $kernel->expects($this->once())
  566. ->method('getHttpKernel')
  567. ->willReturn($httpKernel);
  568. $kernel->boot();
  569. $kernel->terminate(Request::create('/'), new Response());
  570. $this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
  571. // implements TerminableInterface
  572. $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  573. ->disableOriginalConstructor()
  574. ->setMethods(array('terminate'))
  575. ->getMock();
  576. $httpKernelMock
  577. ->expects($this->once())
  578. ->method('terminate');
  579. $kernel = $this->getKernel(array('getHttpKernel'));
  580. $kernel->expects($this->exactly(2))
  581. ->method('getHttpKernel')
  582. ->will($this->returnValue($httpKernelMock));
  583. $kernel->boot();
  584. $kernel->terminate(Request::create('/'), new Response());
  585. }
  586. /**
  587. * Returns a mock for the BundleInterface.
  588. *
  589. * @return BundleInterface
  590. */
  591. protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
  592. {
  593. $bundle = $this
  594. ->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')
  595. ->setMethods(array('getPath', 'getParent', 'getName'))
  596. ->disableOriginalConstructor()
  597. ;
  598. if ($className) {
  599. $bundle->setMockClassName($className);
  600. }
  601. $bundle = $bundle->getMockForAbstractClass();
  602. $bundle
  603. ->expects($this->any())
  604. ->method('getName')
  605. ->will($this->returnValue(null === $bundleName ? get_class($bundle) : $bundleName))
  606. ;
  607. $bundle
  608. ->expects($this->any())
  609. ->method('getPath')
  610. ->will($this->returnValue($dir))
  611. ;
  612. $bundle
  613. ->expects($this->any())
  614. ->method('getParent')
  615. ->will($this->returnValue($parent))
  616. ;
  617. return $bundle;
  618. }
  619. /**
  620. * Returns a mock for the abstract kernel.
  621. *
  622. * @param array $methods Additional methods to mock (besides the abstract ones)
  623. * @param array $bundles Bundles to register
  624. *
  625. * @return Kernel
  626. */
  627. protected function getKernel(array $methods = array(), array $bundles = array())
  628. {
  629. $methods[] = 'registerBundles';
  630. $kernel = $this
  631. ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
  632. ->setMethods($methods)
  633. ->setConstructorArgs(array('test', false))
  634. ->getMockForAbstractClass()
  635. ;
  636. $kernel->expects($this->any())
  637. ->method('registerBundles')
  638. ->will($this->returnValue($bundles))
  639. ;
  640. $p = new \ReflectionProperty($kernel, 'rootDir');
  641. $p->setAccessible(true);
  642. $p->setValue($kernel, __DIR__.'/Fixtures');
  643. return $kernel;
  644. }
  645. protected function getKernelForTest(array $methods = array())
  646. {
  647. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
  648. ->setConstructorArgs(array('test', false))
  649. ->setMethods($methods)
  650. ->getMock();
  651. $p = new \ReflectionProperty($kernel, 'rootDir');
  652. $p->setAccessible(true);
  653. $p->setValue($kernel, __DIR__.'/Fixtures');
  654. return $kernel;
  655. }
  656. }
  657. class TestKernel implements HttpKernelInterface
  658. {
  659. public $terminateCalled = false;
  660. public function terminate()
  661. {
  662. $this->terminateCalled = true;
  663. }
  664. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  665. {
  666. }
  667. }