SleeperTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace League\CLImate\Tests\TerminalObject\Helper;
  3. use League\CLImate\TerminalObject\Helper\Sleeper;
  4. use PHPUnit\Framework\TestCase;
  5. class SleeperTest extends TestCase
  6. {
  7. public function setUp(): void
  8. {
  9. if (\PHP_OS_FAMILY === "Windows") {
  10. $this->markTestSkipped();
  11. }
  12. }
  13. private function assertSleep($expected, $speed)
  14. {
  15. $sleeper = new Sleeper();
  16. $sleeper->speed($speed);
  17. $start = microtime(true);
  18. $sleeper->sleep();
  19. $result = (microtime(true) - $start) * 1000000;
  20. $this->assertGreaterThan($expected * 0.9, $result);
  21. $this->assertLessThan($expected * 1.1, $result);
  22. }
  23. /**
  24. * @test
  25. */
  26. public function it_can_slow_down_the_sleeper_speed()
  27. {
  28. $this->assertSleep(100000, 50);
  29. }
  30. /**
  31. * @test
  32. */
  33. public function it_can_speed_up_the_sleeper_speed()
  34. {
  35. $this->assertSleep(25000, 200);
  36. }
  37. /**
  38. * @test
  39. */
  40. public function it_will_ignore_zero_percentages()
  41. {
  42. $this->assertSleep(50000, 0);
  43. }
  44. /**
  45. * @test
  46. */
  47. public function it_uses_whole_integers_only()
  48. {
  49. $sleeper = new Sleeper();
  50. $result = $sleeper->speed(33);
  51. self::assertSame(151515, $result);
  52. }
  53. }