-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes and improvements to powerMonitor (#445)
* fix: powerMonitor couldn't pass arguments with get methods * feat: additional events * fix: PowerMonitor Facade docblock * feat: added fake class for tests
- Loading branch information
Showing
12 changed files
with
370 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Contracts; | ||
|
||
use Native\Laravel\Enums\SystemIdleStatesEnum; | ||
use Native\Laravel\Enums\ThermalStatesEnum; | ||
|
||
interface PowerMonitor | ||
{ | ||
public function getSystemIdleState(int $threshold): SystemIdleStatesEnum; | ||
|
||
public function getSystemIdleTime(): int; | ||
|
||
public function getCurrentThermalState(): ThermalStatesEnum; | ||
|
||
public function isOnBatteryPower(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Events\PowerMonitor; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ScreenLocked implements ShouldBroadcastNow | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct() {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('nativephp'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Events\PowerMonitor; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ScreenUnlocked implements ShouldBroadcastNow | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct() {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('nativephp'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Events\PowerMonitor; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class Shutdown implements ShouldBroadcastNow | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct() {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('nativephp'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Events\PowerMonitor; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class UserDidBecomeActive implements ShouldBroadcastNow | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct() {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('nativephp'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Events\PowerMonitor; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class UserDidResignActive implements ShouldBroadcastNow | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct() {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('nativephp'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Fakes; | ||
|
||
use Closure; | ||
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract; | ||
use Native\Laravel\Enums\SystemIdleStatesEnum; | ||
use Native\Laravel\Enums\ThermalStatesEnum; | ||
use PHPUnit\Framework\Assert as PHPUnit; | ||
|
||
class PowerMonitorFake implements PowerMonitorContract | ||
{ | ||
public array $getSystemIdleStateCalls = []; | ||
|
||
public int $getSystemIdleStateCount = 0; | ||
|
||
public int $getSystemIdleTimeCount = 0; | ||
|
||
public int $getCurrentThermalStateCount = 0; | ||
|
||
public int $isOnBatteryPowerCount = 0; | ||
|
||
public function getSystemIdleState(int $threshold): SystemIdleStatesEnum | ||
{ | ||
$this->getSystemIdleStateCount++; | ||
|
||
$this->getSystemIdleStateCalls[] = $threshold; | ||
|
||
return SystemIdleStatesEnum::UNKNOWN; | ||
} | ||
|
||
public function getSystemIdleTime(): int | ||
{ | ||
$this->getSystemIdleTimeCount++; | ||
|
||
return 0; | ||
} | ||
|
||
public function getCurrentThermalState(): ThermalStatesEnum | ||
{ | ||
$this->getCurrentThermalStateCount++; | ||
|
||
return ThermalStatesEnum::UNKNOWN; | ||
} | ||
|
||
public function isOnBatteryPower(): bool | ||
{ | ||
$this->isOnBatteryPowerCount++; | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param int|Closure(int): bool $key | ||
*/ | ||
public function assertGetSystemIdleState(int|Closure $key): void | ||
{ | ||
if (is_callable($key) === false) { | ||
PHPUnit::assertContains($key, $this->getSystemIdleStateCalls); | ||
|
||
return; | ||
} | ||
|
||
$hit = empty( | ||
array_filter( | ||
$this->getSystemIdleStateCalls, | ||
fn (string $keyIteration) => $key($keyIteration) === true | ||
) | ||
) === false; | ||
|
||
PHPUnit::assertTrue($hit); | ||
} | ||
|
||
public function assertGetSystemIdleStateCount(int $count): void | ||
{ | ||
PHPUnit::assertSame($count, $this->getSystemIdleStateCount); | ||
} | ||
|
||
public function assertGetSystemIdleTimeCount(int $count): void | ||
{ | ||
PHPUnit::assertSame($count, $this->getSystemIdleTimeCount); | ||
} | ||
|
||
public function assertGetCurrentThermalStateCount(int $count): void | ||
{ | ||
PHPUnit::assertSame($count, $this->getCurrentThermalStateCount); | ||
} | ||
|
||
public function assertIsOnBatteryPowerCount(int $count): void | ||
{ | ||
PHPUnit::assertSame($count, $this->isOnBatteryPowerCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.