Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Added

- `Innmind\Server\Control\Server\Process\Builder`, internal tool to mock processes

## 7.0.0 - 2026-01-31

### Changed
Expand Down
162 changes: 162 additions & 0 deletions src/Server/Process/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
declare(strict_types = 1);

namespace Innmind\Server\Control\Server\Process;

use Innmind\Server\Control\Server\{
Process,
Process\Output\Chunk,
Process\Output\Type,
};
use Innmind\Immutable\{
Sequence,
Str,
};

/**
* @internal
*/
final class Builder
{
/**
* @psalm-mutation-free
*
* @param ?int<2, max> $pid
*/
private function __construct(
private ?int $pid,
private Success|Signaled|TimedOut|Failed $result,
) {
}

/**
* @internal
* @psalm-pure
*
* @param int<2, max> $pid
*/
#[\NoDiscard]
public static function foreground(int $pid): self
{
return new self($pid, new Success(Sequence::of()));
}

/**
* @internal
* @psalm-pure
*/
#[\NoDiscard]
public static function background(): self
{
return new self(null, new Success(Sequence::of()));
}

/**
* @psalm-mutation-free
*
* @param Sequence<Chunk>|list<array{string, 'output'|'error'}> $output
*/
#[\NoDiscard]
public function success(Sequence|array|null $output = null): self
{
return new self(
$this->pid,
new Success(self::output($output)),
);
}

/**
* @psalm-mutation-free
*
* @param Sequence<Chunk>|list<array{string, 'output'|'error'}> $output
*/
#[\NoDiscard]
public function signaled(Sequence|array|null $output = null): self
{
return new self(
$this->pid,
new Signaled(self::output($output)),
);
}

/**
* @psalm-mutation-free
*
* @param Sequence<Chunk>|list<array{string, 'output'|'error'}> $output
*/
#[\NoDiscard]
public function timedOut(Sequence|array|null $output = null): self
{
return new self(
$this->pid,
new TimedOut(self::output($output)),
);
}

/**
* @psalm-mutation-free
*
* @param int<1, 255> $exitCode
* @param Sequence<Chunk>|list<array{string, 'output'|'error'}> $output
*/
#[\NoDiscard]
public function failed(int $exitCode = 1, Sequence|array|null $output = null): self
{
return new self(
$this->pid,
new Failed(
new ExitCode($exitCode),
self::output($output),
),
);
}

/**
* @internal
*/
#[\NoDiscard]
public function build(): Process
{
$pid = $this->pid;
$result = $this->result;

/**
* This a trick to not expose any mock contructor on the Process class.
*
* @psalm-suppress PossiblyNullFunctionCall
* @psalm-suppress MixedReturnStatement
* @psalm-suppress InaccessibleMethod
*/
return (\Closure::bind(
static fn() => new Process(new Mock($pid, $result)),
null,
Process::class,
))();
}

/**
* @psalm-pure
*
* @param Sequence<Chunk>|list<array{string, 'output'|'error'}> $output
*
* @return Sequence<Chunk>
*/
private static function output(Sequence|array|null $output = null): Sequence
{
if (\is_null($output)) {
return Sequence::of();
}

if (\is_array($output)) {
return Sequence::of(...$output)->map(static fn($pair) => Chunk::of(
Str::of($pair[0]),
match ($pair[1]) {
'output' => Type::output,
'error' => Type::error,
},
));
}

return $output;
}
}
3 changes: 3 additions & 0 deletions src/Server/Process/Failed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use Innmind\Immutable\Sequence;

/**
* @psalm-immutable
*/
final class Failed
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Server/Process/Signaled.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use Innmind\Immutable\Sequence;

/**
* @psalm-immutable
*/
final class Signaled
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Server/Process/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use Innmind\Immutable\Sequence;

/**
* @psalm-immutable
*/
final class Success
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Server/Process/TimedOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use Innmind\Immutable\Sequence;

/**
* @psalm-immutable
*/
final class TimedOut
{
/**
Expand Down
Loading