mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 10:17:23 +01:00
Adds nested Higher Order Expectations.
This commit is contained in:
@ -4,8 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Concerns;
|
namespace Pest\Concerns;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Pest\HigherOrderExpectation;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -43,7 +43,7 @@ trait Extendable
|
|||||||
public function __call(string $method, array $parameters)
|
public function __call(string $method, array $parameters)
|
||||||
{
|
{
|
||||||
if (!static::hasExtend($method)) {
|
if (!static::hasExtend($method)) {
|
||||||
return new HigherOrderExpectation($this, $method, $parameters);
|
throw new BadMethodCallException("$method is not a callable method name.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Closure $extend */
|
/** @var Closure $extend */
|
||||||
|
|||||||
29
src/Concerns/RetrievesValues.php
Normal file
29
src/Concerns/RetrievesValues.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Concerns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
trait RetrievesValues
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Safely retrieve the value at the given key from an object or array.
|
||||||
|
*
|
||||||
|
* @param array<mixed>|object $value
|
||||||
|
* @param mixed $default
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function retrieve(string $key, $value, $default = null)
|
||||||
|
{
|
||||||
|
if (is_array($value)) {
|
||||||
|
return $value[$key] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @phpstan-ignore-next-line
|
||||||
|
return $value->$key ?? $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,6 +6,7 @@ namespace Pest;
|
|||||||
|
|
||||||
use BadMethodCallException;
|
use BadMethodCallException;
|
||||||
use Pest\Concerns\Extendable;
|
use Pest\Concerns\Extendable;
|
||||||
|
use Pest\Concerns\RetrievesValues;
|
||||||
use PHPUnit\Framework\Assert;
|
use PHPUnit\Framework\Assert;
|
||||||
use PHPUnit\Framework\Constraint\Constraint;
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
use SebastianBergmann\Exporter\Exporter;
|
use SebastianBergmann\Exporter\Exporter;
|
||||||
@ -18,7 +19,10 @@ use SebastianBergmann\Exporter\Exporter;
|
|||||||
*/
|
*/
|
||||||
final class Expectation
|
final class Expectation
|
||||||
{
|
{
|
||||||
use Extendable;
|
use Extendable {
|
||||||
|
__call as __extendsCall;
|
||||||
|
}
|
||||||
|
use RetrievesValues;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The expectation value.
|
* The expectation value.
|
||||||
@ -696,6 +700,24 @@ final class Expectation
|
|||||||
return $this->exporter->export($value);
|
return $this->exporter->export($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamically handle calls to the class or
|
||||||
|
* creates a new higher order expectation.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed> $parameters
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __call(string $method, array $parameters)
|
||||||
|
{
|
||||||
|
if (!static::hasExtend($method)) {
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->__extendsCall($method, $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class without any arguments
|
* Dynamically calls methods on the class without any arguments
|
||||||
* or creates a new higher order expectation.
|
* or creates a new higher order expectation.
|
||||||
@ -705,7 +727,7 @@ final class Expectation
|
|||||||
public function __get(string $name)
|
public function __get(string $name)
|
||||||
{
|
{
|
||||||
if (!method_exists($this, $name) && !static::hasExtend($name)) {
|
if (!method_exists($this, $name) && !static::hasExtend($name)) {
|
||||||
return new HigherOrderExpectation($this, $name);
|
return new HigherOrderExpectation($this, $this->retrieve($name, $this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @phpstan-ignore-next-line */
|
/* @phpstan-ignore-next-line */
|
||||||
|
|||||||
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Pest;
|
namespace Pest;
|
||||||
|
|
||||||
use Pest\Concerns\Expectable;
|
use Pest\Concerns\Expectable;
|
||||||
|
use Pest\Concerns\RetrievesValues;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -14,6 +15,7 @@ use Pest\Concerns\Expectable;
|
|||||||
final class HigherOrderExpectation
|
final class HigherOrderExpectation
|
||||||
{
|
{
|
||||||
use Expectable;
|
use Expectable;
|
||||||
|
use RetrievesValues;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Expectation
|
* @var Expectation
|
||||||
@ -30,6 +32,11 @@ final class HigherOrderExpectation
|
|||||||
*/
|
*/
|
||||||
private $opposite = false;
|
private $opposite = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $shouldReset = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@ -38,45 +45,12 @@ final class HigherOrderExpectation
|
|||||||
/**
|
/**
|
||||||
* Creates a new higher order expectation.
|
* Creates a new higher order expectation.
|
||||||
*
|
*
|
||||||
* @param array<int|string, mixed>|null $parameters
|
* @param mixed $value
|
||||||
* @phpstan-ignore-next-line
|
|
||||||
*/
|
*/
|
||||||
public function __construct(Expectation $original, string $name, ?array $parameters = null)
|
public function __construct(Expectation $original, $value)
|
||||||
{
|
{
|
||||||
$this->original = $original;
|
$this->original = $original;
|
||||||
$this->name = $name;
|
$this->expectation = $this->expect($value);
|
||||||
|
|
||||||
$this->expectation = $this->expect(
|
|
||||||
is_null($parameters) ? $this->getPropertyValue() : $this->getMethodValue($parameters)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the property value from the original expectation.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function getPropertyValue()
|
|
||||||
{
|
|
||||||
if (is_array($this->original->value)) {
|
|
||||||
return $this->original->value[$this->name];
|
|
||||||
}
|
|
||||||
|
|
||||||
// @phpstan-ignore-next-line
|
|
||||||
return $this->original->value->{$this->name};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the value of the method from the original expectation.
|
|
||||||
*
|
|
||||||
* @param array<int|string, mixed> $arguments
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function getMethodValue(array $arguments)
|
|
||||||
{
|
|
||||||
// @phpstan-ignore-next-line
|
|
||||||
return $this->original->value->{$this->name}(...$arguments);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,8 +70,9 @@ final class HigherOrderExpectation
|
|||||||
*/
|
*/
|
||||||
public function __call(string $name, array $arguments): self
|
public function __call(string $name, array $arguments): self
|
||||||
{
|
{
|
||||||
if (!$this->originalHasMethod($name)) {
|
if (!$this->expectationHasMethod($name)) {
|
||||||
return new self($this->original, $name, $arguments);
|
/* @phpstan-ignore-next-line */
|
||||||
|
return new self($this->original, $this->getValue()->$name(...$arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->performAssertion($name, $arguments);
|
return $this->performAssertion($name, $arguments);
|
||||||
@ -112,8 +87,8 @@ final class HigherOrderExpectation
|
|||||||
return $this->not();
|
return $this->not();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->originalHasMethod($name)) {
|
if (!$this->expectationHasMethod($name)) {
|
||||||
return new self($this->original, $name);
|
return new self($this->original, $this->retrieve($name, $this->getValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->performAssertion($name, []);
|
return $this->performAssertion($name, []);
|
||||||
@ -122,11 +97,21 @@ final class HigherOrderExpectation
|
|||||||
/**
|
/**
|
||||||
* Determines if the original expectation has the given method name.
|
* Determines if the original expectation has the given method name.
|
||||||
*/
|
*/
|
||||||
private function originalHasMethod(string $name): bool
|
private function expectationHasMethod(string $name): bool
|
||||||
{
|
{
|
||||||
return method_exists($this->original, $name) || $this->original::hasExtend($name);
|
return method_exists($this->original, $name) || $this->original::hasExtend($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the applicable value based on the current reset condition.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function getValue()
|
||||||
|
{
|
||||||
|
return $this->shouldReset ? $this->original->value : $this->expectation->value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the given assertion with the current expectation.
|
* Performs the given assertion with the current expectation.
|
||||||
*
|
*
|
||||||
@ -134,13 +119,11 @@ final class HigherOrderExpectation
|
|||||||
*/
|
*/
|
||||||
private function performAssertion(string $name, array $arguments): self
|
private function performAssertion(string $name, array $arguments): self
|
||||||
{
|
{
|
||||||
$expectation = $this->opposite
|
/* @phpstan-ignore-next-line */
|
||||||
? $this->expectation->not()
|
$this->expectation = ($this->opposite ? $this->expectation->not() : $this->expectation)->{$name}(...$arguments);
|
||||||
: $this->expectation;
|
|
||||||
|
|
||||||
$this->expectation = $expectation->{$name}(...$arguments); // @phpstan-ignore-line
|
$this->opposite = false;
|
||||||
|
$this->shouldReset = true;
|
||||||
$this->opposite = false;
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -111,9 +111,11 @@
|
|||||||
✓ it works inside of each
|
✓ it works inside of each
|
||||||
✓ it works with sequence
|
✓ it works with sequence
|
||||||
✓ it can compose complex expectations
|
✓ it can compose complex expectations
|
||||||
|
✓ it can handle nested method calls
|
||||||
|
|
||||||
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
|
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
|
||||||
✓ it can access methods and properties
|
✓ it can access methods and properties
|
||||||
|
✓ it can handle nested methods and properties
|
||||||
|
|
||||||
PASS Tests\Features\Expect\HigherOrder\properties
|
PASS Tests\Features\Expect\HigherOrder\properties
|
||||||
✓ it allows properties to be accessed from the value
|
✓ it allows properties to be accessed from the value
|
||||||
@ -124,6 +126,7 @@
|
|||||||
✓ it works with sequence
|
✓ it works with sequence
|
||||||
✓ it can compose complex expectations
|
✓ it can compose complex expectations
|
||||||
✓ it works with objects
|
✓ it works with objects
|
||||||
|
✓ it works with nested properties
|
||||||
|
|
||||||
PASS Tests\Features\Expect\each
|
PASS Tests\Features\Expect\each
|
||||||
✓ an exception is thrown if the the type is not iterable
|
✓ an exception is thrown if the the type is not iterable
|
||||||
@ -554,5 +557,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 4 incompleted, 7 skipped, 340 passed
|
Tests: 4 incompleted, 7 skipped, 343 passed
|
||||||
|
|
||||||
@ -59,6 +59,14 @@ it('can compose complex expectations', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can handle nested method calls', function () {
|
||||||
|
expect(new HasMethods())
|
||||||
|
->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString()
|
||||||
|
->newInstance()->name()->toEqual('Has Methods')->not->toBeInt
|
||||||
|
->name()->toEqual('Has Methods')
|
||||||
|
->books()->each->toBeArray();
|
||||||
|
});
|
||||||
|
|
||||||
class HasMethods
|
class HasMethods
|
||||||
{
|
{
|
||||||
public function name()
|
public function name()
|
||||||
@ -97,4 +105,9 @@ class HasMethods
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function newInstance()
|
||||||
|
{
|
||||||
|
return new static();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,10 +14,20 @@ it('can access methods and properties', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can handle nested methods and properties', function () {
|
||||||
|
expect(new HasMethodsAndProperties())
|
||||||
|
->meta->foo->bar->toBeString()->toEqual('baz')->not->toBeInt
|
||||||
|
->newInstance()->meta->foo->toBeArray()
|
||||||
|
->newInstance()->multiply(2, 2)->toEqual(4)->not->toEqual(5)
|
||||||
|
->newInstance()->books()->toBeArray();
|
||||||
|
});
|
||||||
|
|
||||||
class HasMethodsAndProperties
|
class HasMethodsAndProperties
|
||||||
{
|
{
|
||||||
public $name = 'Has Methods and Properties';
|
public $name = 'Has Methods and Properties';
|
||||||
|
|
||||||
|
public $meta = ['foo' => ['bar' => 'baz']];
|
||||||
|
|
||||||
public $posts = [
|
public $posts = [
|
||||||
[
|
[
|
||||||
'is_published' => true,
|
'is_published' => true,
|
||||||
@ -47,4 +57,9 @@ class HasMethodsAndProperties
|
|||||||
{
|
{
|
||||||
return $x * $y;
|
return $x * $y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function newInstance()
|
||||||
|
{
|
||||||
|
return new static();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,6 +58,12 @@ it('works with objects', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('works with nested properties', function () {
|
||||||
|
expect(new HasProperties())
|
||||||
|
->nested->foo->bar->toBeString()->toEqual('baz')
|
||||||
|
->posts->toBeArray()->toHaveCount(2);
|
||||||
|
});
|
||||||
|
|
||||||
class HasProperties
|
class HasProperties
|
||||||
{
|
{
|
||||||
public $name = 'foo';
|
public $name = 'foo';
|
||||||
@ -72,4 +78,8 @@ class HasProperties
|
|||||||
'title' => 'Bar',
|
'title' => 'Bar',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public $nested = [
|
||||||
|
'foo' => ['bar' => 'baz'],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user