ConsoleIOTest.php 10 KB

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