1
0
Fork 0
composer/tests/Composer/Test/Command/RunScriptCommandTest.php

231 lines
7.0 KiB
PHP
Raw Normal View History

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;
use Composer\Composer;
use Composer\Config;
use Composer\Script\Event as ScriptEvent;
use Composer\Test\TestCase;
2016-07-21 22:20:56 +00:00
class RunScriptCommandTest extends TestCase
{
/**
* @dataProvider getDevOptions
*/
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';
$input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
2016-07-21 22:20:56 +00:00
$input
->method('getOption')
2022-08-17 12:20:07 +00:00
->will($this->returnValueMap([
['list', false],
['dev', $dev],
['no-dev', $noDev],
]));
2016-07-21 22:20:56 +00:00
$input
->method('getArgument')
2022-08-17 12:20:07 +00:00
->will($this->returnValueMap([
['script', $scriptName],
['args', []],
]));
2016-07-21 22:20:56 +00:00
$input
->method('hasArgument')
->with('command')
->willReturn(false);
$input
->method('isInteractive')
->willReturn(false);
2016-07-21 22:20: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-08-17 12:20:07 +00:00
->with($this->callback(static 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')
2022-08-17 12:20:07 +00:00
->with($scriptName, $expectedDevMode, [])
2020-02-07 22:21:48 +00:00
->willReturn(0);
2016-07-21 22:20:56 +00:00
$composer = $this->createComposerInstance();
$composer->setEventDispatcher($ed);
$command = $this->getMockBuilder('Composer\Command\RunScriptCommand')
2022-08-17 12:20:07 +00:00
->onlyMethods([
2016-07-21 22:20:56 +00:00
'mergeApplicationDefinition',
'getSynopsis',
'initialize',
'requireComposer',
2022-08-17 12:20:07 +00:00
])
2016-07-21 22:20:56 +00:00
->getMock();
$command->expects($this->any())->method('requireComposer')->willReturn($composer);
2016-07-21 22:20:56 +00:00
$command->run($input, $output);
}
public function testCanListScripts(): void
{
$this->initTempComposer([
'scripts' => [
'test' => '@php test',
'fix-cs' => 'php-cs-fixer fix',
],
'scripts-descriptions' => [
'fix-cs' => 'Run the codestyle fixer',
],
]);
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'run-script', '--list' => true]);
$appTester->assertCommandIsSuccessful();
$output = $appTester->getDisplay();
self::assertStringContainsString('Runs the test script as defined in composer.json', $output, 'The default description for the test script should be printed');
self::assertStringContainsString('Run the codestyle fixer', $output, 'The custom description for the fix-cs script should be printed');
}
public function testCanDefineAliases(): void
{
$expectedAliases = ['one', 'two', 'three'];
$this->initTempComposer([
'scripts' => [
'test' => '@php test',
],
'scripts-aliases' => [
'test' => $expectedAliases,
],
]);
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'test', '--help' => true, '--format' => 'json']);
$appTester->assertCommandIsSuccessful();
$output = $appTester->getDisplay();
$array = json_decode($output, true);
$actualAliases = $array['usage'];
array_shift($actualAliases);
self::assertSame($expectedAliases, $actualAliases, 'The custom aliases for the test command should be printed');
}
public function testExecutionOfCustomSymfonyCommand(): void
{
$this->initTempComposer([
'scripts' => [
'test-direct' => 'Test\\MyCommand',
'test-ref' => ['@test-direct --inneropt innerarg'],
],
'autoload' => [
'psr-4' => [
'Test\\' => '',
],
],
]);
file_put_contents('MyCommand.php', <<<'TEST'
<?php
namespace Test;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
class MyCommand extends Command
{
protected function configure(): void
{
$this->setDefinition([
new InputArgument('req-arg', InputArgument::REQUIRED, 'Required arg.'),
new InputArgument('opt-arg', InputArgument::OPTIONAL, 'Optional arg.'),
new InputOption('inneropt', null, InputOption::VALUE_NONE, 'Option.'),
new InputOption('outeropt', null, InputOption::VALUE_OPTIONAL, 'Optional option.'),
]);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln($input->getArgument('req-arg'));
$output->writeln((string) $input->getArgument('opt-arg'));
$output->writeln('inneropt: '.($input->getOption('inneropt') ? 'set' : 'unset'));
$output->writeln('outeropt: '.($input->getOption('outeropt') ? 'set' : 'unset'));
return 2;
}
}
TEST
);
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'test-direct', '--outeropt' => true, 'req-arg' => 'lala']);
self::assertSame('lala
inneropt: unset
outeropt: set
', $appTester->getDisplay(true));
self::assertSame(2, $appTester->getStatusCode());
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'test-ref', '--outeropt' => true, 'req-arg' => 'lala']);
self::assertSame('innerarg
lala
inneropt: set
outeropt: set
', $appTester->getDisplay(true));
self::assertSame(2, $appTester->getStatusCode());
}
2021-10-26 19:05:03 +00:00
/** @return bool[][] **/
public static function getDevOptions(): array
2016-07-21 22:20:56 +00:00
{
2022-08-17 12:20:07 +00:00
return [
[true, true],
[true, false],
[false, true],
[false, false],
];
2016-07-21 22:20:56 +00:00
}
2021-10-26 19:05:03 +00:00
/** @return Composer **/
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;
}
}