shlinkio /
shlink-common
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ShlinkioTest\Shlink\Common\Validation; |
||
| 6 | |||
| 7 | use Laminas\Filter; |
||
| 8 | use Laminas\InputFilter\Input; |
||
| 9 | use Laminas\Validator; |
||
| 10 | use PHPUnit\Framework\TestCase; |
||
| 11 | use ReflectionObject; |
||
| 12 | use Shlinkio\Shlink\Common\Validation\ExcludingValidatorChain; |
||
| 13 | use Shlinkio\Shlink\Common\Validation\InputFactoryTrait; |
||
| 14 | |||
| 15 | use function Functional\map; |
||
| 16 | use function get_class; |
||
| 17 | |||
| 18 | class InputFactoryTraitTest extends TestCase |
||
| 19 | { |
||
| 20 | use InputFactoryTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @test |
||
| 24 | * @dataProvider provideInputArgs |
||
| 25 | */ |
||
| 26 | public function basicInputIsCreatedWithDefaultFilters(array $args, bool $required): void |
||
| 27 | { |
||
| 28 | $input = $this->createInput(...$args); |
||
| 29 | $filters = $this->getFiltersFromInput($input); |
||
| 30 | |||
| 31 | $this->assertEquals($required, $input->isRequired()); |
||
| 32 | $this->assertCount(2, $filters); |
||
| 33 | $this->assertContains(Filter\StripTags::class, $filters); |
||
| 34 | $this->assertContains(Filter\StringTrim::class, $filters); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @test |
||
| 39 | * @dataProvider provideInputArgs |
||
| 40 | */ |
||
| 41 | public function arrayInputIsCreatedWithDefaultFilters(array $args, bool $required): void |
||
| 42 | { |
||
| 43 | $input = $this->createArrayInput(...$args); |
||
| 44 | $filters = $this->getFiltersFromInput($input); |
||
| 45 | |||
| 46 | $this->assertEquals($required, $input->isRequired()); |
||
| 47 | $this->assertCount(2, $filters); |
||
| 48 | $this->assertContains(Filter\StripTags::class, $filters); |
||
| 49 | $this->assertContains(Filter\StringTrim::class, $filters); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @test |
||
| 54 | * @dataProvider provideInputArgs |
||
| 55 | */ |
||
| 56 | public function booleanInputIsCreatedAsExpected(array $args, bool $required): void |
||
| 57 | { |
||
| 58 | $input = $this->createBooleanInput(...$args); |
||
| 59 | $filters = $this->getFiltersFromInput($input); |
||
| 60 | $validators = $input->getValidatorChain()->getValidators(); |
||
| 61 | |||
| 62 | $this->assertEquals($required, $input->isRequired()); |
||
| 63 | $this->assertCount(1, $validators); |
||
| 64 | $this->assertCount(3, $filters); |
||
| 65 | $this->assertContains(Filter\StripTags::class, $filters); |
||
| 66 | $this->assertContains(Filter\StringTrim::class, $filters); |
||
| 67 | $this->assertContains(Filter\Boolean::class, $filters); |
||
| 68 | |||
| 69 | /** @var Validator\NotEmpty $notEmptyValidator */ |
||
| 70 | $notEmptyValidator = $validators[0]['instance']; |
||
| 71 | $calculateTypeValue = (fn (array $type) => $this->calculateTypeValue($type))->bindTo( |
||
| 72 | $notEmptyValidator, |
||
| 73 | Validator\NotEmpty::class, |
||
| 74 | ); |
||
| 75 | $this->assertInstanceOf(Validator\NotEmpty::class, $notEmptyValidator); |
||
| 76 | $this->assertEquals($calculateTypeValue([ |
||
| 77 | Validator\NotEmpty::OBJECT, |
||
| 78 | Validator\NotEmpty::SPACE, |
||
| 79 | Validator\NotEmpty::NULL, |
||
| 80 | Validator\NotEmpty::EMPTY_ARRAY, |
||
| 81 | Validator\NotEmpty::STRING, |
||
| 82 | ]), $notEmptyValidator->getType()); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @test |
||
| 87 | * @dataProvider provideInputArgs |
||
| 88 | */ |
||
| 89 | public function dateInputIsCreatedAsExpected(array $args, bool $required): void |
||
| 90 | { |
||
| 91 | $input = $this->createDateInput(...$args); |
||
| 92 | $filters = $this->getFiltersFromInput($input); |
||
| 93 | $validators = $input->getValidatorChain()->getValidators(); |
||
| 94 | |||
| 95 | $this->assertEquals($required, $input->isRequired()); |
||
| 96 | $this->assertCount(1, $validators); |
||
| 97 | $this->assertCount(2, $filters); |
||
| 98 | $this->assertContains(Filter\StripTags::class, $filters); |
||
| 99 | $this->assertContains(Filter\StringTrim::class, $filters); |
||
| 100 | |||
| 101 | /** @var ExcludingValidatorChain $excludingValidator */ |
||
| 102 | $excludingValidator = $validators[0]['instance']; |
||
| 103 | $this->assertInstanceOf(ExcludingValidatorChain::class, $excludingValidator); |
||
| 104 | |||
| 105 | $ref = new ReflectionObject($excludingValidator); |
||
| 106 | $prop = $ref->getProperty('validators'); |
||
| 107 | $prop->setAccessible(true); |
||
| 108 | $validators = $prop->getValue($excludingValidator); |
||
| 109 | $this->assertCount(2, $validators); |
||
| 110 | $this->assertInstanceOf(Validator\Date::class, $validators[0]); |
||
| 111 | $this->assertInstanceOf(Validator\Date::class, $validators[1]); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function provideInputArgs(): iterable |
||
| 115 | { |
||
| 116 | yield [['foo', true], true]; |
||
| 117 | yield [['foo', false], false]; |
||
| 118 | yield [['foo'], true]; |
||
| 119 | } |
||
| 120 | |||
| 121 | private function getFiltersFromInput(Input $input): array |
||
| 122 | { |
||
| 123 | return map( |
||
| 124 | $input->getFilterChain()->getFilters()->toArray(), |
||
| 125 | fn (Filter\FilterInterface $filter): string => get_class($filter), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 126 | ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 |