RunScriptCommandTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Command;
  12. use Composer\Composer;
  13. use Composer\Config;
  14. use Composer\Script\Event as ScriptEvent;
  15. use Composer\Test\TestCase;
  16. class RunScriptCommandTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider getDevOptions
  20. * @param bool $dev
  21. * @param bool $noDev
  22. */
  23. public function testDetectAndPassDevModeToEventAndToDispatching($dev, $noDev)
  24. {
  25. $scriptName = 'testScript';
  26. $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  27. $input
  28. ->method('getOption')
  29. ->will($this->returnValueMap(array(
  30. array('list', false),
  31. array('dev', $dev),
  32. array('no-dev', $noDev),
  33. )));
  34. $input
  35. ->method('getArgument')
  36. ->will($this->returnValueMap(array(
  37. array('script', $scriptName),
  38. array('args', array()),
  39. )));
  40. $input
  41. ->method('hasArgument')
  42. ->with('command')
  43. ->willReturn(false);
  44. $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  45. $expectedDevMode = $dev || !$noDev;
  46. $ed = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $ed->expects($this->once())
  50. ->method('hasEventListeners')
  51. ->with($this->callback(function (ScriptEvent $event) use ($scriptName, $expectedDevMode) {
  52. return $event->getName() === $scriptName
  53. && $event->isDevMode() === $expectedDevMode;
  54. }))
  55. ->willReturn(true);
  56. $ed->expects($this->once())
  57. ->method('dispatchScript')
  58. ->with($scriptName, $expectedDevMode, array())
  59. ->willReturn(0);
  60. $composer = $this->createComposerInstance();
  61. $composer->setEventDispatcher($ed);
  62. $command = $this->getMockBuilder('Composer\Command\RunScriptCommand')
  63. ->setMethods(array(
  64. 'mergeApplicationDefinition',
  65. 'bind',
  66. 'getSynopsis',
  67. 'initialize',
  68. 'isInteractive',
  69. 'getComposer',
  70. ))
  71. ->getMock();
  72. $command->expects($this->any())->method('getComposer')->willReturn($composer);
  73. $command->method('isInteractive')->willReturn(false);
  74. $command->run($input, $output);
  75. }
  76. public function getDevOptions()
  77. {
  78. return array(
  79. array(true, true),
  80. array(true, false),
  81. array(false, true),
  82. array(false, false),
  83. );
  84. }
  85. private function createComposerInstance()
  86. {
  87. $composer = new Composer;
  88. $config = new Config;
  89. $composer->setConfig($config);
  90. return $composer;
  91. }
  92. }