Passed
Pull Request — master (#4)
by Alex
02:57
created

MetadataTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 122
c 1
b 0
f 0
dl 0
loc 268
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testGetFieldMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound() 0 32 1
A testImplementsMetadataInterface() 0 5 1
A testHasAssociationWillReturnFalseForNonExistingField() 0 12 1
A testGetAssociationMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound() 0 32 1
A testGetFieldMappingWillReturnMappingData() 0 17 1
A testGetNameWillReturnTheEntityClassName() 0 11 1
A testHasFieldWillReturnTrueForExistingField() 0 12 1
A testHasFieldWillReturnFalseForNonExistingField() 0 12 1
A testGetFieldTypeWillReturnTheMappedType() 0 19 1
A testHasAssociationWillReturnTrueForExistingField() 0 12 1
A testGetFieldTypeWillThrowMetadataExceptionIfNotMappingTypeCanBeResolved() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DoctrineQueryFilter\Metadata;
6
7
use Arp\DoctrineQueryFilter\Metadata\Exception\MetadataException;
8
use Arp\DoctrineQueryFilter\Metadata\Metadata;
9
use Arp\DoctrineQueryFilter\Metadata\MetadataInterface;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\ORM\Mapping\MappingException;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * @covers \Arp\DoctrineQueryFilter\Metadata\Metadata
17
 *
18
 * @author  Alex Patterson <[email protected]>
19
 * @package ArpTest\DoctrineQueryFilter\Metadata
20
 */
21
final class MetadataTest extends TestCase
22
{
23
    /**
24
     * @var ClassMetadata|MockObject
25
     */
26
    private $classMetadata;
27
28
    /**
29
     * Prepare the test case dependencies
30
     */
31
    public function setUp(): void
32
    {
33
        $this->classMetadata = $this->createMock(ClassMetadata::class);
34
    }
35
36
    /**
37
     * Assert that the class implements MetadataInterface
38
     */
39
    public function testImplementsMetadataInterface(): void
40
    {
41
        $metadata = new Metadata($this->classMetadata);
42
43
        $this->assertInstanceOf(MetadataInterface::class, $metadata);
44
    }
45
46
    /**
47
     * Assert that getName() will return the entity class name
48
     */
49
    public function testGetNameWillReturnTheEntityClassName(): void
50
    {
51
        $className = 'Foo';
52
53
        $metadata = new Metadata($this->classMetadata);
54
55
        $this->classMetadata->expects($this->once())
56
            ->method('getName')
57
            ->willReturn($className);
58
59
        $this->assertSame($className, $metadata->getName());
60
    }
61
62
    /**
63
     * Assert a boolean FALSE is returned when calling hasField() with an unknown field
64
     */
65
    public function testHasFieldWillReturnFalseForNonExistingField(): void
66
    {
67
        $metadata = new Metadata($this->classMetadata);
68
69
        $fieldName = 'testFieldName';
70
71
        $this->classMetadata->expects($this->once())
72
            ->method('hasField')
73
            ->with($fieldName)
74
            ->willReturn(false);
75
76
        $this->assertFalse($metadata->hasField($fieldName));
77
    }
78
79
    /**
80
     * Assert a boolean FALSE is returned when calling hasField() with an unknown field
81
     */
82
    public function testHasFieldWillReturnTrueForExistingField(): void
83
    {
84
        $metadata = new Metadata($this->classMetadata);
85
86
        $fieldName = 'testFieldName';
87
88
        $this->classMetadata->expects($this->once())
89
            ->method('hasField')
90
            ->with($fieldName)
91
            ->willReturn(true);
92
93
        $this->assertTrue($metadata->hasField($fieldName));
94
    }
95
96
    /**
97
     * Assert that a failure to load field mapping will raise a MappingException error
98
     *
99
     * @throws MetadataException
100
     */
101
    public function testGetFieldMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound(): void
102
    {
103
        $metadata = new Metadata($this->classMetadata);
104
105
        $className = 'FooClassName';
106
        $fieldName = 'fooFieldName';
107
108
        $exceptionMessage = 'This is a test mapping exception message';
109
        $exceptionCode = 456;
110
        $exception = new MappingException($exceptionMessage, $exceptionCode);
111
112
        $this->classMetadata->expects($this->once())
113
            ->method('getFieldMapping')
114
            ->with($fieldName)
115
            ->willThrowException($exception);
116
117
        $this->classMetadata->expects($this->once())
118
            ->method('getName')
119
            ->willReturn($className);
120
121
        $this->expectException(MetadataException::class);
122
        $this->expectExceptionCode($exceptionCode);
123
        $this->expectExceptionMessage(
124
            sprintf(
125
                'Unable to find field mapping for field \'%s::%s\': %s',
126
                $className,
127
                $fieldName,
128
                $exceptionMessage
129
            )
130
        );
131
132
        $metadata->getFieldMapping($fieldName);
133
    }
134
135
    /**
136
     * Assert that mapping data is correctly returned from calls to getFieldMapping()
137
     *
138
     * @throws MetadataException
139
     */
140
    public function testGetFieldMappingWillReturnMappingData(): void
141
    {
142
        $metadata = new Metadata($this->classMetadata);
143
144
        $fieldName = 'fooFieldName';
145
        $mappingData = [
146
            'name' => $fieldName,
147
            'type' => 'test',
148
            'hello' => 123,
149
        ];
150
151
        $this->classMetadata->expects($this->once())
152
            ->method('getFieldMapping')
153
            ->with($fieldName)
154
            ->willReturn($mappingData);
155
156
        $this->assertSame($mappingData, $metadata->getFieldMapping($fieldName));
157
    }
158
159
    /**
160
     * Assert that calls to getFieldType() that fail will throw a MappingException
161
     *
162
     * @throws MetadataException
163
     */
164
    public function testGetFieldTypeWillThrowMetadataExceptionIfNotMappingTypeCanBeResolved(): void
165
    {
166
        $metadata = new Metadata($this->classMetadata);
167
168
        $className = 'FooClassName';
169
        $fieldName = 'fooFieldName';
170
        $mappingData = [
171
            'name' => $className,
172
            // missing the mapping data 'type' will the expected error
173
        ];
174
175
        $this->classMetadata->expects($this->once())
176
            ->method('getFieldMapping')
177
            ->with($fieldName)
178
            ->willReturn($mappingData);
179
180
        $this->classMetadata->expects($this->once())
181
            ->method('getName')
182
            ->willReturn($className);
183
184
        $this->expectException(MetadataException::class);
185
        $this->expectExceptionMessage(
186
            sprintf('Unable to resolve field data type for \'%s::%s\'', $className, $fieldName)
187
        );
188
189
        $metadata->getFieldType($fieldName);
190
    }
191
192
    /**
193
     * Assert that calls to getFieldType() will return the mapped column type
194
     *
195
     * @throws MetadataException
196
     */
197
    public function testGetFieldTypeWillReturnTheMappedType(): void
198
    {
199
        $metadata = new Metadata($this->classMetadata);
200
201
        $className = 'FooClassName';
202
        $fieldName = 'fooFieldName';
203
        $type = 'string';
204
205
        $mappingData = [
206
            'name' => $className,
207
            'type' => $type
208
        ];
209
210
        $this->classMetadata->expects($this->once())
211
            ->method('getFieldMapping')
212
            ->with($fieldName)
213
            ->willReturn($mappingData);
214
215
        $this->assertSame($type, $metadata->getFieldType($fieldName));
216
    }
217
218
    /**
219
     * Assert a boolean FALSE is returned when calling hasAssociation() with an unknown relationship
220
     */
221
    public function testHasAssociationWillReturnFalseForNonExistingField(): void
222
    {
223
        $metadata = new Metadata($this->classMetadata);
224
225
        $fieldName = 'testFieldName';
226
227
        $this->classMetadata->expects($this->once())
228
            ->method('hasAssociation')
229
            ->with($fieldName)
230
            ->willReturn(false);
231
232
        $this->assertFalse($metadata->hasAssociation($fieldName));
233
    }
234
235
    /**
236
     * Assert a boolean TRUE is returned when calling hasAssociation() with an known relationship
237
     */
238
    public function testHasAssociationWillReturnTrueForExistingField(): void
239
    {
240
        $metadata = new Metadata($this->classMetadata);
241
242
        $fieldName = 'testFieldName';
243
244
        $this->classMetadata->expects($this->once())
245
            ->method('hasAssociation')
246
            ->with($fieldName)
247
            ->willReturn(true);
248
249
        $this->assertTrue($metadata->hasAssociation($fieldName));
250
    }
251
252
    /**
253
     * Assert that a failure to load association mapping will raise a MappingException error
254
     *
255
     * @throws MetadataException
256
     */
257
    public function testGetAssociationMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound(): void
258
    {
259
        $metadata = new Metadata($this->classMetadata);
260
261
        $className = 'BarClassName';
262
        $fieldName = 'barFieldName';
263
264
        $exceptionCode = 999;
265
        $exceptionMessage = 'This is a test association mapping exception message';
266
        $exception = new MappingException($exceptionMessage, $exceptionCode);
267
268
        $this->classMetadata->expects($this->once())
269
            ->method('getAssociationMapping')
270
            ->with($fieldName)
271
            ->willThrowException($exception);
272
273
        $this->classMetadata->expects($this->once())
274
            ->method('getName')
275
            ->willReturn($className);
276
277
        $this->expectException(MetadataException::class);
278
        $this->expectExceptionCode($exceptionCode);
279
        $this->expectExceptionMessage(
280
            sprintf(
281
                'Unable to find association mapping for field \'%s::%s\': %s',
282
                $className,
283
                $fieldName,
284
                $exceptionMessage
285
            )
286
        );
287
288
        $metadata->getAssociationMapping($fieldName);
289
    }
290
}
291