mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Adds support for scoped in HigherOrderExpectations.
This commit is contained in:
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Expectations;
|
namespace Pest\Expectations;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Pest\Concerns\Retrievable;
|
use Pest\Concerns\Retrievable;
|
||||||
use Pest\Expectation;
|
use Pest\Expectation;
|
||||||
|
|
||||||
@ -79,6 +80,21 @@ final class HigherOrderExpectation
|
|||||||
return $this->expect($value);
|
return $this->expect($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scope an expectation callback to the current value in
|
||||||
|
* the HigherOrderExpectation chain.
|
||||||
|
*
|
||||||
|
* @param Closure(Expectation<TValue>): void $expectation
|
||||||
|
*
|
||||||
|
* @return HigherOrderExpectation<TOriginalValue, TOriginalValue>
|
||||||
|
*/
|
||||||
|
public function scoped(Closure $expectation): self
|
||||||
|
{
|
||||||
|
$expectation->__invoke($this->expectation);
|
||||||
|
|
||||||
|
return new self($this->original, $this->original->value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class with the given arguments.
|
* Dynamically calls methods on the class with the given arguments.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -74,6 +74,23 @@ it('works with higher order tests')
|
|||||||
->name()->toEqual('Has Methods')
|
->name()->toEqual('Has Methods')
|
||||||
->books()->each->toBeArray;
|
->books()->each->toBeArray;
|
||||||
|
|
||||||
|
it('can use the scoped method to lock into the given level for expectations', function () {
|
||||||
|
expect(new HasMethods())
|
||||||
|
->attributes()->scoped(fn ($attributes) => $attributes
|
||||||
|
->name->toBe('Has Methods')
|
||||||
|
->quantity->toBe(20)
|
||||||
|
)
|
||||||
|
->name()->toBeString()->toBe('Has Methods')
|
||||||
|
->newInstance()->newInstance()->scoped(fn ($instance) => $instance
|
||||||
|
->name()->toBe('Has Methods')
|
||||||
|
->quantity()->toBe(20)
|
||||||
|
->attributes()->scoped(fn ($attributes) => $attributes
|
||||||
|
->name->toBe('Has Methods')
|
||||||
|
->quantity->toBe(20)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
class HasMethods
|
class HasMethods
|
||||||
{
|
{
|
||||||
public function name()
|
public function name()
|
||||||
|
|||||||
Reference in New Issue
Block a user