From 37c40cb7350585191651ce9b29054654b40ec4c5 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Tue, 3 Oct 2023 10:55:57 +0200 Subject: [PATCH 1/4] Add `toContainEquals` expectation --- src/Mixins/Expectation.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index e42fc96a..4473f553 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -196,6 +196,21 @@ public function toContain(mixed ...$needles): self return $this; } + /** + * Asserts that $needle equals an element of the value. + * + * @return self + */ + public function toContainEquals(mixed ...$needles): self + { + foreach ($needles as $needle) { + if (! is_iterable($this->value)) { + InvalidExpectationValue::expected('iterable'); + } + Assert::assertContainsEquals($needle, $this->value); + } + } + /** * Asserts that the value starts with $expected. * From 79f5973e5aa42fc5151f0084ff08f79d5f2beb14 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Tue, 3 Oct 2023 11:09:26 +0200 Subject: [PATCH 2/4] Add tests --- tests/Features/Expect/toContainEquals.php | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/Features/Expect/toContainEquals.php diff --git a/tests/Features/Expect/toContainEquals.php b/tests/Features/Expect/toContainEquals.php new file mode 100644 index 00000000..a789b41d --- /dev/null +++ b/tests/Features/Expect/toContainEquals.php @@ -0,0 +1,36 @@ +toContain('42'); +}); + +test('passes arrays with multiple needles', function () { + expect([1, 2, 42])->toContain('42', '2'); +}); + +test('failures', function () { + expect([1, 2, 42])->toContain('3'); +})->throws(ExpectationFailedException::class); + +test('failures with multiple needles (all failing)', function () { + expect([1, 2, 42])->toContainEquals('3', '4'); +})->throws(ExpectationFailedException::class); + +test('failures with multiple needles (some failing)', function () { + expect([1, 2, 42])->toContainEquals('1', '3', '4'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect([1, 2, 42])->not->toContainEquals('42'); +})->throws(ExpectationFailedException::class); + +test('not failures with multiple needles (all failing)', function () { + expect([1, 2, 42])->not->toContainEquals('42', '2'); +})->throws(ExpectationFailedException::class); + +test('not failures with multiple needles (some failing)', function () { + expect([1, 2, 42])->not->toContainEquals('42', '1'); +})->throws(ExpectationFailedException::class); From 15cd7187e96b5f1339d5ae3b3e033c369a7150de Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Mon, 6 Nov 2023 10:31:48 +0100 Subject: [PATCH 3/4] Update toContainEquals.php --- tests/Features/Expect/toContainEquals.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Features/Expect/toContainEquals.php b/tests/Features/Expect/toContainEquals.php index a789b41d..ea050fcd 100644 --- a/tests/Features/Expect/toContainEquals.php +++ b/tests/Features/Expect/toContainEquals.php @@ -4,15 +4,15 @@ test('passes arrays', function () { - expect([1, 2, 42])->toContain('42'); + expect([1, 2, 42])->toContainEquals('42'); }); test('passes arrays with multiple needles', function () { - expect([1, 2, 42])->toContain('42', '2'); + expect([1, 2, 42])->toContainEquals('42', '2'); }); test('failures', function () { - expect([1, 2, 42])->toContain('3'); + expect([1, 2, 42])->toContainEquals('3'); })->throws(ExpectationFailedException::class); test('failures with multiple needles (all failing)', function () { From e95c4ee636bd16063c5911d2977baba338006f0f Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 25 Jan 2024 14:38:44 +0000 Subject: [PATCH 4/4] feat(toContainEqual): adds method name --- src/Mixins/Expectation.php | 15 +++++++++------ .../{toContainEquals.php => toContainEqual.php} | 16 ++++++++-------- 2 files changed, 17 insertions(+), 14 deletions(-) rename tests/Features/Expect/{toContainEquals.php => toContainEqual.php} (65%) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 4473f553..92bf9e93 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -197,18 +197,21 @@ public function toContain(mixed ...$needles): self } /** - * Asserts that $needle equals an element of the value. - * + * Asserts that $needle equal an element of the value. + * * @return self */ - public function toContainEquals(mixed ...$needles): self + public function toContainEqual(mixed ...$needles): self { + if (! is_iterable($this->value)) { + InvalidExpectationValue::expected('iterable'); + } + foreach ($needles as $needle) { - if (! is_iterable($this->value)) { - InvalidExpectationValue::expected('iterable'); - } Assert::assertContainsEquals($needle, $this->value); } + + return $this; } /** diff --git a/tests/Features/Expect/toContainEquals.php b/tests/Features/Expect/toContainEqual.php similarity index 65% rename from tests/Features/Expect/toContainEquals.php rename to tests/Features/Expect/toContainEqual.php index ea050fcd..efe74920 100644 --- a/tests/Features/Expect/toContainEquals.php +++ b/tests/Features/Expect/toContainEqual.php @@ -4,33 +4,33 @@ test('passes arrays', function () { - expect([1, 2, 42])->toContainEquals('42'); + expect([1, 2, 42])->toContainEqual('42'); }); test('passes arrays with multiple needles', function () { - expect([1, 2, 42])->toContainEquals('42', '2'); + expect([1, 2, 42])->toContainEqual('42', '2'); }); test('failures', function () { - expect([1, 2, 42])->toContainEquals('3'); + expect([1, 2, 42])->toContainEqual('3'); })->throws(ExpectationFailedException::class); test('failures with multiple needles (all failing)', function () { - expect([1, 2, 42])->toContainEquals('3', '4'); + expect([1, 2, 42])->toContainEqual('3', '4'); })->throws(ExpectationFailedException::class); test('failures with multiple needles (some failing)', function () { - expect([1, 2, 42])->toContainEquals('1', '3', '4'); + expect([1, 2, 42])->toContainEqual('1', '3', '4'); })->throws(ExpectationFailedException::class); test('not failures', function () { - expect([1, 2, 42])->not->toContainEquals('42'); + expect([1, 2, 42])->not->toContainEqual('42'); })->throws(ExpectationFailedException::class); test('not failures with multiple needles (all failing)', function () { - expect([1, 2, 42])->not->toContainEquals('42', '2'); + expect([1, 2, 42])->not->toContainEqual('42', '2'); })->throws(ExpectationFailedException::class); test('not failures with multiple needles (some failing)', function () { - expect([1, 2, 42])->not->toContainEquals('42', '1'); + expect([1, 2, 42])->not->toContainEqual('42', '1'); })->throws(ExpectationFailedException::class);