ConsoleIOTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\IO;
  12. use Composer\IO\ConsoleIO;
  13. use Composer\Test\TestCase;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class ConsoleIOTest extends TestCase
  16. {
  17. public function testIsInteractive()
  18. {
  19. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  20. $inputMock->expects($this->at(0))
  21. ->method('isInteractive')
  22. ->will($this->returnValue(true));
  23. $inputMock->expects($this->at(1))
  24. ->method('isInteractive')
  25. ->will($this->returnValue(false));
  26. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  27. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  28. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  29. $this->assertTrue($consoleIO->isInteractive());
  30. $this->assertFalse($consoleIO->isInteractive());
  31. }
  32. public function testWrite()
  33. {
  34. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  35. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  36. $outputMock->expects($this->once())
  37. ->method('getVerbosity')
  38. ->willReturn(OutputInterface::VERBOSITY_NORMAL);
  39. $outputMock->expects($this->once())
  40. ->method('write')
  41. ->with($this->equalTo('some information about something'), $this->equalTo(false));
  42. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  43. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  44. $consoleIO->write('some information about something', false);
  45. }
  46. public function testWriteError()
  47. {
  48. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  49. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\ConsoleOutputInterface')->getMock();
  50. $outputMock->expects($this->once())
  51. ->method('getVerbosity')
  52. ->willReturn(OutputInterface::VERBOSITY_NORMAL);
  53. $outputMock->expects($this->once())
  54. ->method('getErrorOutput')
  55. ->willReturn($outputMock);
  56. $outputMock->expects($this->once())
  57. ->method('write')
  58. ->with($this->equalTo('some information about something'), $this->equalTo(false));
  59. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  60. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  61. $consoleIO->writeError('some information about something', false);
  62. }
  63. public function testWriteWithMultipleLineStringWhenDebugging()
  64. {
  65. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  66. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  67. $outputMock->expects($this->once())
  68. ->method('getVerbosity')
  69. ->willReturn(OutputInterface::VERBOSITY_NORMAL);
  70. $outputMock->expects($this->once())
  71. ->method('write')
  72. ->with(
  73. $this->callback(function ($messages) {
  74. $result = preg_match("[(.*)/(.*) First line]", $messages[0]) > 0;
  75. $result = $result && preg_match("[(.*)/(.*) Second line]", $messages[1]) > 0;
  76. return $result;
  77. }),
  78. $this->equalTo(false)
  79. );
  80. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  81. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  82. $startTime = microtime(true);
  83. $consoleIO->enableDebugging($startTime);
  84. $example = explode('\n', 'First line\nSecond lines');
  85. $consoleIO->write($example, false);
  86. }
  87. public function testOverwrite()
  88. {
  89. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  90. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  91. $outputMock->expects($this->any())
  92. ->method('getVerbosity')
  93. ->willReturn(OutputInterface::VERBOSITY_NORMAL);
  94. $outputMock->expects($this->at(1))
  95. ->method('write')
  96. ->with($this->equalTo('something (<question>strlen = 23</question>)'));
  97. $outputMock->expects($this->at(3))
  98. ->method('write')
  99. ->with($this->equalTo(str_repeat("\x08", 23)), $this->equalTo(false));
  100. $outputMock->expects($this->at(5))
  101. ->method('write')
  102. ->with($this->equalTo('shorter (<comment>12</comment>)'), $this->equalTo(false));
  103. $outputMock->expects($this->at(7))
  104. ->method('write')
  105. ->with($this->equalTo(str_repeat(' ', 11)), $this->equalTo(false));
  106. $outputMock->expects($this->at(9))
  107. ->method('write')
  108. ->with($this->equalTo(str_repeat("\x08", 11)), $this->equalTo(false));
  109. $outputMock->expects($this->at(11))
  110. ->method('write')
  111. ->with($this->equalTo(str_repeat("\x08", 12)), $this->equalTo(false));
  112. $outputMock->expects($this->at(13))
  113. ->method('write')
  114. ->with($this->equalTo('something longer than initial (<info>34</info>)'));
  115. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  116. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  117. $consoleIO->write('something (<question>strlen = 23</question>)');
  118. $consoleIO->overwrite('shorter (<comment>12</comment>)', false);
  119. $consoleIO->overwrite('something longer than initial (<info>34</info>)');
  120. }
  121. public function testAsk()
  122. {
  123. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  124. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  125. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
  126. $setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  127. $helperMock
  128. ->expects($this->once())
  129. ->method('ask')
  130. ->with(
  131. $this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
  132. $this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  133. $this->isInstanceOf('Symfony\Component\Console\Question\Question')
  134. )
  135. ;
  136. $setMock
  137. ->expects($this->once())
  138. ->method('get')
  139. ->with($this->equalTo('question'))
  140. ->will($this->returnValue($helperMock))
  141. ;
  142. $consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
  143. $consoleIO->ask('Why?', 'default');
  144. }
  145. public function testAskConfirmation()
  146. {
  147. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  148. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  149. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
  150. $setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  151. $helperMock
  152. ->expects($this->once())
  153. ->method('ask')
  154. ->with(
  155. $this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
  156. $this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  157. $this->isInstanceOf('Composer\Question\StrictConfirmationQuestion')
  158. )
  159. ;
  160. $setMock
  161. ->expects($this->once())
  162. ->method('get')
  163. ->with($this->equalTo('question'))
  164. ->will($this->returnValue($helperMock))
  165. ;
  166. $consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
  167. $consoleIO->askConfirmation('Why?', 'default');
  168. }
  169. public function testAskAndValidate()
  170. {
  171. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  172. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  173. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
  174. $setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  175. $helperMock
  176. ->expects($this->once())
  177. ->method('ask')
  178. ->with(
  179. $this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
  180. $this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  181. $this->isInstanceOf('Symfony\Component\Console\Question\Question')
  182. )
  183. ;
  184. $setMock
  185. ->expects($this->once())
  186. ->method('get')
  187. ->with($this->equalTo('question'))
  188. ->will($this->returnValue($helperMock))
  189. ;
  190. $validator = function ($value) {
  191. return true;
  192. };
  193. $consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
  194. $consoleIO->askAndValidate('Why?', $validator, 10, 'default');
  195. }
  196. public function testSelect()
  197. {
  198. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  199. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  200. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock();
  201. $setMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  202. $helperMock
  203. ->expects($this->once())
  204. ->method('ask')
  205. ->with(
  206. $this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
  207. $this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  208. $this->isInstanceOf('Symfony\Component\Console\Question\Question')
  209. )
  210. ->will($this->returnValue(array('item2')));
  211. $setMock
  212. ->expects($this->once())
  213. ->method('get')
  214. ->with($this->equalTo('question'))
  215. ->will($this->returnValue($helperMock))
  216. ;
  217. $consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
  218. $result = $consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true);
  219. $this->assertEquals(array('1'), $result);
  220. }
  221. public function testSetAndgetAuthentication()
  222. {
  223. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  224. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  225. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  226. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  227. $consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
  228. $this->assertEquals(
  229. array('username' => 'l3l0', 'password' => 'passwd'),
  230. $consoleIO->getAuthentication('repoName')
  231. );
  232. }
  233. public function testGetAuthenticationWhenDidNotSet()
  234. {
  235. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  236. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  237. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  238. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  239. $this->assertEquals(
  240. array('username' => null, 'password' => null),
  241. $consoleIO->getAuthentication('repoName')
  242. );
  243. }
  244. public function testHasAuthentication()
  245. {
  246. $inputMock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  247. $outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
  248. $helperMock = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock();
  249. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  250. $consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
  251. $this->assertTrue($consoleIO->hasAuthentication('repoName'));
  252. $this->assertFalse($consoleIO->hasAuthentication('repoName2'));
  253. }
  254. }