From f3a748fee363850c4163a98a7de31b3ab57bbfd6 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 19 Sep 2022 11:00:13 +0200 Subject: [PATCH] create Any matcher --- src/Expectation.php | 6 ++++++ src/Matchers/Any.php | 9 +++++++++ src/Mixins/Expectation.php | 10 +++++----- src/Support/NullValue.php | 9 --------- tests/.snapshots/success.txt | 5 ++++- tests/Features/Expect/toHaveKey.php | 8 ++++++++ tests/Features/Expect/toHaveProperty.php | 4 ++++ 7 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 src/Matchers/Any.php delete mode 100644 src/Support/NullValue.php diff --git a/src/Expectation.php b/src/Expectation.php index 846d8705..2cfa4a98 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -14,6 +14,7 @@ use Pest\Exceptions\InvalidExpectationValue; use Pest\Expectations\EachExpectation; use Pest\Expectations\HigherOrderExpectation; use Pest\Expectations\OppositeExpectation; +use Pest\Matchers\Any; use Pest\Support\ExpectationPipeline; use PHPUnit\Framework\Assert; use PHPUnit\Framework\ExpectationFailedException; @@ -339,4 +340,9 @@ final class Expectation || method_exists(Mixins\Expectation::class, $name) || self::hasExtend($name); } + + public function any(): Any + { + return new Any(); + } } diff --git a/src/Matchers/Any.php b/src/Matchers/Any.php new file mode 100644 index 00000000..b3a308a9 --- /dev/null +++ b/src/Matchers/Any.php @@ -0,0 +1,9 @@ + */ - public function toHaveProperty(string $name, mixed $value = new NullValue(), string $failureMessage = ''): self + public function toHaveProperty(string $name, mixed $value = new Any(), string $failureMessage = ''): self { $this->toBeObject(); // @phpstan-ignore-next-line Assert::assertTrue(property_exists($this->value, $name), $failureMessage); - if (! $value instanceof NullValue) { + if (! $value instanceof Any) { /* @phpstan-ignore-next-line */ Assert::assertEquals($value, $this->value->{$name}, $failureMessage); } @@ -559,7 +559,7 @@ final class Expectation * * @return self */ - public function toHaveKey(string|int $key, mixed $value = new NullValue(), string $failureMessage = ''): self + public function toHaveKey(string|int $key, mixed $value = new Any(), string $failureMessage = ''): self { if (is_object($this->value) && method_exists($this->value, 'toArray')) { $array = $this->value->toArray(); @@ -579,7 +579,7 @@ final class Expectation throw new ExpectationFailedException($failureMessage, $exception->getComparisonFailure()); } - if (! $value instanceof NullValue) { + if (! $value instanceof Any) { Assert::assertEquals($value, Arr::get($array, $key), $failureMessage); } diff --git a/src/Support/NullValue.php b/src/Support/NullValue.php deleted file mode 100644 index eb93044e..00000000 --- a/src/Support/NullValue.php +++ /dev/null @@ -1,9 +0,0 @@ -toHaveKey('foo', failureMessage: 'oh no!'); })->throws(ExpectationFailedException::class, 'oh no!'); +test('failures with custom message and Any matcher', function () use ($test_array) { + expect($test_array)->toHaveKey('foo', expect()->any(), 'oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + test('failures with nested key', function () use ($test_array) { expect($test_array)->toHaveKey('d.bar'); })->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'd.bar'"); @@ -36,6 +40,10 @@ test('failures with nested key and custom message', function () use ($test_array expect($test_array)->toHaveKey('d.bar', failureMessage: 'oh no!'); })->throws(ExpectationFailedException::class, 'oh no!'); +test('failures with nested key and custom message with Any matcher', function () use ($test_array) { + expect($test_array)->toHaveKey('d.bar', expect()->any(), 'oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + test('failures with plain key with dots', function () use ($test_array) { expect($test_array)->toHaveKey('missing.key.with.dots'); })->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'missing.key.with.dots'"); diff --git a/tests/Features/Expect/toHaveProperty.php b/tests/Features/Expect/toHaveProperty.php index 538d1663..8547f66e 100644 --- a/tests/Features/Expect/toHaveProperty.php +++ b/tests/Features/Expect/toHaveProperty.php @@ -21,6 +21,10 @@ test('failures with message', function () use ($obj) { expect($obj)->toHaveProperty(name: 'bar', failureMessage: 'oh no!'); })->throws(ExpectationFailedException::class, 'oh no!'); +test('failures with message and Any matcher', function () use ($obj) { + expect($obj)->toHaveProperty('bar', expect()->any(), 'oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + test('not failures', function () use ($obj) { expect($obj)->not->toHaveProperty('foo'); })->throws(ExpectationFailedException::class);