diff --git a/CHANGELOG.md b/CHANGELOG.md index 09dcf01..807f169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ - Requires `innmind/immutable:~5.14` - `Innmind\Server\Status\Server::cpu()` now returns `Innmind\Immutable\Attempt` - `Innmind\Server\Status\Server::memory()` now returns `Innmind\Immutable\Attempt` +- `Innmind\Server\Status\Server::loadAverage()` now returns `Innmind\Immutable\Attempt` + +### Removed + +- `Innmind\Server\Status\Exception\EmptyPathNotAllowed` +- `Innmind\Server\Status\Exception\BytesCannotBeNegative` +- `Innmind\Server\Status\Exception\EmptyCommandNotAllowed` +- `Innmind\Server\Status\Exception\OutOfBoundsPercentage` +- `Innmind\Server\Status\Exception\LowestPidPossibleIsOne` +- `Innmind\Server\Status\Exception\EmptyUserNotAllowed` +- `Innmind\Server\Status\Exception\LoadAverageCannotBeNegative` +- `Innmind\Server\Status\Exception\DomainException` ## 4.1.1 - 2024-09-30 diff --git a/composer.json b/composer.json index 92c4de5..2f6b577 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "innmind/time-continuum": "^4.1.1", "innmind/url": "~4.0", "psr/log": "~3.0", - "innmind/server-control": "~6.0" + "innmind/server-control": "~6.0", + "innmind/validation": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/Exception/BytesCannotBeNegative.php b/src/Exception/BytesCannotBeNegative.php deleted file mode 100644 index 94a6185..0000000 --- a/src/Exception/BytesCannotBeNegative.php +++ /dev/null @@ -1,8 +0,0 @@ -toString(), ) ->map(static fn($cores) => (int) $cores) - ->match( - static fn($cores) => $cores, - static fn() => 1, - ); + ->keep(Is::int()->positive()->asPredicate()) + ->otherwise(static fn() => Maybe::just(1)) + ->map(Cores::of(...)); - $user = $percentages->get('user'); - $sys = $percentages->get('sys'); - $idle = $percentages->get('idle'); + $user = $percentages + ->get('user') + ->flatMap(Percentage::maybe(...)); + $sys = $percentages + ->get('sys') + ->flatMap(Percentage::maybe(...)); + $idle = $percentages + ->get('idle') + ->flatMap(Percentage::maybe(...)); - return Maybe::all($user, $sys, $idle) - ->map(static fn(float $user, float $sys, float $idle) => new Cpu( - new Percentage($user), - new Percentage($sys), - new Percentage($idle), - new Cores($cores), - )) + return Maybe::all($user, $sys, $idle, $cores) + ->map(Cpu::of(...)) ->match( Attempt::result(...), static fn() => Attempt::error(new \RuntimeException('Failed to parse CPU usage')), diff --git a/src/Facade/Cpu/OSXFacade.php b/src/Facade/Cpu/OSXFacade.php index a8706d8..3a09b94 100644 --- a/src/Facade/Cpu/OSXFacade.php +++ b/src/Facade/Cpu/OSXFacade.php @@ -12,6 +12,7 @@ Processes, Command, }; +use Innmind\Validation\Is; use Innmind\Immutable\{ Str, Attempt, @@ -96,18 +97,21 @@ private function parse(Str $output): Attempt ->flatMap(static fn($output) => $output->get('cores')) ->map(static fn($cores) => $cores->toString()) ->map(static fn($cores) => (int) $cores) - ->otherwise(static fn() => Maybe::just(1)); - $user = $percentages->get('user'); - $sys = $percentages->get('sys'); - $idle = $percentages->get('idle'); + ->keep(Is::int()->positive()->asPredicate()) + ->otherwise(static fn() => Maybe::just(1)) + ->map(Cores::of(...)); + $user = $percentages + ->get('user') + ->flatMap(Percentage::maybe(...)); + $sys = $percentages + ->get('sys') + ->flatMap(Percentage::maybe(...)); + $idle = $percentages + ->get('idle') + ->flatMap(Percentage::maybe(...)); return Maybe::all($user, $sys, $idle, $cores) - ->map(static fn(float $user, float $sys, float $idle, int $cores) => new Cpu( - new Percentage($user), - new Percentage($sys), - new Percentage($idle), - new Cores($cores), - )) + ->map(Cpu::of(...)) ->match( Attempt::result(...), static fn() => Attempt::error(new \RuntimeException('Failed to parse CPU usage')), diff --git a/src/Facade/LoadAverage/PhpFacade.php b/src/Facade/LoadAverage/PhpFacade.php index 091ad51..f999c45 100644 --- a/src/Facade/LoadAverage/PhpFacade.php +++ b/src/Facade/LoadAverage/PhpFacade.php @@ -4,17 +4,24 @@ namespace Innmind\Server\Status\Facade\LoadAverage; use Innmind\Server\Status\Server\LoadAverage; +use Innmind\Immutable\Attempt; /** * @internal */ final class PhpFacade { - public function __invoke(): LoadAverage + /** + * @return Attempt + */ + public function __invoke(): Attempt { - /** @var array{0: int, 1: int, 2: int} */ + /** @var array{0: float, 1: float, 2: float} */ $load = \sys_getloadavg(); - return new LoadAverage($load[0], $load[1], $load[2]); + return LoadAverage::maybe($load[0], $load[1], $load[2])->match( + Attempt::result(...), + static fn() => Attempt::error(new \RuntimeException('Failed to load load average')), + ); } } diff --git a/src/Facade/Memory/LinuxFacade.php b/src/Facade/Memory/LinuxFacade.php index e0ee702..3a8c7a4 100644 --- a/src/Facade/Memory/LinuxFacade.php +++ b/src/Facade/Memory/LinuxFacade.php @@ -11,6 +11,7 @@ Processes, Command, }; +use Innmind\Validation\Is; use Innmind\Immutable\{ Str, Map, @@ -67,45 +68,57 @@ public function __invoke(): Attempt */ private function parse(Str $output): Attempt { - /** @var Map */ $amounts = $output ->trim() ->split("\n") ->filter(static fn(Str $line) => $line->matches( '~^('.\implode('|', \array_keys(self::$entries)).'):~', )) - ->reduce( - Map::of(), - static function(Map $map, Str $line): Map { - $elements = $line - ->capture('~^(?P[a-zA-Z]+): +(?P\d+) kB$~') - ->map(static fn($_, $part) => $part->toString()); - - return Maybe::all($elements->get('key'), $elements->get('value')) - ->map(static fn(string $key, string $value) => [$key, (int) $value]) - ->match( - static fn($pair) => ($map)( - self::$entries[$pair[0]], - $pair[1] * 1024, // 1024 represents a kilobyte - ), - static fn() => $map, - ); - }, + ->map( + static fn($line) => $line + ->capture('~^(?P[a-zA-Z]+): +(?P\d+) kB$~') + ->map(static fn($_, $part) => $part->toString()), + ) + ->flatMap( + static fn($elements) => Maybe::all( + $elements->get('key'), + $elements + ->get('value') + ->map(static fn($value) => (int) $value) + ->keep( + Is::int() + ->positive() + ->or(Is::value(0)) + ->asPredicate(), + ) + ->map(Bytes::of(...)), + ) + ->map(static fn(string $key, Bytes $value) => [ + self::$entries[$key], + $value, + ]) + ->toSequence(), ); + $amounts = Map::of(...$amounts->toList()) + ->map(static fn($_, $value) => Bytes::of( + $value->toInt() * 1024, // 1024 represents a kilobyte + )); $total = $amounts->get('total'); $free = $amounts->get('free'); $active = $amounts->get('active'); $swap = $amounts->get('swap'); - $used = Maybe::all($total, $free)->map(static fn(int $total, int $free) => $total - $free); + $used = Maybe::all($total, $free) + ->map(static fn(Bytes $total, Bytes $free) => $total->toInt() - $free->toInt()) + ->keep( + Is::int() + ->positive() + ->or(Is::value(0)) + ->asPredicate(), + ) + ->map(Bytes::of(...)); return Maybe::all($total, $active, $free, $swap, $used) - ->map(static fn(int $total, int $active, int $free, int $swap, int $used) => new Memory( - new Bytes($total), - new Bytes($active), - new Bytes($free), - new Bytes($swap), - new Bytes($used), - )) + ->map(Memory::of(...)) ->match( Attempt::result(...), static fn() => Attempt::error(new \RuntimeException('Failed to parse memory usage')), diff --git a/src/Facade/Memory/OSXFacade.php b/src/Facade/Memory/OSXFacade.php index 4eb3b9c..a094fab 100644 --- a/src/Facade/Memory/OSXFacade.php +++ b/src/Facade/Memory/OSXFacade.php @@ -46,7 +46,8 @@ public function __invoke(): Attempt ->trim() ->capture('~^hw.memsize: (?P\d+)$~') ->get('total') - ->map(static fn($total) => $total->toString()); + ->map(static fn($total) => $total->toString()) + ->flatMap(Bytes::maybe(...)); $swap = $this ->run( Command::foreground('sysctl') @@ -56,7 +57,7 @@ public function __invoke(): Attempt ->capture('~used = (?P\d+[\.,]?\d*[KMGTP])~') ->get('swap') ->map(static fn($swap) => $swap->toString()) - ->flatMap(static fn($swap) => Bytes::of($swap)); + ->flatMap(Bytes::maybe(...)); $amounts = $this ->run( Command::foreground('top') @@ -74,10 +75,10 @@ public function __invoke(): Attempt ->map(static fn($_, $amount) => $amount->toString()); $unused = $amounts ->get('unused') - ->flatMap(static fn($unused) => Bytes::of($unused)); + ->flatMap(Bytes::maybe(...)); $used = $amounts ->get('used') - ->flatMap(static fn($used) => Bytes::of($used)); + ->flatMap(Bytes::maybe(...)); $active = $this ->run( Command::foreground('vm_stat')->pipe( @@ -88,16 +89,13 @@ public function __invoke(): Attempt ->trim() ->capture('~(?P\d+)~') ->get('active') - ->map(static fn($active) => $active->toString()); + ->map(static fn($active) => $active->toString()) + ->flatMap(Bytes::maybe(...)) + ->map(static fn($bytes) => $bytes->toInt() * 4096) + ->map(Bytes::of(...)); return Maybe::all($total, $active, $unused, $swap, $used) - ->map(static fn(string $total, string $active, Bytes $unused, Bytes $swap, Bytes $used) => new Memory( - new Bytes((int) $total), - new Bytes(((int) $active) * 4096), - $unused, - $swap, - $used, - )) + ->map(Memory::of(...)) ->match( Attempt::result(...), static fn() => Attempt::error(new \RuntimeException('Failed to parse memory usage')), diff --git a/src/Server.php b/src/Server.php index 4be95f3..8357826 100644 --- a/src/Server.php +++ b/src/Server.php @@ -25,7 +25,11 @@ public function cpu(): Attempt; */ public function memory(): Attempt; public function processes(): Processes; - public function loadAverage(): LoadAverage; + + /** + * @return Attempt + */ + public function loadAverage(): Attempt; public function disk(): Disk; public function tmp(): Path; } diff --git a/src/Server/Cpu.php b/src/Server/Cpu.php index 815f22c..ce6c02e 100644 --- a/src/Server/Cpu.php +++ b/src/Server/Cpu.php @@ -13,21 +13,24 @@ */ final class Cpu { - private Percentage $user; - private Percentage $system; - private Percentage $idle; - private Cores $cores; + private function __construct( + private Percentage $user, + private Percentage $system, + private Percentage $idle, + private Cores $cores, + ) { + } - public function __construct( + /** + * @psalm-pure + */ + public static function of( Percentage $user, Percentage $system, Percentage $idle, Cores $cores, - ) { - $this->user = $user; - $this->system = $system; - $this->idle = $idle; - $this->cores = $cores; + ): self { + return new self($user, $system, $idle, $cores); } public function user(): Percentage diff --git a/src/Server/Cpu/Cores.php b/src/Server/Cpu/Cores.php index 9f4f2bf..0fde59a 100644 --- a/src/Server/Cpu/Cores.php +++ b/src/Server/Cpu/Cores.php @@ -3,27 +3,32 @@ namespace Innmind\Server\Status\Server\Cpu; -use Innmind\Server\Status\Exception\DomainException; - /** * @psalm-immutable */ final class Cores { - private int $value; + /** + * @param int<1, max> $value + */ + private function __construct( + private int $value, + ) { + } /** - * @throws DomainException + * @psalm-pure + * + * @param int<1, max> $value */ - public function __construct(int $value) + public static function of(int $value): self { - if ($value < 1) { - throw new DomainException((string) $value); - } - - $this->value = $value; + return new self($value); } + /** + * @return int<1, max> + */ public function toInt(): int { return $this->value; diff --git a/src/Server/Cpu/Percentage.php b/src/Server/Cpu/Percentage.php index f3ed13e..17c787c 100644 --- a/src/Server/Cpu/Percentage.php +++ b/src/Server/Cpu/Percentage.php @@ -3,25 +3,31 @@ namespace Innmind\Server\Status\Server\Cpu; -use Innmind\Server\Status\Exception\OutOfBoundsPercentage; +use Innmind\Immutable\Maybe; /** * @psalm-immutable */ final class Percentage { - private float $value; + private function __construct( + private float $value, + ) { + } /** - * @throws OutOfBoundsPercentage + * @psalm-pure + * + * @return Maybe */ - public function __construct(float $value) + public static function maybe(float $value): Maybe { if ($value < 0) { - throw new OutOfBoundsPercentage((string) $value); + /** @var Maybe */ + return Maybe::nothing(); } - $this->value = $value; + return Maybe::just(new self($value)); } public function toFloat(): float diff --git a/src/Server/Disk/UnixDisk.php b/src/Server/Disk/UnixDisk.php index 2ca2e3b..ff817db 100644 --- a/src/Server/Disk/UnixDisk.php +++ b/src/Server/Disk/UnixDisk.php @@ -14,6 +14,7 @@ Command, Process\Output, }; +use Innmind\Validation\Is; use Innmind\Immutable\{ Str, Set, @@ -98,26 +99,26 @@ private function parse(Str $output): Set ->map(static fn($parts) => $columns->zip($parts)) ->map(static fn($parts) => Map::of(...$parts->toList())); $volumes = $partsByLine->map(static function($parts): Maybe { - $mountPoint = $parts->get('mountPoint'); + $mountPoint = $parts + ->get('mountPoint') + ->keep(Is::string()->nonEmpty()->asPredicate()) + ->map(MountPoint::of(...)); $size = $parts ->get('size') - ->flatMap(static fn($size) => Bytes::of($size)); + ->flatMap(Bytes::maybe(...)); $available = $parts ->get('available') - ->flatMap(static fn($available) => Bytes::of($available)); + ->flatMap(Bytes::maybe(...)); $used = $parts ->get('used') - ->flatMap(static fn($used) => Bytes::of($used)); - $usage = $parts->get('usage'); + ->flatMap(Bytes::maybe(...)); + $usage = $parts + ->get('usage') + ->map(static fn($value) => (float) $value) + ->flatMap(Usage::maybe(...)); return Maybe::all($mountPoint, $size, $available, $used, $usage) - ->map(static fn(string $mountPoint, Bytes $size, Bytes $available, Bytes $used, string $usage) => new Volume( - new MountPoint($mountPoint), - $size, - $available, - $used, - new Usage((float) $usage), - )); + ->map(Volume::of(...)); }); return $volumes diff --git a/src/Server/Disk/Volume.php b/src/Server/Disk/Volume.php index 487e423..5fe52b9 100644 --- a/src/Server/Disk/Volume.php +++ b/src/Server/Disk/Volume.php @@ -9,26 +9,31 @@ Memory\Bytes, }; +/** + * @psalm-immutable + */ final class Volume { - private MountPoint $mountPoint; - private Bytes $size; - private Bytes $available; - private Bytes $used; - private Usage $usage; + private function __construct( + private MountPoint $mountPoint, + private Bytes $size, + private Bytes $available, + private Bytes $used, + private Usage $usage, + ) { + } - public function __construct( + /** + * @psalm-pure + */ + public static function of( MountPoint $mountPoint, Bytes $size, Bytes $available, Bytes $used, Usage $usage, - ) { - $this->mountPoint = $mountPoint; - $this->size = $size; - $this->available = $available; - $this->used = $used; - $this->usage = $usage; + ): self { + return new self($mountPoint, $size, $available, $used, $usage); } public function mountPoint(): MountPoint diff --git a/src/Server/Disk/Volume/MountPoint.php b/src/Server/Disk/Volume/MountPoint.php index 42b7327..85e8eb5 100644 --- a/src/Server/Disk/Volume/MountPoint.php +++ b/src/Server/Disk/Volume/MountPoint.php @@ -3,22 +3,27 @@ namespace Innmind\Server\Status\Server\Disk\Volume; -use Innmind\Server\Status\Exception\EmptyPathNotAllowed; - +/** + * @psalm-immutable + */ final class MountPoint { - private string $value; + /** + * @param non-empty-string $value + */ + private function __construct( + private string $value, + ) { + } /** - * @throws EmptyPathNotAllowed + * @psalm-pure + * + * @param non-empty-string $value */ - public function __construct(string $value) + public static function of(string $value): self { - if ($value === '') { - throw new EmptyPathNotAllowed; - } - - $this->value = $value; + return new self($value); } public function equals(self $point): bool @@ -31,6 +36,9 @@ public function is(string $point): bool return $this->value === $point; } + /** + * @return non-empty-string + */ public function toString(): string { return $this->value; diff --git a/src/Server/Disk/Volume/Usage.php b/src/Server/Disk/Volume/Usage.php index 5bdf518..f922f9d 100644 --- a/src/Server/Disk/Volume/Usage.php +++ b/src/Server/Disk/Volume/Usage.php @@ -3,22 +3,31 @@ namespace Innmind\Server\Status\Server\Disk\Volume; -use Innmind\Server\Status\Exception\OutOfBoundsPercentage; +use Innmind\Immutable\Maybe; +/** + * @psalm-immutable + */ final class Usage { - private float $value; + private function __construct( + private float $value, + ) { + } /** - * @throws OutOfBoundsPercentage + * @psalm-pure + * + * @return Maybe */ - public function __construct(float $value) + public static function maybe(float $value): Maybe { if ($value < 0 || $value > 100) { - throw new OutOfBoundsPercentage((string) $value); + /** @var Maybe */ + return Maybe::nothing(); } - $this->value = $value; + return Maybe::just(new self($value)); } public function toFloat(): float diff --git a/src/Server/LoadAverage.php b/src/Server/LoadAverage.php index e819c66..81fd144 100644 --- a/src/Server/LoadAverage.php +++ b/src/Server/LoadAverage.php @@ -3,34 +3,36 @@ namespace Innmind\Server\Status\Server; -use Innmind\Server\Status\Exception\LoadAverageCannotBeNegative; +use Innmind\Immutable\Maybe; /** * @psalm-immutable */ final class LoadAverage { - private float $lastMinute; - private float $lastFiveMinutes; - private float $lastFifteenMinutes; + private function __construct( + private float $lastMinute, + private float $lastFiveMinutes, + private float $lastFifteenMinutes, + ) { + } /** - * @throws LoadAverageCannotBeNegative + * @psalm-pure + * + * @return Maybe */ - public function __construct( + public static function maybe( float $lastMinute, float $lastFiveMinutes, float $lastFifteenMinutes, - ) { + ): Maybe { if ($lastMinute < 0 || $lastFiveMinutes < 0 || $lastFifteenMinutes < 0) { - throw new LoadAverageCannotBeNegative( - (string) \min($lastMinute, $lastFiveMinutes, $lastFifteenMinutes), - ); + /** @var Maybe */ + return Maybe::nothing(); } - $this->lastMinute = $lastMinute; - $this->lastFiveMinutes = $lastFiveMinutes; - $this->lastFifteenMinutes = $lastFifteenMinutes; + return Maybe::just(new self($lastMinute, $lastFiveMinutes, $lastFifteenMinutes)); } public function lastMinute(): float diff --git a/src/Server/Memory.php b/src/Server/Memory.php index 27918c1..87af0a2 100644 --- a/src/Server/Memory.php +++ b/src/Server/Memory.php @@ -10,24 +10,26 @@ */ final class Memory { - private Bytes $total; - private Bytes $active; - private Bytes $free; - private Bytes $swap; - private Bytes $used; + private function __construct( + private Bytes $total, + private Bytes $active, + private Bytes $free, + private Bytes $swap, + private Bytes $used, + ) { + } - public function __construct( + /** + * @psalm-pure + */ + public static function of( Bytes $total, Bytes $active, Bytes $free, Bytes $swap, Bytes $used, - ) { - $this->total = $total; - $this->active = $active; - $this->free = $free; - $this->swap = $swap; - $this->used = $used; + ): self { + return new self($total, $active, $free, $swap, $used); } public function total(): Bytes diff --git a/src/Server/Memory/Bytes.php b/src/Server/Memory/Bytes.php index 3bdbdd5..1b1ed5c 100644 --- a/src/Server/Memory/Bytes.php +++ b/src/Server/Memory/Bytes.php @@ -3,7 +3,7 @@ namespace Innmind\Server\Status\Server\Memory; -use Innmind\Server\Status\Exception\BytesCannotBeNegative; +use Innmind\Validation\Is; use Innmind\Immutable\{ Str, Maybe, @@ -21,63 +21,27 @@ final class Bytes private const TERABYTES = 1024 ** 5; private const PETABYTES = 1024 ** 6; - private int $value; - private string $string; + /** + * @param int<0, max> $value + */ + private function __construct( + private int $value, + ) { + } /** - * @throws BytesCannotBeNegative + * @psalm-pure + * + * @param int<0, max> $value */ - public function __construct(int $value) + public static function of(int $value): self { - if ($value < 0) { - throw new BytesCannotBeNegative((string) $value); - } - - $this->value = $value; - $this->string = $value.'B'; - - switch (true) { - case $value < self::BYTES: - $this->string = $value.'B'; - break; - - case $value < self::KILOBYTES: - $this->string = \sprintf( - '%sKB', - \round($value/self::BYTES, 3), - ); - break; - - case $value < self::MEGABYTES: - $this->string = \sprintf( - '%sMB', - \round($value/self::KILOBYTES, 3), - ); - break; - - case $value < self::GIGABYTES: - $this->string = \sprintf( - '%sGB', - \round($value/self::MEGABYTES, 3), - ); - break; - - case $value < self::TERABYTES: - $this->string = \sprintf( - '%sTB', - \round($value/self::GIGABYTES, 3), - ); - break; - - case $value < self::PETABYTES: - $this->string = \sprintf( - '%sPB', - \round($value/self::TERABYTES, 3), - ); - break; - } + return new self($value); } + /** + * @return int<0, max> + */ public function toInt(): int { return $this->value; @@ -85,26 +49,59 @@ public function toInt(): int public function toString(): string { - return $this->string; + return match (true) { + $this->value < self::BYTES => $this->value.'B', + $this->value < self::KILOBYTES => \sprintf( + '%sKB', + \round($this->value/self::BYTES, 3), + ), + $this->value < self::MEGABYTES => \sprintf( + '%sMB', + \round($this->value/self::KILOBYTES, 3), + ), + $this->value < self::GIGABYTES => \sprintf( + '%sGB', + \round($this->value/self::MEGABYTES, 3), + ), + $this->value < self::TERABYTES => \sprintf( + '%sTB', + \round($this->value/self::GIGABYTES, 3), + ), + $this->value < self::PETABYTES => \sprintf( + '%sPB', + \round($this->value/self::TERABYTES, 3), + ), + }; } /** + * @psalm-pure + * * @return Maybe */ - public static function of(string $bytes): Maybe + public static function maybe(string $bytes): Maybe { if ($bytes === (string) (int) $bytes) { - return Maybe::just(new self((int) $bytes)); + return Maybe::just((int) $bytes) + ->keep( + Is::int() + ->positive() + ->or(Is::value(0)) + ->asPredicate(), + ) + ->map(static fn($value) => new self($value)); } - return Maybe::just(Str::of($bytes)) - ->filter(static fn($bytes) => $bytes->length() >= 2) + return Str::of($bytes) + ->maybe(static fn($bytes) => $bytes->length() >= 2) ->flatMap(static fn($bytes) => self::attemptLinux($bytes)->otherwise( static fn() => self::attemptDarwin($bytes), )); } /** + * @psalm-pure + * * @return Maybe */ private static function attemptDarwin(Str $bytes): Maybe @@ -116,6 +113,8 @@ private static function attemptDarwin(Str $bytes): Maybe } /** + * @psalm-pure + * * @return Maybe */ private static function attemptLinux(Str $bytes): Maybe @@ -127,6 +126,8 @@ private static function attemptLinux(Str $bytes): Maybe } /** + * @psalm-pure + * * @return Maybe */ private static function fromUnit(Str $bytes, Str $unit): Maybe @@ -154,6 +155,12 @@ private static function fromUnit(Str $bytes, Str $unit): Maybe return Maybe::of($multiplier) ->map(static fn($multiplier) => (int) (((float) $bytes->toString()) * (float) $multiplier)) + ->keep( + Is::int() + ->positive() + ->or(Is::value(0)) + ->asPredicate(), + ) ->map(static fn($bytes) => new self($bytes)); } } diff --git a/src/Server/Process.php b/src/Server/Process.php index 0906086..43f3ebd 100644 --- a/src/Server/Process.php +++ b/src/Server/Process.php @@ -18,31 +18,33 @@ */ final class Process { - private Pid $pid; - private User $user; - private Percentage $cpu; - private Memory $memory; - /** @var Maybe */ - private Maybe $start; - private Command $command; + /** + * @param Maybe $start + */ + private function __construct( + private Pid $pid, + private User $user, + private Percentage $cpu, + private Memory $memory, + private Maybe $start, + private Command $command, + ) { + } /** + * @psalm-pure + * * @param Maybe $start */ - public function __construct( + public static function of( Pid $pid, User $user, Percentage $cpu, Memory $memory, Maybe $start, Command $command, - ) { - $this->pid = $pid; - $this->user = $user; - $this->cpu = $cpu; - $this->memory = $memory; - $this->start = $start; - $this->command = $command; + ): self { + return new self($pid, $user, $cpu, $memory, $start, $command); } public function pid(): Pid diff --git a/src/Server/Process/Command.php b/src/Server/Process/Command.php index 7260c9f..8c0eb09 100644 --- a/src/Server/Process/Command.php +++ b/src/Server/Process/Command.php @@ -3,7 +3,6 @@ namespace Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\Exception\EmptyCommandNotAllowed; use Innmind\Immutable\{ RegExp, Str, @@ -14,18 +13,22 @@ */ final class Command { - private string $value; + /** + * @param non-empty-string $value + */ + private function __construct( + private string $value, + ) { + } /** - * @throws EmptyCommandNotAllowed + * @psalm-pure + * + * @param non-empty-string $value */ - public function __construct(string $value) + public static function of(string $value): self { - if ($value === '') { - throw new EmptyCommandNotAllowed; - } - - $this->value = $value; + return new self($value); } public function matches(RegExp $pattern): bool @@ -33,6 +36,9 @@ public function matches(RegExp $pattern): bool return $pattern->matches(Str::of($this->value)); } + /** + * @return non-empty-string + */ public function toString(): string { return $this->value; diff --git a/src/Server/Process/Memory.php b/src/Server/Process/Memory.php index 4713804..a4c79f0 100644 --- a/src/Server/Process/Memory.php +++ b/src/Server/Process/Memory.php @@ -3,25 +3,31 @@ namespace Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\Exception\OutOfBoundsPercentage; +use Innmind\Immutable\Maybe; /** * @psalm-immutable */ final class Memory { - private float $value; + private function __construct( + private float $value, + ) { + } /** - * @throws OutOfBoundsPercentage + * @psalm-pure + * + * @return Maybe */ - public function __construct(float $value) + public static function maybe(float $value): Maybe { if ($value < 0 || $value > 100) { - throw new OutOfBoundsPercentage((string) $value); + /** @var Maybe */ + return Maybe::nothing(); } - $this->value = $value; + return Maybe::just(new self($value)); } public function toFloat(): float diff --git a/src/Server/Process/Pid.php b/src/Server/Process/Pid.php index 436e9ef..990d64c 100644 --- a/src/Server/Process/Pid.php +++ b/src/Server/Process/Pid.php @@ -3,25 +3,27 @@ namespace Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\Exception\LowestPidPossibleIsOne; - /** * @psalm-immutable */ final class Pid { - private int $value; + /** + * @param int<1, max> $value + */ + private function __construct( + private int $value, + ) { + } /** - * @throws LowestPidPossibleIsOne + * @psalm-pure + * + * @param int<1, max> $value */ - public function __construct(int $value) + public static function of(int $value): self { - if ($value < 1) { - throw new LowestPidPossibleIsOne((string) $value); - } - - $this->value = $value; + return new self($value); } public function equals(self $pid): bool @@ -34,6 +36,9 @@ public function is(int $value): bool return $this->value === $value; } + /** + * @return int<1, max> + */ public function toInt(): int { return $this->value; diff --git a/src/Server/Process/User.php b/src/Server/Process/User.php index 93dbd78..2b3a7e1 100644 --- a/src/Server/Process/User.php +++ b/src/Server/Process/User.php @@ -3,27 +3,32 @@ namespace Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\Exception\EmptyUserNotAllowed; - /** * @psalm-immutable */ final class User { - private string $value; + /** + * @param non-empty-string $value + */ + private function __construct( + private string $value, + ) { + } /** - * @throws EmptyUserNotAllowed + * @psalm-pure + * + * @param non-empty-string $value */ - public function __construct(string $value) + public static function of(string $value): self { - if ($value === '') { - throw new EmptyUserNotAllowed; - } - - $this->value = $value; + return new self($value); } + /** + * @return non-empty-string + */ public function toString(): string { return $this->value; diff --git a/src/Server/Processes/UnixProcesses.php b/src/Server/Processes/UnixProcesses.php index 50b3fa1..8027daf 100644 --- a/src/Server/Processes/UnixProcesses.php +++ b/src/Server/Processes/UnixProcesses.php @@ -17,6 +17,7 @@ Clock, Format, }; +use Innmind\Validation\Is; use Innmind\Immutable\{ Str, Sequence, @@ -110,20 +111,36 @@ private function parse(Str $output): Set $parts = $parts ->drop(5) ->map(static fn($part) => $part->toString()); - $user = $parts->get(0); - $pid = $parts->get(1); - $percentage = $parts->get(2); - $memory = $parts->get(3); - $command = $parts->get(4); + $user = $parts + ->get(0) + ->keep(Is::string()->nonEmpty()->asPredicate()) + ->map(User::of(...)); + $pid = $parts + ->get(1) + ->map(static fn($value) => (int) $value) + ->keep(Is::int()->positive()->asPredicate()) + ->map(Pid::of(...)); + $percentage = $parts + ->get(2) + ->map(static fn($value) => (float) $value) + ->flatMap(Percentage::maybe(...)); + $memory = $parts + ->get(3) + ->map(static fn($value) => (float) $value) + ->flatMap(Memory::maybe(...)); + $command = $parts + ->get(4) + ->keep(Is::string()->nonEmpty()->asPredicate()) + ->map(Command::of(...)); return Maybe::all($user, $pid, $percentage, $memory, $command) - ->map(fn(string $user, string $pid, string $percentage, string $memory, string $command) => new Process( - new Pid((int) $pid), - new User($user), - new Percentage((float) $percentage), - new Memory((float) $memory), + ->map(fn(User $user, Pid $pid, Percentage $percentage, Memory $memory, Command $command) => Process::of( + $pid, + $user, + $percentage, + $memory, $this->clock->at($start, Format::of('D M j H:i:s Y')), - new Command($command), + $command, )); }); diff --git a/src/Servers/Linux.php b/src/Servers/Linux.php index a39a0e4..110754f 100644 --- a/src/Servers/Linux.php +++ b/src/Servers/Linux.php @@ -6,7 +6,6 @@ use Innmind\Server\Status\{ Server, Server\Processes, - Server\LoadAverage, Facade\Cpu\LinuxFacade as CpuFacade, Facade\Memory\LinuxFacade as MemoryFacade, Facade\LoadAverage\PhpFacade as LoadAverageFacade, @@ -55,7 +54,7 @@ public function processes(): Processes } #[\Override] - public function loadAverage(): LoadAverage + public function loadAverage(): Attempt { return ($this->loadAverage)(); } diff --git a/src/Servers/Logger.php b/src/Servers/Logger.php index 14ea4ed..d74c917 100644 --- a/src/Servers/Logger.php +++ b/src/Servers/Logger.php @@ -6,7 +6,6 @@ use Innmind\Server\Status\{ Server, Server\Processes, - Server\LoadAverage, Server\Disk, }; use Innmind\Url\Path; @@ -66,16 +65,17 @@ public function processes(): Processes } #[\Override] - public function loadAverage(): LoadAverage + public function loadAverage(): Attempt { - $loadAverage = $this->server->loadAverage(); - $this->logger->debug('Load average: {one} {five} {fifteen}', [ - 'one' => $loadAverage->lastMinute(), - 'five' => $loadAverage->lastFiveMinutes(), - 'fifteen' => $loadAverage->lastFifteenMinutes(), - ]); + return $this->server->loadAverage()->map(function($loadAverage) { + $this->logger->debug('Load average: {one} {five} {fifteen}', [ + 'one' => $loadAverage->lastMinute(), + 'five' => $loadAverage->lastFiveMinutes(), + 'fifteen' => $loadAverage->lastFifteenMinutes(), + ]); - return $loadAverage; + return $loadAverage; + }); } #[\Override] diff --git a/src/Servers/OSX.php b/src/Servers/OSX.php index edf3901..a50b2ab 100644 --- a/src/Servers/OSX.php +++ b/src/Servers/OSX.php @@ -6,7 +6,6 @@ use Innmind\Server\Status\{ Server, Server\Processes, - Server\LoadAverage, Facade\Cpu\OSXFacade as CpuFacade, Facade\Memory\OSXFacade as MemoryFacade, Facade\LoadAverage\PhpFacade as LoadAverageFacade, @@ -56,7 +55,7 @@ public function processes(): Processes } #[\Override] - public function loadAverage(): LoadAverage + public function loadAverage(): Attempt { return ($this->loadAverage)(); } diff --git a/tests/Facade/LoadAverage/PhpFacadeTest.php b/tests/Facade/LoadAverage/PhpFacadeTest.php index cd47abb..7c293ee 100644 --- a/tests/Facade/LoadAverage/PhpFacadeTest.php +++ b/tests/Facade/LoadAverage/PhpFacadeTest.php @@ -15,6 +15,6 @@ public function testInterface() { $facade = new PhpFacade; - $this->assertInstanceOf(LoadAverage::class, $facade()); + $this->assertInstanceOf(LoadAverage::class, $facade()->unwrap()); } } diff --git a/tests/Facade/Memory/LinuxFacadeTest.php b/tests/Facade/Memory/LinuxFacadeTest.php index 25147e4..deefca5 100644 --- a/tests/Facade/Memory/LinuxFacadeTest.php +++ b/tests/Facade/Memory/LinuxFacadeTest.php @@ -36,10 +36,7 @@ public function testInterface() $facade = new LinuxFacade($this->server->processes()); - $this->assertInstanceOf(Memory::class, $facade()->match( - static fn($memory) => $memory, - static fn() => null, - )); + $this->assertInstanceOf(Memory::class, $facade()->unwrap()); } public function testReturnNohingWhenInfoNotAccessible() diff --git a/tests/Server/Cpu/CoresTest.php b/tests/Server/Cpu/CoresTest.php index c68ee35..2191f83 100644 --- a/tests/Server/Cpu/CoresTest.php +++ b/tests/Server/Cpu/CoresTest.php @@ -3,26 +3,16 @@ namespace Tests\Innmind\Server\Status\Server\Cpu; -use Innmind\Server\Status\{ - Server\Cpu\Cores, - Exception\DomainException, -}; +use Innmind\Server\Status\Server\Cpu\Cores; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class CoresTest extends TestCase { public function testInterface() { - $cores = new Cores(8); + $cores = Cores::of(8); $this->assertSame(8, $cores->toInt()); $this->assertSame('8', $cores->toString()); } - - public function testThrowWhenCoresLowerThanOne() - { - $this->expectException(DomainException::class); - - new Cores(0); - } } diff --git a/tests/Server/Cpu/PercentageTest.php b/tests/Server/Cpu/PercentageTest.php index d6a598c..7ac0270 100644 --- a/tests/Server/Cpu/PercentageTest.php +++ b/tests/Server/Cpu/PercentageTest.php @@ -3,26 +3,28 @@ namespace Tests\Innmind\Server\Status\Server\Cpu; -use Innmind\Server\Status\{ - Server\Cpu\Percentage, - Exception\OutOfBoundsPercentage, -}; +use Innmind\Server\Status\Server\Cpu\Percentage; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PercentageTest extends TestCase { public function testInterface() { - $percentage = new Percentage(42.24); + $percentage = Percentage::maybe(42.24)->match( + static fn($percentage) => $percentage, + static fn() => null, + ); + $this->assertNotNull($percentage); $this->assertSame(42.24, $percentage->toFloat()); $this->assertSame('42.24%', $percentage->toString()); } - public function testThrowWhenPercentageLowerThanZero() + public function testReturnNothingWhenPercentageLowerThanZero() { - $this->expectException(OutOfBoundsPercentage::class); - - new Percentage(-1); + $this->assertNull(Percentage::maybe(-1)->match( + static fn($percentage) => $percentage, + static fn() => null, + )); } } diff --git a/tests/Server/CpuTest.php b/tests/Server/CpuTest.php index fb387eb..3e7dab3 100644 --- a/tests/Server/CpuTest.php +++ b/tests/Server/CpuTest.php @@ -14,11 +14,20 @@ class CpuTest extends TestCase { public function testInterface() { - $cpu = new Cpu( - $user = new Percentage(31), - $system = new Percentage(33), - $idle = new Percentage(36), - $cores = new Cores(4), + $cpu = Cpu::of( + $user = Percentage::maybe(31)->match( + static fn($percentage) => $percentage, + static fn() => throw new \Exception('Should be valid'), + ), + $system = Percentage::maybe(33)->match( + static fn($percentage) => $percentage, + static fn() => throw new \Exception('Should be valid'), + ), + $idle = Percentage::maybe(36)->match( + static fn($percentage) => $percentage, + static fn() => throw new \Exception('Should be valid'), + ), + $cores = Cores::of(4), ); $this->assertSame($user, $cpu->user()); diff --git a/tests/Server/Disk/LoggerDiskTest.php b/tests/Server/Disk/LoggerDiskTest.php index bf2adb2..3c377c4 100644 --- a/tests/Server/Disk/LoggerDiskTest.php +++ b/tests/Server/Disk/LoggerDiskTest.php @@ -40,7 +40,7 @@ public function testGet() { $disk = new LoggerDisk($this->disk(), new NullLogger); - $this->assertInstanceOf(Volume::class, $disk->get(new MountPoint('/'))->match( + $this->assertInstanceOf(Volume::class, $disk->get(MountPoint::of('/'))->match( static fn($volume) => $volume, static fn() => null, )); diff --git a/tests/Server/Disk/UnixDiskTest.php b/tests/Server/Disk/UnixDiskTest.php index 30503e2..88fe325 100644 --- a/tests/Server/Disk/UnixDiskTest.php +++ b/tests/Server/Disk/UnixDiskTest.php @@ -56,7 +56,7 @@ public function testGet() { $volume = $this ->disk - ->get(new MountPoint('/')) + ->get(MountPoint::of('/')) ->match( static fn($volume) => $volume, static fn() => null, diff --git a/tests/Server/Disk/Volume/MountPointTest.php b/tests/Server/Disk/Volume/MountPointTest.php index 64451d0..842620c 100644 --- a/tests/Server/Disk/Volume/MountPointTest.php +++ b/tests/Server/Disk/Volume/MountPointTest.php @@ -3,25 +3,15 @@ namespace Tests\Innmind\Server\Status\Server\Disk\Volume; -use Innmind\Server\Status\{ - Server\Disk\Volume\MountPoint, - Exception\EmptyPathNotAllowed, -}; +use Innmind\Server\Status\Server\Disk\Volume\MountPoint; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MountPointTest extends TestCase { public function testInterface() { - $mountPoint = new MountPoint('foo'); + $mountPoint = MountPoint::of('foo'); $this->assertSame('foo', $mountPoint->toString()); } - - public function testThrowWhenEmptyMountPoint() - { - $this->expectException(EmptyPathNotAllowed::class); - - new MountPoint(''); - } } diff --git a/tests/Server/Disk/Volume/UsageTest.php b/tests/Server/Disk/Volume/UsageTest.php index 3ceae01..c74da00 100644 --- a/tests/Server/Disk/Volume/UsageTest.php +++ b/tests/Server/Disk/Volume/UsageTest.php @@ -3,33 +3,36 @@ namespace Tests\Innmind\Server\Status\Server\Disk\Volume; -use Innmind\Server\Status\{ - Server\Disk\Volume\Usage, - Exception\OutOfBoundsPercentage, -}; +use Innmind\Server\Status\Server\Disk\Volume\Usage; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class UsageTest extends TestCase { public function testInterface() { - $usage = new Usage(42.24); + $usage = Usage::maybe(42.24)->match( + static fn($usage) => $usage, + static fn() => null, + ); + $this->assertNotNull($usage); $this->assertSame(42.24, $usage->toFloat()); $this->assertSame('42.24%', $usage->toString()); } - public function testThrowWhenUsageLowerThanZero() + public function testReturnNothingWhenUsageLowerThanZero() { - $this->expectException(OutOfBoundsPercentage::class); - - new Usage(-1); + $this->assertNull(Usage::maybe(-1)->match( + static fn($usage) => $usage, + static fn() => null, + )); } - public function testThrowWhenUsageHigherThanHundred() + public function testReturnNothingWhenUsageHigherThanHundred() { - $this->expectException(OutOfBoundsPercentage::class); - - new Usage(101); + $this->assertNull(Usage::maybe(101)->match( + static fn($usage) => $usage, + static fn() => null, + )); } } diff --git a/tests/Server/Disk/VolumeTest.php b/tests/Server/Disk/VolumeTest.php index d121e89..46ad4d6 100644 --- a/tests/Server/Disk/VolumeTest.php +++ b/tests/Server/Disk/VolumeTest.php @@ -15,12 +15,15 @@ class VolumeTest extends TestCase { public function testInterface() { - $volume = new Volume( - $mount = new MountPoint('/'), - $size = new Bytes(42), - $available = new Bytes(42), - $used = new Bytes(42), - $usage = new Usage(100), + $volume = Volume::of( + $mount = MountPoint::of('/'), + $size = Bytes::of(42), + $available = Bytes::of(42), + $used = Bytes::of(42), + $usage = Usage::maybe(100)->match( + static fn($usage) => $usage, + static fn() => throw new \Exception('Should be valid'), + ), ); $this->assertSame($mount, $volume->mountPoint()); diff --git a/tests/Server/LoadAverageTest.php b/tests/Server/LoadAverageTest.php index def5262..16e4879 100644 --- a/tests/Server/LoadAverageTest.php +++ b/tests/Server/LoadAverageTest.php @@ -3,41 +3,45 @@ namespace Tests\Innmind\Server\Status\Server; -use Innmind\Server\Status\{ - Server\LoadAverage, - Exception\LoadAverageCannotBeNegative, -}; +use Innmind\Server\Status\Server\LoadAverage; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class LoadAverageTest extends TestCase { public function testInterface() { - $load = new LoadAverage(1, 5, 15); + $load = LoadAverage::maybe(1, 5, 15)->match( + static fn($load) => $load, + static fn() => null, + ); + $this->assertNotNull($load); $this->assertSame(1.0, $load->lastMinute()); $this->assertSame(5.0, $load->lastFiveMinutes()); $this->assertSame(15.0, $load->lastFifteenMinutes()); } - public function testThrowWhenNegativeLastMinuteLoad() + public function testReturnNothingWhenNegativeLastMinuteLoad() { - $this->expectException(LoadAverageCannotBeNegative::class); - - new LoadAverage(-1, 5, 15); + $this->assertNull(LoadAverage::maybe(-1, 5, 15)->match( + static fn($load) => $load, + static fn() => null, + )); } - public function testThrowWhenNegativeLastFiveMinuteLoad() + public function testReturnNothingWhenNegativeLastFiveMinuteLoad() { - $this->expectException(LoadAverageCannotBeNegative::class); - - new LoadAverage(1, -5, 15); + $this->assertNull(LoadAverage::maybe(1, -5, 15)->match( + static fn($load) => $load, + static fn() => null, + )); } - public function testThrowWhenNegativeLastFifteenMinuteLoad() + public function testReturnNothingWhenNegativeLastFifteenMinuteLoad() { - $this->expectException(LoadAverageCannotBeNegative::class); - - new LoadAverage(1, 5, -15); + $this->assertNull(LoadAverage::maybe(1, 5, -15)->match( + static fn($load) => $load, + static fn() => null, + )); } } diff --git a/tests/Server/Memory/BytesTest.php b/tests/Server/Memory/BytesTest.php index 46230ed..79690a4 100644 --- a/tests/Server/Memory/BytesTest.php +++ b/tests/Server/Memory/BytesTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Server\Status\Server\Memory; -use Innmind\Server\Status\{ - Server\Memory\Bytes, - Exception\BytesCannotBeNegative, -}; +use Innmind\Server\Status\Server\Memory\Bytes; use Innmind\BlackBox\PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\DataProvider; @@ -15,23 +12,16 @@ class BytesTest extends TestCase #[DataProvider('steps')] public function testInterface($value, $expected) { - $bytes = new Bytes($value); + $bytes = Bytes::of($value); $this->assertSame($value, $bytes->toInt()); $this->assertSame($expected, $bytes->toString()); } - public function testThrowWhenNegative() - { - $this->expectException(BytesCannotBeNegative::class); - - new Bytes(-1); - } - #[DataProvider('strings')] public function testFromString($string, $expected) { - $bytes = Bytes::of($string)->match( + $bytes = Bytes::maybe($string)->match( static fn($bytes) => $bytes, static fn() => null, ); @@ -42,7 +32,7 @@ public function testFromString($string, $expected) public function testReturnNothingWhenUnknownFormat() { - $this->assertNull(Bytes::of('42Br')->match( + $this->assertNull(Bytes::maybe('42Br')->match( static fn($bytes) => $bytes, static fn() => null, )); @@ -51,7 +41,7 @@ public function testReturnNothingWhenUnknownFormat() #[DataProvider('invalidStrings')] public function testReturnNothingWhenStringTooShort($string) { - $this->assertNull(Bytes::of($string)->match( + $this->assertNull(Bytes::maybe($string)->match( static fn($bytes) => $bytes, static fn() => null, )); diff --git a/tests/Server/MemoryTest.php b/tests/Server/MemoryTest.php index e33c392..d7a1df9 100644 --- a/tests/Server/MemoryTest.php +++ b/tests/Server/MemoryTest.php @@ -13,12 +13,12 @@ class MemoryTest extends TestCase { public function testInterface() { - $memory = new Memory( - $total = new Bytes(42), - $active = new Bytes(42), - $free = new Bytes(42), - $swap = new Bytes(42), - $used = new Bytes(42), + $memory = Memory::of( + $total = Bytes::of(42), + $active = Bytes::of(42), + $free = Bytes::of(42), + $swap = Bytes::of(42), + $used = Bytes::of(42), ); $this->assertSame($total, $memory->total()); diff --git a/tests/Server/Process/CommandTest.php b/tests/Server/Process/CommandTest.php index 0dc8934..dd2244a 100644 --- a/tests/Server/Process/CommandTest.php +++ b/tests/Server/Process/CommandTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\{ - Server\Process\Command, - Exception\EmptyCommandNotAllowed, -}; +use Innmind\Server\Status\Server\Process\Command; use Innmind\Immutable\RegExp; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -14,23 +11,16 @@ class CommandTest extends TestCase { public function testInterface() { - $command = new Command('foo'); + $command = Command::of('foo'); $this->assertSame('foo', $command->toString()); } public function testMatches() { - $command = new Command('foo'); + $command = Command::of('foo'); $this->assertTrue($command->matches(RegExp::of('/^foo/'))); $this->assertFalse($command->matches(RegExp::of('/bar/'))); } - - public function testThrowWhenEmptyCommand() - { - $this->expectException(EmptyCommandNotAllowed::class); - - new Command(''); - } } diff --git a/tests/Server/Process/MemoryTest.php b/tests/Server/Process/MemoryTest.php index 6f9999d..cd9fd86 100644 --- a/tests/Server/Process/MemoryTest.php +++ b/tests/Server/Process/MemoryTest.php @@ -3,33 +3,36 @@ namespace Tests\Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\{ - Server\Process\Memory, - Exception\OutOfBoundsPercentage, -}; +use Innmind\Server\Status\Server\Process\Memory; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MemoryTest extends TestCase { public function testInterface() { - $memory = new Memory(42.24); + $memory = Memory::maybe(42.24)->match( + static fn($memory) => $memory, + static fn() => null, + ); + $this->assertNotNull($memory); $this->assertSame(42.24, $memory->toFloat()); $this->assertSame('42.24%', $memory->toString()); } - public function testThrowWhenMemoryLowerThanZero() + public function testReturnNothingWhenMemoryLowerThanZero() { - $this->expectException(OutOfBoundsPercentage::class); - - new Memory(-1); + $this->assertNull(Memory::maybe(-1)->match( + static fn($memory) => $memory, + static fn() => null, + )); } - public function testThrowWhenMemoryHigherThanHundred() + public function testReturnNothingWhenMemoryHigherThanHundred() { - $this->expectException(OutOfBoundsPercentage::class); - - new Memory(101); + $this->assertNull(Memory::maybe(101)->match( + static fn($memory) => $memory, + static fn() => null, + )); } } diff --git a/tests/Server/Process/PidTest.php b/tests/Server/Process/PidTest.php index fd9e15f..63641bc 100644 --- a/tests/Server/Process/PidTest.php +++ b/tests/Server/Process/PidTest.php @@ -3,26 +3,16 @@ namespace Tests\Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\{ - Server\Process\Pid, - Exception\LowestPidPossibleIsOne, -}; +use Innmind\Server\Status\Server\Process\Pid; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PidTest extends TestCase { public function testInterface() { - $pid = new Pid(42); + $pid = Pid::of(42); $this->assertSame(42, $pid->toInt()); $this->assertSame('42', $pid->toString()); } - - public function testThrowWhenPidTooLow() - { - $this->expectException(LowestPidPossibleIsOne::class); - - new Pid(0); - } } diff --git a/tests/Server/Process/UserTest.php b/tests/Server/Process/UserTest.php index 9421623..180ea2c 100644 --- a/tests/Server/Process/UserTest.php +++ b/tests/Server/Process/UserTest.php @@ -3,25 +3,15 @@ namespace Tests\Innmind\Server\Status\Server\Process; -use Innmind\Server\Status\{ - Server\Process\User, - Exception\EmptyUserNotAllowed, -}; +use Innmind\Server\Status\Server\Process\User; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class UserTest extends TestCase { public function testInterface() { - $user = new User('foo'); + $user = User::of('foo'); $this->assertSame('foo', $user->toString()); } - - public function testThrowWhenEmptyUser() - { - $this->expectException(EmptyUserNotAllowed::class); - - new User(''); - } } diff --git a/tests/Server/ProcessTest.php b/tests/Server/ProcessTest.php index a5c6b51..3da229c 100644 --- a/tests/Server/ProcessTest.php +++ b/tests/Server/ProcessTest.php @@ -27,13 +27,19 @@ public function testInterface() $this ->forAll(PointInTime::any()) ->then(function($pointInTime) { - $process = new Process( - $pid = new Pid(1), - $user = new User('root'), - $cpu = new Percentage(42), - $memory = new Memory(42), + $process = Process::of( + $pid = Pid::of(1), + $user = User::of('root'), + $cpu = Percentage::maybe(42)->match( + static fn($percentage) => $percentage, + static fn() => throw new \Exception('Should be valid'), + ), + $memory = Memory::maybe(42)->match( + static fn($memory) => $memory, + static fn() => throw new \Exception('Should be valid'), + ), $start = Maybe::just($pointInTime), - $command = new Command('/sbin/launchd'), + $command = Command::of('/sbin/launchd'), ); $this->assertSame($pid, $process->pid()); diff --git a/tests/Server/Processes/LoggerProcessesTest.php b/tests/Server/Processes/LoggerProcessesTest.php index 5815672..c75bf7e 100644 --- a/tests/Server/Processes/LoggerProcessesTest.php +++ b/tests/Server/Processes/LoggerProcessesTest.php @@ -40,7 +40,7 @@ public function testGet() { $processes = new LoggerProcesses($this->processes(), new NullLogger); - $this->assertInstanceOf(Process::class, $processes->get(new Pid(1))->match( + $this->assertInstanceOf(Process::class, $processes->get(Pid::of(1))->match( static fn($process) => $process, static fn() => null, )); diff --git a/tests/Server/Processes/UnixProcessesTest.php b/tests/Server/Processes/UnixProcessesTest.php index 6646658..79ec228 100644 --- a/tests/Server/Processes/UnixProcessesTest.php +++ b/tests/Server/Processes/UnixProcessesTest.php @@ -76,7 +76,7 @@ public function testGet() { $process = $this ->processes - ->get(new Pid(1)) + ->get(Pid::of(1)) ->match( static fn($process) => $process, static fn() => null, @@ -91,7 +91,7 @@ public function testReturnNothingWhenProcessDoesntExist() $this->assertNull( $this ->processes - ->get(new Pid(42424)) + ->get(Pid::of(42424)) ->match( static fn($process) => $process, static fn() => null, diff --git a/tests/Servers/LinuxTest.php b/tests/Servers/LinuxTest.php index 3cc8fdb..c9dec45 100644 --- a/tests/Servers/LinuxTest.php +++ b/tests/Servers/LinuxTest.php @@ -73,10 +73,7 @@ public function testMemory() $this ->server ->memory() - ->match( - static fn($memory) => $memory, - static fn() => null, - ), + ->unwrap(), ); } @@ -87,7 +84,7 @@ public function testProcesses() public function testLoadAverage() { - $this->assertInstanceOf(LoadAverage::class, $this->server->loadAverage()); + $this->assertInstanceOf(LoadAverage::class, $this->server->loadAverage()->unwrap()); } public function testDisk() diff --git a/tests/Servers/LoggerTest.php b/tests/Servers/LoggerTest.php index f62b0ba..8a3b3b9 100644 --- a/tests/Servers/LoggerTest.php +++ b/tests/Servers/LoggerTest.php @@ -66,7 +66,7 @@ public function testLoadAverage() { $server = new Logger($this->server(), new NullLogger); - $this->assertInstanceOf(LoadAverage::class, $server->loadAverage()); + $this->assertInstanceOf(LoadAverage::class, $server->loadAverage()->unwrap()); } public function testDisk() diff --git a/tests/Servers/OSXTest.php b/tests/Servers/OSXTest.php index 1285102..23d18a2 100644 --- a/tests/Servers/OSXTest.php +++ b/tests/Servers/OSXTest.php @@ -86,7 +86,7 @@ public function testProcesses() public function testLoadAverage() { - $this->assertInstanceOf(LoadAverage::class, $this->server->loadAverage()); + $this->assertInstanceOf(LoadAverage::class, $this->server->loadAverage()->unwrap()); } public function testDisk()