From dfcdaa3f8e4561db0b08f1d72593732d50ab7b66 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Tue, 22 Aug 2023 11:10:25 +0700 Subject: [PATCH 1/3] feat: `toHaveSameSize` expectation --- src/Mixins/Expectation.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 6b69014b..57040711 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -6,6 +6,7 @@ namespace Pest\Mixins; use BadMethodCallException; use Closure; +use Countable; use DateTimeInterface; use Error; use InvalidArgumentException; @@ -272,6 +273,22 @@ final class Expectation return $this; } + /** + * Asserts that the size of the value and $expected are the same. + * + * @return self + */ + public function toHaveSameSize(Countable|iterable $expected, string $message = ''): self + { + if (! is_countable($this->value) && ! is_iterable($this->value)) { + InvalidExpectationValue::expected('countable|iterable'); + } + + Assert::assertSameSize($expected, $this->value, $message); + + return $this; + } + /** * Asserts that the value contains the property $name. * From 2192373becf6a6483853393668236b4d6ac27533 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Tue, 22 Aug 2023 11:10:38 +0700 Subject: [PATCH 2/3] test: `toHaveSameSize` --- tests/Features/Expect/toHaveSameSize.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/Features/Expect/toHaveSameSize.php diff --git a/tests/Features/Expect/toHaveSameSize.php b/tests/Features/Expect/toHaveSameSize.php new file mode 100644 index 00000000..9039f0c1 --- /dev/null +++ b/tests/Features/Expect/toHaveSameSize.php @@ -0,0 +1,24 @@ +toHaveSameSize([1]); +})->throws(InvalidExpectationValue::class, 'Invalid expectation value type. Expected [countable|iterable].'); + +test('pass', function () { + expect([1, 2, 3])->toHaveSameSize([4, 5, 6]); +}); + +test('failures', function () { + expect([1, 2, 3])->toHaveSameSize([1]); +})->throws(ExpectationFailedException::class); + +test('failures with message', function () { + expect([1, 2, 3])->toHaveSameSize([1], 'oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect([1, 2, 3])->not->toHaveSameSize([1]); +}); From b6e3ffafa7520076157a470af082f2ccf26a2c6e Mon Sep 17 00:00:00 2001 From: Thai Nguyen Hung Date: Wed, 23 Aug 2023 08:14:27 +0700 Subject: [PATCH 3/3] fix: phpstan --- src/Mixins/Expectation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 57040711..d9ff744d 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -6,7 +6,6 @@ namespace Pest\Mixins; use BadMethodCallException; use Closure; -use Countable; use DateTimeInterface; use Error; use InvalidArgumentException; @@ -276,9 +275,10 @@ final class Expectation /** * Asserts that the size of the value and $expected are the same. * + * @param array $expected * @return self */ - public function toHaveSameSize(Countable|iterable $expected, string $message = ''): self + public function toHaveSameSize(iterable $expected, string $message = ''): self { if (! is_countable($this->value) && ! is_iterable($this->value)) { InvalidExpectationValue::expected('countable|iterable');