diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index f802bc11..3139f7bf 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,21 @@ 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::assertGreaterThanOrEqual(0, $depth, $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..6a08c5d3 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -401,6 +401,14 @@ ✓ pass ✓ failures ✓ failures with custom message + ✓ not failures + + PASS Tests\Features\Expect\toBeDeepOf + ✓ pass + ✓ failures + ✓ failures when not array passed + ✓ failures when depth is negative + ✓ failures with custom message ✓ not failures PASS Tests\Features\Expect\toBeDigits @@ -1584,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, 1095 passed (2648 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 new file mode 100644 index 00000000..9eef6f09 --- /dev/null +++ b/tests/Features/Expect/toBeDeepOf.php @@ -0,0 +1,31 @@ +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 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!'); + +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..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, 1085 passed (2624 assertions)') + ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1091 passed (2663 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();