Skip to content

Commit

Permalink
Merge pull request #974 from erikgaal/expect-to-contain-equals
Browse files Browse the repository at this point in the history
[2.x] Add `toContainEquals` expectation
  • Loading branch information
nunomaduro authored Jan 25, 2024
2 parents 2e7fec6 + e95c4ee commit 0aecd5d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ public function toContain(mixed ...$needles): self
return $this;
}

/**
* Asserts that $needle equal an element of the value.
*
* @return self<TValue>
*/
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.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Features/Expect/toContainEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;


test('passes arrays', function () {
expect([1, 2, 42])->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);

0 comments on commit 0aecd5d

Please sign in to comment.