FunctionalTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. class FunctionalTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected function setUp()
  16. {
  17. if (!class_exists('Assetic\\AssetManager')) {
  18. $this->markTestSkipped('Assetic is not available.');
  19. }
  20. $cache = __DIR__.'/Resources/cache';
  21. if (!is_dir($cache)) {
  22. mkdir($cache);
  23. } else {
  24. shell_exec('rm -rf '.escapeshellarg(__DIR__.'/Resources/cache/*'));
  25. }
  26. }
  27. protected function tearDown()
  28. {
  29. shell_exec('rm -rf '.escapeshellarg(__DIR__.'/Resources/cache'));
  30. }
  31. /**
  32. * @dataProvider provideDebugAndAssetCount
  33. */
  34. public function testKernel($debug, $count)
  35. {
  36. $kernel = new TestKernel('test', $debug);
  37. $kernel->boot();
  38. $container = $kernel->getContainer();
  39. $names = $container->get('assetic.asset_manager')->getNames();
  40. $this->assertEquals($count, count($names));
  41. }
  42. /**
  43. * @dataProvider provideDebugAndAssetCount
  44. */
  45. public function testRoutes($debug, $count)
  46. {
  47. $kernel = new TestKernel('test', $debug);
  48. $kernel->boot();
  49. $container = $kernel->getContainer();
  50. $routes = $container->get('router')->getRouteCollection()->all();
  51. $matches = 0;
  52. foreach (array_keys($routes) as $name) {
  53. if (0 === strpos($name, 'assetic_')) {
  54. ++$matches;
  55. }
  56. }
  57. $this->assertEquals($count, $matches);
  58. }
  59. public function testTwigRenderDebug()
  60. {
  61. $kernel = new TestKernel('test', true);
  62. $kernel->boot();
  63. $container = $kernel->getContainer();
  64. $container->enterScope('request');
  65. $container->set('request', new Request());
  66. $content = $container->get('templating')->render('::layout.html.twig');
  67. $crawler = new Crawler($content);
  68. $this->assertEquals(3, count($crawler->filter('link[href$=".css"]')));
  69. $this->assertEquals(2, count($crawler->filter('script[src$=".js"]')));
  70. }
  71. public function testPhpRenderDebug()
  72. {
  73. $this->markTestIncomplete('PHP templating is not ready yet.');
  74. $kernel = new TestKernel('test', true);
  75. $kernel->boot();
  76. $container = $kernel->getContainer();
  77. $container->enterScope('request');
  78. $container->set('request', new Request());
  79. $content = $container->get('templating')->render('::layout.html.php');
  80. $crawler = new Crawler($content);
  81. $this->assertEquals(3, count($crawler->filter('link[href$=".css"]')));
  82. $this->assertEquals(2, count($crawler->filter('script[src$=".js"]')));
  83. }
  84. public function provideDebugAndAssetCount()
  85. {
  86. return array(
  87. array(true, 5),
  88. array(false, 2),
  89. );
  90. }
  91. }