AnimationTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use Mockery;
  4. class AnimationTest extends TestBase
  5. {
  6. use ExitToTopFrames, ExitToBottomFrames, ExitToLeftFrames, ExitToRightFrames, RunFrames;
  7. protected function emptyFrame()
  8. {
  9. $this->shouldWrite("\e[m\e[0m")->ordered();
  10. $this->shouldWrite("\e[m\e[0m")->ordered();
  11. $this->shouldWrite("\e[m\e[0m")->ordered();
  12. $this->shouldWrite("\e[m\e[0m")->ordered();
  13. $this->shouldWrite("\e[m\e[0m")->ordered();
  14. $this->shouldWrite("\e[m\e[0m")->ordered();
  15. }
  16. protected function blankLines($count = 1)
  17. {
  18. for ($i = 1; $i <= $count; $i++) {
  19. $this->shouldWrite("\e[m\r\e[K\e[0m")->ordered();
  20. }
  21. }
  22. protected function getSleeper($count)
  23. {
  24. $sleeper = Mockery::mock('League\CLImate\TerminalObject\Helper\Sleeper');
  25. $sleeper->shouldReceive('sleep')->times($count);
  26. return $sleeper;
  27. }
  28. protected function runAsc($base, $end)
  29. {
  30. for ($i = 1; $i <= $end; $i++) {
  31. $this->{$base . $i}();
  32. }
  33. }
  34. protected function runDesc($base, $end)
  35. {
  36. for ($i = $end; $i >= 1; $i--) {
  37. $this->{$base . $i}();
  38. }
  39. }
  40. protected function assertScrolledRight()
  41. {
  42. $this->assertEnteredFromLeft();
  43. $this->assertExitedRight();
  44. }
  45. protected function assertEnteredFromLeft()
  46. {
  47. $this->emptyFrame();
  48. $this->runDesc('exitLeftFrame', 8);
  49. $this->fullArtExitLeftPlus();
  50. }
  51. protected function assertEnteredFromRight()
  52. {
  53. $this->runDesc('exitRightFrameEnd', 8);
  54. for ($i = 71; $i >= 0; $i--) {
  55. $this->exitRightFrame($i);
  56. }
  57. }
  58. protected function assertExitedLeft()
  59. {
  60. $this->runAsc('exitLeftFrame', 8);
  61. }
  62. protected function assertExitedRight()
  63. {
  64. for ($i = 0; $i <= 71; $i++) {
  65. $this->exitRightFrame($i);
  66. }
  67. $this->runAsc('exitRightFrameEnd', 9);
  68. }
  69. /** @test */
  70. public function it_can_exit_to_top()
  71. {
  72. $this->fullArtExitTop();
  73. $this->fullArtExitTopPlus(3);
  74. $this->runAsc('exitTopFrame', 6);
  75. $this->exitTopFrame6();
  76. $this->cli->animation('404', $this->getSleeper(11))->exitTo('top');
  77. }
  78. /** @test */
  79. public function it_can_enter_from_top()
  80. {
  81. $this->emptyFrame();
  82. $this->runDesc('exitTopFrame', 6);
  83. $this->fullArtExitTopPlus(4);
  84. $this->cli->animation('404', $this->getSleeper(11))->enterFrom('top');
  85. }
  86. /** @test */
  87. public function it_can_exit_to_bottom()
  88. {
  89. $this->fullArtExitBottom();
  90. $this->fullArtExitBottomPlus(3);
  91. $this->runAsc('exitBottomFrame', 6);
  92. $this->exitBottomFrame6();
  93. $this->cli->animation('404', $this->getSleeper(11))->exitTo('bottom');
  94. }
  95. /** @test */
  96. public function it_can_enter_from_bottom()
  97. {
  98. $this->emptyFrame();
  99. $this->runDesc('exitBottomFrame', 6);
  100. $this->fullArtExitBottomPlus(4);
  101. $this->cli->animation('404', $this->getSleeper(11))->enterFrom('bottom');
  102. }
  103. /** @test */
  104. public function it_can_exit_to_left()
  105. {
  106. $this->fullArtExitLeft();
  107. $this->fullArtExitLeftPlus(4);
  108. $this->assertExitedLeft();
  109. $this->exitLeftFrame9();
  110. $this->cli->addArt(__DIR__ . '/art');
  111. $this->cli->animation('4', $this->getSleeper(14))->exitTo('left');
  112. }
  113. /** @test */
  114. public function it_can_enter_from_left()
  115. {
  116. $this->assertEnteredFromLeft();
  117. $this->fullArtExitLeftPlus(4);
  118. $this->cli->addArt(__DIR__ . '/art');
  119. $this->cli->animation('4', $this->getSleeper(14))->enterFrom('left');
  120. }
  121. /** @test */
  122. public function it_can_exit_to_right()
  123. {
  124. $this->fullArtExitRight();
  125. $this->exitRightFrame(0, 3);
  126. $this->assertExitedRight();
  127. $this->cli->addArt(__DIR__ . '/art');
  128. $this->cli->animation('4', $this->getSleeper(85))->exitTo('right');
  129. }
  130. /** @test */
  131. public function it_can_enter_from_right()
  132. {
  133. $this->enterRightFrame1();
  134. $this->assertEnteredFromRight();
  135. $this->exitRightFrame(0, 4);
  136. $this->cli->addArt(__DIR__ . '/art');
  137. $this->cli->animation('4', $this->getSleeper(85))->enterFrom('right');
  138. }
  139. /** @test */
  140. public function it_will_scroll_to_the_right_by_default()
  141. {
  142. $this->assertScrolledRight();
  143. $this->cli->addArt(__DIR__ . '/art');
  144. $this->cli->animation('4', $this->getSleeper(91))->scroll();
  145. }
  146. /** @test */
  147. public function it_can_scroll_to_the_right()
  148. {
  149. $this->assertScrolledRight();
  150. $this->cli->addArt(__DIR__ . '/art');
  151. $this->cli->animation('4', $this->getSleeper(91))->scroll('right');
  152. }
  153. /** @test */
  154. public function it_can_scroll_to_the_left()
  155. {
  156. $this->emptyFrame();
  157. $this->assertEnteredFromRight();
  158. $this->fullArtExitLeftPlus();
  159. $this->assertExitedLeft();
  160. $this->exitRightFrameEnd9();
  161. $this->cli->addArt(__DIR__ . '/art');
  162. $this->cli->animation('4', $this->getSleeper(91))->scroll('left');
  163. }
  164. /** @test */
  165. public function it_can_scroll_up()
  166. {
  167. $this->emptyFrame();
  168. $this->runDesc('exitBottomFrame', 5);
  169. $this->fullArtExitBottomPlus();
  170. $this->runAsc('exitTopFrame', 6);
  171. $this->cli->animation('404', $this->getSleeper(13))->scroll('up');
  172. }
  173. /** @test */
  174. public function it_can_scroll_down()
  175. {
  176. $this->emptyFrame();
  177. $this->runDesc('exitTopFrame', 5);
  178. $this->fullArtExitBottomPlus();
  179. $this->runAsc('exitBottomFrame', 6);
  180. $this->cli->animation('404', $this->getSleeper(13))->scroll('down');
  181. }
  182. /** @test */
  183. public function it_can_run_a_directory_animation()
  184. {
  185. $this->runAsc('runFrames', 5);
  186. $this->cli->addArt(__DIR__ . '/art');
  187. $this->cli->animation('work-it', $this->getSleeper(5))->run();
  188. }
  189. /** @test */
  190. public function it_404s_when_it_gets_invalid_art()
  191. {
  192. $this->emptyFrame();
  193. $this->runDesc('exitBottomFrame', 6);
  194. $this->fullArtExitBottomPlus(4);
  195. $this->cli->animation('does-not-exist', $this->getSleeper(11))->enterFrom('bottom');
  196. }
  197. }