MetadataTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testGetFieldMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound() 0 26 1
A testImplementsMetadataInterface() 0 5 1
A testHasAssociationWillReturnFalseForNonExistingField() 0 12 1
A testGetAssociationMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound() 0 26 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
final class MetadataTest extends TestCase
19
{
20
    /**
21
     * @var ClassMetadata<object>&MockObject
22
     */
23
    private ClassMetadata $classMetadata;
24
25
    public function setUp(): void
26
    {
27
        $this->classMetadata = $this->createMock(ClassMetadata::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...g\ClassMetadata::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\ORM\Mapping\ClassMetadata of property $classMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    /**
31
     * Assert that the class implements MetadataInterface
32
     */
33
    public function testImplementsMetadataInterface(): void
34
    {
35
        $metadata = new Metadata($this->classMetadata);
36
37
        $this->assertInstanceOf(MetadataInterface::class, $metadata);
38
    }
39
40
    /**
41
     * Assert that getName() will return the entity class name
42
     */
43
    public function testGetNameWillReturnTheEntityClassName(): void
44
    {
45
        $className = 'Foo';
46
47
        $metadata = new Metadata($this->classMetadata);
48
49
        $this->classMetadata->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\ORM\Mapping\ClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->classMetadata->/** @scrutinizer ignore-call */ 
50
                              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
            ->method('getName')
51
            ->willReturn($className);
52
53
        $this->assertSame($className, $metadata->getName());
54
    }
55
56
    /**
57
     * Assert a boolean FALSE is returned when calling hasField() with an unknown field
58
     */
59
    public function testHasFieldWillReturnFalseForNonExistingField(): void
60
    {
61
        $metadata = new Metadata($this->classMetadata);
62
63
        $fieldName = 'testFieldName';
64
65
        $this->classMetadata->expects($this->once())
66
            ->method('hasField')
67
            ->with($fieldName)
68
            ->willReturn(false);
69
70
        $this->assertFalse($metadata->hasField($fieldName));
71
    }
72
73
    /**
74
     * Assert a boolean FALSE is returned when calling hasField() with an unknown field
75
     */
76
    public function testHasFieldWillReturnTrueForExistingField(): void
77
    {
78
        $metadata = new Metadata($this->classMetadata);
79
80
        $fieldName = 'testFieldName';
81
82
        $this->classMetadata->expects($this->once())
83
            ->method('hasField')
84
            ->with($fieldName)
85
            ->willReturn(true);
86
87
        $this->assertTrue($metadata->hasField($fieldName));
88
    }
89
90
    /**
91
     * Assert that a failure to load field mapping will raise a MappingException error
92
     *
93
     * @throws MetadataException
94
     */
95
    public function testGetFieldMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound(): void
96
    {
97
        $metadata = new Metadata($this->classMetadata);
98
99
        $className = 'FooClassName';
100
        $fieldName = 'fooFieldName';
101
102
        $exceptionCode = 456;
103
        $exception = new MappingException('This is a test mapping exception message', $exceptionCode);
104
105
        $this->classMetadata->expects($this->once())
106
            ->method('getFieldMapping')
107
            ->with($fieldName)
108
            ->willThrowException($exception);
109
110
        $this->classMetadata->expects($this->once())
111
            ->method('getName')
112
            ->willReturn($className);
113
114
        $this->expectException(MetadataException::class);
115
        $this->expectExceptionCode($exceptionCode);
116
        $this->expectExceptionMessage(
117
            sprintf('Unable to find field mapping for field \'%s::%s\'', $className, $fieldName)
118
        );
119
120
        $metadata->getFieldMapping($fieldName);
121
    }
122
123
    /**
124
     * Assert that mapping data is correctly returned from calls to getFieldMapping()
125
     *
126
     * @throws MetadataException
127
     */
128
    public function testGetFieldMappingWillReturnMappingData(): void
129
    {
130
        $metadata = new Metadata($this->classMetadata);
131
132
        $fieldName = 'fooFieldName';
133
        $mappingData = [
134
            'name' => $fieldName,
135
            'type' => 'test',
136
            'hello' => 123,
137
        ];
138
139
        $this->classMetadata->expects($this->once())
140
            ->method('getFieldMapping')
141
            ->with($fieldName)
142
            ->willReturn($mappingData);
143
144
        $this->assertSame($mappingData, $metadata->getFieldMapping($fieldName));
145
    }
146
147
    /**
148
     * Assert that calls to getFieldType() that fail will throw a MappingException
149
     *
150
     * @throws MetadataException
151
     */
152
    public function testGetFieldTypeWillThrowMetadataExceptionIfNotMappingTypeCanBeResolved(): void
153
    {
154
        $metadata = new Metadata($this->classMetadata);
155
156
        $className = 'FooClassName';
157
        $fieldName = 'fooFieldName';
158
        $mappingData = [
159
            'name' => $className,
160
            // missing the mapping data 'type' will the expected error
161
        ];
162
163
        $this->classMetadata->expects($this->once())
164
            ->method('getFieldMapping')
165
            ->with($fieldName)
166
            ->willReturn($mappingData);
167
168
        $this->classMetadata->expects($this->once())
169
            ->method('getName')
170
            ->willReturn($className);
171
172
        $this->expectException(MetadataException::class);
173
        $this->expectExceptionMessage(
174
            sprintf('Unable to resolve field data type for \'%s::%s\'', $className, $fieldName)
175
        );
176
177
        $metadata->getFieldType($fieldName);
178
    }
179
180
    /**
181
     * Assert that calls to getFieldType() will return the mapped column type
182
     *
183
     * @throws MetadataException
184
     */
185
    public function testGetFieldTypeWillReturnTheMappedType(): void
186
    {
187
        $metadata = new Metadata($this->classMetadata);
188
189
        $className = 'FooClassName';
190
        $fieldName = 'fooFieldName';
191
        $type = 'string';
192
193
        $mappingData = [
194
            'name' => $className,
195
            'type' => $type
196
        ];
197
198
        $this->classMetadata->expects($this->once())
199
            ->method('getFieldMapping')
200
            ->with($fieldName)
201
            ->willReturn($mappingData);
202
203
        $this->assertSame($type, $metadata->getFieldType($fieldName));
204
    }
205
206
    /**
207
     * Assert a boolean FALSE is returned when calling hasAssociation() with an unknown relationship
208
     */
209
    public function testHasAssociationWillReturnFalseForNonExistingField(): void
210
    {
211
        $metadata = new Metadata($this->classMetadata);
212
213
        $fieldName = 'testFieldName';
214
215
        $this->classMetadata->expects($this->once())
216
            ->method('hasAssociation')
217
            ->with($fieldName)
218
            ->willReturn(false);
219
220
        $this->assertFalse($metadata->hasAssociation($fieldName));
221
    }
222
223
    /**
224
     * Assert a boolean TRUE is returned when calling hasAssociation() with a known relationship
225
     */
226
    public function testHasAssociationWillReturnTrueForExistingField(): void
227
    {
228
        $metadata = new Metadata($this->classMetadata);
229
230
        $fieldName = 'testFieldName';
231
232
        $this->classMetadata->expects($this->once())
233
            ->method('hasAssociation')
234
            ->with($fieldName)
235
            ->willReturn(true);
236
237
        $this->assertTrue($metadata->hasAssociation($fieldName));
238
    }
239
240
    /**
241
     * Assert that a failure to load association mapping will raise a MappingException error
242
     *
243
     * @throws MetadataException
244
     */
245
    public function testGetAssociationMappingWillThrowAMetadataExceptionIfTheMappingCannotBeFound(): void
246
    {
247
        $metadata = new Metadata($this->classMetadata);
248
249
        $className = 'BarClassName';
250
        $fieldName = 'barFieldName';
251
252
        $exceptionCode = 999;
253
        $exception = new MappingException('This is a test association mapping exception message', $exceptionCode);
254
255
        $this->classMetadata->expects($this->once())
256
            ->method('getAssociationMapping')
257
            ->with($fieldName)
258
            ->willThrowException($exception);
259
260
        $this->classMetadata->expects($this->once())
261
            ->method('getName')
262
            ->willReturn($className);
263
264
        $this->expectException(MetadataException::class);
265
        $this->expectExceptionCode($exceptionCode);
266
        $this->expectExceptionMessage(
267
            sprintf('Unable to find association mapping for field \'%s::%s\'', $className, $fieldName)
268
        );
269
270
        $metadata->getAssociationMapping($fieldName);
271
    }
272
}
273