DumpCommandTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Output\NullOutput;
  14. class DumpCommandTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $writeTo;
  17. private $application;
  18. private $definition;
  19. private $kernel;
  20. private $container;
  21. private $am;
  22. protected function setUp()
  23. {
  24. if (!class_exists('Assetic\\AssetManager')) {
  25. $this->markTestSkipped('Assetic is not available.');
  26. }
  27. $this->writeTo = sys_get_temp_dir().'/assetic_dump';
  28. $this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
  35. $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
  36. $this->am = $this->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->application->expects($this->any())
  40. ->method('getDefinition')
  41. ->will($this->returnValue($this->definition));
  42. $this->definition->expects($this->any())
  43. ->method('getArguments')
  44. ->will($this->returnValue(array()));
  45. $this->definition->expects($this->any())
  46. ->method('getOptions')
  47. ->will($this->returnValue(array()));
  48. $this->application->expects($this->any())
  49. ->method('getKernel')
  50. ->will($this->returnValue($this->kernel));
  51. $this->kernel->expects($this->any())
  52. ->method('getContainer')
  53. ->will($this->returnValue($this->container));
  54. $this->container->expects($this->any())
  55. ->method('getParameter')
  56. ->with('assetic.write_to')
  57. ->will($this->returnValue($this->writeTo));
  58. $this->container->expects($this->once())
  59. ->method('get')
  60. ->with('assetic.asset_manager')
  61. ->will($this->returnValue($this->am));
  62. $this->command = new DumpCommand();
  63. $this->command->setApplication($this->application);
  64. }
  65. protected function tearDown()
  66. {
  67. if (is_dir($this->writeTo)) {
  68. array_map('unlink', glob($this->writeTo.'/*'));
  69. rmdir($this->writeTo);
  70. }
  71. }
  72. public function testEmptyAssetManager()
  73. {
  74. $this->am->expects($this->once())
  75. ->method('getNames')
  76. ->will($this->returnValue(array()));
  77. $this->command->run(new ArrayInput(array()), new NullOutput());
  78. }
  79. public function testDumpOne()
  80. {
  81. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  82. $this->am->expects($this->once())
  83. ->method('getNames')
  84. ->will($this->returnValue(array('test_asset')));
  85. $this->am->expects($this->once())
  86. ->method('get')
  87. ->with('test_asset')
  88. ->will($this->returnValue($asset));
  89. $this->am->expects($this->once())
  90. ->method('getFormula')
  91. ->with('test_asset')
  92. ->will($this->returnValue(array()));
  93. $this->am->expects($this->once())
  94. ->method('isDebug')
  95. ->will($this->returnValue(false));
  96. $asset->expects($this->once())
  97. ->method('getTargetUrl')
  98. ->will($this->returnValue('test_asset.css'));
  99. $asset->expects($this->once())
  100. ->method('dump')
  101. ->will($this->returnValue('/* test_asset */'));
  102. $this->command->run(new ArrayInput(array()), new NullOutput());
  103. $this->assertFileExists($this->writeTo.'/test_asset.css');
  104. $this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
  105. }
  106. public function testDumpDebug()
  107. {
  108. $asset = $this->getMock('Assetic\\Asset\\AssetCollection');
  109. $leaf = $this->getMock('Assetic\\Asset\\AssetInterface');
  110. $this->am->expects($this->once())
  111. ->method('getNames')
  112. ->will($this->returnValue(array('test_asset')));
  113. $this->am->expects($this->once())
  114. ->method('get')
  115. ->with('test_asset')
  116. ->will($this->returnValue($asset));
  117. $this->am->expects($this->once())
  118. ->method('getFormula')
  119. ->with('test_asset')
  120. ->will($this->returnValue(array()));
  121. $this->am->expects($this->once())
  122. ->method('isDebug')
  123. ->will($this->returnValue(true));
  124. $asset->expects($this->once())
  125. ->method('getTargetUrl')
  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->once())
  131. ->method('getIterator')
  132. ->will($this->returnValue(new \ArrayIterator(array($leaf))));
  133. $leaf->expects($this->once())
  134. ->method('getTargetUrl')
  135. ->will($this->returnValue('test_leaf.css'));
  136. $leaf->expects($this->once())
  137. ->method('dump')
  138. ->will($this->returnValue('/* test_leaf */'));
  139. $this->command->run(new ArrayInput(array()), new NullOutput());
  140. $this->assertFileExists($this->writeTo.'/test_asset.css');
  141. $this->assertFileExists($this->writeTo.'/test_leaf.css');
  142. $this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
  143. $this->assertEquals('/* test_leaf */', file_get_contents($this->writeTo.'/test_leaf.css'));
  144. }
  145. }