diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 883e3fd6..0151ddd3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] - php: ['7.3', '7.4', '8.0'] + php: ['7.3', '7.4', '8.0', '8.1'] dependency-version: [prefer-lowest, prefer-stable] name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }} diff --git a/src/Expectation.php b/src/Expectation.php index e76a30bd..1b5d1f03 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -266,14 +266,16 @@ final class Expectation /** * Asserts that $needle is an element of the value. * - * @param mixed $needle + * @param mixed $needles */ - public function toContain($needle): Expectation + public function toContain(...$needles): Expectation { - if (is_string($this->value)) { - Assert::assertStringContainsString($needle, $this->value); - } else { - Assert::assertContains($needle, $this->value); + foreach ($needles as $needle) { + if (is_string($this->value)) { + Assert::assertStringContainsString($needle, $this->value); + } else { + Assert::assertContains($needle, $this->value); + } } return $this; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index fa2b86fa..57bc7e1d 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -325,9 +325,16 @@ PASS Tests\Features\Expect\toContain ✓ passes strings + ✓ passes strings with multiple needles ✓ passes arrays + ✓ passes arrays with multiple needles + ✓ passes with array needles ✓ failures + ✓ failures with multiple needles (all failing) + ✓ failures with multiple needles (some failing) ✓ not failures + ✓ not failures with multiple needles (all failing) + ✓ not failures with multiple needles (some failing) PASS Tests\Features\Expect\toEndWith ✓ pass @@ -578,9 +585,6 @@ PASS Tests\Visual\Help ✓ visual snapshot of help command output - PASS Tests\Visual\JUnit - ✓ it is can successfully call all public methods - PASS Tests\Visual\SingleTestOrDirectory ✓ allows to run a single test ✓ allows to run a directory @@ -590,9 +594,6 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - PASS Tests\Visual\TeamCity - ✓ it is can successfully call all public methods - PASS Tests\Features\Depends ✓ first ✓ second @@ -607,5 +608,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 383 passed + Tests: 4 incompleted, 9 skipped, 388 passed \ No newline at end of file diff --git a/tests/Features/Expect/toContain.php b/tests/Features/Expect/toContain.php index 04d586ff..0d17e56c 100644 --- a/tests/Features/Expect/toContain.php +++ b/tests/Features/Expect/toContain.php @@ -3,17 +3,45 @@ use PHPUnit\Framework\ExpectationFailedException; test('passes strings', function () { - expect([1, 2, 42])->toContain(42); + expect('Nuno')->toContain('Nu'); +}); + +test('passes strings with multiple needles', function () { + expect('Nuno')->toContain('Nu', 'no'); }); test('passes arrays', function () { - expect('Nuno')->toContain('Nu'); + expect([1, 2, 42])->toContain(42); +}); + +test('passes arrays with multiple needles', function () { + expect([1, 2, 42])->toContain(42, 2); +}); + +test('passes with array needles', function () { + expect([[1, 2, 3], 2, 42])->toContain(42, [1, 2, 3]); }); test('failures', function () { expect([1, 2, 42])->toContain(3); })->throws(ExpectationFailedException::class); +test('failures with multiple needles (all failing)', function () { + expect([1, 2, 42])->toContain(3, 4); +})->throws(ExpectationFailedException::class); + +test('failures with multiple needles (some failing)', function () { + expect([1, 2, 42])->toContain(1, 3, 4); +})->throws(ExpectationFailedException::class); + test('not failures', function () { expect([1, 2, 42])->not->toContain(42); })->throws(ExpectationFailedException::class); + +test('not failures with multiple needles (all failing)', function () { + expect([1, 2, 42])->not->toContain(42, 2); +})->throws(ExpectationFailedException::class); + +test('not failures with multiple needles (some failing)', function () { + expect([1, 2, 42])->not->toContain(42, 1); +})->throws(ExpectationFailedException::class);