|
| 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