DumpCommandTest.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $this->writeTo = sys_get_temp_dir().'/assetic_dump';
  29. $this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
  36. $this->helperSet = $this->getMock('Symfony\\Component\\Console\\Helper\\HelperSet');
  37. $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  38. $this->am = $this->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->application->expects($this->any())
  42. ->method('getDefinition')
  43. ->will($this->returnValue($this->definition));
  44. $this->definition->expects($this->any())
  45. ->method('getArguments')
  46. ->will($this->returnValue(array()));
  47. $this->definition->expects($this->any())
  48. ->method('getOptions')
  49. ->will($this->returnValue(array(
  50. new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages.'),
  51. new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'),
  52. new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'),
  53. )));
  54. $this->application->expects($this->any())
  55. ->method('getKernel')
  56. ->will($this->returnValue($this->kernel));
  57. $this->application->expects($this->once())
  58. ->method('getHelperSet')
  59. ->will($this->returnValue($this->helperSet));
  60. $this->kernel->expects($this->any())
  61. ->method('getContainer')
  62. ->will($this->returnValue($this->container));
  63. $this->container->expects($this->any())
  64. ->method('getParameter')
  65. ->with('assetic.write_to')
  66. ->will($this->returnValue($this->writeTo));
  67. $this->container->expects($this->once())
  68. ->method('get')
  69. ->with('assetic.asset_manager')
  70. ->will($this->returnValue($this->am));
  71. $this->command = new DumpCommand();
  72. $this->command->setApplication($this->application);
  73. }
  74. protected function tearDown()
  75. {
  76. if (is_dir($this->writeTo)) {
  77. array_map('unlink', glob($this->writeTo.'/*'));
  78. rmdir($this->writeTo);
  79. }
  80. }
  81. public function testEmptyAssetManager()
  82. {
  83. $this->am->expects($this->once())
  84. ->method('getNames')
  85. ->will($this->returnValue(array()));
  86. $this->command->run(new ArrayInput(array()), new NullOutput());
  87. }
  88. public function testDumpOne()
  89. {
  90. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  91. $this->am->expects($this->once())
  92. ->method('getNames')
  93. ->will($this->returnValue(array('test_asset')));
  94. $this->am->expects($this->once())
  95. ->method('get')
  96. ->with('test_asset')
  97. ->will($this->returnValue($asset));
  98. $this->am->expects($this->once())
  99. ->method('getFormula')
  100. ->with('test_asset')
  101. ->will($this->returnValue(array()));
  102. $this->am->expects($this->once())
  103. ->method('isDebug')
  104. ->will($this->returnValue(false));
  105. $asset->expects($this->once())
  106. ->method('getTargetPath')
  107. ->will($this->returnValue('test_asset.css'));
  108. $asset->expects($this->once())
  109. ->method('dump')
  110. ->will($this->returnValue('/* test_asset */'));
  111. $this->command->run(new ArrayInput(array()), new NullOutput());
  112. $this->assertFileExists($this->writeTo.'/test_asset.css');
  113. $this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
  114. }
  115. public function testDumpDebug()
  116. {
  117. $asset = $this->getMock('Assetic\\Asset\\AssetCollection');
  118. $leaf = $this->getMock('Assetic\\Asset\\AssetInterface');
  119. $this->am->expects($this->once())
  120. ->method('getNames')
  121. ->will($this->returnValue(array('test_asset')));
  122. $this->am->expects($this->once())
  123. ->method('get')
  124. ->with('test_asset')
  125. ->will($this->returnValue($asset));
  126. $this->am->expects($this->once())
  127. ->method('getFormula')
  128. ->with('test_asset')
  129. ->will($this->returnValue(array()));
  130. $this->am->expects($this->once())
  131. ->method('isDebug')
  132. ->will($this->returnValue(true));
  133. $asset->expects($this->once())
  134. ->method('getTargetPath')
  135. ->will($this->returnValue('test_asset.css'));
  136. $asset->expects($this->once())
  137. ->method('dump')
  138. ->will($this->returnValue('/* test_asset */'));
  139. $asset->expects($this->once())
  140. ->method('getIterator')
  141. ->will($this->returnValue(new \ArrayIterator(array($leaf))));
  142. $leaf->expects($this->once())
  143. ->method('getTargetPath')
  144. ->will($this->returnValue('test_leaf.css'));
  145. $leaf->expects($this->once())
  146. ->method('dump')
  147. ->will($this->returnValue('/* test_leaf */'));
  148. $this->command->run(new ArrayInput(array()), new NullOutput());
  149. $this->assertFileExists($this->writeTo.'/test_asset.css');
  150. $this->assertFileExists($this->writeTo.'/test_leaf.css');
  151. $this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
  152. $this->assertEquals('/* test_leaf */', file_get_contents($this->writeTo.'/test_leaf.css'));
  153. }
  154. }