|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ayesh\PHP_Timer\Tests; |
| 4 | + |
| 5 | +use Ayesh\PHP_Timer\Timer; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +/** |
| 9 | + * @package Ayesh\PHP_Timer\Tests |
| 10 | + * @group time-sensitive |
| 11 | + * @deprecated |
| 12 | + */ |
| 13 | +class TimerTest extends TestCase { |
| 14 | + |
| 15 | + public function testUnsupportedKeyType(){ |
| 16 | + $this->expectException(\TypeError::class); |
| 17 | + Timer::start(new \stdClass()); |
| 18 | + } |
| 19 | + |
| 20 | + private function sleepHalfSec(int $count = 1) { |
| 21 | + usleep(500000 * $count); |
| 22 | + } |
| 23 | + |
| 24 | + public function testStopValuesRetained() { |
| 25 | + Timer::start(__FUNCTION__); |
| 26 | + Timer::stop(__FUNCTION__); |
| 27 | + $stopped_at = Timer::read(__FUNCTION__, Timer::FORMAT_PRECISE); |
| 28 | + $this->sleepHalfSec(); |
| 29 | + $this->assertEquals(Timer::read(__FUNCTION__, Timer::FORMAT_PRECISE), $stopped_at); |
| 30 | + } |
| 31 | + |
| 32 | + public function testUnstoppedValuesContinue() { |
| 33 | + Timer::start(__FUNCTION__); |
| 34 | + $stopped_at = Timer::read(__FUNCTION__); |
| 35 | + usleep(500); |
| 36 | + $this->assertGreaterThan($stopped_at, Timer::read(__FUNCTION__, Timer::FORMAT_PRECISE)); |
| 37 | + } |
| 38 | + |
| 39 | + public function testIndividualTimersRunConcurrent() { |
| 40 | + Timer::resetAll(); |
| 41 | + |
| 42 | + Timer::start(1); |
| 43 | + Timer::start(2); |
| 44 | + Timer::start(3); |
| 45 | + |
| 46 | + Timer::stop(1); |
| 47 | + $timer_1 = Timer::read(1, Timer::FORMAT_PRECISE); |
| 48 | + |
| 49 | + usleep(500); |
| 50 | + |
| 51 | + $this->assertNotEquals(Timer::read(2, Timer::FORMAT_PRECISE), $timer_1); |
| 52 | + $this->assertNotEquals(Timer::read(3, Timer::FORMAT_PRECISE), $timer_1); |
| 53 | + $this->assertEquals(Timer::read(1, Timer::FORMAT_PRECISE), $timer_1); |
| 54 | + } |
| 55 | + |
| 56 | + public function testMultipleStartsQueued() { |
| 57 | + $key = __FUNCTION__; |
| 58 | + Timer::start($key); |
| 59 | + $this->sleepHalfSec(); |
| 60 | + $timer_1 = Timer::read($key, Timer::FORMAT_PRECISE); |
| 61 | + $this->assertGreaterThanOrEqual(450, $timer_1); |
| 62 | + |
| 63 | + Timer::start($key); |
| 64 | + $this->sleepHalfSec(); |
| 65 | + $timer_2 = Timer::read($key, Timer::FORMAT_PRECISE); |
| 66 | + $this->assertGreaterThanOrEqual(900, $timer_2); |
| 67 | + |
| 68 | + Timer::start($key); |
| 69 | + $this->sleepHalfSec(2); |
| 70 | + $timer_3 = Timer::read($key, Timer::FORMAT_PRECISE); |
| 71 | + $this->assertGreaterThanOrEqual(1900, $timer_3); |
| 72 | + |
| 73 | + $this->assertGreaterThan($timer_1, $timer_2); |
| 74 | + $this->assertGreaterThan($timer_2, $timer_3); |
| 75 | + $this->assertGreaterThan($timer_1 + $timer_2, $timer_3); |
| 76 | + } |
| 77 | + |
| 78 | + public function testMultipleStartCallsQueued_2() { |
| 79 | + $key = 'foo'; |
| 80 | + Timer::start($key); |
| 81 | + $this->assertLessThan(500, Timer::read($key)); |
| 82 | + $this->sleepHalfSec(2); |
| 83 | + $this->assertGreaterThanOrEqual(1000, Timer::read($key)); |
| 84 | + Timer::start($key); |
| 85 | + $this->sleepHalfSec(); |
| 86 | + $this->assertGreaterThanOrEqual(1500, Timer::read($key)); |
| 87 | + $this->assertLessThan(2000, Timer::read($key)); |
| 88 | + } |
| 89 | + |
| 90 | + public function testStopAndGoTimer() { |
| 91 | + Timer::start(__FUNCTION__); |
| 92 | + usleep(1000); |
| 93 | + Timer::stop(__FUNCTION__); |
| 94 | + usleep(5000); |
| 95 | + Timer::start(__FUNCTION__); |
| 96 | + usleep(2000); |
| 97 | + $timer = Timer::read(__FUNCTION__, Timer::FORMAT_SECONDS); |
| 98 | + |
| 99 | + $this->assertGreaterThanOrEqual(0.003, $timer); |
| 100 | + $this->assertLessThan(0.008, $timer); |
| 101 | + } |
| 102 | + |
| 103 | + public function testResetRestsTimer() { |
| 104 | + Timer::resetAll(); |
| 105 | + Timer::start(__FUNCTION__); |
| 106 | + Timer::reset(__FUNCTION__); |
| 107 | + $this->expectException(\LogicException::class); |
| 108 | + Timer::read(__FUNCTION__); |
| 109 | + } |
| 110 | + |
| 111 | + public function testTimerFormat_Seconds() { |
| 112 | + Timer::start(__FUNCTION__); |
| 113 | + |
| 114 | + usleep(1000); |
| 115 | + $read = Timer::read(__FUNCTION__, Timer::FORMAT_SECONDS); |
| 116 | + $this->assertGreaterThanOrEqual('0.001', $read); |
| 117 | + |
| 118 | + usleep(1000); |
| 119 | + $read = Timer::read(__FUNCTION__, Timer::FORMAT_SECONDS); |
| 120 | + $this->assertGreaterThanOrEqual('0.002', $read); |
| 121 | + } |
| 122 | + |
| 123 | + public function testTimerFormat_Unspecified() { |
| 124 | + Timer::start(__FUNCTION__); |
| 125 | + usleep(1500); |
| 126 | + $read = Timer::read(__FUNCTION__, 'unspecified'); |
| 127 | + |
| 128 | + $this->assertGreaterThanOrEqual(1, $read); |
| 129 | + } |
| 130 | + |
| 131 | + public function testTimerFormat_Milliseconds() { |
| 132 | + Timer::start(__FUNCTION__); |
| 133 | + usleep(1500); |
| 134 | + $read = Timer::read(__FUNCTION__, Timer::FORMAT_MILLISECONDS); |
| 135 | + |
| 136 | + $this->assertGreaterThanOrEqual(1, $read); |
| 137 | + } |
| 138 | + |
| 139 | + public function testStopWithoutInitializing() { |
| 140 | + Timer::resetAll(); |
| 141 | + $this->expectException(\LogicException::class); |
| 142 | + Timer::stop(__FUNCTION__); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + public function testValidSecondsCount() { |
| 147 | + Timer::start(__FUNCTION__); |
| 148 | + $this->assertInternalType('string', Timer::read(__FUNCTION__)); |
| 149 | + } |
| 150 | + |
| 151 | + public function testDenyAccessWithoutInitializing() { |
| 152 | + $this->expectException(\LogicException::class); |
| 153 | + Timer::resetAll(); |
| 154 | + Timer::read(); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | +} |
0 commit comments