CLImateTest.php 5.0 KB

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