Skip to content

Commit

Permalink
Merge pull request #8 from feature-ninja/feature/named-and-lazy-values
Browse files Browse the repository at this point in the history
Add support for named and lazy values
  • Loading branch information
rojtjo authored Sep 11, 2024
2 parents 5ea92bb + 735753d commit b3c8082
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@ public static function create(array $matrix): Generator

foreach (self::recurse($matrix, []) as $result) {
$key = implode(' ', array_map(
fn (mixed $value, int $i) => sprintf(
fn (Value $value, int $i) => sprintf(
'%s=%s',
$names[$i],
json_encode($value),
$value->label,
),
$result,
array_keys($result),
));

$result = array_map(
fn (Value $value) => $value->resolve(),
$result,
);

yield $key => $result;
}
}

/**
* @param array<int, mixed> $matrix
* @param array<int, mixed> $head
* @return Generator<int, array<int, mixed>>
* @return Generator<int, array<int, Value>>
*/
private static function recurse(array $matrix, array $head): Generator
{
Expand All @@ -46,6 +51,10 @@ private static function recurse(array $matrix, array $head): Generator
}

foreach (array_shift($matrix) as $value) {
if (! $value instanceof Value) {
$value = Value::wrap($value);
}

foreach (self::recurse($matrix, [...$head, $value]) as $result) {
yield $result;
}
Expand Down
26 changes: 16 additions & 10 deletions src/MatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ public function cases(): void
{
$result = Matrix::create([
'php' => [8.3, 8.4],
'node' => [18, 20],
'postgres' => ['v15', 'v16'],
'node' => [
Value::of('18', 18),
Value::of('20', 20),
],
'postgres' => [
Value::of('v15', '15.0'),
Value::lazy('v16', fn () => '16.0'),
],
]);

$this->assertSame([
'php=8.3 node=18 postgres="v15"' => [8.3, 18, 'v15'],
'php=8.3 node=18 postgres="v16"' => [8.3, 18, 'v16'],
'php=8.3 node=20 postgres="v15"' => [8.3, 20, 'v15'],
'php=8.3 node=20 postgres="v16"' => [8.3, 20, 'v16'],
'php=8.4 node=18 postgres="v15"' => [8.4, 18, 'v15'],
'php=8.4 node=18 postgres="v16"' => [8.4, 18, 'v16'],
'php=8.4 node=20 postgres="v15"' => [8.4, 20, 'v15'],
'php=8.4 node=20 postgres="v16"' => [8.4, 20, 'v16'],
'php=8.3 node=18 postgres=v15' => [8.3, 18, '15.0'],
'php=8.3 node=18 postgres=v16' => [8.3, 18, '16.0'],
'php=8.3 node=20 postgres=v15' => [8.3, 20, '15.0'],
'php=8.3 node=20 postgres=v16' => [8.3, 20, '16.0'],
'php=8.4 node=18 postgres=v15' => [8.4, 18, '15.0'],
'php=8.4 node=18 postgres=v16' => [8.4, 18, '16.0'],
'php=8.4 node=20 postgres=v15' => [8.4, 20, '15.0'],
'php=8.4 node=20 postgres=v16' => [8.4, 20, '16.0'],
], iterator_to_array($result));
}
}
38 changes: 38 additions & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace FeatureNinja\TestingMatrix;

use Closure;

final readonly class Value
{
public function __construct(
public string $label,
public Closure $resolve,
) {}

public static function of(string $label, mixed $value): self
{
return new self(
$label,
fn () => $value,
);
}

public static function wrap(mixed $value): self
{
return self::of(json_encode($value), $value);
}

public static function lazy(string $label, callable $resolve): self
{
return new self($label, $resolve(...));
}

public function resolve(): mixed
{
return ($this->resolve)();
}
}

0 comments on commit b3c8082

Please sign in to comment.