123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace League\CLImate\Tests;
- use League\CLImate\Util\Output;
- use Mockery;
- class OutputTest extends TestBase
- {
- /** @test */
- public function it_can_output_content()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with("Oh, hey there.\n");
- $output = new Output;
- $output->add('out', $out);
- $output->defaultTo('out');
- $output->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_output_content_without_a_new_line()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with("Oh, hey there.");
- $output = new Output;
- $output->add('out', $out);
- $output->defaultTo('out');
- $output->sameLine()->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_default_to_a_writer()
- {
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->once()->with("Oh, hey there.");
- $output = new Output;
- $output->add('error', $error);
- $output->defaultTo('error');
- $output->sameLine()->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_default_to_multiple_writers()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with("Oh, hey there.");
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->once()->with("Oh, hey there.");
- $output = new Output;
- $output->add('out', $out);
- $output->add('error', $error);
- $output->defaultTo(['out', 'error']);
- $output->sameLine()->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_add_a_default()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with("Oh, hey there.");
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->once()->with("Oh, hey there.");
- $output = new Output;
- $output->add('out', $out);
- $output->add('error', $error);
- $output->defaultTo('out');
- $output->addDefault('error');
- $output->sameLine()->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_handle_multiple_writers_for_one_key()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with('Oh, hey there.');
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->once()->with('Oh, hey there.');
- $output = new Output;
- $output->add('combo', [$out, $error]);
- $output->defaultTo('combo');
- $output->sameLine()->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_take_existing_writer_keys_and_resolve_them()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with('Oh, hey there.');
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->once()->with('Oh, hey there.');
- $output = new Output;
- $output->add('out', $out);
- $output->add('error', $error);
- $output->add('combo', ['out', 'error']);
- $output->defaultTo('combo');
- $output->sameLine()->write('Oh, hey there.');
- }
- /** @test */
- public function it_can_get_the_available_writers()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $buffer = Mockery::mock('League\CLImate\Util\Writer\Buffer');
- $output = new Output;
- $output->add('out', $out);
- $output->add('error', $error);
- $output->add('buffer', $buffer);
- $output->add('combo', [$out, $buffer]);
- $output->add('key-combo', ['out', 'error']);
- $output->add('mixed-combo', ['out', $error]);
- $available = [
- 'out' => get_class($out),
- 'error' => get_class($error),
- 'buffer' => get_class($buffer),
- 'combo' => [
- get_class($out),
- get_class($buffer),
- ],
- 'key-combo' => [
- get_class($out),
- get_class($error),
- ],
- 'mixed-combo' => [
- get_class($out),
- get_class($error),
- ],
- ];
- $this->assertSame($available, $output->getAvailable());
- }
- /** @test */
- public function it_will_yell_if_writer_does_not_implement_writer_interface()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\Wat');
- $output = new Output;
- $this->setExpectedException(
- 'Exception',
- 'Class [' . get_class($out) . '] must implement League\CLImate\Util\Writer\WriterInterface.'
- );
- $output->add('out', $out);
- }
- /** @test */
- public function it_will_yell_if_trying_to_add_a_non_existent_nested_writer_key()
- {
- $output = new Output;
- $this->setExpectedException(
- 'Exception',
- 'Unable to resolve writer [nothin]'
- );
- $output->add('out', ['nothin']);
- }
- /** @test */
- public function it_can_write_to_a_non_default_writer_once()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with('Second time.');
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->once()->with('First time.');
- $output = new Output;
- $output->add('out', $out);
- $output->add('error', $error);
- $output->defaultTo('out');
- $output->once('error')->sameLine()->write('First time.');
- $output->sameLine()->write('Second time.');
- }
- /** @test */
- public function it_will_persist_writer_if_told_to()
- {
- $out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
- $out->shouldReceive('write')->once()->with('Second time.');
- $error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
- $error->shouldReceive('write')->times(3)->with('First time.');
- $output = new Output;
- $output->add('out', $out);
- $output->add('error', $error);
- $output->defaultTo('out');
- $output->persist();
- $output->once('error')->sameLine()->write('First time.');
- $output->sameLine()->write('First time.');
- $output->sameLine()->write('First time.');
- $output->persist(false);
- $output->sameLine()->write('Second time.');
- }
- /** @test */
- public function it_can_retrieve_a_writer()
- {
- $buffer = Mockery::mock('League\CLImate\Util\Writer\Buffer');
- $buffer->shouldReceive('write')->once()->with('Oh, hey there.');
- $buffer->shouldReceive('get')->once()->andReturn('Oh, hey there.');
- $output = new Output;
- $output->add('buffer', $buffer);
- $output->defaultTo('buffer');
- $output->sameLine()->write('Oh, hey there.');
- $this->assertSame($output->get('buffer')->get(), 'Oh, hey there.');
- }
- }
|