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\DoctrineMongoDBAdminBundle\Tests\Guesser; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ODM\MongoDB\Mapping\MappingException; |
18
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
19
|
|
|
use PHPUnit\Framework\MockObject\Stub; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Guesser\TypeGuesser; |
22
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager; |
23
|
|
|
use Symfony\Component\Form\Guess\Guess; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Marko Kunic <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class TypeGuesserTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Stub&ModelManager |
32
|
|
|
*/ |
33
|
|
|
private $modelManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TypeGuesser |
37
|
|
|
*/ |
38
|
|
|
private $guesser; |
39
|
|
|
|
40
|
|
|
protected function setUp(): void |
41
|
|
|
{ |
42
|
|
|
$this->modelManager = $this->createStub(ModelManager::class); |
43
|
|
|
$this->guesser = new TypeGuesser(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGuessTypeNoMetadata(): void |
47
|
|
|
{ |
48
|
|
|
$class = 'FakeClass'; |
49
|
|
|
$property = 'fakeProperty'; |
50
|
|
|
$this->modelManager |
51
|
|
|
->method('getParentMetadataForProperty') |
52
|
|
|
->with($class, $property) |
53
|
|
|
->willThrowException(new MappingException()); |
54
|
|
|
|
55
|
|
|
$result = $this->guesser->guessType($class, $property, $this->modelManager); |
56
|
|
|
|
57
|
|
|
$this->assertSame('text', $result->getType()); |
58
|
|
|
$this->assertSame(Guess::LOW_CONFIDENCE, $result->getConfidence()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @dataProvider associationData |
63
|
|
|
*/ |
64
|
|
|
public function testGuessTypeWithAssociation(string $mappingType, string $type): void |
65
|
|
|
{ |
66
|
|
|
$classMetadata = $this->createMock(ClassMetadata::class); |
67
|
|
|
|
68
|
|
|
$class = 'FakeClass'; |
69
|
|
|
$property = 'fakeProperty'; |
70
|
|
|
|
71
|
|
|
$classMetadata |
72
|
|
|
->method('hasAssociation') |
73
|
|
|
->with($property) |
74
|
|
|
->willReturn(true); |
75
|
|
|
|
76
|
|
|
$classMetadata->fieldMappings = [$property => ['type' => $mappingType]]; |
77
|
|
|
|
78
|
|
|
$this->modelManager |
79
|
|
|
->method('getParentMetadataForProperty') |
80
|
|
|
->with($class, $property) |
81
|
|
|
->willReturn([ |
82
|
|
|
$classMetadata, |
83
|
|
|
$property, |
84
|
|
|
'notUsed', |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$result = $this->guesser->guessType($class, $property, $this->modelManager); |
88
|
|
|
|
89
|
|
|
$this->assertSame($type, $result->getType()); |
90
|
|
|
$this->assertSame(Guess::HIGH_CONFIDENCE, $result->getConfidence()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function associationData(): array |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
'many-to-one' => [ |
97
|
|
|
ClassMetadata::ONE, |
98
|
|
|
'mongo_one', |
99
|
|
|
], |
100
|
|
|
'one-to-many' => [ |
101
|
|
|
ClassMetadata::MANY, |
102
|
|
|
'mongo_many', |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @dataProvider noAssociationData |
109
|
|
|
*/ |
110
|
|
|
public function testGuessTypeNoAssociation(string $type, string $resultType, int $confidence): void |
111
|
|
|
{ |
112
|
|
|
$classMetadata = $this->createMock(ClassMetadata::class); |
113
|
|
|
|
114
|
|
|
$class = 'FakeClass'; |
115
|
|
|
$property = 'fakeProperty'; |
116
|
|
|
|
117
|
|
|
$classMetadata |
118
|
|
|
->method('hasAssociation') |
119
|
|
|
->with($property) |
120
|
|
|
->willReturn(false); |
121
|
|
|
|
122
|
|
|
$classMetadata |
123
|
|
|
->method('getTypeOfField') |
124
|
|
|
->with($property) |
125
|
|
|
->willReturn($type); |
126
|
|
|
|
127
|
|
|
$this->modelManager |
128
|
|
|
->method('getParentMetadataForProperty') |
129
|
|
|
->with($class, $property) |
130
|
|
|
->willReturn([ |
131
|
|
|
$classMetadata, |
132
|
|
|
$property, |
133
|
|
|
'notUsed', |
134
|
|
|
]); |
135
|
|
|
|
136
|
|
|
$result = $this->guesser->guessType($class, $property, $this->modelManager); |
137
|
|
|
|
138
|
|
|
$this->assertSame($resultType, $result->getType()); |
139
|
|
|
$this->assertSame($confidence, $result->getConfidence()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function noAssociationData(): array |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
'collection' => [ |
146
|
|
|
Type::COLLECTION, |
147
|
|
|
'array', |
148
|
|
|
Guess::HIGH_CONFIDENCE, |
149
|
|
|
], |
150
|
|
|
'hash' => [ |
151
|
|
|
Type::HASH, |
152
|
|
|
'array', |
153
|
|
|
Guess::HIGH_CONFIDENCE, |
154
|
|
|
], |
155
|
|
|
'bool' => [ |
156
|
|
|
Type::BOOL, |
157
|
|
|
'boolean', |
158
|
|
|
Guess::HIGH_CONFIDENCE, |
159
|
|
|
], |
160
|
|
|
'timestamp' => [ |
161
|
|
|
Type::TIMESTAMP, |
162
|
|
|
'datetime', |
163
|
|
|
Guess::HIGH_CONFIDENCE, |
164
|
|
|
], |
165
|
|
|
'datetime_immutable' => [ |
166
|
|
|
Type::DATE_IMMUTABLE, |
167
|
|
|
'date', |
168
|
|
|
Guess::HIGH_CONFIDENCE, |
169
|
|
|
], |
170
|
|
|
'date' => [ |
171
|
|
|
Type::DATE, |
172
|
|
|
'date', |
173
|
|
|
Guess::HIGH_CONFIDENCE, |
174
|
|
|
], |
175
|
|
|
'float' => [ |
176
|
|
|
Type::FLOAT, |
177
|
|
|
'number', |
178
|
|
|
Guess::MEDIUM_CONFIDENCE, |
179
|
|
|
], |
180
|
|
|
'integer' => [ |
181
|
|
|
Type::INT, |
182
|
|
|
'integer', |
183
|
|
|
Guess::MEDIUM_CONFIDENCE, |
184
|
|
|
], |
185
|
|
|
'string' => [ |
186
|
|
|
Type::STRING, |
187
|
|
|
'text', |
188
|
|
|
Guess::MEDIUM_CONFIDENCE, |
189
|
|
|
], |
190
|
|
|
'somefake' => [ |
191
|
|
|
'somefake', |
192
|
|
|
'text', |
193
|
|
|
Guess::LOW_CONFIDENCE, |
194
|
|
|
], |
195
|
|
|
]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|