mirror of
https://github.com/pestphp/pest.git
synced 2026-03-08 00:37:22 +01:00
Merge branch 'master' into feat/retry
This commit is contained in:
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Concerns;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
@ -22,7 +21,7 @@ trait Extendable
|
||||
/**
|
||||
* Register a new extend.
|
||||
*/
|
||||
public static function extend(string $name, Closure $extend): void
|
||||
public function extend(string $name, Closure $extend): void
|
||||
{
|
||||
static::$extends[$name] = $extend;
|
||||
}
|
||||
@ -34,21 +33,4 @@ trait Extendable
|
||||
{
|
||||
return array_key_exists($name, static::$extends);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically handle calls to the class.
|
||||
*
|
||||
* @param array<int, mixed> $parameters
|
||||
*/
|
||||
public function __call(string $method, array $parameters): mixed
|
||||
{
|
||||
if (!static::hasExtend($method)) {
|
||||
throw new BadMethodCallException("$method is not a callable method name.");
|
||||
}
|
||||
|
||||
/** @var Closure $extend */
|
||||
$extend = static::$extends[$method]->bindTo($this, static::class);
|
||||
|
||||
return $extend(...$parameters);
|
||||
}
|
||||
}
|
||||
|
||||
65
src/Concerns/Pipeable.php
Normal file
65
src/Concerns/Pipeable.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
trait Pipeable
|
||||
{
|
||||
/**
|
||||
* The list of pipes.
|
||||
*
|
||||
* @var array<string, array<Closure(Closure, mixed ...$arguments): void>>
|
||||
*/
|
||||
private static array $pipes = [];
|
||||
|
||||
/**
|
||||
* Register a pipe to be applied before an expectation is checked.
|
||||
*/
|
||||
public function pipe(string $name, Closure $pipe): void
|
||||
{
|
||||
self::$pipes[$name][] = $pipe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an interceptor that should replace an existing expectation.
|
||||
*
|
||||
* @param string|Closure(mixed $value, mixed ...$arguments):bool $filter
|
||||
*/
|
||||
public function intercept(string $name, string|Closure $filter, Closure $handler): void
|
||||
{
|
||||
if (is_string($filter)) {
|
||||
$filter = function ($value) use ($filter): bool {
|
||||
return $value instanceof $filter;
|
||||
};
|
||||
}
|
||||
|
||||
$this->pipe($name, function ($next, ...$arguments) use ($handler, $filter) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
if ($filter($this->value, ...$arguments)) {
|
||||
//@phpstan-ignore-next-line
|
||||
$handler->bindTo($this, get_class($this))(...$arguments);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$next();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get th list of pipes by the given name.
|
||||
*
|
||||
* @return array<int, Closure>
|
||||
*/
|
||||
private function pipes(string $name, object $context, string $scope): array
|
||||
{
|
||||
return array_map(fn (Closure $pipe) => $pipe->bindTo($context, $scope), self::$pipes[$name] ?? []);
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@ namespace Pest\Concerns;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
trait RetrievesValues
|
||||
trait Retrievable
|
||||
{
|
||||
/**
|
||||
* @template TRetrievableValue
|
||||
@ -7,6 +7,7 @@ namespace Pest\Concerns;
|
||||
use Closure;
|
||||
use Pest\Support\ChainableClosure;
|
||||
use Pest\Support\ExceptionTrace;
|
||||
use Pest\Support\Reflection;
|
||||
use Pest\TestSuite;
|
||||
use Throwable;
|
||||
|
||||
@ -210,7 +211,28 @@ trait Testable
|
||||
*/
|
||||
private function __resolveTestArguments(array $arguments): array
|
||||
{
|
||||
return array_map(fn ($data) => $data instanceof Closure ? $this->__callClosure($data, []) : $data, $arguments);
|
||||
if (count($arguments) !== 1) {
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
if (!$arguments[0] instanceof Closure) {
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
$underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure');
|
||||
$testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest));
|
||||
|
||||
if (in_array($testParameterTypes[0], ['Closure', 'callable'])) {
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
$boundDatasetResult = $this->__callClosure($arguments[0], []);
|
||||
|
||||
if (count($testParameterTypes) === 1 || !is_array($boundDatasetResult)) {
|
||||
return [$boundDatasetResult];
|
||||
}
|
||||
|
||||
return array_values($boundDatasetResult);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
21
src/Exceptions/ExpectationNotFound.php
Normal file
21
src/Exceptions/ExpectationNotFound.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class ExpectationNotFound extends Exception
|
||||
{
|
||||
/**
|
||||
* Creates a new ExpectationNotFound instance from the given name.
|
||||
*/
|
||||
public static function fromName(string $name): ExpectationNotFound
|
||||
{
|
||||
return new self("Expectation [$name] does not exist.");
|
||||
}
|
||||
}
|
||||
@ -6,48 +6,43 @@ namespace Pest;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Closure;
|
||||
use InvalidArgumentException;
|
||||
use Pest\Concerns\Extendable;
|
||||
use Pest\Concerns\RetrievesValues;
|
||||
use Pest\Concerns\Pipeable;
|
||||
use Pest\Concerns\Retrievable;
|
||||
use Pest\Exceptions\ExpectationNotFound;
|
||||
use Pest\Exceptions\InvalidExpectationValue;
|
||||
use Pest\Support\Arr;
|
||||
use Pest\Support\NullClosure;
|
||||
use Pest\Expectations\EachExpectation;
|
||||
use Pest\Expectations\HigherOrderExpectation;
|
||||
use Pest\Expectations\OppositeExpectation;
|
||||
use Pest\Support\ExpectationPipeline;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
use ReflectionFunction;
|
||||
use ReflectionNamedType;
|
||||
use SebastianBergmann\Exporter\Exporter;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @property Expectation $not Creates the opposite expectation.
|
||||
* @property Each $each Creates an expectation on each element on the traversable value.
|
||||
* @property Expectation $not Creates the opposite expectation.
|
||||
* @property EachExpectation $each Creates an expectation on each element on the traversable value.
|
||||
*
|
||||
* @mixin Mixins\Expectation<TValue>
|
||||
*/
|
||||
final class Expectation
|
||||
{
|
||||
use RetrievesValues, Extendable {
|
||||
__call as __extendsCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* The exporter instance, if any.
|
||||
*
|
||||
* @readonly
|
||||
*/
|
||||
private ?Exporter $exporter = null;
|
||||
use Extendable;
|
||||
use Pipeable;
|
||||
use Retrievable;
|
||||
|
||||
/**
|
||||
* Creates a new expectation.
|
||||
*
|
||||
* @param TValue $value
|
||||
*/
|
||||
public function __construct(public mixed $value)
|
||||
{
|
||||
public function __construct(
|
||||
public mixed $value
|
||||
) {
|
||||
// ..
|
||||
}
|
||||
|
||||
/**
|
||||
@ -121,9 +116,9 @@ final class Expectation
|
||||
/**
|
||||
* Creates an expectation on each item of the iterable "value".
|
||||
*
|
||||
* @return Each<TValue>
|
||||
* @return EachExpectation<TValue>
|
||||
*/
|
||||
public function each(callable $callback = null): Each
|
||||
public function each(callable $callback = null): EachExpectation
|
||||
{
|
||||
if (!is_iterable($this->value)) {
|
||||
throw new BadMethodCallException('Expectation value is not iterable.');
|
||||
@ -135,7 +130,7 @@ final class Expectation
|
||||
}
|
||||
}
|
||||
|
||||
return new Each($this);
|
||||
return new EachExpectation($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -264,850 +259,73 @@ final class Expectation
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same type and
|
||||
* value. Used on objects, it asserts that two
|
||||
* variables reference the same object.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBe(mixed $expected): Expectation
|
||||
{
|
||||
Assert::assertSame($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is empty.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeEmpty(): Expectation
|
||||
{
|
||||
Assert::assertEmpty($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is true.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeTrue(): Expectation
|
||||
{
|
||||
Assert::assertTrue($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is truthy.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeTruthy(): Expectation
|
||||
{
|
||||
Assert::assertTrue((bool) $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is false.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeFalse(): Expectation
|
||||
{
|
||||
Assert::assertFalse($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is falsy.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeFalsy(): Expectation
|
||||
{
|
||||
Assert::assertFalse((bool) $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is greater than $expected.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeGreaterThan(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertGreaterThan($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is greater than or equal to $expected.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeGreaterThanOrEqual(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertGreaterThanOrEqual($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is less than or equal to $expected.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeLessThan(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertLessThan($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is less than $expected.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeLessThanOrEqual(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertLessThanOrEqual($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that $needle is an element of the value.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toContain(mixed ...$needles): Expectation
|
||||
{
|
||||
foreach ($needles as $needle) {
|
||||
if (is_string($this->value)) {
|
||||
// @phpstan-ignore-next-line
|
||||
Assert::assertStringContainsString((string) $needle, $this->value);
|
||||
} else {
|
||||
if (!is_iterable($this->value)) {
|
||||
InvalidExpectationValue::expected('iterable');
|
||||
}
|
||||
Assert::assertContains($needle, $this->value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value starts with $expected.
|
||||
*
|
||||
* @param non-empty-string $expected
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toStartWith(string $expected): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertStringStartsWith($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value ends with $expected.
|
||||
*
|
||||
* @param non-empty-string $expected
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toEndWith(string $expected): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertStringEndsWith($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that $number matches value's Length.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toHaveLength(int $number): Expectation
|
||||
{
|
||||
if (is_string($this->value)) {
|
||||
Assert::assertEquals($number, mb_strlen($this->value));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (is_iterable($this->value)) {
|
||||
return $this->toHaveCount($number);
|
||||
}
|
||||
|
||||
if (is_object($this->value)) {
|
||||
if (method_exists($this->value, 'toArray')) {
|
||||
$array = $this->value->toArray();
|
||||
} else {
|
||||
$array = (array) $this->value;
|
||||
}
|
||||
|
||||
Assert::assertCount($number, $array);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
throw new BadMethodCallException('Expectation value length is not countable.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that $count matches the number of elements of the value.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toHaveCount(int $count): Expectation
|
||||
{
|
||||
if (!is_countable($this->value) && !is_iterable($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertCount($count, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value contains the property $name.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toHaveProperty(string $name, mixed $value = null): Expectation
|
||||
{
|
||||
$this->toBeObject();
|
||||
|
||||
//@phpstan-ignore-next-line
|
||||
Assert::assertTrue(property_exists($this->value, $name));
|
||||
|
||||
if (func_num_args() > 1) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
Assert::assertEquals($value, $this->value->{$name});
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value contains the provided properties $names.
|
||||
*
|
||||
* @param iterable<array-key, string> $names
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toHaveProperties(iterable $names): Expectation
|
||||
{
|
||||
foreach ($names as $name) {
|
||||
$this->toHaveProperty($name);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same value.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toEqual(mixed $expected): Expectation
|
||||
{
|
||||
Assert::assertEquals($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same value.
|
||||
* The contents of $expected and the $this->value are
|
||||
* canonicalized before they are compared. For instance, when the two
|
||||
* variables $expected and $this->value are arrays, then these arrays
|
||||
* are sorted before they are compared. When $expected and $this->value
|
||||
* are objects, each object is converted to an array containing all
|
||||
* private, protected and public attributes.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toEqualCanonicalizing(mixed $expected): Expectation
|
||||
{
|
||||
Assert::assertEqualsCanonicalizing($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the absolute difference between the value and $expected
|
||||
* is lower than $delta.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toEqualWithDelta(mixed $expected, float $delta): Expectation
|
||||
{
|
||||
Assert::assertEqualsWithDelta($expected, $this->value, $delta);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is one of the given values.
|
||||
*
|
||||
* @param iterable<int|string, mixed> $values
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeIn(iterable $values): Expectation
|
||||
{
|
||||
Assert::assertContains($this->value, $values);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is infinite.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeInfinite(): Expectation
|
||||
{
|
||||
Assert::assertInfinite($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is an instance of $class.
|
||||
*
|
||||
* @param class-string $class
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeInstanceOf(string $class): Expectation
|
||||
{
|
||||
Assert::assertInstanceOf($class, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is an array.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeArray(): Expectation
|
||||
{
|
||||
Assert::assertIsArray($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type bool.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeBool(): Expectation
|
||||
{
|
||||
Assert::assertIsBool($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type callable.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeCallable(): Expectation
|
||||
{
|
||||
Assert::assertIsCallable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type float.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeFloat(): Expectation
|
||||
{
|
||||
Assert::assertIsFloat($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type int.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeInt(): Expectation
|
||||
{
|
||||
Assert::assertIsInt($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type iterable.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeIterable(): Expectation
|
||||
{
|
||||
Assert::assertIsIterable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type numeric.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeNumeric(): Expectation
|
||||
{
|
||||
Assert::assertIsNumeric($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type object.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeObject(): Expectation
|
||||
{
|
||||
Assert::assertIsObject($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type resource.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeResource(): Expectation
|
||||
{
|
||||
Assert::assertIsResource($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type scalar.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeScalar(): Expectation
|
||||
{
|
||||
Assert::assertIsScalar($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type string.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeString(): Expectation
|
||||
{
|
||||
Assert::assertIsString($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a JSON string.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeJson(): Expectation
|
||||
{
|
||||
Assert::assertIsString($this->value);
|
||||
|
||||
//@phpstan-ignore-next-line
|
||||
Assert::assertJson($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is NAN.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeNan(): Expectation
|
||||
{
|
||||
Assert::assertNan($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is null.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeNull(): Expectation
|
||||
{
|
||||
Assert::assertNull($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value array has the provided $key.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toHaveKey(string|int $key, mixed $value = null): Expectation
|
||||
{
|
||||
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
|
||||
$array = $this->value->toArray();
|
||||
} else {
|
||||
$array = (array) $this->value;
|
||||
}
|
||||
|
||||
try {
|
||||
Assert::assertTrue(Arr::has($array, $key));
|
||||
|
||||
/* @phpstan-ignore-next-line */
|
||||
} catch (ExpectationFailedException $exception) {
|
||||
throw new ExpectationFailedException("Failed asserting that an array has the key '$key'", $exception->getComparisonFailure());
|
||||
}
|
||||
|
||||
if (func_num_args() > 1) {
|
||||
Assert::assertEquals($value, Arr::get($array, $key));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value array has the provided $keys.
|
||||
*
|
||||
* @param array<int, int|string> $keys
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toHaveKeys(array $keys): Expectation
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->toHaveKey($key);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a directory.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeDirectory(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertDirectoryExists($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a directory and is readable.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeReadableDirectory(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertDirectoryIsReadable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a directory and is writable.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeWritableDirectory(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertDirectoryIsWritable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a file.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeFile(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertFileExists($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a file and is readable.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeReadableFile(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertFileIsReadable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a file and is writable.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toBeWritableFile(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
Assert::assertFileIsWritable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value array matches the given array subset.
|
||||
*
|
||||
* @param iterable<int|string, mixed> $array
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toMatchArray(iterable|object $array): Expectation
|
||||
{
|
||||
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
|
||||
$valueAsArray = $this->value->toArray();
|
||||
} else {
|
||||
$valueAsArray = (array) $this->value;
|
||||
}
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
Assert::assertArrayHasKey($key, $valueAsArray);
|
||||
|
||||
Assert::assertEquals(
|
||||
$value,
|
||||
$valueAsArray[$key],
|
||||
sprintf(
|
||||
'Failed asserting that an array has a key %s with the value %s.',
|
||||
$this->export($key),
|
||||
$this->export($valueAsArray[$key]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value object matches a subset
|
||||
* of the properties of an given object.
|
||||
*
|
||||
* @param iterable<string, mixed>|object $object
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toMatchObject(iterable|object $object): Expectation
|
||||
{
|
||||
foreach ((array) $object as $property => $value) {
|
||||
if (!is_object($this->value) && !is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('object|string');
|
||||
}
|
||||
|
||||
Assert::assertTrue(property_exists($this->value, $property));
|
||||
|
||||
/* @phpstan-ignore-next-line */
|
||||
$propertyValue = $this->value->{$property};
|
||||
Assert::assertEquals(
|
||||
$value,
|
||||
$propertyValue,
|
||||
sprintf(
|
||||
'Failed asserting that an object has a property %s with the value %s.',
|
||||
$this->export($property),
|
||||
$this->export($propertyValue),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value matches a regular expression.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toMatch(string $expression): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
Assert::assertMatchesRegularExpression($expression, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value matches a constraint.
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toMatchConstraint(Constraint $constraint): Expectation
|
||||
{
|
||||
Assert::assertThat($this->value, $constraint);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that executing value throws an exception.
|
||||
*
|
||||
* @param (Closure(Throwable): mixed)|string $exception
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function toThrow(callable|string $exception, string $exceptionMessage = null): Expectation
|
||||
{
|
||||
$callback = NullClosure::create();
|
||||
|
||||
if ($exception instanceof Closure) {
|
||||
$callback = $exception;
|
||||
$parameters = (new ReflectionFunction($exception))->getParameters();
|
||||
|
||||
if (1 !== count($parameters)) {
|
||||
throw new InvalidArgumentException('The given closure must have a single parameter type-hinted as the class string.');
|
||||
}
|
||||
|
||||
if (!($type = $parameters[0]->getType()) instanceof ReflectionNamedType) {
|
||||
throw new InvalidArgumentException('The given closure\'s parameter must be type-hinted as the class string.');
|
||||
}
|
||||
|
||||
$exception = $type->getName();
|
||||
}
|
||||
|
||||
try {
|
||||
($this->value)();
|
||||
} catch (Throwable $e) { // @phpstan-ignore-line
|
||||
if (!class_exists($exception)) {
|
||||
Assert::assertStringContainsString($exception, $e->getMessage());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($exceptionMessage !== null) {
|
||||
Assert::assertStringContainsString($exceptionMessage, $e->getMessage());
|
||||
}
|
||||
|
||||
Assert::assertInstanceOf($exception, $e);
|
||||
$callback($e);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (!class_exists($exception)) {
|
||||
throw new ExpectationFailedException("Exception with message \"$exception\" not thrown.");
|
||||
}
|
||||
|
||||
throw new ExpectationFailedException("Exception \"$exception\" not thrown.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the given value.
|
||||
*/
|
||||
private function export(mixed $value): string
|
||||
{
|
||||
if ($this->exporter === null) {
|
||||
$this->exporter = new Exporter();
|
||||
}
|
||||
|
||||
return $this->exporter->export($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically handle calls to the class or
|
||||
* creates a new higher order expectation.
|
||||
* Dynamically calls methods on the class or creates a new higher order expectation.
|
||||
*
|
||||
* @param array<int, mixed> $parameters
|
||||
*
|
||||
* @return HigherOrderExpectation<TValue, mixed>|self<TValue>|mixed
|
||||
* @return Expectation<TValue>|HigherOrderExpectation<Expectation<TValue>, TValue>
|
||||
*/
|
||||
public function __call(string $method, array $parameters)
|
||||
public function __call(string $method, array $parameters): Expectation|HigherOrderExpectation
|
||||
{
|
||||
if (!Expectation::hasExtend($method)) {
|
||||
if (!self::hasMethod($method)) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
|
||||
}
|
||||
|
||||
return $this->__extendsCall($method, $parameters);
|
||||
ExpectationPipeline::for($this->getExpectationClosure($method))
|
||||
->send(...$parameters)
|
||||
->through($this->pipes($method, $this, Expectation::class))
|
||||
->run();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically calls methods on the class without any arguments
|
||||
* or creates a new higher order expectation.
|
||||
* Creates a new expectation closure from the given name.
|
||||
*
|
||||
* @return self<TValue>|OppositeExpectation<TValue>|Each<TValue>|HigherOrderExpectation<TValue, mixed>
|
||||
* @throws ExpectationNotFound
|
||||
*/
|
||||
public function __get(string $name): Expectation|OppositeExpectation|Each|HigherOrderExpectation
|
||||
private function getExpectationClosure(string $name): Closure
|
||||
{
|
||||
if (!method_exists($this, $name) && !Expectation::hasExtend($name)) {
|
||||
if (method_exists(Mixins\Expectation::class, $name)) {
|
||||
//@phpstan-ignore-next-line
|
||||
return Closure::fromCallable([new Mixins\Expectation($this->value), $name]);
|
||||
}
|
||||
|
||||
if (self::hasExtend($name)) {
|
||||
$extend = self::$extends[$name]->bindTo($this, Expectation::class);
|
||||
|
||||
if ($extend != false) {
|
||||
return $extend;
|
||||
}
|
||||
}
|
||||
|
||||
throw ExpectationNotFound::fromName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically calls methods on the class without any arguments or creates a new higher order expectation.
|
||||
*
|
||||
* @return Expectation<TValue>|OppositeExpectation<TValue>|EachExpectation<TValue>|HigherOrderExpectation<Expectation<TValue>, TValue|null>|TValue
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
if (!self::hasMethod($name)) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
return new HigherOrderExpectation($this, $this->retrieve($name, $this->value));
|
||||
}
|
||||
|
||||
/* @phpstan-ignore-next-line */
|
||||
return $this->{$name}();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given expectation method exists.
|
||||
*/
|
||||
public static function hasMethod(string $name): bool
|
||||
{
|
||||
return method_exists(self::class, $name)
|
||||
|| method_exists(Mixins\Expectation::class, $name)
|
||||
|| self::hasExtend($name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
namespace Pest\Expectations;
|
||||
|
||||
use function expect;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -11,7 +14,7 @@ namespace Pest;
|
||||
*
|
||||
* @mixin Expectation<TValue>
|
||||
*/
|
||||
final class Each
|
||||
final class EachExpectation
|
||||
{
|
||||
private bool $opposite = false;
|
||||
|
||||
@ -43,7 +46,7 @@ final class Each
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function not(): Each
|
||||
public function not(): EachExpectation
|
||||
{
|
||||
$this->opposite = true;
|
||||
|
||||
@ -57,7 +60,7 @@ final class Each
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function __call(string $name, array $arguments): Each
|
||||
public function __call(string $name, array $arguments): EachExpectation
|
||||
{
|
||||
foreach ($this->original->value as $item) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
@ -74,7 +77,7 @@ final class Each
|
||||
*
|
||||
* @return self<TValue>
|
||||
*/
|
||||
public function __get(string $name): Each
|
||||
public function __get(string $name): EachExpectation
|
||||
{
|
||||
/* @phpstan-ignore-next-line */
|
||||
return $this->$name();
|
||||
@ -2,9 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
namespace Pest\Expectations;
|
||||
|
||||
use Pest\Concerns\RetrievesValues;
|
||||
use Pest\Concerns\Retrievable;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -16,12 +17,12 @@ use Pest\Concerns\RetrievesValues;
|
||||
*/
|
||||
final class HigherOrderExpectation
|
||||
{
|
||||
use RetrievesValues;
|
||||
use Retrievable;
|
||||
|
||||
/**
|
||||
* @var Expectation<TValue>|Each<TValue>
|
||||
* @var Expectation<TValue>|EachExpectation<TValue>
|
||||
*/
|
||||
private Expectation|Each $expectation;
|
||||
private Expectation|EachExpectation $expectation;
|
||||
|
||||
private bool $opposite = false;
|
||||
|
||||
@ -121,7 +122,7 @@ final class HigherOrderExpectation
|
||||
*/
|
||||
private function expectationHasMethod(string $name): bool
|
||||
{
|
||||
return method_exists($this->original, $name) || $this->original::hasExtend($name);
|
||||
return method_exists($this->original, $name) || $this->original::hasMethod($name) || $this->original::hasExtend($name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2,8 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
namespace Pest\Expectations;
|
||||
|
||||
use Pest\Expectation;
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
use SebastianBergmann\Exporter\Exporter;
|
||||
|
||||
@ -150,7 +150,7 @@ final class TestCaseFactory
|
||||
eval("
|
||||
namespace $namespace;
|
||||
|
||||
use Pest\Datasets as __PestDatasets;
|
||||
use Pest\Repositories\DatasetsRepository as __PestDatasets;
|
||||
use Pest\TestSuite as __PestTestSuite;
|
||||
|
||||
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
|
||||
|
||||
@ -5,10 +5,10 @@ declare(strict_types=1);
|
||||
namespace Pest\Factories;
|
||||
|
||||
use Closure;
|
||||
use Pest\Datasets;
|
||||
use Pest\Exceptions\ShouldNotHappen;
|
||||
use Pest\Factories\Concerns\HigherOrderable;
|
||||
use Pest\Plugins\Retry;
|
||||
use Pest\Repositories\DatasetsRepository;
|
||||
use Pest\Support\Str;
|
||||
use Pest\TestSuite;
|
||||
use PHPUnit\Framework\Assert;
|
||||
@ -159,7 +159,7 @@ final class TestCaseMethodFactory
|
||||
*/
|
||||
private function buildDatasetForEvaluation(string $methodName, string $dataProviderName): string
|
||||
{
|
||||
Datasets::with($this->filename, $methodName, $this->datasets);
|
||||
DatasetsRepository::with($this->filename, $methodName, $this->datasets);
|
||||
|
||||
return <<<EOF
|
||||
|
||||
|
||||
@ -2,14 +2,13 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Datasets;
|
||||
use Pest\Expectation;
|
||||
use Pest\PendingCalls\AfterEachCall;
|
||||
use Pest\PendingCalls\BeforeEachCall;
|
||||
use Pest\PendingCalls\TestCall;
|
||||
use Pest\PendingCalls\UsesCall;
|
||||
use Pest\Repositories\DatasetsRepository;
|
||||
use Pest\Support\Backtrace;
|
||||
use Pest\Support\Extendable;
|
||||
use Pest\Support\HigherOrderTapProxy;
|
||||
use Pest\TestSuite;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@ -20,16 +19,12 @@ if (!function_exists('expect')) {
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param TValue $value the Value
|
||||
* @param TValue $value
|
||||
*
|
||||
* @return Expectation<TValue>|Extendable
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
function expect($value = null): Expectation|Extendable
|
||||
function expect(mixed $value = null): Expectation
|
||||
{
|
||||
if (func_num_args() === 0) {
|
||||
return new Extendable(Expectation::class);
|
||||
}
|
||||
|
||||
return new Expectation($value);
|
||||
}
|
||||
}
|
||||
@ -66,7 +61,7 @@ if (!function_exists('dataset')) {
|
||||
*/
|
||||
function dataset(string $name, Closure|iterable $dataset): void
|
||||
{
|
||||
Datasets::set($name, $dataset);
|
||||
DatasetsRepository::set($name, $dataset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@ final class PestDatasetCommand extends Command
|
||||
/** @var string $name */
|
||||
$name = $this->argument('name');
|
||||
|
||||
$relativePath = sprintf(testDirectory('Datasets/%s.php'), ucfirst($name));
|
||||
$relativePath = sprintf(testDirectory('DatasetsRepository/%s.php'), ucfirst($name));
|
||||
|
||||
/* @phpstan-ignore-next-line */
|
||||
$target = base_path($relativePath);
|
||||
|
||||
860
src/Mixins/Expectation.php
Normal file
860
src/Mixins/Expectation.php
Normal file
@ -0,0 +1,860 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Mixins;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Closure;
|
||||
use InvalidArgumentException;
|
||||
use Pest\Exceptions\InvalidExpectationValue;
|
||||
use Pest\Support\Arr;
|
||||
use Pest\Support\NullClosure;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
use ReflectionFunction;
|
||||
use ReflectionNamedType;
|
||||
use SebastianBergmann\Exporter\Exporter;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @mixin \Pest\Expectation<TValue>
|
||||
*/
|
||||
final class Expectation
|
||||
{
|
||||
/**
|
||||
* The exporter instance, if any.
|
||||
*
|
||||
* @readonly
|
||||
*/
|
||||
private Exporter|null $exporter = null;
|
||||
|
||||
/**
|
||||
* Creates a new expectation.
|
||||
*
|
||||
* @param TValue $value
|
||||
*/
|
||||
public function __construct(
|
||||
public mixed $value
|
||||
) {
|
||||
// ..
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same type and
|
||||
* value. Used on objects, it asserts that two
|
||||
* variables reference the same object.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBe(mixed $expected): Expectation
|
||||
{
|
||||
Assert::assertSame($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is empty.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeEmpty(): Expectation
|
||||
{
|
||||
Assert::assertEmpty($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is true.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeTrue(): Expectation
|
||||
{
|
||||
Assert::assertTrue($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is truthy.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeTruthy(): Expectation
|
||||
{
|
||||
Assert::assertTrue((bool) $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is false.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeFalse(): Expectation
|
||||
{
|
||||
Assert::assertFalse($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is falsy.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeFalsy(): Expectation
|
||||
{
|
||||
Assert::assertFalse((bool) $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is greater than $expected.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeGreaterThan(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertGreaterThan($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is greater than or equal to $expected.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeGreaterThanOrEqual(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertGreaterThanOrEqual($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is less than or equal to $expected.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeLessThan(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertLessThan($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is less than $expected.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeLessThanOrEqual(int|float $expected): Expectation
|
||||
{
|
||||
Assert::assertLessThanOrEqual($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that $needle is an element of the value.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toContain(mixed ...$needles): Expectation
|
||||
{
|
||||
foreach ($needles as $needle) {
|
||||
if (is_string($this->value)) {
|
||||
// @phpstan-ignore-next-line
|
||||
Assert::assertStringContainsString((string) $needle, $this->value);
|
||||
} else {
|
||||
if (!is_iterable($this->value)) {
|
||||
InvalidExpectationValue::expected('iterable');
|
||||
}
|
||||
Assert::assertContains($needle, $this->value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value starts with $expected.
|
||||
*
|
||||
* @param non-empty-string $expected
|
||||
*
|
||||
*@return Expectation<TValue>
|
||||
*/
|
||||
public function toStartWith(string $expected): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertStringStartsWith($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value ends with $expected.
|
||||
*
|
||||
* @param non-empty-string $expected
|
||||
*
|
||||
*@return Expectation<TValue>
|
||||
*/
|
||||
public function toEndWith(string $expected): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertStringEndsWith($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that $number matches value's Length.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toHaveLength(int $number): Expectation
|
||||
{
|
||||
if (is_string($this->value)) {
|
||||
Assert::assertEquals($number, mb_strlen($this->value));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (is_iterable($this->value)) {
|
||||
return $this->toHaveCount($number);
|
||||
}
|
||||
|
||||
if (is_object($this->value)) {
|
||||
if (method_exists($this->value, 'toArray')) {
|
||||
$array = $this->value->toArray();
|
||||
} else {
|
||||
$array = (array) $this->value;
|
||||
}
|
||||
|
||||
Assert::assertCount($number, $array);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
throw new BadMethodCallException('Expectation value length is not countable.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that $count matches the number of elements of the value.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toHaveCount(int $count): Expectation
|
||||
{
|
||||
if (!is_countable($this->value) && !is_iterable($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertCount($count, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value contains the property $name.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toHaveProperty(string $name, mixed $value = null): Expectation
|
||||
{
|
||||
$this->toBeObject();
|
||||
|
||||
//@phpstan-ignore-next-line
|
||||
Assert::assertTrue(property_exists($this->value, $name));
|
||||
|
||||
if (func_num_args() > 1) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
Assert::assertEquals($value, $this->value->{$name});
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value contains the provided properties $names.
|
||||
*
|
||||
* @param iterable<array-key, string> $names
|
||||
*
|
||||
*@return Expectation<TValue>
|
||||
*/
|
||||
public function toHaveProperties(iterable $names): Expectation
|
||||
{
|
||||
foreach ($names as $name) {
|
||||
$this->toHaveProperty($name);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same value.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toEqual(mixed $expected): Expectation
|
||||
{
|
||||
Assert::assertEquals($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same value.
|
||||
* The contents of $expected and the $this->value are
|
||||
* canonicalized before they are compared. For instance, when the two
|
||||
* variables $expected and $this->value are arrays, then these arrays
|
||||
* are sorted before they are compared. When $expected and $this->value
|
||||
* are objects, each object is converted to an array containing all
|
||||
* private, protected and public attributes.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toEqualCanonicalizing(mixed $expected): Expectation
|
||||
{
|
||||
Assert::assertEqualsCanonicalizing($expected, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the absolute difference between the value and $expected
|
||||
* is lower than $delta.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toEqualWithDelta(mixed $expected, float $delta): Expectation
|
||||
{
|
||||
Assert::assertEqualsWithDelta($expected, $this->value, $delta);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is one of the given values.
|
||||
*
|
||||
* @param iterable<int|string, mixed> $values
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeIn(iterable $values): Expectation
|
||||
{
|
||||
Assert::assertContains($this->value, $values);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is infinite.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeInfinite(): Expectation
|
||||
{
|
||||
Assert::assertInfinite($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is an instance of $class.
|
||||
*
|
||||
* @param class-string $class
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeInstanceOf(string $class): Expectation
|
||||
{
|
||||
Assert::assertInstanceOf($class, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is an array.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeArray(): Expectation
|
||||
{
|
||||
Assert::assertIsArray($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type bool.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeBool(): Expectation
|
||||
{
|
||||
Assert::assertIsBool($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type callable.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeCallable(): Expectation
|
||||
{
|
||||
Assert::assertIsCallable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type float.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeFloat(): Expectation
|
||||
{
|
||||
Assert::assertIsFloat($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type int.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeInt(): Expectation
|
||||
{
|
||||
Assert::assertIsInt($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type iterable.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeIterable(): Expectation
|
||||
{
|
||||
Assert::assertIsIterable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type numeric.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeNumeric(): Expectation
|
||||
{
|
||||
Assert::assertIsNumeric($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type object.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeObject(): Expectation
|
||||
{
|
||||
Assert::assertIsObject($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type resource.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeResource(): Expectation
|
||||
{
|
||||
Assert::assertIsResource($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type scalar.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeScalar(): Expectation
|
||||
{
|
||||
Assert::assertIsScalar($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is of type string.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeString(): Expectation
|
||||
{
|
||||
Assert::assertIsString($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a JSON string.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeJson(): Expectation
|
||||
{
|
||||
Assert::assertIsString($this->value);
|
||||
|
||||
//@phpstan-ignore-next-line
|
||||
Assert::assertJson($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is NAN.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeNan(): Expectation
|
||||
{
|
||||
Assert::assertNan($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is null.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeNull(): Expectation
|
||||
{
|
||||
Assert::assertNull($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value array has the provided $key.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toHaveKey(string|int $key, mixed $value = null): Expectation
|
||||
{
|
||||
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
|
||||
$array = $this->value->toArray();
|
||||
} else {
|
||||
$array = (array) $this->value;
|
||||
}
|
||||
|
||||
try {
|
||||
Assert::assertTrue(Arr::has($array, $key));
|
||||
|
||||
/* @phpstan-ignore-next-line */
|
||||
} catch (ExpectationFailedException $exception) {
|
||||
throw new ExpectationFailedException("Failed asserting that an array has the key '$key'", $exception->getComparisonFailure());
|
||||
}
|
||||
|
||||
if (func_num_args() > 1) {
|
||||
Assert::assertEquals($value, Arr::get($array, $key));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value array has the provided $keys.
|
||||
*
|
||||
* @param array<int, int|string> $keys
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toHaveKeys(array $keys): Expectation
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->toHaveKey($key);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a directory.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeDirectory(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertDirectoryExists($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a directory and is readable.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeReadableDirectory(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertDirectoryIsReadable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a directory and is writable.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeWritableDirectory(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertDirectoryIsWritable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a file.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeFile(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertFileExists($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a file and is readable.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeReadableFile(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
|
||||
Assert::assertFileIsReadable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value is a file and is writable.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toBeWritableFile(): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
Assert::assertFileIsWritable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value array matches the given array subset.
|
||||
*
|
||||
* @param iterable<int|string, mixed> $array
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toMatchArray(iterable|object $array): Expectation
|
||||
{
|
||||
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
|
||||
$valueAsArray = $this->value->toArray();
|
||||
} else {
|
||||
$valueAsArray = (array) $this->value;
|
||||
}
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
Assert::assertArrayHasKey($key, $valueAsArray);
|
||||
|
||||
Assert::assertEquals(
|
||||
$value,
|
||||
$valueAsArray[$key],
|
||||
sprintf(
|
||||
'Failed asserting that an array has a key %s with the value %s.',
|
||||
$this->export($key),
|
||||
$this->export($valueAsArray[$key]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value object matches a subset
|
||||
* of the properties of an given object.
|
||||
*
|
||||
* @param iterable<string, mixed>|object $object
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toMatchObject(iterable|object $object): Expectation
|
||||
{
|
||||
foreach ((array) $object as $property => $value) {
|
||||
if (!is_object($this->value) && !is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('object|string');
|
||||
}
|
||||
|
||||
Assert::assertTrue(property_exists($this->value, $property));
|
||||
|
||||
/* @phpstan-ignore-next-line */
|
||||
$propertyValue = $this->value->{$property};
|
||||
Assert::assertEquals(
|
||||
$value,
|
||||
$propertyValue,
|
||||
sprintf(
|
||||
'Failed asserting that an object has a property %s with the value %s.',
|
||||
$this->export($property),
|
||||
$this->export($propertyValue),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value matches a regular expression.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toMatch(string $expression): Expectation
|
||||
{
|
||||
if (!is_string($this->value)) {
|
||||
InvalidExpectationValue::expected('string');
|
||||
}
|
||||
Assert::assertMatchesRegularExpression($expression, $this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the value matches a constraint.
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toMatchConstraint(Constraint $constraint): Expectation
|
||||
{
|
||||
Assert::assertThat($this->value, $constraint);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that executing value throws an exception.
|
||||
*
|
||||
* @param (Closure(Throwable): mixed)|string $exception
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
*/
|
||||
public function toThrow(callable|string $exception, string $exceptionMessage = null): Expectation
|
||||
{
|
||||
$callback = NullClosure::create();
|
||||
|
||||
if ($exception instanceof Closure) {
|
||||
$callback = $exception;
|
||||
$parameters = (new ReflectionFunction($exception))->getParameters();
|
||||
|
||||
if (1 !== count($parameters)) {
|
||||
throw new InvalidArgumentException('The given closure must have a single parameter type-hinted as the class string.');
|
||||
}
|
||||
|
||||
if (!($type = $parameters[0]->getType()) instanceof ReflectionNamedType) {
|
||||
throw new InvalidArgumentException('The given closure\'s parameter must be type-hinted as the class string.');
|
||||
}
|
||||
|
||||
$exception = $type->getName();
|
||||
}
|
||||
|
||||
try {
|
||||
($this->value)();
|
||||
} catch (Throwable $e) { // @phpstan-ignore-line
|
||||
if (!class_exists($exception)) {
|
||||
Assert::assertStringContainsString($exception, $e->getMessage());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($exceptionMessage !== null) {
|
||||
Assert::assertStringContainsString($exceptionMessage, $e->getMessage());
|
||||
}
|
||||
|
||||
Assert::assertInstanceOf($exception, $e);
|
||||
$callback($e);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (!class_exists($exception)) {
|
||||
throw new ExpectationFailedException("Exception with message \"$exception\" not thrown.");
|
||||
}
|
||||
|
||||
throw new ExpectationFailedException("Exception \"$exception\" not thrown.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the given value.
|
||||
*/
|
||||
private function export(mixed $value): string
|
||||
{
|
||||
if ($this->exporter === null) {
|
||||
$this->exporter = new Exporter();
|
||||
}
|
||||
|
||||
return $this->exporter->export($value);
|
||||
}
|
||||
}
|
||||
@ -7,8 +7,7 @@ namespace Pest;
|
||||
final class Plugin
|
||||
{
|
||||
/**
|
||||
* The lazy callables to be executed
|
||||
* once the test suite boots.
|
||||
* The lazy callables to be executed once the test suite boots.
|
||||
*
|
||||
* @var array<int, callable>
|
||||
*
|
||||
|
||||
50
src/Plugins/Memory.php
Normal file
50
src/Plugins/Memory.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins;
|
||||
|
||||
use Pest\Contracts\Plugins\AddsOutput;
|
||||
use Pest\Contracts\Plugins\HandlesArguments;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Memory implements AddsOutput, HandlesArguments
|
||||
{
|
||||
/** @var OutputInterface */
|
||||
private $output;
|
||||
|
||||
private bool $enabled = false;
|
||||
|
||||
public function __construct(OutputInterface $output)
|
||||
{
|
||||
$this->output = $output;
|
||||
}
|
||||
|
||||
public function handleArguments(array $arguments): array
|
||||
{
|
||||
foreach ($arguments as $index => $argument) {
|
||||
if ($argument === '--memory') {
|
||||
unset($arguments[$index]);
|
||||
|
||||
$this->enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($arguments);
|
||||
}
|
||||
|
||||
public function addOutput(int $result): int
|
||||
{
|
||||
if ($this->enabled) {
|
||||
$this->output->writeln(sprintf(
|
||||
' <fg=white;options=bold>Memory: </><fg=default>%s MB</>',
|
||||
round(memory_get_usage(true) / pow(1000, 2), 3)
|
||||
));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
namespace Pest\Repositories;
|
||||
|
||||
use Closure;
|
||||
use Pest\Exceptions\DatasetAlreadyExist;
|
||||
@ -15,7 +15,7 @@ use Traversable;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Datasets
|
||||
final class DatasetsRepository
|
||||
{
|
||||
/**
|
||||
* Holds the datasets.
|
||||
98
src/Support/ExpectationPipeline.php
Normal file
98
src/Support/ExpectationPipeline.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class ExpectationPipeline
|
||||
{
|
||||
/**
|
||||
* The list of pipes.
|
||||
*
|
||||
* @var array<int, Closure>
|
||||
*/
|
||||
private array $pipes = [];
|
||||
|
||||
/**
|
||||
* The list of passables.
|
||||
*
|
||||
* @var array<int, mixed>
|
||||
*/
|
||||
private array $passables;
|
||||
|
||||
/**
|
||||
* The expectation closure.
|
||||
*/
|
||||
private Closure $closure;
|
||||
|
||||
/**
|
||||
* Creates a new instance of Expectation Pipeline.
|
||||
*/
|
||||
public function __construct(Closure $closure)
|
||||
{
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of Expectation Pipeline with given closure.
|
||||
*/
|
||||
public static function for(Closure $closure): self
|
||||
{
|
||||
return new self($closure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the list of passables.
|
||||
*/
|
||||
public function send(mixed ...$passables): self
|
||||
{
|
||||
$this->passables = array_values($passables);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the list of pipes.
|
||||
*
|
||||
* @param array<int, Closure> $pipes
|
||||
*/
|
||||
public function through(array $pipes): self
|
||||
{
|
||||
$this->pipes = $pipes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the pipeline.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$pipeline = array_reduce(
|
||||
array_reverse($this->pipes),
|
||||
$this->carry(),
|
||||
function (): void {
|
||||
($this->closure)(...$this->passables);
|
||||
}
|
||||
);
|
||||
|
||||
$pipeline();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Closure that will carry of the expectation.
|
||||
*/
|
||||
public function carry(): Closure
|
||||
{
|
||||
return function ($stack, $pipe): Closure {
|
||||
return function () use ($stack, $pipe) {
|
||||
return $pipe($stack, ...$this->passables);
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use Closure;
|
||||
|
||||
final class Extendable
|
||||
{
|
||||
/**
|
||||
* Creates a new extendable instance.
|
||||
*/
|
||||
public function __construct(
|
||||
private string $extendableClass
|
||||
) {
|
||||
// ..
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom extend.
|
||||
*/
|
||||
public function extend(string $name, Closure $extend): void
|
||||
{
|
||||
$this->extendableClass::extend($name, $extend);
|
||||
}
|
||||
}
|
||||
@ -205,4 +205,12 @@ final class Reflection
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getFunctionVariable(Closure $function, string $key)
|
||||
{
|
||||
return (new ReflectionFunction($function))->getStaticVariables()[$key] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
namespace Pest\TestCases;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
Reference in New Issue
Block a user