AnimationTest.php 7.1 KB

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