Skip to content

Commit c96a7c5

Browse files
authored
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
1 parent 5bb2e17 commit c96a7c5

12 files changed

+370
-6
lines changed

Diff for: src/Client/Client.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function __construct()
2121
->asJson();
2222
}
2323

24-
public function get(string $endpoint): Response
24+
public function get(string $endpoint, array|string|null $query = null): Response
2525
{
26-
return $this->client->get($endpoint);
26+
return $this->client->get($endpoint, $query);
2727
}
2828

2929
public function post(string $endpoint, array $data = []): Response

Diff for: src/Contracts/PowerMonitor.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Native\Laravel\Contracts;
4+
5+
use Native\Laravel\Enums\SystemIdleStatesEnum;
6+
use Native\Laravel\Enums\ThermalStatesEnum;
7+
8+
interface PowerMonitor
9+
{
10+
public function getSystemIdleState(int $threshold): SystemIdleStatesEnum;
11+
12+
public function getSystemIdleTime(): int;
13+
14+
public function getCurrentThermalState(): ThermalStatesEnum;
15+
16+
public function isOnBatteryPower(): bool;
17+
}

Diff for: src/Events/PowerMonitor/ScreenLocked.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class ScreenLocked implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

Diff for: src/Events/PowerMonitor/ScreenUnlocked.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class ScreenUnlocked implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

Diff for: src/Events/PowerMonitor/Shutdown.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class Shutdown implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

Diff for: src/Events/PowerMonitor/UserDidBecomeActive.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class UserDidBecomeActive implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

Diff for: src/Events/PowerMonitor/UserDidResignActive.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class UserDidResignActive implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

Diff for: src/Facades/PowerMonitor.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
namespace Native\Laravel\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6+
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
7+
use Native\Laravel\Fakes\PowerMonitorFake;
68

79
/**
8-
* @method static \Native\Laravel\Enums\SystemIdelStatesEnum getSystemIdleState(int $threshold)
10+
* @method static \Native\Laravel\Enums\SystemIdleStatesEnum getSystemIdleState(int $threshold)
911
* @method static int getSystemIdleTime()
1012
* @method static \Native\Laravel\Enums\ThermalStatesEnum getCurrentThermalState()
1113
* @method static bool isOnBatteryPower()
1214
*/
1315
class PowerMonitor extends Facade
1416
{
15-
protected static function getFacadeAccessor()
17+
public static function fake()
1618
{
17-
return \Native\Laravel\PowerMonitor::class;
19+
return tap(static::getFacadeApplication()->make(PowerMonitorFake::class), function ($fake) {
20+
static::swap($fake);
21+
});
22+
}
23+
24+
protected static function getFacadeAccessor(): string
25+
{
26+
return PowerMonitorContract::class;
1827
}
1928
}

Diff for: src/Fakes/PowerMonitorFake.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

Diff for: src/NativeServiceProvider.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
use Native\Laravel\Commands\SeedDatabaseCommand;
1717
use Native\Laravel\Contracts\ChildProcess as ChildProcessContract;
1818
use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract;
19+
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
1920
use Native\Laravel\Contracts\WindowManager as WindowManagerContract;
2021
use Native\Laravel\Events\EventWatcher;
2122
use Native\Laravel\Exceptions\Handler;
2223
use Native\Laravel\GlobalShortcut as GlobalShortcutImplementation;
2324
use Native\Laravel\Logging\LogWatcher;
25+
use Native\Laravel\PowerMonitor as PowerMonitorImplementation;
2426
use Native\Laravel\Windows\WindowManager as WindowManagerImplementation;
2527
use Spatie\LaravelPackageTools\Package;
2628
use Spatie\LaravelPackageTools\PackageServiceProvider;
@@ -66,6 +68,10 @@ public function packageRegistered()
6668
return $app->make(GlobalShortcutImplementation::class);
6769
});
6870

71+
$this->app->bind(PowerMonitorContract::class, function (Foundation $app) {
72+
return $app->make(PowerMonitorImplementation::class);
73+
});
74+
6975
if (config('nativephp-internal.running')) {
7076
$this->app->singleton(
7177
\Illuminate\Contracts\Debug\ExceptionHandler::class,

Diff for: src/PowerMonitor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Native\Laravel;
44

55
use Native\Laravel\Client\Client;
6+
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
67
use Native\Laravel\Enums\SystemIdleStatesEnum;
78
use Native\Laravel\Enums\ThermalStatesEnum;
89

9-
class PowerMonitor
10+
class PowerMonitor implements PowerMonitorContract
1011
{
1112
public function __construct(protected Client $client) {}
1213

0 commit comments

Comments
 (0)