CLImateTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\Exceptions\InvalidArgumentException;
  4. use League\CLImate\Exceptions\UnexpectedValueException;
  5. use League\CLImate\Tests\CustomObject\BasicObject;
  6. use League\CLImate\Tests\CustomObject\BasicObjectArgument;
  7. class CLImateTest extends TestBase
  8. {
  9. /**
  10. * @test
  11. * @doesNotPerformAssertions
  12. */
  13. public function it_can_echo_out_a_string()
  14. {
  15. $this->shouldWrite("\e[mThis would go out to the console.\e[0m");
  16. $this->shouldHavePersisted();
  17. $this->cli->out('This would go out to the console.');
  18. }
  19. /**
  20. * @test
  21. * @doesNotPerformAssertions
  22. */
  23. public function it_can_chain_the_out_method()
  24. {
  25. $this->shouldWrite("\e[mThis is a line.\e[0m");
  26. $this->shouldWrite("\e[mThis is another line.\e[0m");
  27. $this->shouldHavePersisted(2);
  28. $this->cli->out('This is a line.')->out('This is another line.');
  29. }
  30. /**
  31. * @test
  32. * @doesNotPerformAssertions
  33. */
  34. public function it_can_write_content_to_a_different_output()
  35. {
  36. $this->shouldWrite("\e[mThis is to the buffer.\e[0m");
  37. $this->output->shouldReceive('defaultTo')->with('out')->once();
  38. $this->output->shouldReceive('once')->with('buffer')->once();
  39. $this->shouldHavePersisted();
  40. // Just double check that this is the default output
  41. $this->cli->output->defaultTo('out');
  42. $this->cli->to('buffer')->out('This is to the buffer.');
  43. }
  44. /**
  45. * @test
  46. * @doesNotPerformAssertions
  47. */
  48. public function it_can_be_extended_using_a_basic_object_as_string()
  49. {
  50. $this->shouldWrite("\e[mBy Custom Object: This is something my custom object is handling.\e[0m");
  51. $this->shouldHavePersisted();
  52. $this->cli->extend('League\CLImate\Tests\CustomObject\Basic');
  53. $this->cli->basic('This is something my custom object is handling.');
  54. }
  55. /**
  56. * @test
  57. * @doesNotPerformAssertions
  58. */
  59. public function it_can_be_extended_using_a_basic_object()
  60. {
  61. $this->shouldWrite("\e[mThis just outputs this.\e[0m");
  62. $this->shouldHavePersisted();
  63. $this->cli->extend(new BasicObject);
  64. $this->cli->basicObject();
  65. }
  66. /**
  67. * @test
  68. * @doesNotPerformAssertions
  69. */
  70. public function it_can_be_extended_using_a_basic_object_with_argument_setter()
  71. {
  72. $this->shouldWrite("\e[mHey: This is the thing that will print to the console.\e[0m");
  73. $this->shouldHavePersisted();
  74. $this->cli->extend(new BasicObjectArgument);
  75. $this->cli->basicObjectArgument('This is the thing that will print to the console.');
  76. }
  77. /** @test */
  78. public function it_can_be_extended_using_a_dynamic_object()
  79. {
  80. $this->cli->extend('League\CLImate\Tests\CustomObject\Dynamic');
  81. $obj = $this->cli->dynamic();
  82. $this->assertInstanceOf('League\CLImate\Tests\CustomObject\Dynamic', $obj);
  83. }
  84. /** @test */
  85. public function it_will_yell_if_extending_and_class_doesnt_exist()
  86. {
  87. $class = 'League\CLImate\Tests\CustomObject\NowhereToBeFound';
  88. $this->expectException(UnexpectedValueException::class);
  89. $this->expectExceptionMessage("Class does not exist: {$class}");
  90. $this->cli->extend($class);
  91. }
  92. /** @test */
  93. public function it_will_yell_if_it_doesnt_implement_proper_interfaces()
  94. {
  95. $class = 'League\CLImate\Tests\CustomObject\Dummy';
  96. $this->expectException(InvalidArgumentException::class);
  97. $this->expectExceptionMessage("Class must implement either");
  98. $this->cli->extend($class);
  99. }
  100. /**
  101. * @test
  102. * @doesNotPerformAssertions
  103. */
  104. public function it_will_accept_a_custom_key_for_an_extension()
  105. {
  106. $this->shouldWrite("\e[mBy Custom Object: This is something my custom object is handling.\e[0m");
  107. $this->shouldHavePersisted();
  108. $this->cli->extend('League\CLImate\Tests\CustomObject\Basic', 'myCustomMethod');
  109. $this->cli->myCustomMethod('This is something my custom object is handling.');
  110. }
  111. /** @test */
  112. public function it_will_accept_an_array_of_extensions()
  113. {
  114. $this->shouldWrite("\e[mBy Custom Object: This is something my custom object is handling.\e[0m");
  115. $this->shouldHavePersisted();
  116. $extensions = [
  117. 'League\CLImate\Tests\CustomObject\Basic',
  118. 'League\CLImate\Tests\CustomObject\Dynamic',
  119. ];
  120. $this->cli->extend($extensions);
  121. $this->cli->basic('This is something my custom object is handling.');
  122. $obj = $this->cli->dynamic();
  123. $this->assertInstanceOf('League\CLImate\Tests\CustomObject\Dynamic', $obj);
  124. }
  125. /** @test */
  126. public function it_will_accept_an_array_of_extensions_with_custom_keys()
  127. {
  128. $this->shouldWrite("\e[mBy Custom Object: This is something my custom object is handling.\e[0m");
  129. $this->shouldHavePersisted();
  130. $extensions = [
  131. 'whatever' => 'League\CLImate\Tests\CustomObject\Basic',
  132. 'something' => 'League\CLImate\Tests\CustomObject\Dynamic',
  133. ];
  134. $this->cli->extend($extensions);
  135. $this->cli->whatever('This is something my custom object is handling.');
  136. $obj = $this->cli->something();
  137. $this->assertInstanceOf('League\CLImate\Tests\CustomObject\Dynamic', $obj);
  138. }
  139. }