TwigFormulaLoaderTest.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  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 Assetic\Test\Extension\Twig;
  11. use Assetic\Factory\AssetFactory;
  12. use Assetic\Extension\Twig\AsseticExtension;
  13. use Assetic\Extension\Twig\TwigFormulaLoader;
  14. class TwigFormulaLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $am;
  17. private $fm;
  18. private $twig;
  19. protected function setUp()
  20. {
  21. if (!class_exists('Twig_Environment')) {
  22. $this->markTestSkipped('Twig is not installed.');
  23. }
  24. $this->am = $this->getMock('Assetic\\AssetManager');
  25. $this->fm = $this->getMock('Assetic\\FilterManager');
  26. $factory = new AssetFactory(__DIR__.'/templates');
  27. $factory->setAssetManager($this->am);
  28. $factory->setFilterManager($this->fm);
  29. $twig = new \Twig_Environment();
  30. $twig->addExtension(new AsseticExtension($factory, array(
  31. 'some_func' => array(
  32. 'filter' => 'some_filter',
  33. 'options' => array('output' => 'css/*.css'),
  34. ),
  35. )));
  36. $this->loader = new TwigFormulaLoader($twig);
  37. }
  38. public function testMixture()
  39. {
  40. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  41. $expected = array(
  42. 'mixture' => array(
  43. array('foo', 'foo/*', '@foo'),
  44. array(),
  45. array(
  46. 'output' => 'packed/mixture',
  47. 'name' => 'mixture',
  48. 'debug' => false,
  49. 'combine' => null,
  50. ),
  51. ),
  52. );
  53. $resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
  54. $resource->expects($this->once())
  55. ->method('getContent')
  56. ->will($this->returnValue(file_get_contents(__DIR__.'/templates/mixture.twig')));
  57. $this->am->expects($this->any())
  58. ->method('get')
  59. ->with('foo')
  60. ->will($this->returnValue($asset));
  61. $formulae = $this->loader->load($resource);
  62. $this->assertEquals($expected, $formulae);
  63. }
  64. public function testFunction()
  65. {
  66. $expected = array(
  67. 'my_asset' => array(
  68. array('path/to/asset'),
  69. array('some_filter'),
  70. array('output' => 'css/*.css', 'name' => 'my_asset'),
  71. ),
  72. );
  73. $resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
  74. $resource->expects($this->once())
  75. ->method('getContent')
  76. ->will($this->returnValue(file_get_contents(__DIR__.'/templates/function.twig')));
  77. $formulae = $this->loader->load($resource);
  78. $this->assertEquals($expected, $formulae);
  79. }
  80. public function testUnclosedTag()
  81. {
  82. $resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
  83. $resource->expects($this->once())
  84. ->method('getContent')
  85. ->will($this->returnValue(file_get_contents(__DIR__.'/templates/unclosed_tag.twig')));
  86. $formulae = $this->loader->load($resource);
  87. $this->assertEquals(array(), $formulae);
  88. }
  89. }