2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-01-22 21:06:09 +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\IO;
|
|
|
|
|
|
|
|
use Composer\IO\ConsoleIO;
|
2021-12-07 10:03:51 +00:00
|
|
|
use Composer\Pcre\Preg;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2016-01-28 13:41:19 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
class ConsoleIOTest extends TestCase
|
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsInteractive(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
2021-12-09 16:09:07 +00:00
|
|
|
$inputMock->expects($this->exactly(2))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('isInteractive')
|
2021-12-09 16:09:07 +00:00
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
);
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($consoleIO->isInteractive());
|
|
|
|
self::assertFalse($consoleIO->isInteractive());
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testWrite(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('getVerbosity')
|
|
|
|
->willReturn(OutputInterface::VERBOSITY_NORMAL);
|
2012-01-22 21:06:09 +00:00
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('write')
|
|
|
|
->with($this->equalTo('some information about something'), $this->equalTo(false));
|
2018-04-12 08:24:56 +00:00
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
$consoleIO->write('some information about something', false);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testWriteError(): void
|
2015-02-06 12:52:44 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\ConsoleOutputInterface')->getMock();
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('getVerbosity')
|
|
|
|
->willReturn(OutputInterface::VERBOSITY_NORMAL);
|
2015-02-06 12:52:44 +00:00
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('getErrorOutput')
|
|
|
|
->willReturn($outputMock);
|
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('write')
|
|
|
|
->with($this->equalTo('some information about something'), $this->equalTo(false));
|
2018-04-12 08:24:56 +00:00
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2015-02-06 12:52:44 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
$consoleIO->writeError('some information about something', false);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testWriteWithMultipleLineStringWhenDebugging(): void
|
2015-01-28 07:49:38 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('getVerbosity')
|
|
|
|
->willReturn(OutputInterface::VERBOSITY_NORMAL);
|
2015-01-28 07:49:38 +00:00
|
|
|
$outputMock->expects($this->once())
|
|
|
|
->method('write')
|
|
|
|
->with(
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->callback(static function ($messages): bool {
|
2021-12-07 10:03:51 +00:00
|
|
|
$result = Preg::isMatch("[(.*)/(.*) First line]", $messages[0]);
|
|
|
|
$result = $result && Preg::isMatch("[(.*)/(.*) Second line]", $messages[1]);
|
2015-02-24 14:22:54 +00:00
|
|
|
|
2015-01-28 10:46:38 +00:00
|
|
|
return $result;
|
2015-01-28 07:49:38 +00:00
|
|
|
}),
|
|
|
|
$this->equalTo(false)
|
|
|
|
);
|
2018-04-12 08:24:56 +00:00
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2015-01-28 07:49:38 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
$startTime = microtime(true);
|
|
|
|
$consoleIO->enableDebugging($startTime);
|
|
|
|
|
|
|
|
$example = explode('\n', 'First line\nSecond lines');
|
|
|
|
$consoleIO->write($example, false);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOverwrite(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
2012-03-06 18:08:15 +00:00
|
|
|
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->any())
|
|
|
|
->method('getVerbosity')
|
|
|
|
->willReturn(OutputInterface::VERBOSITY_NORMAL);
|
2021-12-09 16:09:07 +00:00
|
|
|
$outputMock->expects($this->atLeast(7))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2023-06-07 12:35:16 +00:00
|
|
|
->willReturnCallback(function (...$args) {
|
|
|
|
static $series = null;
|
|
|
|
|
|
|
|
if ($series === null) {
|
|
|
|
$series = [
|
|
|
|
['something (<question>strlen = 23</question>)', true],
|
|
|
|
[str_repeat("\x08", 23), false],
|
|
|
|
['shorter (<comment>12</comment>)', false],
|
|
|
|
[str_repeat(' ', 11), false],
|
|
|
|
[str_repeat("\x08", 11), false],
|
|
|
|
[str_repeat("\x08", 12), false],
|
|
|
|
['something longer than initial (<info>34</info>)', false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($series) > 0) {
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertSame(array_shift($series), [$args[0], $args[1]]);
|
2023-06-07 12:35:16 +00:00
|
|
|
}
|
|
|
|
});
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
2012-03-06 18:08:15 +00:00
|
|
|
$consoleIO->write('something (<question>strlen = 23</question>)');
|
|
|
|
$consoleIO->overwrite('shorter (<comment>12</comment>)', false);
|
|
|
|
$consoleIO->overwrite('something longer than initial (<info>34</info>)');
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAsk(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
|
|
|
|
$setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2015-04-30 10:16:18 +00:00
|
|
|
$helperMock
|
|
|
|
->expects($this->once())
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('ask')
|
2015-04-30 10:16:18 +00:00
|
|
|
->with(
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Question\Question')
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
$setMock
|
|
|
|
->expects($this->once())
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('get')
|
2015-04-30 10:16:18 +00:00
|
|
|
->with($this->equalTo('question'))
|
|
|
|
->will($this->returnValue($helperMock))
|
|
|
|
;
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2015-04-30 10:16:18 +00:00
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
|
2012-01-22 21:06:09 +00:00
|
|
|
$consoleIO->ask('Why?', 'default');
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAskConfirmation(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
|
|
|
|
$setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2015-04-30 10:16:18 +00:00
|
|
|
$helperMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->with(
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
|
2017-04-10 20:21:53 +00:00
|
|
|
$this->isInstanceOf('Composer\Question\StrictConfirmationQuestion')
|
2015-04-30 10:16:18 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
$setMock
|
|
|
|
->expects($this->once())
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('get')
|
2015-04-30 10:16:18 +00:00
|
|
|
->with($this->equalTo('question'))
|
|
|
|
->will($this->returnValue($helperMock))
|
|
|
|
;
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2015-04-30 10:16:18 +00:00
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
|
2021-08-21 15:41:52 +00:00
|
|
|
$consoleIO->askConfirmation('Why?', false);
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAskAndValidate(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
|
|
|
|
$setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2015-04-30 10:16:18 +00:00
|
|
|
$helperMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->with(
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Question\Question')
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
$setMock
|
|
|
|
->expects($this->once())
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('get')
|
2015-04-30 10:16:18 +00:00
|
|
|
->with($this->equalTo('question'))
|
|
|
|
->will($this->returnValue($helperMock))
|
|
|
|
;
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$validator = static function ($value): bool {
|
2017-12-18 15:02:48 +00:00
|
|
|
return true;
|
2017-05-22 06:16:45 +00:00
|
|
|
};
|
2015-04-30 10:16:18 +00:00
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
|
2017-05-22 06:16:45 +00:00
|
|
|
$consoleIO->askAndValidate('Why?', $validator, 10, 'default');
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSelect(): void
|
2014-08-12 09:12:07 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
|
|
|
|
$setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2017-11-06 15:29:55 +00:00
|
|
|
|
|
|
|
$helperMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->with(
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
|
|
|
|
$this->isInstanceOf('Symfony\Component\Console\Question\Question')
|
|
|
|
)
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(['item2']));
|
2017-11-06 15:29:55 +00:00
|
|
|
|
|
|
|
$setMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($this->equalTo('question'))
|
|
|
|
->will($this->returnValue($helperMock))
|
|
|
|
;
|
2014-08-12 09:12:07 +00:00
|
|
|
|
2017-11-06 15:29:55 +00:00
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
|
2022-08-17 12:20:07 +00:00
|
|
|
$result = $consoleIO->select('Select item', ["item1", "item2"], 'item1', false, "Error message", true);
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['1'], $result);
|
2014-08-12 09:12:07 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 22:30:29 +00:00
|
|
|
public function testSetAndGetAuthentication(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
2012-11-07 12:33:50 +00:00
|
|
|
$consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(
|
2022-08-17 12:20:07 +00:00
|
|
|
['username' => 'l3l0', 'password' => 'passwd'],
|
2012-11-07 12:33:50 +00:00
|
|
|
$consoleIO->getAuthentication('repoName')
|
2012-01-22 21:06:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetAuthenticationWhenDidNotSet(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(
|
2022-08-17 12:20:07 +00:00
|
|
|
['username' => null, 'password' => null],
|
2012-11-07 12:33:50 +00:00
|
|
|
$consoleIO->getAuthentication('repoName')
|
2012-01-22 21:06:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHasAuthentication(): void
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
|
|
|
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
|
|
|
|
$helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
2012-11-07 12:33:50 +00:00
|
|
|
$consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
|
2012-01-22 21:06:09 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($consoleIO->hasAuthentication('repoName'));
|
|
|
|
self::assertFalse($consoleIO->hasAuthentication('repoName2'));
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
}
|