ConsoleIOTest.php 8.0 KB

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