Skip to content

Commit

Permalink
Fixes and improvements to powerMonitor (#445)
Browse files Browse the repository at this point in the history
* 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
SRWieZ authored Dec 18, 2024
1 parent 5bb2e17 commit c96a7c5
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function __construct()
->asJson();
}

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

public function post(string $endpoint, array $data = []): Response
Expand Down
17 changes: 17 additions & 0 deletions src/Contracts/PowerMonitor.php
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;
}
23 changes: 23 additions & 0 deletions src/Events/PowerMonitor/ScreenLocked.php
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'),
];
}
}
23 changes: 23 additions & 0 deletions src/Events/PowerMonitor/ScreenUnlocked.php
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'),
];
}
}
23 changes: 23 additions & 0 deletions src/Events/PowerMonitor/Shutdown.php
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'),
];
}
}
23 changes: 23 additions & 0 deletions src/Events/PowerMonitor/UserDidBecomeActive.php
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'),
];
}
}
23 changes: 23 additions & 0 deletions src/Events/PowerMonitor/UserDidResignActive.php
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'),
];
}
}
15 changes: 12 additions & 3 deletions src/Facades/PowerMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
namespace Native\Laravel\Facades;

use Illuminate\Support\Facades\Facade;
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
use Native\Laravel\Fakes\PowerMonitorFake;

/**
* @method static \Native\Laravel\Enums\SystemIdelStatesEnum getSystemIdleState(int $threshold)
* @method static \Native\Laravel\Enums\SystemIdleStatesEnum getSystemIdleState(int $threshold)
* @method static int getSystemIdleTime()
* @method static \Native\Laravel\Enums\ThermalStatesEnum getCurrentThermalState()
* @method static bool isOnBatteryPower()
*/
class PowerMonitor extends Facade
{
protected static function getFacadeAccessor()
public static function fake()
{
return \Native\Laravel\PowerMonitor::class;
return tap(static::getFacadeApplication()->make(PowerMonitorFake::class), function ($fake) {
static::swap($fake);
});
}

protected static function getFacadeAccessor(): string
{
return PowerMonitorContract::class;
}
}
93 changes: 93 additions & 0 deletions src/Fakes/PowerMonitorFake.php
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);
}
}
6 changes: 6 additions & 0 deletions src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
use Native\Laravel\Commands\SeedDatabaseCommand;
use Native\Laravel\Contracts\ChildProcess as ChildProcessContract;
use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract;
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
use Native\Laravel\Contracts\WindowManager as WindowManagerContract;
use Native\Laravel\Events\EventWatcher;
use Native\Laravel\Exceptions\Handler;
use Native\Laravel\GlobalShortcut as GlobalShortcutImplementation;
use Native\Laravel\Logging\LogWatcher;
use Native\Laravel\PowerMonitor as PowerMonitorImplementation;
use Native\Laravel\Windows\WindowManager as WindowManagerImplementation;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand Down Expand Up @@ -66,6 +68,10 @@ public function packageRegistered()
return $app->make(GlobalShortcutImplementation::class);
});

$this->app->bind(PowerMonitorContract::class, function (Foundation $app) {
return $app->make(PowerMonitorImplementation::class);
});

if (config('nativephp-internal.running')) {
$this->app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
Expand Down
3 changes: 2 additions & 1 deletion src/PowerMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Native\Laravel;

use Native\Laravel\Client\Client;
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
use Native\Laravel\Enums\SystemIdleStatesEnum;
use Native\Laravel\Enums\ThermalStatesEnum;

class PowerMonitor
class PowerMonitor implements PowerMonitorContract
{
public function __construct(protected Client $client) {}

Expand Down
Loading

0 comments on commit c96a7c5

Please sign in to comment.