PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/kriswallsmith/assetic/tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php

https://bitbucket.org/renta/jobeet2.loc
PHP | 232 lines | 164 code | 42 blank | 26 comment | 1 complexity | e6a709fd92dcf40ae8e24b67cdc29d0c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, Apache-2.0, CC-BY-3.0
  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. class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject|\Assetic\AssetManager
  17. */
  18. private $am;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject|\Assetic\FilterManager
  21. */
  22. private $fm;
  23. /**
  24. * @var AssetFactory
  25. */
  26. private $factory;
  27. /**
  28. * @var \Twig_Environment
  29. */
  30. private $twig;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject|\Assetic\ValueSupplierInterface
  33. */
  34. private $valueSupplier;
  35. protected function setUp()
  36. {
  37. if (!class_exists('Twig_Environment')) {
  38. $this->markTestSkipped('Twig is not installed.');
  39. }
  40. $this->am = $this->getMock('Assetic\\AssetManager');
  41. $this->fm = $this->getMock('Assetic\\FilterManager');
  42. $this->valueSupplier = $this->getMock('Assetic\ValueSupplierInterface');
  43. $this->factory = new AssetFactory(__DIR__.'/templates');
  44. $this->factory->setAssetManager($this->am);
  45. $this->factory->setFilterManager($this->fm);
  46. $this->twig = new \Twig_Environment();
  47. $this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__.'/templates'));
  48. $this->twig->addExtension(new AsseticExtension($this->factory, array(), $this->valueSupplier));
  49. }
  50. public function testReference()
  51. {
  52. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  53. $this->am->expects($this->any())
  54. ->method('get')
  55. ->with('foo')
  56. ->will($this->returnValue($asset));
  57. $xml = $this->renderXml('reference.twig');
  58. $this->assertEquals(1, count($xml->asset));
  59. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  60. }
  61. public function testGlob()
  62. {
  63. $xml = $this->renderXml('glob.twig');
  64. $this->assertEquals(1, count($xml->asset));
  65. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  66. }
  67. public function testAbsolutePath()
  68. {
  69. $xml = $this->renderXml('absolute_path.twig');
  70. $this->assertEquals(1, count($xml->asset));
  71. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  72. }
  73. public function testFilters()
  74. {
  75. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  76. $this->fm->expects($this->at(0))
  77. ->method('get')
  78. ->with('foo')
  79. ->will($this->returnValue($filter));
  80. $this->fm->expects($this->at(1))
  81. ->method('get')
  82. ->with('bar')
  83. ->will($this->returnValue($filter));
  84. $xml = $this->renderXml('filters.twig');
  85. $this->assertEquals(1, count($xml->asset));
  86. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  87. }
  88. public function testOptionalFilter()
  89. {
  90. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  91. $this->fm->expects($this->once())
  92. ->method('get')
  93. ->with('foo')
  94. ->will($this->returnValue($filter));
  95. $xml = $this->renderXml('optional_filter.twig');
  96. $this->assertEquals(1, count($xml->asset));
  97. $this->assertStringStartsWith('css/', (string) $xml->asset['url']);
  98. }
  99. public function testOutputPattern()
  100. {
  101. $xml = $this->renderXml('output_pattern.twig');
  102. $this->assertEquals(1, count($xml->asset));
  103. $this->assertStringStartsWith('css/packed/', (string) $xml->asset['url']);
  104. $this->assertStringEndsWith('.css', (string) $xml->asset['url']);
  105. }
  106. public function testOutput()
  107. {
  108. $xml = $this->renderXml('output_url.twig');
  109. $this->assertEquals(1, count($xml->asset));
  110. $this->assertEquals('explicit_url.css', (string) $xml->asset['url']);
  111. }
  112. public function testMixture()
  113. {
  114. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  115. $this->am->expects($this->any())
  116. ->method('get')
  117. ->with('foo')
  118. ->will($this->returnValue($asset));
  119. $xml = $this->renderXml('mixture.twig');
  120. $this->assertEquals(1, count($xml->asset));
  121. $this->assertEquals('packed/mixture', (string) $xml->asset['url']);
  122. }
  123. public function testDebug()
  124. {
  125. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  126. $this->fm->expects($this->once())
  127. ->method('get')
  128. ->with('bar')
  129. ->will($this->returnValue($filter));
  130. $xml = $this->renderXml('debug.twig');
  131. $this->assertEquals(2, count($xml->asset));
  132. $this->assertStringStartsWith('css/packed_', (string) $xml->asset[0]['url']);
  133. $this->assertStringEndsWith('.css', (string) $xml->asset[0]['url']);
  134. }
  135. public function testCombine()
  136. {
  137. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  138. $this->fm->expects($this->once())
  139. ->method('get')
  140. ->with('bar')
  141. ->will($this->returnValue($filter));
  142. $xml = $this->renderXml('combine.twig');
  143. $this->assertEquals(1, count($xml->asset));
  144. $this->assertEquals('css/packed.css', (string) $xml->asset[0]['url']);
  145. }
  146. public function testImage()
  147. {
  148. $xml = $this->renderXml('image.twig');
  149. $this->assertEquals(1, count($xml->image));
  150. $this->assertStringEndsWith('.png', (string) $xml->image[0]['url']);
  151. }
  152. public function testFilterFunction()
  153. {
  154. $filter = $this->getMock('Assetic\\Filter\\FilterInterface');
  155. $this->fm->expects($this->once())
  156. ->method('get')
  157. ->with('some_filter')
  158. ->will($this->returnValue($filter));
  159. $this->twig->addExtension(new AsseticExtension($this->factory, array(
  160. 'some_func' => array(
  161. 'filter' => 'some_filter',
  162. 'options' => array('output' => 'css/*.css'),
  163. ),
  164. )));
  165. $xml = $this->renderXml('function.twig');
  166. $this->assertEquals(1, count($xml->asset));
  167. $this->assertStringEndsWith('.css', (string) $xml->asset[0]['url']);
  168. }
  169. public function testVariables()
  170. {
  171. $this->valueSupplier->expects($this->once())
  172. ->method('getValues')
  173. ->will($this->returnValue(array('foo' => 'a', 'bar' => 'b')));
  174. $xml = $this->renderXml('variables.twig');
  175. $this->assertEquals(2, $xml->url->count());
  176. $this->assertEquals("js/7d0828c_foo_1.a.b.js", (string) $xml->url[0]);
  177. $this->assertEquals("js/7d0828c_variable_input.a_2.a.b.js", (string) $xml->url[1]);
  178. }
  179. /**
  180. * @expectedException \Twig_Error
  181. */
  182. public function testUnclosedTag()
  183. {
  184. $this->renderXml('unclosed_tag.twig');
  185. }
  186. private function renderXml($name, $context = array())
  187. {
  188. return new \SimpleXMLElement($this->twig->loadTemplate($name)->render($context));
  189. }
  190. }