Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toBeDeepOf() #1300

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use Throwable;
use Traversable;

use function Pest\getArrayDepth;

/**
* @internal
*
Expand Down Expand Up @@ -437,6 +439,21 @@ public function toBeArray(string $message = ''): self
return $this;
}

/**
* Asserts that the value is an array of depth = $depth.
*
* @return self<TValue>
*/
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.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ function testDirectory(string $file = ''): string
{
return TestSuite::getInstance()->testPath.DIRECTORY_SEPARATOR.$file;
}

/**
* Returns array depth.
*
* @param array<mixed> $array
*/
function getArrayDepth(array $array): int
{
$depth = 0;

foreach ($array as $elem) {
if (is_array($elem)) {
$depth = getArrayDepth($elem) + 1;
}
}

return $depth;
}
10 changes: 9 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1101 passed (2687 assertions)
31 changes: 31 additions & 0 deletions tests/Features/Expect/toBeDeepOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

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 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);
2 changes: 1 addition & 1 deletion tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down