Skip to content

Commit de4ebe2

Browse files
committed
test: Integration test for recursion depth check in Validator and Filter
1 parent 74ae9ea commit de4ebe2

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Test\Integration\Filter;
4+
5+
use Magento\Framework\App\ObjectManager;
6+
use Magento\TestFramework\Fixture\AppArea;
7+
use PHPUnit\Framework\TestCase;
8+
use Loki\Components\Component\ComponentInterface;
9+
use Loki\Components\Exception\RecursionException;
10+
use Loki\Components\Filter\Filter;
11+
12+
#[AppArea('frontend')]
13+
class FilterTest extends TestCase
14+
{
15+
public function testDeeplyNestedArrayThrowsRecursionException(): void
16+
{
17+
$filter = ObjectManager::getInstance()->get(Filter::class);
18+
$component = $this->createComponent();
19+
20+
$this->expectException(RecursionException::class);
21+
$this->expectExceptionMessage('Too many array levels');
22+
23+
$filter->filter($component, $this->buildNestedArray(12));
24+
}
25+
26+
public function testShallowArrayDoesNotThrow(): void
27+
{
28+
$filter = ObjectManager::getInstance()->get(Filter::class);
29+
$component = $this->createComponent();
30+
31+
$data = $this->buildNestedArray(3);
32+
$result = $filter->filter($component, $data);
33+
34+
$this->assertSame($data, $result);
35+
}
36+
37+
private function createComponent(): ComponentInterface
38+
{
39+
$component = $this->createMock(ComponentInterface::class);
40+
$component->method('getFilters')->willReturn([]);
41+
42+
return $component;
43+
}
44+
45+
private function buildNestedArray(int $levels): array
46+
{
47+
$value = 'value';
48+
for ($i = 0; $i < $levels; $i++) {
49+
$value = [$value];
50+
}
51+
52+
return $value;
53+
}
54+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Test\Integration\Validator;
4+
5+
use Loki\Components\Component\ComponentInterface;
6+
use Loki\Components\Exception\RecursionException;
7+
use Loki\Components\Validator\Validator;
8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\TestFramework\Fixture\AppArea;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[AppArea('frontend')]
13+
class ValidatorTest extends TestCase
14+
{
15+
public function testThrowsRecursionExceptionWhenDepthExceeded(): void
16+
{
17+
$validator = ObjectManager::getInstance()->get(Validator::class);
18+
$component = $this->createMock(ComponentInterface::class);
19+
20+
$this->expectException(RecursionException::class);
21+
$this->expectExceptionMessage('Too many array levels');
22+
23+
$validator->validate($component, $this->buildNestedArray(12));
24+
}
25+
26+
public function testDoesNotThrowForShallowData(): void
27+
{
28+
$validator = ObjectManager::getInstance()->get(Validator::class);
29+
30+
$component = $this->createMock(ComponentInterface::class);
31+
$component->method('getValidators')->willReturn([]);
32+
33+
$this->assertTrue($validator->validate($component, 'foo'));
34+
$this->assertTrue($validator->validate($component, ['foo', 'bar']));
35+
}
36+
37+
public function testDepthArgumentTriggersRecursionExceptionDirectly(): void
38+
{
39+
$validator = ObjectManager::getInstance()->get(Validator::class);
40+
$component = $this->createMock(ComponentInterface::class);
41+
42+
$this->expectException(RecursionException::class);
43+
$this->expectExceptionMessage('Too many array levels');
44+
45+
$validator->validate($component, null, '', 10);
46+
}
47+
48+
private function buildNestedArray(int $levels): array
49+
{
50+
$data = ['value'];
51+
for ($i = 0; $i < $levels; $i++) {
52+
$data = [$data];
53+
}
54+
55+
return $data;
56+
}
57+
}

0 commit comments

Comments
 (0)