1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Validation; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Shlinkio\Shlink\Common\Validation\InputFactoryTrait; |
8
|
|
|
use Zend\Filter; |
9
|
|
|
use Zend\InputFilter\Input; |
10
|
|
|
use Zend\Validator; |
11
|
|
|
|
12
|
|
|
use function Functional\map; |
13
|
|
|
use function get_class; |
14
|
|
|
|
15
|
|
|
class InputFactoryTraitTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
use InputFactoryTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
* @dataProvider provideRequired |
22
|
|
|
*/ |
23
|
|
|
public function basicInputIsCreatedWithDefaultFilters(bool $required): void |
24
|
|
|
{ |
25
|
|
|
$input = $this->createInput('foo', $required); |
26
|
|
|
$filters = $this->getFiltersFromInput($input); |
27
|
|
|
|
28
|
|
|
$this->assertEquals($required, $input->isRequired()); |
29
|
|
|
$this->assertCount(2, $filters); |
30
|
|
|
$this->assertContains(Filter\StripTags::class, $filters); |
31
|
|
|
$this->assertContains(Filter\StringTrim::class, $filters); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
* @dataProvider provideRequired |
37
|
|
|
*/ |
38
|
|
|
public function booleanInputIsCreatedAsExpected(bool $required): void |
39
|
|
|
{ |
40
|
|
|
$input = $this->createBooleanInput('foo', $required); |
41
|
|
|
$filters = $this->getFiltersFromInput($input); |
42
|
|
|
$validators = $input->getValidatorChain()->getValidators(); |
43
|
|
|
|
44
|
|
|
$this->assertEquals($required, $input->isRequired()); |
45
|
|
|
$this->assertCount(1, $validators); |
46
|
|
|
$this->assertCount(3, $filters); |
47
|
|
|
$this->assertContains(Filter\StripTags::class, $filters); |
48
|
|
|
$this->assertContains(Filter\StringTrim::class, $filters); |
49
|
|
|
$this->assertContains(Filter\Boolean::class, $filters); |
50
|
|
|
|
51
|
|
|
/** @var Validator\NotEmpty $notEmptyValidator */ |
52
|
|
|
$notEmptyValidator = $validators[0]['instance']; |
53
|
|
|
$calculateTypeValue = (function (array $type) { |
54
|
|
|
return $this->calculateTypeValue($type); |
|
|
|
|
55
|
|
|
})->bindTo($notEmptyValidator, Validator\NotEmpty::class); |
56
|
|
|
$this->assertInstanceOf(Validator\NotEmpty::class, $notEmptyValidator); |
57
|
|
|
$this->assertEquals($calculateTypeValue([ |
58
|
|
|
Validator\NotEmpty::OBJECT, |
59
|
|
|
Validator\NotEmpty::SPACE, |
60
|
|
|
Validator\NotEmpty::NULL, |
61
|
|
|
Validator\NotEmpty::EMPTY_ARRAY, |
62
|
|
|
Validator\NotEmpty::STRING, |
63
|
|
|
]), $notEmptyValidator->getType()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function provideRequired(): iterable |
67
|
|
|
{ |
68
|
|
|
yield [true]; |
69
|
|
|
yield [false]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getFiltersFromInput(Input $input): array |
73
|
|
|
{ |
74
|
|
|
return map($input->getFilterChain()->getFilters()->toArray(), static function (Filter\FilterInterface $filter) { |
75
|
|
|
return get_class($filter); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.