OutputTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace League\CLImate\Tests\Util;
  3. use League\CLImate\Exceptions\InvalidArgumentException;
  4. use League\CLImate\Tests\TestBase;
  5. use League\CLImate\Util\Output;
  6. use League\CLImate\Util\Writer\Buffer;
  7. use League\CLImate\Util\Writer\StdErr;
  8. use League\CLImate\Util\Writer\StdOut;
  9. use Mockery;
  10. use function get_class;
  11. class OutputTest extends TestBase
  12. {
  13. /**
  14. * @test
  15. * @doesNotPerformAssertions
  16. */
  17. public function it_can_output_content()
  18. {
  19. $out = Mockery::mock(StdOut::class);
  20. $out->shouldReceive('write')->once()->with("Oh, hey there." . \PHP_EOL);
  21. $output = new Output();
  22. $output->add('out', $out);
  23. $output->defaultTo('out');
  24. $output->write('Oh, hey there.');
  25. }
  26. /**
  27. * @test
  28. * @doesNotPerformAssertions
  29. */
  30. public function it_can_output_content_without_a_new_line()
  31. {
  32. $out = Mockery::mock(StdOut::class);
  33. $out->shouldReceive('write')->once()->with("Oh, hey there.");
  34. $output = new Output();
  35. $output->add('out', $out);
  36. $output->defaultTo('out');
  37. $output->sameLine()->write('Oh, hey there.');
  38. }
  39. /**
  40. * @test
  41. * @doesNotPerformAssertions
  42. */
  43. public function it_can_default_to_a_writer()
  44. {
  45. $error = Mockery::mock(StdErr::class);
  46. $error->shouldReceive('write')->once()->with("Oh, hey there.");
  47. $output = new Output();
  48. $output->add('error', $error);
  49. $output->defaultTo('error');
  50. $output->sameLine()->write('Oh, hey there.');
  51. }
  52. /**
  53. * @test
  54. * @doesNotPerformAssertions
  55. */
  56. public function it_can_default_to_multiple_writers()
  57. {
  58. $out = Mockery::mock(StdOut::class);
  59. $out->shouldReceive('write')->once()->with("Oh, hey there.");
  60. $error = Mockery::mock(StdErr::class);
  61. $error->shouldReceive('write')->once()->with("Oh, hey there.");
  62. $output = new Output();
  63. $output->add('out', $out);
  64. $output->add('error', $error);
  65. $output->defaultTo(['out', 'error']);
  66. $output->sameLine()->write('Oh, hey there.');
  67. }
  68. /**
  69. * @test
  70. * @doesNotPerformAssertions
  71. */
  72. public function it_can_add_a_default()
  73. {
  74. $out = Mockery::mock(StdOut::class);
  75. $out->shouldReceive('write')->once()->with("Oh, hey there.");
  76. $error = Mockery::mock(StdErr::class);
  77. $error->shouldReceive('write')->once()->with("Oh, hey there.");
  78. $output = new Output();
  79. $output->add('out', $out);
  80. $output->add('error', $error);
  81. $output->defaultTo('out');
  82. $output->addDefault('error');
  83. $output->sameLine()->write('Oh, hey there.');
  84. }
  85. /**
  86. * @test
  87. * @doesNotPerformAssertions
  88. */
  89. public function it_can_handle_multiple_writers_for_one_key()
  90. {
  91. $out = Mockery::mock(StdOut::class);
  92. $out->shouldReceive('write')->once()->with('Oh, hey there.');
  93. $error = Mockery::mock(StdErr::class);
  94. $error->shouldReceive('write')->once()->with('Oh, hey there.');
  95. $output = new Output();
  96. $output->add('combo', [$out, $error]);
  97. $output->defaultTo('combo');
  98. $output->sameLine()->write('Oh, hey there.');
  99. }
  100. /**
  101. * @test
  102. * @doesNotPerformAssertions
  103. */
  104. public function it_can_take_existing_writer_keys_and_resolve_them()
  105. {
  106. $out = Mockery::mock(StdOut::class);
  107. $out->shouldReceive('write')->once()->with('Oh, hey there.');
  108. $error = Mockery::mock(StdErr::class);
  109. $error->shouldReceive('write')->once()->with('Oh, hey there.');
  110. $output = new Output();
  111. $output->add('out', $out);
  112. $output->add('error', $error);
  113. $output->add('combo', ['out', 'error']);
  114. $output->defaultTo('combo');
  115. $output->sameLine()->write('Oh, hey there.');
  116. }
  117. /** @test */
  118. public function it_can_get_the_available_writers()
  119. {
  120. $out = Mockery::mock(StdOut::class);
  121. $error = Mockery::mock(StdErr::class);
  122. $buffer = Mockery::mock(Buffer::class);
  123. $output = new Output();
  124. $output->add('out', $out);
  125. $output->add('error', $error);
  126. $output->add('buffer', $buffer);
  127. $output->add('combo', [$out, $buffer]);
  128. $output->add('key-combo', ['out', 'error']);
  129. $output->add('mixed-combo', ['out', $error]);
  130. $available = [
  131. 'out' => get_class($out),
  132. 'error' => get_class($error),
  133. 'buffer' => get_class($buffer),
  134. 'combo' => [
  135. get_class($out),
  136. get_class($buffer),
  137. ],
  138. 'key-combo' => [
  139. get_class($out),
  140. get_class($error),
  141. ],
  142. 'mixed-combo' => [
  143. get_class($out),
  144. get_class($error),
  145. ],
  146. ];
  147. $this->assertSame($available, $output->getAvailable());
  148. }
  149. /** @test */
  150. public function it_will_yell_if_writer_does_not_implement_writer_interface()
  151. {
  152. $out = Mockery::mock();
  153. $output = new Output();
  154. $this->expectException(InvalidArgumentException::class);
  155. $this->expectExceptionMessage("Class [" . get_class($out) . "] must implement League\CLImate\Util\Writer\WriterInterface.");
  156. $output->add('out', $out);
  157. }
  158. /** @test */
  159. public function it_will_yell_if_trying_to_add_a_non_existent_nested_writer_key()
  160. {
  161. $output = new Output();
  162. $this->expectException(InvalidArgumentException::class);
  163. $this->expectExceptionMessage("Unable to resolve writer [nothin]");
  164. $output->add('out', ['nothin']);
  165. }
  166. /**
  167. * @test
  168. * @doesNotPerformAssertions
  169. */
  170. public function it_can_write_to_a_non_default_writer_once()
  171. {
  172. $out = Mockery::mock(StdOut::class);
  173. $out->shouldReceive('write')->once()->with('Second time.');
  174. $error = Mockery::mock(StdErr::class);
  175. $error->shouldReceive('write')->once()->with('First time.');
  176. $output = new Output();
  177. $output->add('out', $out);
  178. $output->add('error', $error);
  179. $output->defaultTo('out');
  180. $output->once('error')->sameLine()->write('First time.');
  181. $output->sameLine()->write('Second time.');
  182. }
  183. /**
  184. * @test
  185. * @doesNotPerformAssertions
  186. */
  187. public function it_will_persist_writer_if_told_to()
  188. {
  189. $out = Mockery::mock(StdOut::class);
  190. $out->shouldReceive('write')->once()->with('Second time.');
  191. $error = Mockery::mock(StdErr::class);
  192. $error->shouldReceive('write')->times(3)->with('First time.');
  193. $output = new Output();
  194. $output->add('out', $out);
  195. $output->add('error', $error);
  196. $output->defaultTo('out');
  197. $output->persist();
  198. $output->once('error')->sameLine()->write('First time.');
  199. $output->sameLine()->write('First time.');
  200. $output->sameLine()->write('First time.');
  201. $output->persist(false);
  202. $output->sameLine()->write('Second time.');
  203. }
  204. /** @test */
  205. public function it_can_retrieve_a_writer()
  206. {
  207. $buffer = Mockery::mock(Buffer::class);
  208. $buffer->shouldReceive('write')->once()->with('Oh, hey there.');
  209. $buffer->shouldReceive('get')->once()->andReturn('Oh, hey there.');
  210. $output = new Output();
  211. $output->add('buffer', $buffer);
  212. $output->defaultTo('buffer');
  213. $output->sameLine()->write('Oh, hey there.');
  214. $this->assertSame($output->get('buffer')->get(), 'Oh, hey there.');
  215. }
  216. }