FunctionalTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\Tests;
  11. use Symfony\Component\DomCrawler\Crawler;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Bundle\FrameworkBundle\Util\Filesystem;
  14. /**
  15. * @group functional
  16. */
  17. class FunctionalTest extends \PHPUnit_Framework_TestCase
  18. {
  19. protected $cacheDir;
  20. protected function setUp()
  21. {
  22. if (!class_exists('Assetic\\AssetManager')) {
  23. $this->markTestSkipped('Assetic is not available.');
  24. }
  25. $this->cacheDir = __DIR__.'/Resources/cache';
  26. if (file_exists($this->cacheDir)) {
  27. $filesystem = new Filesystem();
  28. $filesystem->remove($this->cacheDir);
  29. }
  30. mkdir($this->cacheDir, 0777, true);
  31. }
  32. protected function tearDown()
  33. {
  34. $filesystem = new Filesystem();
  35. $filesystem->remove($this->cacheDir);
  36. }
  37. /**
  38. * @dataProvider provideDebugAndAssetCount
  39. */
  40. public function testKernel($debug, $count)
  41. {
  42. $kernel = new TestKernel('test', $debug);
  43. $kernel->boot();
  44. $this->assertEquals($count, count($kernel->getContainer()->get('assetic.asset_manager')->getNames()));
  45. }
  46. /**
  47. * @dataProvider provideDebugAndAssetCount
  48. */
  49. public function testRoutes($debug, $count)
  50. {
  51. $kernel = new TestKernel('test', $debug);
  52. $kernel->boot();
  53. $matches = 0;
  54. foreach (array_keys($kernel->getContainer()->get('router')->getRouteCollection()->all()) as $name) {
  55. if (0 === strpos($name, 'assetic_')) {
  56. ++$matches;
  57. }
  58. }
  59. $this->assertEquals($count, $matches);
  60. }
  61. public function testTwigRenderDebug()
  62. {
  63. $kernel = new TestKernel('test', true);
  64. $kernel->boot();
  65. $container = $kernel->getContainer();
  66. $container->enterScope('request');
  67. $container->set('request', new Request());
  68. $content = $container->get('templating')->render('::layout.html.twig');
  69. $crawler = new Crawler($content);
  70. $this->assertEquals(3, count($crawler->filter('link[href$=".css"]')));
  71. $this->assertEquals(2, count($crawler->filter('script[src$=".js"]')));
  72. }
  73. public function testPhpRenderDebug()
  74. {
  75. $kernel = new TestKernel('test', true);
  76. $kernel->boot();
  77. $container = $kernel->getContainer();
  78. $container->enterScope('request');
  79. $container->set('request', new Request());
  80. $content = $container->get('templating')->render('::layout.html.php');
  81. $crawler = new Crawler($content);
  82. $this->assertEquals(3, count($crawler->filter('link[href$=".css"]')));
  83. $this->assertEquals(2, count($crawler->filter('script[src$=".js"]')));
  84. }
  85. public function provideDebugAndAssetCount()
  86. {
  87. // totals include assets defined in both php and twig templates
  88. return array(
  89. array(true, 8),
  90. array(false, 3),
  91. );
  92. }
  93. }