Merge pull request #457 from cerbero90/feature/avoid-nested-expectations

Avoid nested expectations with `and()`
This commit is contained in:
Nuno Maduro
2022-01-16 13:53:22 +00:00
committed by GitHub
3 changed files with 11 additions and 2 deletions

View File

@ -56,7 +56,7 @@ final class Expectation
*/ */
public function and(mixed $value): Expectation public function and(mixed $value): Expectation
{ {
return new self($value); return $value instanceof static ? $value : new self($value);
} }
/** /**

View File

@ -44,7 +44,7 @@ final class HigherOrderCallables
* *
* @param callable|TValue $value * @param callable|TValue $value
* *
* @return Expectation<TValue> * @return Expectation<(callable(): mixed)|TValue>
*/ */
public function and(mixed $value) public function and(mixed $value)
{ {

View File

@ -48,6 +48,15 @@ it('can start a new higher order expectation using the and syntax in higher orde
->toBeArray() ->toBeArray()
->foo->toEqual('bar'); ->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 class HasMethodsAndProperties
{ {
public $name = 'Has Methods and Properties'; public $name = 'Has Methods and Properties';