AsseticExtensionTest.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 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. class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $am;
  16. private $fm;
  17. private $factory;
  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. $this->factory = new AssetFactory(__DIR__.'/templates');
  27. $this->factory->setAssetManager($this->am);
  28. $this->factory->setFilterManager($this->fm);
  29. $this->twig = new \Twig_Environment();
  30. $this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__.'/templates'));
  31. $this->twig->addExtension(new AsseticExtension($this->factory));
  32. }
  33. public function testReference()
  34. {
  35. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  36. $this->am->expects($this->any())
  37. ->method('get')
  38. ->with('foo')
  39. ->will($this->returnValue($asset));
  40. $xml = $this->renderXml('reference.twig');
  41. $this->assertEquals(1, count($xml->asset));
  42. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  43. }
  44. public function testGlob()
  45. {
  46. $xml = $this->renderXml('glob.twig');
  47. $this->assertEquals(1, count($xml->asset));
  48. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  49. }
  50. public function testAbsolutePath()
  51. {
  52. $xml = $this->renderXml('absolute_path.twig');
  53. $this->assertEquals(1, count($xml->asset));
  54. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  55. }
  56. public function testFilters()
  57. {
  58. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  59. $this->fm->expects($this->at(0))
  60. ->method('get')
  61. ->with('foo')
  62. ->will($this->returnValue($filter));
  63. $this->fm->expects($this->at(1))
  64. ->method('get')
  65. ->with('bar')
  66. ->will($this->returnValue($filter));
  67. $xml = $this->renderXml('filters.twig');
  68. $this->assertEquals(1, count($xml->asset));
  69. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  70. }
  71. public function testOptionalFilter()
  72. {
  73. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  74. $this->fm->expects($this->once())
  75. ->method('get')
  76. ->with('foo')
  77. ->will($this->returnValue($filter));
  78. $xml = $this->renderXml('optional_filter.twig');
  79. $this->assertEquals(1, count($xml->asset));
  80. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  81. }
  82. public function testOutputPattern()
  83. {
  84. $xml = $this->renderXml('output_pattern.twig');
  85. $this->assertEquals(1, count($xml->asset));
  86. $this->assertStringStartsWith('css/packed/', (string) $xml->asset['url']);
  87. $this->assertStringEndsWith('.css', (string) $xml->asset['url']);
  88. }
  89. public function testOutput()
  90. {
  91. $xml = $this->renderXml('output_url.twig');
  92. $this->assertEquals(1, count($xml->asset));
  93. $this->assertEquals('explicit_url.css', (string) $xml->asset['url']);
  94. }
  95. public function testMixture()
  96. {
  97. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  98. $this->am->expects($this->any())
  99. ->method('get')
  100. ->with('foo')
  101. ->will($this->returnValue($asset));
  102. $xml = $this->renderXml('mixture.twig');
  103. $this->assertEquals(1, count($xml->asset));
  104. $this->assertEquals('packed/mixture', (string) $xml->asset['url']);
  105. }
  106. public function testDebug()
  107. {
  108. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  109. $this->fm->expects($this->once())
  110. ->method('get')
  111. ->with('bar')
  112. ->will($this->returnValue($filter));
  113. $xml = $this->renderXml('debug.twig');
  114. $this->assertEquals(2, count($xml->asset));
  115. $this->assertStringStartsWith('css/packed_', (string) $xml->asset[0]['url']);
  116. $this->assertStringEndsWith('.css', (string) $xml->asset[0]['url']);
  117. }
  118. public function testCombine()
  119. {
  120. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  121. $this->fm->expects($this->once())
  122. ->method('get')
  123. ->with('bar')
  124. ->will($this->returnValue($filter));
  125. $xml = $this->renderXml('combine.twig');
  126. $this->assertEquals(1, count($xml->asset));
  127. $this->assertEquals('css/packed.css', (string) $xml->asset[0]['url']);
  128. }
  129. public function testImage()
  130. {
  131. $xml = $this->renderXml('image.twig');
  132. $this->assertEquals(1, count($xml->image));
  133. $this->assertStringEndsWith('.png', (string) $xml->image[0]['url']);
  134. }
  135. public function testFilterFunction()
  136. {
  137. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  138. $this->fm->expects($this->once())
  139. ->method('get')
  140. ->with('some_filter')
  141. ->will($this->returnValue($filter));
  142. $this->twig->addExtension(new AsseticExtension($this->factory, array(
  143. 'some_func' => array(
  144. 'filter' => 'some_filter',
  145. 'options' => array('output' => 'css/*.css'),
  146. ),
  147. )));
  148. $xml = $this->renderXml('function.twig');
  149. $this->assertEquals(1, count($xml->asset));
  150. $this->assertStringEndsWith('.css', (string) $xml->asset[0]['url']);
  151. }
  152. private function renderXml($name, $context = array())
  153. {
  154. return new \SimpleXMLElement($this->twig->loadTemplate($name)->render($context));
  155. }
  156. }