OutputTest.php 7.3 KB

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