PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/assetic-bundle/Symfony/Bundle/AsseticBundle/Tests/Command/DumpCommandTest.php

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