CLImateTest.php 5.1 KB

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