1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\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 ApiClientTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var ApiClient - 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
|
|
|
['uuid' => new Uuid(), 'forbidden' => new Forbidden()] |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$this->sut = new ApiClient($this->rulesFactoryMock); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function createValidatorProvider(): array |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
'empty-data' => [ |
42
|
|
|
[], |
43
|
|
|
false, |
44
|
|
|
], |
45
|
|
|
'valid-data' => [ |
46
|
|
|
[ |
47
|
|
|
'user_id' => '5c032f90-bf10-4a77-81aa-b0b1254a8f66', |
48
|
|
|
'description' => 'foo', |
49
|
|
|
], |
50
|
|
|
true, |
51
|
|
|
], |
52
|
|
|
'valid-data-missing-all-not-required' => [ |
53
|
|
|
[ |
54
|
|
|
'user_id' => '5c032f90-bf10-4a77-81aa-b0b1254a8f66', |
55
|
|
|
'description' => 'foo', |
56
|
|
|
], |
57
|
|
|
true, |
58
|
|
|
], |
59
|
|
|
'valid-data-with-admin-resources' => [ |
60
|
|
|
[ |
61
|
|
|
'user_id' => '5c032f90-bf10-4a77-81aa-b0b1254a8f66', |
62
|
|
|
'description' => 'foo', |
63
|
|
|
'admin_resource_ids' => ['bar', 'baz'], |
64
|
|
|
], |
65
|
|
|
true, |
66
|
|
|
], |
67
|
|
|
'invalid-has-id' => [ |
68
|
|
|
[ |
69
|
|
|
'id' => '465c91df-9cc7-47e2-a2ef-8fe645753148', |
70
|
|
|
'user_id' => '5c032f90-bf10-4a77-81aa-b0b1254a8f66', |
71
|
|
|
'description' => 'foo', |
72
|
|
|
], |
73
|
|
|
false, |
74
|
|
|
], |
75
|
|
|
'invalid-user-id-missing' => [ |
76
|
|
|
[ |
77
|
|
|
'description' => 'foo', |
78
|
|
|
], |
79
|
|
|
false, |
80
|
|
|
], |
81
|
|
|
'invalid-user-id-not-uuid' => [ |
82
|
|
|
[ |
83
|
|
|
'user_id' => '5c032f90-bf10-4a77-81aa-b0b1254a8f6', |
84
|
|
|
'description' => 'foo', |
85
|
|
|
], |
86
|
|
|
false, |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider createValidatorProvider |
93
|
|
|
* |
94
|
|
|
* @param array $data |
95
|
|
|
* @param bool $expectedResult |
96
|
|
|
*/ |
97
|
|
|
public function testCreateValidator(array $data, bool $expectedResult) |
98
|
|
|
{ |
99
|
|
|
$validator = $this->sut->createValidator(); |
100
|
|
|
|
101
|
|
|
$this->assertInstanceOf(IValidator::class, $validator); |
102
|
|
|
|
103
|
|
|
$actualResult = $validator->isValid($data); |
104
|
|
|
|
105
|
|
|
$this->assertSame($expectedResult, $actualResult); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|