diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 0d0b4d92..6d723876 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -196,6 +196,24 @@ public function toContain(mixed ...$needles): self return $this; } + /** + * Asserts that $needle equal an element of the value. + * + * @return self + */ + public function toContainEqual(mixed ...$needles): self + { + if (! is_iterable($this->value)) { + InvalidExpectationValue::expected('iterable'); + } + + foreach ($needles as $needle) { + Assert::assertContainsEquals($needle, $this->value); + } + + return $this; + } + /** * Asserts that the value starts with $expected. * diff --git a/tests/Features/Expect/toContainEqual.php b/tests/Features/Expect/toContainEqual.php new file mode 100644 index 00000000..efe74920 --- /dev/null +++ b/tests/Features/Expect/toContainEqual.php @@ -0,0 +1,36 @@ +toContainEqual('42'); +}); + +test('passes arrays with multiple needles', function () { + expect([1, 2, 42])->toContainEqual('42', '2'); +}); + +test('failures', function () { + expect([1, 2, 42])->toContainEqual('3'); +})->throws(ExpectationFailedException::class); + +test('failures with multiple needles (all failing)', function () { + expect([1, 2, 42])->toContainEqual('3', '4'); +})->throws(ExpectationFailedException::class); + +test('failures with multiple needles (some failing)', function () { + expect([1, 2, 42])->toContainEqual('1', '3', '4'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect([1, 2, 42])->not->toContainEqual('42'); +})->throws(ExpectationFailedException::class); + +test('not failures with multiple needles (all failing)', function () { + 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->toContainEqual('42', '1'); +})->throws(ExpectationFailedException::class);