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

/vendor/symfony/assetic-bundle/Tests/Command/DumpCommandTest.php

https://gitlab.com/freebird/WebApp
PHP | 220 lines | 184 code | 25 blank | 11 comment | 4 complexity | 40503d143533b901f12584e879f3676e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Command;
  11. use Symfony\Bundle\AsseticBundle\Command\DumpCommand;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\NullOutput;
  15. class DumpCommandTest extends \PHPUnit_Framework_TestCase
  16. {
  17. private $writeTo;
  18. private $application;
  19. private $definition;
  20. private $kernel;
  21. private $container;
  22. private $am;
  23. private $helperSet;
  24. /**
  25. * @var DumpCommand
  26. */
  27. private $command;
  28. protected function setUp()
  29. {
  30. if (!class_exists('Assetic\\AssetManager')) {
  31. $this->markTestSkipped('Assetic is not available.');
  32. }
  33. if (!class_exists('Symfony\Component\Console\Application')) {
  34. $this->markTestSkipped('Symfony Console is not available.');
  35. }
  36. $this->writeTo = sys_get_temp_dir().'/assetic_dump';
  37. $this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
  44. $this->helperSet = $this->getMock('Symfony\\Component\\Console\\Helper\\HelperSet');
  45. $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  46. $this->am = $this->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->application->expects($this->any())
  50. ->method('getDefinition')
  51. ->will($this->returnValue($this->definition));
  52. $this->definition->expects($this->any())
  53. ->method('getArguments')
  54. ->will($this->returnValue(array()));
  55. $this->definition->expects($this->any())
  56. ->method('getOptions')
  57. ->will($this->returnValue(array(
  58. new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages.'),
  59. new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'),
  60. new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'),
  61. )));
  62. $this->application->expects($this->any())
  63. ->method('getKernel')
  64. ->will($this->returnValue($this->kernel));
  65. $this->application->expects($this->once())
  66. ->method('getHelperSet')
  67. ->will($this->returnValue($this->helperSet));
  68. $this->kernel->expects($this->any())
  69. ->method('getContainer')
  70. ->will($this->returnValue($this->container));
  71. $writeTo = $this->writeTo;
  72. $this->container->expects($this->any())
  73. ->method('getParameter')
  74. ->will($this->returnCallback(function ($p) use ($writeTo) {
  75. if ('assetic.write_to' === $p) {
  76. return $writeTo;
  77. } elseif ('assetic.variables' === $p) {
  78. return array();
  79. }
  80. throw new \RuntimeException(sprintf('Unknown parameter "%s".', $p));
  81. }));
  82. $this->container->expects($this->once())
  83. ->method('get')
  84. ->with('assetic.asset_manager')
  85. ->will($this->returnValue($this->am));
  86. $this->command = new DumpCommand();
  87. $this->command->setApplication($this->application);
  88. }
  89. protected function tearDown()
  90. {
  91. if (is_dir($this->writeTo)) {
  92. array_map('unlink', glob($this->writeTo.'/*'));
  93. rmdir($this->writeTo);
  94. }
  95. }
  96. public function testEmptyAssetManager()
  97. {
  98. $this->am->expects($this->once())
  99. ->method('getNames')
  100. ->will($this->returnValue(array()));
  101. $this->command->run(new ArrayInput(array()), new NullOutput());
  102. }
  103. public function testDumpOne()
  104. {
  105. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  106. $this->am->expects($this->once())
  107. ->method('getNames')
  108. ->will($this->returnValue(array('test_asset')));
  109. $this->am->expects($this->once())
  110. ->method('get')
  111. ->with('test_asset')
  112. ->will($this->returnValue($asset));
  113. $this->am->expects($this->once())
  114. ->method('hasFormula')
  115. ->with('test_asset')
  116. ->will($this->returnValue(true));
  117. $this->am->expects($this->once())
  118. ->method('getFormula')
  119. ->with('test_asset')
  120. ->will($this->returnValue(array()));
  121. $this->am->expects($this->exactly(2))
  122. ->method('isDebug')
  123. ->will($this->returnValue(false));
  124. $asset->expects($this->once())
  125. ->method('getTargetPath')
  126. ->will($this->returnValue('test_asset.css'));
  127. $asset->expects($this->once())
  128. ->method('dump')
  129. ->will($this->returnValue('/* test_asset */'));
  130. $asset->expects($this->any())
  131. ->method('getVars')
  132. ->will($this->returnValue(array()));
  133. $asset->expects($this->any())
  134. ->method('getValues')
  135. ->will($this->returnValue(array()));
  136. $this->command->run(new ArrayInput(array()), new NullOutput());
  137. $this->assertFileExists($this->writeTo.'/test_asset.css');
  138. $this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
  139. }
  140. public function testDumpDebug()
  141. {
  142. $asset = $this->getMock('Assetic\\Asset\\AssetCollection');
  143. $leaf = $this->getMock('Assetic\\Asset\\AssetInterface');
  144. $this->am->expects($this->once())
  145. ->method('getNames')
  146. ->will($this->returnValue(array('test_asset')));
  147. $this->am->expects($this->once())
  148. ->method('get')
  149. ->with('test_asset')
  150. ->will($this->returnValue($asset));
  151. $this->am->expects($this->once())
  152. ->method('hasFormula')
  153. ->with('test_asset')
  154. ->will($this->returnValue(true));
  155. $this->am->expects($this->once())
  156. ->method('getFormula')
  157. ->with('test_asset')
  158. ->will($this->returnValue(array()));
  159. $this->am->expects($this->exactly(2))
  160. ->method('isDebug')
  161. ->will($this->returnValue(true));
  162. $asset->expects($this->once())
  163. ->method('getTargetPath')
  164. ->will($this->returnValue('test_asset.css'));
  165. $asset->expects($this->once())
  166. ->method('dump')
  167. ->will($this->returnValue('/* test_asset */'));
  168. $asset->expects($this->once())
  169. ->method('getIterator')
  170. ->will($this->returnValue(new \ArrayIterator(array($leaf))));
  171. $asset->expects($this->any())
  172. ->method('getVars')
  173. ->will($this->returnValue(array()));
  174. $asset->expects($this->any())
  175. ->method('getValues')
  176. ->will($this->returnValue(array()));
  177. $leaf->expects($this->once())
  178. ->method('getTargetPath')
  179. ->will($this->returnValue('test_leaf.css'));
  180. $leaf->expects($this->once())
  181. ->method('dump')
  182. ->will($this->returnValue('/* test_leaf */'));
  183. $leaf->expects($this->any())
  184. ->method('getVars')
  185. ->will($this->returnValue(array()));
  186. $leaf->expects($this->any())
  187. ->method('getValues')
  188. ->will($this->returnValue(array()));
  189. $this->command->run(new ArrayInput(array()), new NullOutput());
  190. $this->assertFileExists($this->writeTo.'/test_asset.css');
  191. $this->assertFileExists($this->writeTo.'/test_leaf.css');
  192. $this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
  193. $this->assertEquals('/* test_leaf */', file_get_contents($this->writeTo.'/test_leaf.css'));
  194. }
  195. }