2012-01-22 21:06:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2013-09-25 08:14:42 +00:00
|
|
|
use Composer\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
|
|
|
|
{
|
|
|
|
public function testIsInteractive()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$inputMock->expects($this->at(0))
|
|
|
|
->method('isInteractive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$inputMock->expects($this->at(1))
|
|
|
|
->method('isInteractive')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
|
|
|
|
$this->assertTrue($consoleIO->isInteractive());
|
|
|
|
$this->assertFalse($consoleIO->isInteractive());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrite()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
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));
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
$consoleIO->write('some information about something', false);
|
|
|
|
}
|
|
|
|
|
2015-02-06 12:52:44 +00:00
|
|
|
public function testWriteError()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\ConsoleOutputInterface');
|
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));
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
$consoleIO->writeError('some information about something', false);
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:49:38 +00:00
|
|
|
public function testWriteWithMultipleLineStringWhenDebugging()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
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(
|
2015-02-24 14:22:54 +00:00
|
|
|
$this->callback(function ($messages) {
|
2015-01-28 10:46:38 +00:00
|
|
|
$result = preg_match("[(.*)/(.*) First line]", $messages[0]) > 0;
|
|
|
|
$result &= preg_match("[(.*)/(.*) Second line]", $messages[1]) > 0;
|
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)
|
|
|
|
);
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
$startTime = microtime(true);
|
|
|
|
$consoleIO->enableDebugging($startTime);
|
|
|
|
|
|
|
|
$example = explode('\n', 'First line\nSecond lines');
|
|
|
|
$consoleIO->write($example, false);
|
|
|
|
}
|
|
|
|
|
2012-01-22 21:06:09 +00:00
|
|
|
public function testOverwrite()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
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);
|
|
|
|
$outputMock->expects($this->at(1))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo('something (<question>strlen = 23</question>)'));
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->at(3))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo(str_repeat("\x08", 23)), $this->equalTo(false));
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->at(5))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo('shorter (<comment>12</comment>)'), $this->equalTo(false));
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->at(7))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo(str_repeat(' ', 11)), $this->equalTo(false));
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->at(9))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo(str_repeat("\x08", 11)), $this->equalTo(false));
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->at(11))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo(str_repeat("\x08", 12)), $this->equalTo(false));
|
2016-01-28 13:41:19 +00:00
|
|
|
$outputMock->expects($this->at(13))
|
2012-01-22 21:06:09 +00:00
|
|
|
->method('write')
|
2012-03-06 18:08:15 +00:00
|
|
|
->with($this->equalTo('something longer than initial (<info>34</info>)'));
|
2012-01-22 21:06:09 +00:00
|
|
|
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testAsk()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
2015-04-30 10:16:18 +00:00
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
|
|
|
|
$setMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAskConfirmation()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
2015-04-30 10:16:18 +00:00
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
|
|
|
|
$setMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
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);
|
2012-01-22 21:06:09 +00:00
|
|
|
$consoleIO->askConfirmation('Why?', 'default');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAskAndValidate()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
2015-04-30 10:16:18 +00:00
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
|
|
|
|
$setMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
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
|
|
|
|
2017-05-22 06:16:45 +00:00
|
|
|
$validator = function ($value) {
|
|
|
|
return true;
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
2014-08-12 09:12:07 +00:00
|
|
|
public function testSelect()
|
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
2017-11-06 15:29:55 +00:00
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
|
|
|
|
$setMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$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())
|
|
|
|
->method('get')
|
|
|
|
->with($this->equalTo('question'))
|
|
|
|
->will($this->returnValue($helperMock))
|
|
|
|
;
|
2014-08-12 09:12:07 +00:00
|
|
|
|
2016-02-29 15:50:04 +00:00
|
|
|
$inputMock->expects($this->once())
|
|
|
|
->method('isInteractive')
|
2017-11-06 15:29:55 +00:00
|
|
|
->will($this->returnValue(true))
|
|
|
|
;
|
2014-08-12 09:12:07 +00:00
|
|
|
|
2017-11-06 15:29:55 +00:00
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
|
2014-08-12 09:12:07 +00:00
|
|
|
$consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true);
|
|
|
|
}
|
|
|
|
|
2012-11-07 12:33:50 +00:00
|
|
|
public function testSetAndgetAuthentication()
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('username' => 'l3l0', 'password' => 'passwd'),
|
2012-11-07 12:33:50 +00:00
|
|
|
$consoleIO->getAuthentication('repoName')
|
2012-01-22 21:06:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:06:53 +00:00
|
|
|
public function testGetAuthenticationWhenDidNotSet()
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('username' => null, 'password' => null),
|
2012-11-07 12:33:50 +00:00
|
|
|
$consoleIO->getAuthentication('repoName')
|
2012-01-22 21:06:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:06:53 +00:00
|
|
|
public function testHasAuthentication()
|
2012-01-22 21:06:09 +00:00
|
|
|
{
|
|
|
|
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
|
|
|
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
|
|
|
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
|
|
|
|
|
|
|
|
$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
|
|
|
|
2012-11-07 12:33:50 +00:00
|
|
|
$this->assertTrue($consoleIO->hasAuthentication('repoName'));
|
|
|
|
$this->assertFalse($consoleIO->hasAuthentication('repoName2'));
|
2012-01-22 21:06:09 +00:00
|
|
|
}
|
|
|
|
}
|