|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace eZ\Publish\SPI\FieldType\Generic\Tests; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\SPI\FieldType\Tests\FieldTypeTest; |
|
12
|
|
|
use eZ\Publish\SPI\FieldType\ValueSerializerInterface; |
|
13
|
|
|
use eZ\Publish\SPI\FieldType\Generic\Tests\Stubs\Type as GenericFieldTypeStub; |
|
14
|
|
|
use eZ\Publish\SPI\FieldType\Generic\Tests\Stubs\Value as GenericFieldValueStub; |
|
15
|
|
|
use eZ\Publish\SPI\FieldType\ValidationError; |
|
16
|
|
|
use eZ\Publish\SPI\Exception\InvalidArgumentException; |
|
17
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
18
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
19
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
20
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
21
|
|
|
|
|
22
|
|
|
class GenericTest extends FieldTypeTest |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var \eZ\Publish\SPI\FieldType\ValueSerializerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
25
|
|
|
private $serializer; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \Symfony\Component\Validator\Validator\ValidatorInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
28
|
|
|
private $validator; |
|
29
|
|
|
|
|
30
|
|
|
protected function setUp(): void |
|
31
|
|
|
{ |
|
32
|
|
|
parent::setUp(); |
|
33
|
|
|
|
|
34
|
|
|
$this->serializer = $this->createSerializerMock(); |
|
35
|
|
|
$this->validator = $this->createMock(ValidatorInterface::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @dataProvider provideValidDataForValidate |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testValidateValid($fieldDefinitionData, $value): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->validator |
|
44
|
|
|
->method('validate') |
|
45
|
|
|
->with($value, null) |
|
46
|
|
|
->willReturn($this->createMock(ConstraintViolationListInterface::class)); |
|
47
|
|
|
|
|
48
|
|
|
parent::testValidateValid($fieldDefinitionData, $value); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @dataProvider provideInvalidDataForValidate |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testValidateInvalid($fieldDefinitionData, $value, $errors): void |
|
55
|
|
|
{ |
|
56
|
|
|
$constraintViolationList = new ConstraintViolationList(array_map(function (ValidationError $error) { |
|
57
|
|
|
return new ConstraintViolation((string) $error->getTranslatableMessage()); |
|
|
|
|
|
|
58
|
|
|
}, $errors)); |
|
59
|
|
|
|
|
60
|
|
|
$this->validator |
|
61
|
|
|
->method('validate') |
|
62
|
|
|
->with($value, null) |
|
63
|
|
|
->willReturn($constraintViolationList); |
|
64
|
|
|
|
|
65
|
|
|
parent::testValidateInvalid($fieldDefinitionData, $value, $errors); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function provideFieldTypeIdentifier(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return 'generic'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function createFieldTypeUnderTest() |
|
74
|
|
|
{ |
|
75
|
|
|
return new GenericFieldTypeStub($this->serializer, $this->validator); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function getValidatorConfigurationSchemaExpectation(): array |
|
79
|
|
|
{ |
|
80
|
|
|
return []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getSettingsSchemaExpectation(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function getEmptyValueExpectation() |
|
89
|
|
|
{ |
|
90
|
|
|
return new GenericFieldValueStub(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function provideInvalidInputForAcceptValue(): array |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
|
|
[ |
|
97
|
|
|
23, |
|
98
|
|
|
InvalidArgumentException::class, |
|
99
|
|
|
], |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function provideValidInputForAcceptValue(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
|
|
[ |
|
107
|
|
|
null, |
|
108
|
|
|
new GenericFieldValueStub(), |
|
109
|
|
|
], |
|
110
|
|
|
[ |
|
111
|
|
|
'{"value": "foo"}', |
|
112
|
|
|
new GenericFieldValueStub('foo'), |
|
113
|
|
|
], |
|
114
|
|
|
[ |
|
115
|
|
|
new GenericFieldValueStub('foo'), |
|
116
|
|
|
new GenericFieldValueStub('foo'), |
|
117
|
|
|
], |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
View Code Duplication |
public function provideInputForToHash(): array |
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
[ |
|
125
|
|
|
new GenericFieldValueStub(), |
|
126
|
|
|
null, |
|
127
|
|
|
], |
|
128
|
|
|
[ |
|
129
|
|
|
new GenericFieldValueStub('foo'), |
|
130
|
|
|
['value' => 'foo'], |
|
131
|
|
|
], |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
View Code Duplication |
public function provideInputForFromHash(): array |
|
136
|
|
|
{ |
|
137
|
|
|
return [ |
|
138
|
|
|
[ |
|
139
|
|
|
null, |
|
140
|
|
|
new GenericFieldValueStub(), |
|
141
|
|
|
], |
|
142
|
|
|
[ |
|
143
|
|
|
['value' => 'foo'], |
|
144
|
|
|
new GenericFieldValueStub('foo'), |
|
145
|
|
|
], |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function provideDataForGetName(): array |
|
150
|
|
|
{ |
|
151
|
|
|
return [ |
|
152
|
|
|
[new GenericFieldValueStub('This is a generic value.'), [], 'en_GB', 'This is a generic value.'], |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
private function createSerializerMock(): ValueSerializerInterface |
|
157
|
|
|
{ |
|
158
|
|
|
$serializer = $this->createMock(ValueSerializerInterface::class); |
|
159
|
|
|
|
|
160
|
|
|
$serializer |
|
|
|
|
|
|
161
|
|
|
->method('decode') |
|
162
|
|
|
->willReturnCallback(function (string $json) { |
|
163
|
|
|
return json_decode($json, true); |
|
164
|
|
|
}); |
|
165
|
|
|
|
|
166
|
|
|
$serializer |
|
|
|
|
|
|
167
|
|
|
->method('normalize') |
|
168
|
|
|
->willReturnCallback(function (GenericFieldValueStub $value) { |
|
169
|
|
|
return [ |
|
170
|
|
|
'value' => $value->getValue(), |
|
171
|
|
|
]; |
|
172
|
|
|
}); |
|
173
|
|
|
|
|
174
|
|
|
$serializer |
|
|
|
|
|
|
175
|
|
|
->method('denormalize') |
|
176
|
|
|
->willReturnCallback(function (array $data, string $valueClass) { |
|
177
|
|
|
$this->assertEquals($valueClass, GenericFieldValueStub::class); |
|
178
|
|
|
|
|
179
|
|
|
return new GenericFieldValueStub($data['value']); |
|
180
|
|
|
}); |
|
181
|
|
|
|
|
182
|
|
|
return $serializer; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|