ConsoleIOTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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('isDecorated')
  86. ->willReturn(true);
  87. $outputMock->expects($this->at(2))
  88. ->method('write')
  89. ->with($this->equalTo(str_repeat("\x08", 23)), $this->equalTo(false));
  90. $outputMock->expects($this->at(3))
  91. ->method('write')
  92. ->with($this->equalTo('shorter (<comment>12</comment>)'), $this->equalTo(false));
  93. $outputMock->expects($this->at(4))
  94. ->method('write')
  95. ->with($this->equalTo(str_repeat(' ', 11)), $this->equalTo(false));
  96. $outputMock->expects($this->at(5))
  97. ->method('write')
  98. ->with($this->equalTo(str_repeat("\x08", 11)), $this->equalTo(false));
  99. $outputMock->expects($this->at(6))
  100. ->method('isDecorated')
  101. ->willReturn(true);
  102. $outputMock->expects($this->at(7))
  103. ->method('write')
  104. ->with($this->equalTo(str_repeat("\x08", 12)), $this->equalTo(false));
  105. $outputMock->expects($this->at(8))
  106. ->method('write')
  107. ->with($this->equalTo('something longer than initial (<info>34</info>)'));
  108. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  109. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  110. $consoleIO->write('something (<question>strlen = 23</question>)');
  111. $consoleIO->overwrite('shorter (<comment>12</comment>)', false);
  112. $consoleIO->overwrite('something longer than initial (<info>34</info>)');
  113. }
  114. public function testAsk()
  115. {
  116. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  117. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  118. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  119. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  120. $dialogMock->expects($this->once())
  121. ->method('ask')
  122. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  123. $this->equalTo('Why?'),
  124. $this->equalTo('default'));
  125. $helperMock->expects($this->once())
  126. ->method('get')
  127. ->with($this->equalTo('dialog'))
  128. ->will($this->returnValue($dialogMock));
  129. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  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. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  137. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  138. $dialogMock->expects($this->once())
  139. ->method('askConfirmation')
  140. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  141. $this->equalTo('Why?'),
  142. $this->equalTo('default'));
  143. $helperMock->expects($this->once())
  144. ->method('get')
  145. ->with($this->equalTo('dialog'))
  146. ->will($this->returnValue($dialogMock));
  147. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  148. $consoleIO->askConfirmation('Why?', 'default');
  149. }
  150. public function testAskAndValidate()
  151. {
  152. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  153. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  154. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  155. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  156. $dialogMock->expects($this->once())
  157. ->method('askAndValidate')
  158. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  159. $this->equalTo('Why?'),
  160. $this->equalTo('validator'),
  161. $this->equalTo(10),
  162. $this->equalTo('default'));
  163. $helperMock->expects($this->once())
  164. ->method('get')
  165. ->with($this->equalTo('dialog'))
  166. ->will($this->returnValue($dialogMock));
  167. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  168. $consoleIO->askAndValidate('Why?', 'validator', 10, 'default');
  169. }
  170. public function testSetAndgetAuthentication()
  171. {
  172. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  173. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  174. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  175. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  176. $consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
  177. $this->assertEquals(
  178. array('username' => 'l3l0', 'password' => 'passwd'),
  179. $consoleIO->getAuthentication('repoName')
  180. );
  181. }
  182. public function testgetAuthenticationWhenDidNotSet()
  183. {
  184. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  185. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  186. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  187. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  188. $this->assertEquals(
  189. array('username' => null, 'password' => null),
  190. $consoleIO->getAuthentication('repoName')
  191. );
  192. }
  193. public function testhasAuthentication()
  194. {
  195. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  196. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  197. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  198. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  199. $consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
  200. $this->assertTrue($consoleIO->hasAuthentication('repoName'));
  201. $this->assertFalse($consoleIO->hasAuthentication('repoName2'));
  202. }
  203. }