Skip to content
Open
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
33 changes: 19 additions & 14 deletions src/Factories/ImmutableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use function iterator_to_array;

/**
* @template TClass
* @template TClass of object
*/
abstract class ImmutableFactory
{
Expand All @@ -30,19 +30,25 @@ final public function __construct(

/**
* @param array<string, mixed> $state
* @return static<TClass>
* @return static
*/
public function state(array $state): static
{
return new static($this->faker, [...$this->state, ...$state], $this->count);
/** @var static<TClass> $next */
$next = new static($this->faker, [...$this->state, ...$state], $this->count);

return $next;
}

/**
* @return static<TClass>
* @return static
*/
public function times(int $count): static
{
return new static($this->faker, $this->state, $count);
/** @var static<TClass> $next */
$next = new static($this->faker, $this->state, $count);

return $next;
}

/**
Expand All @@ -66,10 +72,9 @@ private function resolveValue(mixed $value): mixed
return $value;
}

return match ($value->count > 1) {
true => $value->makeMany(),
default => $value->makeOne(),
};
return $value->count > 1
? $value->makeMany()
: $value->makeOne();
}

/**
Expand All @@ -87,7 +92,7 @@ public function raw(array $attributes = []): array

/**
* @param array<string, mixed> $attributes
* @return array<int, TClass>
* @return list<array<string, mixed>>
*/
public function rawMany(array $attributes = []): array
{
Expand All @@ -107,14 +112,14 @@ public function rawCollection(array $attributes = []): Collection
* @param array<string, mixed> $attributes
* @return TClass
*/
public function makeOne(array $attributes = []): mixed
public function makeOne(array $attributes = []): object
{
return $this->instance($this->raw($attributes));
}

/**
* @param array<string, mixed> $attributes
* @return array<TClass>
* @return list<TClass>
*/
public function makeMany(array $attributes = []): array
{
Expand All @@ -127,6 +132,6 @@ public function makeMany(array $attributes = []): array
*/
public function makeCollection(array $attributes = []): Collection
{
return Collection::times($this->count, fn (): mixed => $this->makeOne($attributes));
return Collection::times($this->count, fn (): object => $this->makeOne($attributes));
}
}
}
2 changes: 1 addition & 1 deletion src/Factories/InstanceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use ReflectionProperty;

/**
* @template TClass
* @template TClass of object
*/
final readonly class InstanceFactory
{
Expand Down
76 changes: 49 additions & 27 deletions src/PHPUnit/DataProviders/EnumCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
*/
final readonly class EnumCase
{
/**
* @var array<int, TValue>
*/
/** @var TValue */
public UnitEnum $instance;

/** @var array<int, TValue> */
private array $options;

public function __construct(
/* @var TValue */
public UnitEnum $instance,
/* @param array<int, TValue> ...$options */
UnitEnum ...$options,
) {
/**
* @param TValue $instance
* @param TValue ...$options
*/
public function __construct(UnitEnum $instance, UnitEnum ...$options)
{
in_array($instance, $options, true) or throw new ValueError('Options should contain the given instance.');

foreach ($options as $option) {
Expand All @@ -40,6 +41,7 @@ public function __construct(
);
}

$this->instance = $instance;
$this->options = $options;
}

Expand All @@ -51,51 +53,71 @@ public function differentInstance(): UnitEnum
count($this->options) > 1 or throw new LogicException(
self::class . ' was configured with a single option and can therefore not return a different instance.',
);
$differentOptions = array_filter($this->options, fn (UnitEnum $option): bool => $option !== $this->instance);

$differentOptions = array_filter(
$this->options,
fn (UnitEnum $option): bool => $option !== $this->instance,
);

return $differentOptions[array_rand($differentOptions)];
}

/**
* @param class-string<TValue> $enumFQCN
* @return iterable<array<self<TValue>>>
* @return iterable<array{self<TValue>}>
*/
public static function cases(string $enumFQCN): iterable
{
/** @var ReflectionEnumUnitCase $case */
foreach (new ReflectionEnum($enumFQCN)->getCases() as $case) {
// @phpstan-ignore generator.valueType
yield "$enumFQCN::$case->name" => [
new self($case->getValue(), ...$enumFQCN::cases()),
];
/** @var TValue $value */
$value = $case->getValue();
/** @var array<int, TValue> $all */
$all = $enumFQCN::cases();
/** @var self<TValue> $enumCase */
$enumCase = new self($value, ...$all);

yield "$enumFQCN::{$case->name}" => [$enumCase];
}
}

/**
* @param TValue ...$options
* @return iterable<array<self<TValue>>>
* @return iterable<array{self<TValue>}>
*/
public static function options(UnitEnum ...$options): iterable
{
/** @var array<int, TValue> $options */
foreach ($options as $case) {
yield "$case->name" => [new self($case, ...$options)];
/** @var TValue $case */
/** @var self<TValue> $enumCase */
$enumCase = new self($case, ...$options);
yield "{$case->name}" => [$enumCase];
}
}

/**
* @param class-string<TValue> $enumFQCN
* @return iterable<array<self<TValue>>>
* @param TValue ...$except
* @return iterable<array{self<TValue>}>
*/
public static function except(string $enumFQCN, UnitEnum ...$except): iterable
{
$options = array_map(function (ReflectionEnumUnitCase $reflection) use ($except): ?UnitEnum {
$case = $reflection->getValue();
/** @var class-string<TValue> $enumFQCN */
/** @var array<int, TValue> $except */
$options = array_map(
function (ReflectionEnumUnitCase $reflection) use ($except): ?UnitEnum {
$case = $reflection->getValue();

return in_array($case, $except, true) ? null : $case;
},
new ReflectionEnum($enumFQCN)->getCases(),
);

return match (in_array($case, $except, true)) {
true => null,
false => $case,
};
}, new ReflectionEnum($enumFQCN)->getCases());
/** @var array<int, TValue|null> $options */
/** @var array<int, TValue> $filtered */
$filtered = array_filter($options);

yield from self::options(...array_filter($options));
yield from self::options(...$filtered);
}
}
}