|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Native\Laravel\Fakes; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract; |
| 7 | +use Native\Laravel\Enums\SystemIdleStatesEnum; |
| 8 | +use Native\Laravel\Enums\ThermalStatesEnum; |
| 9 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 10 | + |
| 11 | +class PowerMonitorFake implements PowerMonitorContract |
| 12 | +{ |
| 13 | + public array $getSystemIdleStateCalls = []; |
| 14 | + |
| 15 | + public int $getSystemIdleStateCount = 0; |
| 16 | + |
| 17 | + public int $getSystemIdleTimeCount = 0; |
| 18 | + |
| 19 | + public int $getCurrentThermalStateCount = 0; |
| 20 | + |
| 21 | + public int $isOnBatteryPowerCount = 0; |
| 22 | + |
| 23 | + public function getSystemIdleState(int $threshold): SystemIdleStatesEnum |
| 24 | + { |
| 25 | + $this->getSystemIdleStateCount++; |
| 26 | + |
| 27 | + $this->getSystemIdleStateCalls[] = $threshold; |
| 28 | + |
| 29 | + return SystemIdleStatesEnum::UNKNOWN; |
| 30 | + } |
| 31 | + |
| 32 | + public function getSystemIdleTime(): int |
| 33 | + { |
| 34 | + $this->getSystemIdleTimeCount++; |
| 35 | + |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + public function getCurrentThermalState(): ThermalStatesEnum |
| 40 | + { |
| 41 | + $this->getCurrentThermalStateCount++; |
| 42 | + |
| 43 | + return ThermalStatesEnum::UNKNOWN; |
| 44 | + } |
| 45 | + |
| 46 | + public function isOnBatteryPower(): bool |
| 47 | + { |
| 48 | + $this->isOnBatteryPowerCount++; |
| 49 | + |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param int|Closure(int): bool $key |
| 55 | + */ |
| 56 | + public function assertGetSystemIdleState(int|Closure $key): void |
| 57 | + { |
| 58 | + if (is_callable($key) === false) { |
| 59 | + PHPUnit::assertContains($key, $this->getSystemIdleStateCalls); |
| 60 | + |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $hit = empty( |
| 65 | + array_filter( |
| 66 | + $this->getSystemIdleStateCalls, |
| 67 | + fn (string $keyIteration) => $key($keyIteration) === true |
| 68 | + ) |
| 69 | + ) === false; |
| 70 | + |
| 71 | + PHPUnit::assertTrue($hit); |
| 72 | + } |
| 73 | + |
| 74 | + public function assertGetSystemIdleStateCount(int $count): void |
| 75 | + { |
| 76 | + PHPUnit::assertSame($count, $this->getSystemIdleStateCount); |
| 77 | + } |
| 78 | + |
| 79 | + public function assertGetSystemIdleTimeCount(int $count): void |
| 80 | + { |
| 81 | + PHPUnit::assertSame($count, $this->getSystemIdleTimeCount); |
| 82 | + } |
| 83 | + |
| 84 | + public function assertGetCurrentThermalStateCount(int $count): void |
| 85 | + { |
| 86 | + PHPUnit::assertSame($count, $this->getCurrentThermalStateCount); |
| 87 | + } |
| 88 | + |
| 89 | + public function assertIsOnBatteryPowerCount(int $count): void |
| 90 | + { |
| 91 | + PHPUnit::assertSame($count, $this->isOnBatteryPowerCount); |
| 92 | + } |
| 93 | +} |
0 commit comments