PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/kriswallsmith/assetic/tests/Assetic/Test/AssetWriterTest.php

https://bitbucket.org/arturoblack/tomoyo
PHP | 207 lines | 164 code | 28 blank | 15 comment | 0 complexity | ee6341bfc6b33667276243a342db7899 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, Apache-2.0, LGPL-2.1, CC-BY-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2013 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;
  11. use Assetic\Asset\FileAsset;
  12. use Assetic\AssetManager;
  13. use Assetic\AssetWriter;
  14. class AssetWriterTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected function setUp()
  17. {
  18. $this->dir = sys_get_temp_dir().'/assetic_tests_'.rand(11111, 99999);
  19. mkdir($this->dir);
  20. $this->writer = new AssetWriter($this->dir, array(
  21. 'locale' => array('en', 'de', 'fr'),
  22. 'browser' => array('ie', 'firefox', 'other'),
  23. 'gzip' => array('gzip', '')
  24. ));
  25. }
  26. protected function tearDown()
  27. {
  28. array_map('unlink', glob($this->dir.'/*'));
  29. rmdir($this->dir);
  30. }
  31. public function testWriteManagerAssets()
  32. {
  33. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  34. $am = $this->getMock('Assetic\\AssetManager');
  35. $am->expects($this->once())
  36. ->method('getNames')
  37. ->will($this->returnValue(array('foo')));
  38. $am->expects($this->once())
  39. ->method('get')
  40. ->with('foo')
  41. ->will($this->returnValue($asset));
  42. $asset->expects($this->atLeastOnce())
  43. ->method('getTargetPath')
  44. ->will($this->returnValue('target_url'));
  45. $asset->expects($this->once())
  46. ->method('dump')
  47. ->will($this->returnValue('content'));
  48. $asset->expects($this->atLeastOnce())
  49. ->method('getVars')
  50. ->will($this->returnValue(array()));
  51. $asset->expects($this->atLeastOnce())
  52. ->method('getValues')
  53. ->will($this->returnValue(array()));
  54. $this->writer->writeManagerAssets($am);
  55. $this->assertFileExists($this->dir.'/target_url');
  56. $this->assertEquals('content', file_get_contents($this->dir.'/target_url'));
  57. }
  58. public function testWriteAssetWithVars()
  59. {
  60. $asset = $this->getMock('Assetic\Asset\AssetInterface');
  61. $asset->expects($this->atLeastOnce())
  62. ->method('getVars')
  63. ->will($this->returnValue(array('locale')));
  64. $self = $this;
  65. $expectedValues = array(
  66. array('locale' => 'en'),
  67. array('locale' => 'de'),
  68. array('locale' => 'fr'),
  69. );
  70. $asset->expects($this->exactly(3))
  71. ->method('setValues')
  72. ->will($this->returnCallback(function($values) use ($self, $expectedValues) {
  73. static $counter = 0;
  74. $self->assertEquals($expectedValues[$counter++], $values);
  75. }));
  76. $asset->expects($this->exactly(3))
  77. ->method('getValues')
  78. ->will($this->returnCallback(function() use ($expectedValues) {
  79. static $counter = 0;
  80. return $expectedValues[$counter++];
  81. }));
  82. $asset->expects($this->exactly(3))
  83. ->method('dump')
  84. ->will($this->onConsecutiveCalls('en', 'de', 'fr'));
  85. $asset->expects($this->atLeastOnce())
  86. ->method('getTargetPath')
  87. ->will($this->returnValue('target.{locale}'));
  88. $this->writer->writeAsset($asset);
  89. $this->assertFileExists($this->dir.'/target.en');
  90. $this->assertFileExists($this->dir.'/target.de');
  91. $this->assertFileExists($this->dir.'/target.fr');
  92. $this->assertEquals('en', file_get_contents($this->dir.'/target.en'));
  93. $this->assertEquals('de', file_get_contents($this->dir.'/target.de'));
  94. $this->assertEquals('fr', file_get_contents($this->dir.'/target.fr'));
  95. }
  96. public function testAssetWithInputVars()
  97. {
  98. $asset = new FileAsset(__DIR__.'/Fixture/messages.{locale}.js',
  99. array(), null, null, array('locale'));
  100. $asset->setTargetPath('messages.{locale}.js');
  101. $this->writer->writeAsset($asset);
  102. $this->assertFileExists($this->dir.'/messages.en.js');
  103. $this->assertFileExists($this->dir.'/messages.de.js');
  104. $this->assertFileExists($this->dir.'/messages.fr.js');
  105. $this->assertEquals('var messages = {"text.greeting": "Hello %name%!"};',
  106. file_get_contents($this->dir.'/messages.en.js'));
  107. $this->assertEquals('var messages = {"text.greeting": "Hallo %name%!"};',
  108. file_get_contents($this->dir.'/messages.de.js'));
  109. $this->assertEquals('var messages = {"text.greet": "All\u00f4 %name%!"};',
  110. file_get_contents($this->dir.'/messages.fr.js'));
  111. }
  112. /**
  113. * @dataProvider getCombinationTests
  114. */
  115. public function testGetCombinations($vars, $expectedCombinations)
  116. {
  117. $ref = new \ReflectionMethod($this->writer, 'getCombinations');
  118. $ref->setAccessible(true);
  119. $this->assertEquals($expectedCombinations, $ref->invoke($this->writer, $vars));
  120. }
  121. public function getCombinationTests()
  122. {
  123. $tests = array();
  124. // no variables
  125. $tests[] = array(
  126. array(),
  127. array(array())
  128. );
  129. // one variables
  130. $tests[] = array(
  131. array('locale'),
  132. array(
  133. array('locale' => 'en'),
  134. array('locale' => 'de'),
  135. array('locale' => 'fr'),
  136. )
  137. );
  138. // two variables
  139. $tests[] = array(
  140. array('locale', 'browser'),
  141. array(
  142. array('locale' => 'en', 'browser' => 'ie'),
  143. array('locale' => 'de', 'browser' => 'ie'),
  144. array('locale' => 'fr', 'browser' => 'ie'),
  145. array('locale' => 'en', 'browser' => 'firefox'),
  146. array('locale' => 'de', 'browser' => 'firefox'),
  147. array('locale' => 'fr', 'browser' => 'firefox'),
  148. array('locale' => 'en', 'browser' => 'other'),
  149. array('locale' => 'de', 'browser' => 'other'),
  150. array('locale' => 'fr', 'browser' => 'other'),
  151. )
  152. );
  153. // three variables
  154. $tests[] = array(
  155. array('locale', 'browser', 'gzip'),
  156. array(
  157. array('locale' => 'en', 'browser' => 'ie', 'gzip' => 'gzip'),
  158. array('locale' => 'de', 'browser' => 'ie', 'gzip' => 'gzip'),
  159. array('locale' => 'fr', 'browser' => 'ie', 'gzip' => 'gzip'),
  160. array('locale' => 'en', 'browser' => 'firefox', 'gzip' => 'gzip'),
  161. array('locale' => 'de', 'browser' => 'firefox', 'gzip' => 'gzip'),
  162. array('locale' => 'fr', 'browser' => 'firefox', 'gzip' => 'gzip'),
  163. array('locale' => 'en', 'browser' => 'other', 'gzip' => 'gzip'),
  164. array('locale' => 'de', 'browser' => 'other', 'gzip' => 'gzip'),
  165. array('locale' => 'fr', 'browser' => 'other', 'gzip' => 'gzip'),
  166. array('locale' => 'en', 'browser' => 'ie', 'gzip' => ''),
  167. array('locale' => 'de', 'browser' => 'ie', 'gzip' => ''),
  168. array('locale' => 'fr', 'browser' => 'ie', 'gzip' => ''),
  169. array('locale' => 'en', 'browser' => 'firefox', 'gzip' => ''),
  170. array('locale' => 'de', 'browser' => 'firefox', 'gzip' => ''),
  171. array('locale' => 'fr', 'browser' => 'firefox', 'gzip' => ''),
  172. array('locale' => 'en', 'browser' => 'other', 'gzip' => ''),
  173. array('locale' => 'de', 'browser' => 'other', 'gzip' => ''),
  174. array('locale' => 'fr', 'browser' => 'other', 'gzip' => ''),
  175. )
  176. );
  177. return $tests;
  178. }
  179. }