create Any matcher

This commit is contained in:
Fabio Ivona
2022-09-19 11:00:13 +02:00
parent 8275d7e08d
commit f3a748fee3
7 changed files with 36 additions and 15 deletions

View File

@ -14,6 +14,7 @@ use Pest\Exceptions\InvalidExpectationValue;
use Pest\Expectations\EachExpectation; use Pest\Expectations\EachExpectation;
use Pest\Expectations\HigherOrderExpectation; use Pest\Expectations\HigherOrderExpectation;
use Pest\Expectations\OppositeExpectation; use Pest\Expectations\OppositeExpectation;
use Pest\Matchers\Any;
use Pest\Support\ExpectationPipeline; use Pest\Support\ExpectationPipeline;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
@ -339,4 +340,9 @@ final class Expectation
|| method_exists(Mixins\Expectation::class, $name) || method_exists(Mixins\Expectation::class, $name)
|| self::hasExtend($name); || self::hasExtend($name);
} }
public function any(): Any
{
return new Any();
}
} }

9
src/Matchers/Any.php Normal file
View File

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Pest\Matchers;
final class Any
{
}

View File

@ -9,9 +9,9 @@ use Closure;
use Error; use Error;
use InvalidArgumentException; use InvalidArgumentException;
use Pest\Exceptions\InvalidExpectationValue; use Pest\Exceptions\InvalidExpectationValue;
use Pest\Matchers\Any;
use Pest\Support\Arr; use Pest\Support\Arr;
use Pest\Support\NullClosure; use Pest\Support\NullClosure;
use Pest\Support\NullValue;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
@ -272,14 +272,14 @@ final class Expectation
* *
* @return self<TValue> * @return self<TValue>
*/ */
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(); $this->toBeObject();
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
Assert::assertTrue(property_exists($this->value, $name), $failureMessage); Assert::assertTrue(property_exists($this->value, $name), $failureMessage);
if (! $value instanceof NullValue) { if (! $value instanceof Any) {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
Assert::assertEquals($value, $this->value->{$name}, $failureMessage); Assert::assertEquals($value, $this->value->{$name}, $failureMessage);
} }
@ -559,7 +559,7 @@ final class Expectation
* *
* @return self<TValue> * @return self<TValue>
*/ */
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')) { if (is_object($this->value) && method_exists($this->value, 'toArray')) {
$array = $this->value->toArray(); $array = $this->value->toArray();
@ -579,7 +579,7 @@ final class Expectation
throw new ExpectationFailedException($failureMessage, $exception->getComparisonFailure()); throw new ExpectationFailedException($failureMessage, $exception->getComparisonFailure());
} }
if (! $value instanceof NullValue) { if (! $value instanceof Any) {
Assert::assertEquals($value, Arr::get($array, $key), $failureMessage); Assert::assertEquals($value, Arr::get($array, $key), $failureMessage);
} }

View File

@ -1,9 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
final class NullValue
{
}

View File

@ -512,8 +512,10 @@
✓ pass with value check and plain key with dots ✓ pass with value check and plain key with dots
✓ failures ✓ failures
✓ failures with custom message ✓ failures with custom message
✓ failures with custom message and Any matcher
✓ failures with nested key ✓ failures with nested key
✓ failures with nested key and custom message ✓ failures with nested key and custom message
✓ failures with nested key and custom message with Any matcher
✓ failures with plain key with dots ✓ failures with plain key with dots
✓ fails with wrong value ✓ fails with wrong value
✓ fails with wrong value and nested key ✓ fails with wrong value and nested key
@ -556,6 +558,7 @@
✓ pass ✓ pass
✓ failures ✓ failures
✓ failures with message ✓ failures with message
✓ failures with message and Any matcher
✓ not failures ✓ not failures
PASS Tests\Features\Expect\toMatch PASS Tests\Features\Expect\toMatch
@ -815,4 +818,4 @@
PASS Tests\Visual\Version PASS Tests\Visual\Version
✓ visual snapshot of help command output ✓ visual snapshot of help command output
Tests: 4 incomplete, 1 todo, 18 skipped, 559 passed (1450 assertions) Tests: 4 incomplete, 1 todo, 18 skipped, 562 passed (1460 assertions)

View File

@ -28,6 +28,10 @@ test('failures with custom message', function () use ($test_array) {
expect($test_array)->toHaveKey('foo', failureMessage: 'oh no!'); expect($test_array)->toHaveKey('foo', failureMessage: 'oh no!');
})->throws(ExpectationFailedException::class, '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) { test('failures with nested key', function () use ($test_array) {
expect($test_array)->toHaveKey('d.bar'); expect($test_array)->toHaveKey('d.bar');
})->throws(ExpectationFailedException::class, "Failed asserting that an array has the key '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!'); expect($test_array)->toHaveKey('d.bar', failureMessage: 'oh no!');
})->throws(ExpectationFailedException::class, '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) { test('failures with plain key with dots', function () use ($test_array) {
expect($test_array)->toHaveKey('missing.key.with.dots'); expect($test_array)->toHaveKey('missing.key.with.dots');
})->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'missing.key.with.dots'"); })->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'missing.key.with.dots'");

View File

@ -21,6 +21,10 @@ test('failures with message', function () use ($obj) {
expect($obj)->toHaveProperty(name: 'bar', failureMessage: 'oh no!'); expect($obj)->toHaveProperty(name: 'bar', failureMessage: 'oh no!');
})->throws(ExpectationFailedException::class, '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) { test('not failures', function () use ($obj) {
expect($obj)->not->toHaveProperty('foo'); expect($obj)->not->toHaveProperty('foo');
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);