ConsoleIOTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. 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 testOverwrite()
  43. {
  44. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  45. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  46. $outputMock->expects($this->at(0))
  47. ->method('write')
  48. ->with($this->equalTo("\x08"), $this->equalTo(false));
  49. $outputMock->expects($this->at(19))
  50. ->method('write')
  51. ->with($this->equalTo("\x08"), $this->equalTo(false));
  52. $outputMock->expects($this->at(20))
  53. ->method('write')
  54. ->with($this->equalTo('some information'), $this->equalTo(false));
  55. $outputMock->expects($this->at(21))
  56. ->method('write')
  57. ->with($this->equalTo(' '), $this->equalTo(false));
  58. $outputMock->expects($this->at(24))
  59. ->method('write')
  60. ->with($this->equalTo(' '), $this->equalTo(false));
  61. $outputMock->expects($this->at(25))
  62. ->method('write')
  63. ->with($this->equalTo("\x08"), $this->equalTo(false));
  64. $outputMock->expects($this->at(28))
  65. ->method('write')
  66. ->with($this->equalTo("\x08"), $this->equalTo(false));
  67. $outputMock->expects($this->at(29))
  68. ->method('write')
  69. ->with($this->equalTo(''));
  70. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  71. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  72. $consoleIO->overwrite('some information', true, 20);
  73. }
  74. public function testAsk()
  75. {
  76. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  77. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  78. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  79. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  80. $dialogMock->expects($this->once())
  81. ->method('ask')
  82. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  83. $this->equalTo('Why?'),
  84. $this->equalTo('default'));
  85. $helperMock->expects($this->once())
  86. ->method('get')
  87. ->with($this->equalTo('dialog'))
  88. ->will($this->returnValue($dialogMock));
  89. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  90. $consoleIO->ask('Why?', 'default');
  91. }
  92. public function testAskConfirmation()
  93. {
  94. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  95. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  96. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  97. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  98. $dialogMock->expects($this->once())
  99. ->method('askConfirmation')
  100. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  101. $this->equalTo('Why?'),
  102. $this->equalTo('default'));
  103. $helperMock->expects($this->once())
  104. ->method('get')
  105. ->with($this->equalTo('dialog'))
  106. ->will($this->returnValue($dialogMock));
  107. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  108. $consoleIO->askConfirmation('Why?', 'default');
  109. }
  110. public function testAskAndValidate()
  111. {
  112. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  113. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  114. $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
  115. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  116. $dialogMock->expects($this->once())
  117. ->method('askAndValidate')
  118. ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
  119. $this->equalTo('Why?'),
  120. $this->equalTo('validator'),
  121. $this->equalTo(10),
  122. $this->equalTo('default'));
  123. $helperMock->expects($this->once())
  124. ->method('get')
  125. ->with($this->equalTo('dialog'))
  126. ->will($this->returnValue($dialogMock));
  127. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  128. $consoleIO->askAndValidate('Why?', 'validator', 10, 'default');
  129. }
  130. public function testSetAndGetAuthorization()
  131. {
  132. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  133. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  134. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  135. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  136. $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd');
  137. $this->assertEquals(
  138. array('username' => 'l3l0', 'password' => 'passwd'),
  139. $consoleIO->getAuthorization('repoName')
  140. );
  141. }
  142. public function testGetAuthorizationWhenDidNotSet()
  143. {
  144. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  145. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  146. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  147. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  148. $this->assertEquals(
  149. array('username' => null, 'password' => null),
  150. $consoleIO->getAuthorization('repoName')
  151. );
  152. }
  153. public function testHasAuthorization()
  154. {
  155. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  156. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  157. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  158. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  159. $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd');
  160. $this->assertTrue($consoleIO->hasAuthorization('repoName'));
  161. $this->assertFalse($consoleIO->hasAuthorization('repoName2'));
  162. }
  163. public function testGetLastUsername()
  164. {
  165. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  166. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  167. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  168. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  169. $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd');
  170. $consoleIO->setAuthorization('repoName2', 'l3l02', 'passwd2');
  171. $this->assertEquals('l3l02', $consoleIO->getLastUsername());
  172. }
  173. public function testGetLastPassword()
  174. {
  175. $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  176. $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  177. $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
  178. $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
  179. $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd');
  180. $consoleIO->setAuthorization('repoName2', 'l3l02', 'passwd2');
  181. $this->assertEquals('passwd2', $consoleIO->getLastPassword());
  182. }
  183. }