mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Merge pull request #457 from cerbero90/feature/avoid-nested-expectations
Avoid nested expectations with `and()`
This commit is contained in:
@ -56,7 +56,7 @@ final class Expectation
|
||||
*/
|
||||
public function and(mixed $value): Expectation
|
||||
{
|
||||
return new self($value);
|
||||
return $value instanceof static ? $value : new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -44,7 +44,7 @@ final class HigherOrderCallables
|
||||
*
|
||||
* @param callable|TValue $value
|
||||
*
|
||||
* @return Expectation<TValue>
|
||||
* @return Expectation<(callable(): mixed)|TValue>
|
||||
*/
|
||||
public function and(mixed $value)
|
||||
{
|
||||
|
||||
@ -48,6 +48,15 @@ it('can start a new higher order expectation using the and syntax in higher orde
|
||||
->toBeArray()
|
||||
->foo->toEqual('bar');
|
||||
|
||||
it('can start a new higher order expectation using the and syntax without nesting expectations', function () {
|
||||
expect(new HasMethodsAndProperties())
|
||||
->toBeInstanceOf(HasMethodsAndProperties::class)
|
||||
->meta
|
||||
->sequence(
|
||||
function ($value, $key) { $value->toBeArray()->and($key)->toBe('foo'); },
|
||||
);
|
||||
});
|
||||
|
||||
class HasMethodsAndProperties
|
||||
{
|
||||
public $name = 'Has Methods and Properties';
|
||||
|
||||
Reference in New Issue
Block a user