ConsoleIOTest.php 8.3 KB

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