From 6e8a34ef28810970c39ac1f8ff15b64a78d238ba Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 17 Oct 2024 02:15:36 +0100 Subject: [PATCH 1/2] toBeDeepOf() --- src/Mixins/Expectation.php | 16 +++++++++++++++ src/Pest.php | 18 +++++++++++++++++ tests/.snapshots/success.txt | 9 ++++++++- tests/Features/Expect/toBeDeepOf.php | 29 ++++++++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/Features/Expect/toBeDeepOf.php diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index f802bc11..5b953b39 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -27,6 +27,8 @@ use Throwable; use Traversable; +use function Pest\getArrayDepth; + /** * @internal * @@ -437,6 +439,20 @@ public function toBeArray(string $message = ''): self return $this; } + /** + * Asserts that the value is an array of depth = $depth. + * + * @return self + */ + public function toBeDeepOf(int $depth, string $message = ''): self + { + Assert::assertIsArray($this->value, $message); + + Assert::assertEquals(getArrayDepth($this->value), $depth, $message); + + return $this; + } + /** * Asserts that the value is a list. * diff --git a/src/Pest.php b/src/Pest.php index 3f5b230e..b6af0681 100644 --- a/src/Pest.php +++ b/src/Pest.php @@ -13,3 +13,21 @@ function testDirectory(string $file = ''): string { return TestSuite::getInstance()->testPath.DIRECTORY_SEPARATOR.$file; } + +/** + * Returns array depth. + * + * @param array $array + */ +function getArrayDepth(array $array): int +{ + $depth = 0; + + foreach ($array as $elem) { + if (is_array($elem)) { + $depth = getArrayDepth($elem) + 1; + } + } + + return $depth; +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index da75f2d2..756d5b7a 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -401,6 +401,13 @@ ✓ pass ✓ failures ✓ failures with custom message + ✓ not failures + + PASS Tests\Features\Expect\toBeDeepOf + ✓ pass + ✓ failures + ✓ failures when not array passed + ✓ failures with custom message ✓ not failures PASS Tests\Features\Expect\toBeDigits @@ -1584,4 +1591,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1095 passed (2648 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1100 passed (2669 assertions) \ No newline at end of file diff --git a/tests/Features/Expect/toBeDeepOf.php b/tests/Features/Expect/toBeDeepOf.php new file mode 100644 index 00000000..20ef8ae2 --- /dev/null +++ b/tests/Features/Expect/toBeDeepOf.php @@ -0,0 +1,29 @@ +group('deep'); + +test('pass', function () { + expect([])->toBeDeepOf(0); + expect([1, 2, 3])->toBeDeepOf(0); + expect([1, 2 => [1, 2], 3 => [1]])->toBeDeepOf(1); + expect([1, 2 => [1, 2], 3 => [1 => [1]]])->toBeDeepOf(2); + expect('1, 2, 3')->not->toBeDeepOf(1); +}); + +test('failures', function () { + expect([1, 2, 3])->toBeDeepOf(1); +})->throws(ExpectationFailedException::class); + +test('failures when not array passed', function () { + expect('not array')->toBeDeepOf(1); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect([1, 2, 3])->toBeDeepOf(1, 'oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect([1, 2, 3])->not->toBeDeepOf(0); +})->throws(ExpectationFailedException::class); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 59f7263a..176bc72c 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1085 passed (2624 assertions)') + ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1090 passed (2645 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows(); From b89f25a8c306d1c31e9ad73c2b5c07b8a0eb16f0 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 17 Oct 2024 02:29:30 +0100 Subject: [PATCH 2/2] make sure depth is greater or equal zero --- src/Mixins/Expectation.php | 1 + tests/.snapshots/success.txt | 3 ++- tests/Features/Expect/toBeDeepOf.php | 6 ++++-- tests/Visual/Parallel.php | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 5b953b39..3139f7bf 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -447,6 +447,7 @@ public function toBeArray(string $message = ''): self public function toBeDeepOf(int $depth, string $message = ''): self { Assert::assertIsArray($this->value, $message); + Assert::assertGreaterThanOrEqual(0, $depth, $message); Assert::assertEquals(getArrayDepth($this->value), $depth, $message); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 756d5b7a..6a08c5d3 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -407,6 +407,7 @@ ✓ pass ✓ failures ✓ failures when not array passed + ✓ failures when depth is negative ✓ failures with custom message ✓ not failures @@ -1591,4 +1592,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1100 passed (2669 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1101 passed (2687 assertions) \ No newline at end of file diff --git a/tests/Features/Expect/toBeDeepOf.php b/tests/Features/Expect/toBeDeepOf.php index 20ef8ae2..9eef6f09 100644 --- a/tests/Features/Expect/toBeDeepOf.php +++ b/tests/Features/Expect/toBeDeepOf.php @@ -2,8 +2,6 @@ use PHPUnit\Framework\ExpectationFailedException; -uses()->group('deep'); - test('pass', function () { expect([])->toBeDeepOf(0); expect([1, 2, 3])->toBeDeepOf(0); @@ -20,6 +18,10 @@ expect('not array')->toBeDeepOf(1); })->throws(ExpectationFailedException::class); +test('failures when depth is negative', function () { + expect([])->toBeDeepOf(-1); +})->throws(ExpectationFailedException::class); + test('failures with custom message', function () { expect([1, 2, 3])->toBeDeepOf(1, 'oh no!'); })->throws(ExpectationFailedException::class, 'oh no!'); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 176bc72c..e1821413 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1090 passed (2645 assertions)') + ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1091 passed (2663 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();