|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Tests\Guesser; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
17
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Guesser\TypeGuesser; |
|
20
|
|
|
use Sonata\DoctrineORMAdminBundle\Model\ModelManager; |
|
21
|
|
|
use Symfony\Component\Form\Guess\Guess; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Marko Kunic <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class TypeGuesserTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
private $modelManager; |
|
29
|
|
|
private $guesser; |
|
30
|
|
|
|
|
31
|
|
|
protected function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->modelManager = $this->prophesize(ModelManager::class); |
|
34
|
|
|
$this->guesser = new TypeGuesser(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGuessTypeNoMetadata(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->modelManager->getParentMetadataForProperty( |
|
40
|
|
|
$class = 'FakeClass', |
|
41
|
|
|
$property = 'fakeProperty' |
|
42
|
|
|
)->willThrow(MappingException::class); |
|
43
|
|
|
|
|
44
|
|
|
$result = $this->guesser->guessType($class, $property, $this->modelManager->reveal()); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertSame('text', $result->getType()); |
|
47
|
|
|
$this->assertSame(Guess::LOW_CONFIDENCE, $result->getConfidence()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @dataProvider associationData |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testGuessTypeWithAssociation($mappingType, $type): void |
|
54
|
|
|
{ |
|
55
|
|
|
$classMetadata = $this->prophesize(ClassMetadata::class); |
|
56
|
|
|
|
|
57
|
|
|
$classMetadata->hasAssociation($property = 'fakeProperty')->willReturn(true); |
|
58
|
|
|
$classMetadata->getAssociationMapping($property) |
|
59
|
|
|
->willReturn(['type' => $mappingType]); |
|
60
|
|
|
|
|
61
|
|
|
$this->modelManager->getParentMetadataForProperty( |
|
62
|
|
|
$class = 'FakeClass', |
|
63
|
|
|
$property |
|
64
|
|
|
)->willReturn([$classMetadata, $property, 'notUsed']); |
|
65
|
|
|
|
|
66
|
|
|
$result = $this->guesser->guessType($class, $property, $this->modelManager->reveal()); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertSame($type, $result->getType()); |
|
69
|
|
|
$this->assertSame(Guess::HIGH_CONFIDENCE, $result->getConfidence()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function associationData(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
|
|
'many-to-one' => [ |
|
76
|
|
|
ClassMetadata::MANY_TO_ONE, |
|
77
|
|
|
'orm_many_to_one', |
|
78
|
|
|
], |
|
79
|
|
|
'one-to-many' => [ |
|
80
|
|
|
ClassMetadata::ONE_TO_MANY, |
|
81
|
|
|
'orm_one_to_many', |
|
82
|
|
|
], |
|
83
|
|
|
'one-to-one' => [ |
|
84
|
|
|
ClassMetadata::ONE_TO_ONE, |
|
85
|
|
|
'orm_one_to_one', |
|
86
|
|
|
], |
|
87
|
|
|
'many-to-many' => [ |
|
88
|
|
|
ClassMetadata::MANY_TO_MANY, |
|
89
|
|
|
'orm_many_to_many', |
|
90
|
|
|
], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @dataProvider noAssociationData |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testGuessTypeNoAssociation($type, $resultType, $confidence): void |
|
98
|
|
|
{ |
|
99
|
|
|
$classMetadata = $this->prophesize(ClassMetadata::class); |
|
100
|
|
|
|
|
101
|
|
|
$classMetadata->hasAssociation($property = 'fakeProperty')->willReturn(false); |
|
102
|
|
|
$classMetadata->getTypeOfField($property)->willReturn($type); |
|
103
|
|
|
|
|
104
|
|
|
$this->modelManager->getParentMetadataForProperty( |
|
105
|
|
|
$class = 'FakeClass', |
|
106
|
|
|
$property |
|
107
|
|
|
)->willReturn([$classMetadata, $property, 'notUsed']); |
|
108
|
|
|
|
|
109
|
|
|
$result = $this->guesser->guessType($class, $property, $this->modelManager->reveal()); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertSame($resultType, $result->getType()); |
|
112
|
|
|
$this->assertSame($confidence, $result->getConfidence()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function noAssociationData(): array |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
'array' => [ |
|
119
|
|
|
$array = 'array', |
|
120
|
|
|
$array, |
|
121
|
|
|
Guess::HIGH_CONFIDENCE, |
|
122
|
|
|
], |
|
123
|
|
|
'simple_array' => [ |
|
124
|
|
|
'simple_array', |
|
125
|
|
|
$array, |
|
126
|
|
|
Guess::HIGH_CONFIDENCE, |
|
127
|
|
|
], |
|
128
|
|
|
'json_array' => [ |
|
129
|
|
|
'json_array', |
|
130
|
|
|
$array, |
|
131
|
|
|
Guess::HIGH_CONFIDENCE, |
|
132
|
|
|
], |
|
133
|
|
|
'json' => [ |
|
134
|
|
|
'json', |
|
135
|
|
|
$array, |
|
136
|
|
|
Guess::HIGH_CONFIDENCE, |
|
137
|
|
|
], |
|
138
|
|
|
'boolean' => [ |
|
139
|
|
|
$boolean = 'boolean', |
|
140
|
|
|
$boolean, |
|
141
|
|
|
Guess::HIGH_CONFIDENCE, |
|
142
|
|
|
], |
|
143
|
|
|
'datetime' => [ |
|
144
|
|
|
$datetime = 'datetime', |
|
145
|
|
|
$datetime, |
|
146
|
|
|
Guess::HIGH_CONFIDENCE, |
|
147
|
|
|
], |
|
148
|
|
|
'datetime_immutable' => [ |
|
149
|
|
|
'datetime_immutable', |
|
150
|
|
|
$datetime, |
|
151
|
|
|
Guess::HIGH_CONFIDENCE, |
|
152
|
|
|
], |
|
153
|
|
|
'vardatetime' => [ |
|
154
|
|
|
'vardatetime', |
|
155
|
|
|
$datetime, |
|
156
|
|
|
Guess::HIGH_CONFIDENCE, |
|
157
|
|
|
], |
|
158
|
|
|
'datetimetz' => [ |
|
159
|
|
|
'datetimetz', |
|
160
|
|
|
$datetime, |
|
161
|
|
|
Guess::HIGH_CONFIDENCE, |
|
162
|
|
|
], |
|
163
|
|
|
'datetimetz_immutable' => [ |
|
164
|
|
|
'datetimetz_immutable', |
|
165
|
|
|
$datetime, |
|
166
|
|
|
Guess::HIGH_CONFIDENCE, |
|
167
|
|
|
], |
|
168
|
|
|
'date' => [ |
|
169
|
|
|
$date = 'date', |
|
170
|
|
|
$date, |
|
171
|
|
|
Guess::HIGH_CONFIDENCE, |
|
172
|
|
|
], |
|
173
|
|
|
'date_immutable' => [ |
|
174
|
|
|
'date_immutable', |
|
175
|
|
|
$date, |
|
176
|
|
|
Guess::HIGH_CONFIDENCE, |
|
177
|
|
|
], |
|
178
|
|
|
'decimal' => [ |
|
179
|
|
|
'decimal', |
|
180
|
|
|
$number = 'number', |
|
181
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
182
|
|
|
], |
|
183
|
|
|
'float' => [ |
|
184
|
|
|
'float', |
|
185
|
|
|
$number, |
|
186
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
187
|
|
|
], |
|
188
|
|
|
'integer' => [ |
|
189
|
|
|
$integer = 'integer', |
|
190
|
|
|
$integer, |
|
191
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
192
|
|
|
], |
|
193
|
|
|
'bigint' => [ |
|
194
|
|
|
'bigint', |
|
195
|
|
|
$integer, |
|
196
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
197
|
|
|
], |
|
198
|
|
|
'smallint' => [ |
|
199
|
|
|
'smallint', |
|
200
|
|
|
$integer, |
|
201
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
202
|
|
|
], |
|
203
|
|
|
'string' => [ |
|
204
|
|
|
'string', |
|
205
|
|
|
$text = 'text', |
|
206
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
207
|
|
|
], |
|
208
|
|
|
'text' => [ |
|
209
|
|
|
'text', |
|
210
|
|
|
'textarea', |
|
211
|
|
|
Guess::MEDIUM_CONFIDENCE, |
|
212
|
|
|
], |
|
213
|
|
|
'time' => [ |
|
214
|
|
|
$time = 'time', |
|
215
|
|
|
$time, |
|
216
|
|
|
Guess::HIGH_CONFIDENCE, |
|
217
|
|
|
], |
|
218
|
|
|
'time_immutable' => [ |
|
219
|
|
|
'time_immutable', |
|
220
|
|
|
$time, |
|
221
|
|
|
Guess::HIGH_CONFIDENCE, |
|
222
|
|
|
], |
|
223
|
|
|
'somefake' => [ |
|
224
|
|
|
'somefake', |
|
225
|
|
|
$text, |
|
226
|
|
|
Guess::LOW_CONFIDENCE, |
|
227
|
|
|
], |
|
228
|
|
|
]; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|