PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/composer/composer/tests/Composer/Test/IO/ConsoleIOTest.php

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