1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Validation\Factory; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\TestDouble\Validation\StubRulesFactory; |
8
|
|
|
use AbterPhp\Framework\Validation\Rules\Forbidden; |
9
|
|
|
use AbterPhp\Framework\Validation\Rules\Uuid; |
10
|
|
|
use Opulence\Validation\IValidator; |
11
|
|
|
use Opulence\Validation\Rules\Factories\RulesFactory; |
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class FileTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var File - System Under Test */ |
18
|
|
|
protected $sut; |
19
|
|
|
|
20
|
|
|
/** @var RulesFactory|MockObject */ |
21
|
|
|
protected $rulesFactoryMock; |
22
|
|
|
|
23
|
|
|
public function setUp(): void |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
$this->rulesFactoryMock = StubRulesFactory::createRulesFactory( |
28
|
|
|
$this, |
29
|
|
|
[ |
30
|
|
|
'forbidden' => new Forbidden(), |
31
|
|
|
'uuid' => new Uuid(), |
32
|
|
|
] |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->sut = new File($this->rulesFactoryMock); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function createValidatorSuccessProvider(): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
'empty-data' => [ |
45
|
|
|
[], |
46
|
|
|
], |
47
|
|
|
'valid-data-missing-all-not-required' => [ |
48
|
|
|
[ |
49
|
|
|
'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051', |
50
|
|
|
], |
51
|
|
|
], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider createValidatorSuccessProvider |
57
|
|
|
* |
58
|
|
|
* @param array $data |
59
|
|
|
*/ |
60
|
|
|
public function testCreateValidatorSuccess(array $data) |
61
|
|
|
{ |
62
|
|
|
$validator = $this->sut->createValidator(); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(IValidator::class, $validator); |
65
|
|
|
|
66
|
|
|
$actualResult = $validator->isValid($data); |
67
|
|
|
|
68
|
|
|
$this->assertTrue($actualResult); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function createValidatorFailureProvider(): array |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
'invalid-id-present' => [ |
78
|
|
|
[ |
79
|
|
|
'id' => '465c91df-9cc7-47e2-a2ef-8fe645753148', |
80
|
|
|
'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b051', |
81
|
|
|
], |
82
|
|
|
], |
83
|
|
|
'invalid-category-not-uuid' => [ |
84
|
|
|
[ |
85
|
|
|
'category_id' => '69da7b0b-8315-43c9-8f5d-a6a5ea09b05', |
86
|
|
|
], |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider createValidatorFailureProvider |
93
|
|
|
* |
94
|
|
|
* @param array $data |
95
|
|
|
*/ |
96
|
|
|
public function testCreateValidatorFailure(array $data) |
97
|
|
|
{ |
98
|
|
|
$validator = $this->sut->createValidator(); |
99
|
|
|
|
100
|
|
|
$this->assertInstanceOf(IValidator::class, $validator); |
101
|
|
|
|
102
|
|
|
$actualResult = $validator->isValid($data); |
103
|
|
|
|
104
|
|
|
$this->assertFalse($actualResult); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|