ConsoleIOTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 testWriteWithMultipleLineStringWhenDebugging()
  43. {
  44. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  45. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  46. $outputMock->expects($this->once())
  47. ->method('write')
  48. ->with(
  49. $this->callback(function($messages){
  50. $result = preg_match("[(.*)/(.*) First line]", $messages[0]) > 0;
  51. $result &= preg_match("[(.*)/(.*) Second line]", $messages[1]) > 0;
  52. return $result;
  53. }),
  54. $this->equalTo(false)
  55. );
  56. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  57. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  58. $startTime = microtime(true);
  59. $consoleIO->enableDebugging($startTime);
  60. $example = explode('\n', 'First line\nSecond lines');
  61. $consoleIO->write($example, false);
  62. }
  63. public function testOverwrite()
  64. {
  65. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  66. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  67. $outputMock->expects($this->at(0))
  68. ->method('write')
  69. ->with($this->equalTo('something (<question>strlen = 23</question>)'));
  70. $outputMock->expects($this->at(1))
  71. ->method('isDecorated')
  72. ->willReturn(true);
  73. $outputMock->expects($this->at(2))
  74. ->method('write')
  75. ->with($this->equalTo(str_repeat("\x08", 23)), $this->equalTo(false));
  76. $outputMock->expects($this->at(3))
  77. ->method('write')
  78. ->with($this->equalTo('shorter (<comment>12</comment>)'), $this->equalTo(false));
  79. $outputMock->expects($this->at(4))
  80. ->method('write')
  81. ->with($this->equalTo(str_repeat(' ', 11)), $this->equalTo(false));
  82. $outputMock->expects($this->at(5))
  83. ->method('write')
  84. ->with($this->equalTo(str_repeat("\x08", 11)), $this->equalTo(false));
  85. $outputMock->expects($this->at(6))
  86. ->method('isDecorated')
  87. ->willReturn(true);
  88. $outputMock->expects($this->at(7))
  89. ->method('write')
  90. ->with($this->equalTo(str_repeat("\x08", 12)), $this->equalTo(false));
  91. $outputMock->expects($this->at(8))
  92. ->method('write')
  93. ->with($this->equalTo('something longer than initial (<info>34</info>)'));
  94. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  95. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  96. $consoleIO->write('something (<question>strlen = 23</question>)');
  97. $consoleIO->overwrite('shorter (<comment>12</comment>)', false);
  98. $consoleIO->overwrite('something longer than initial (<info>34</info>)');
  99. }
  100. public function testAsk()
  101. {
  102. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  103. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  104. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  105. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  106. $dialogMock->expects($this->once())
  107. ->method('ask')
  108. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  109. $this->equalTo('Why?'),
  110. $this->equalTo('default'));
  111. $helperMock->expects($this->once())
  112. ->method('get')
  113. ->with($this->equalTo('dialog'))
  114. ->will($this->returnValue($dialogMock));
  115. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  116. $consoleIO->ask('Why?', 'default');
  117. }
  118. public function testAskConfirmation()
  119. {
  120. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  121. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  122. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  123. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  124. $dialogMock->expects($this->once())
  125. ->method('askConfirmation')
  126. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  127. $this->equalTo('Why?'),
  128. $this->equalTo('default'));
  129. $helperMock->expects($this->once())
  130. ->method('get')
  131. ->with($this->equalTo('dialog'))
  132. ->will($this->returnValue($dialogMock));
  133. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  134. $consoleIO->askConfirmation('Why?', 'default');
  135. }
  136. public function testAskAndValidate()
  137. {
  138. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  139. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  140. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  141. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  142. $dialogMock->expects($this->once())
  143. ->method('askAndValidate')
  144. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  145. $this->equalTo('Why?'),
  146. $this->equalTo('validator'),
  147. $this->equalTo(10),
  148. $this->equalTo('default'));
  149. $helperMock->expects($this->once())
  150. ->method('get')
  151. ->with($this->equalTo('dialog'))
  152. ->will($this->returnValue($dialogMock));
  153. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  154. $consoleIO->askAndValidate('Why?', 'validator', 10, 'default');
  155. }
  156. public function testSetAndgetAuthentication()
  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\HelperSet');
  161. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  162. $consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
  163. $this->assertEquals(
  164. array('username' => 'l3l0', 'password' => 'passwd'),
  165. $consoleIO->getAuthentication('repoName')
  166. );
  167. }
  168. public function testgetAuthenticationWhenDidNotSet()
  169. {
  170. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  171. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  172. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  173. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  174. $this->assertEquals(
  175. array('username' => null, 'password' => null),
  176. $consoleIO->getAuthentication('repoName')
  177. );
  178. }
  179. public function testhasAuthentication()
  180. {
  181. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  182. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  183. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  184. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  185. $consoleIO->setAuthentication('repoName', 'l3l0', 'passwd');
  186. $this->assertTrue($consoleIO->hasAuthentication('repoName'));
  187. $this->assertFalse($consoleIO->hasAuthentication('repoName2'));
  188. }
  189. }