2022-02-23 15:58:18 +00:00
< ? php declare ( strict_types = 1 );
2016-07-21 22:20:56 +00:00
/*
* This file is part of Composer .
*
* ( c ) Nils Adermann < naderman @ naderman . de >
* Jordi Boggiano < j . boggiano @ seld . be >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Composer\Test\Command ;
2022-04-07 11:17:28 +00:00
use Composer\Command\RunScriptCommand ;
2016-07-21 22:20:56 +00:00
use Composer\Composer ;
use Composer\Config ;
2022-04-07 11:17:28 +00:00
use Composer\Console\Application ;
2016-07-21 22:20:56 +00:00
use Composer\Script\Event as ScriptEvent ;
2018-11-12 14:23:32 +00:00
use Composer\Test\TestCase ;
2022-04-07 11:17:28 +00:00
use Composer\Util\Filesystem ;
use Symfony\Component\Console\Tester\ApplicationTester ;
2016-07-21 22:20:56 +00:00
class RunScriptCommandTest extends TestCase
{
2022-04-07 11:17:28 +00:00
/**
* @ var string
*/
private $home ;
public function setUp () : void
{
$this -> home = $this -> getUniqueTmpDirectory ();
}
protected function tearDown () : void
{
parent :: tearDown ();
$fs = new Filesystem ();
$fs -> removeDirectory ( $this -> home );
}
2016-07-21 22:20:56 +00:00
/**
* @ dataProvider getDevOptions
* @ param bool $dev
* @ param bool $noDev
*/
2022-02-22 15:47:09 +00:00
public function testDetectAndPassDevModeToEventAndToDispatching ( bool $dev , bool $noDev ) : void
2016-07-21 22:20:56 +00:00
{
$scriptName = 'testScript' ;
2018-04-12 08:24:56 +00:00
$input = $this -> getMockBuilder ( 'Symfony\Component\Console\Input\InputInterface' ) -> getMock ();
2016-07-21 22:20:56 +00:00
$input
-> method ( 'getOption' )
-> will ( $this -> returnValueMap ( array (
array ( 'list' , false ),
array ( 'dev' , $dev ),
array ( 'no-dev' , $noDev ),
)));
$input
-> method ( 'getArgument' )
-> will ( $this -> returnValueMap ( array (
array ( 'script' , $scriptName ),
array ( 'args' , array ()),
)));
$input
-> method ( 'hasArgument' )
-> with ( 'command' )
-> willReturn ( false );
2021-12-09 21:14:04 +00:00
$input
-> method ( 'isInteractive' )
-> willReturn ( false );
2016-07-21 22:20:56 +00:00
2018-04-12 08:24:56 +00:00
$output = $this -> getMockBuilder ( 'Symfony\Component\Console\Output\OutputInterface' ) -> getMock ();
2016-07-21 22:20:56 +00:00
$expectedDevMode = $dev || ! $noDev ;
$ed = $this -> getMockBuilder ( 'Composer\EventDispatcher\EventDispatcher' )
-> disableOriginalConstructor ()
-> getMock ();
$ed -> expects ( $this -> once ())
-> method ( 'hasEventListeners' )
2022-02-21 12:42:28 +00:00
-> with ( $this -> callback ( function ( ScriptEvent $event ) use ( $scriptName , $expectedDevMode ) : bool {
2016-07-21 22:20:56 +00:00
return $event -> getName () === $scriptName
&& $event -> isDevMode () === $expectedDevMode ;
}))
-> willReturn ( true );
$ed -> expects ( $this -> once ())
-> method ( 'dispatchScript' )
2020-02-07 22:21:48 +00:00
-> with ( $scriptName , $expectedDevMode , array ())
-> willReturn ( 0 );
2016-07-21 22:20:56 +00:00
$composer = $this -> createComposerInstance ();
$composer -> setEventDispatcher ( $ed );
$command = $this -> getMockBuilder ( 'Composer\Command\RunScriptCommand' )
2021-12-09 21:14:04 +00:00
-> onlyMethods ( array (
2016-07-21 22:20:56 +00:00
'mergeApplicationDefinition' ,
'getSynopsis' ,
'initialize' ,
2022-02-16 12:24:57 +00:00
'requireComposer' ,
2016-07-21 22:20:56 +00:00
))
-> getMock ();
2022-02-16 12:24:57 +00:00
$command -> expects ( $this -> any ()) -> method ( 'requireComposer' ) -> willReturn ( $composer );
2016-07-21 22:20:56 +00:00
$command -> run ( $input , $output );
}
2022-04-07 11:17:28 +00:00
public function testCanListScripts () : void
{
$manifest = [
'scripts' => [
'test' => '@php test' ,
'fix-cs' => 'php-cs-fixer fix' ,
],
'scripts-descriptions' => [
'fix-cs' => 'Run the codestyle fixer' ,
],
];
file_put_contents ( $this -> home . '/composer.json' , json_encode ( $manifest ));
$application = new Application ();
$application -> setAutoExit ( false );
$application -> add ( new RunScriptCommand ());
$tester = new ApplicationTester ( $application );
$tester -> run ([ 'command' => 'run-script' , '--list' => true , '-d' => $this -> home ]);
$tester -> assertCommandIsSuccessful ();
$output = $tester -> getDisplay ();
$this -> assertStringContainsString ( 'Runs the test script as defined in composer.json.' , $output , 'The default description for the test script should be printed' );
$this -> assertStringContainsString ( 'Run the codestyle fixer' , $output , 'The custom description for the fix-cs script should be printed' );
}
2021-10-26 19:05:03 +00:00
/** @return bool[][] **/
2022-02-18 10:22:01 +00:00
public function getDevOptions () : array
2016-07-21 22:20:56 +00:00
{
return array (
array ( true , true ),
array ( true , false ),
array ( false , true ),
array ( false , false ),
);
}
2021-10-26 19:05:03 +00:00
/** @return Composer **/
2022-02-18 10:22:01 +00:00
private function createComposerInstance () : Composer
2016-07-21 22:20:56 +00:00
{
$composer = new Composer ;
2017-03-08 14:07:29 +00:00
$config = new Config ;
2016-07-21 22:20:56 +00:00
$composer -> setConfig ( $config );
return $composer ;
}
}