Completed
Push — master ( 4a4bf4...63fe9e )
by Gerrit
02:30
created

MappingAnnotationDriverTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 9
dl 0
loc 97
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A shouldReadAnnotations() 0 70 2
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Tests\Mapping\Drivers;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Mapping\Drivers\MappingAnnotationDriver;
15
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
16
use Addiks\RDMBundle\Mapping\Annotation\Service;
17
use ReflectionProperty;
18
use Doctrine\Common\Annotations\Reader;
19
use Addiks\RDMBundle\Mapping\ServiceMapping;
20
use Addiks\RDMBundle\Mapping\EntityMapping;
21
use Addiks\RDMBundle\Mapping\Annotation\Choice;
22
use Addiks\RDMBundle\Mapping\ChoiceMapping;
23
use Doctrine\DBAL\Schema\Column;
24
use Doctrine\DBAL\Types\Type;
25
use Addiks\RDMBundle\Mapping\MappingInterface;
26
27
final class MappingAnnotationDriverTest extends TestCase
28
{
29
30
    /**
31
     * @var MappingAnnotationDriver
32
     */
33
    private $mappingDriver;
34
35
    /**
36
     * @var Reader
37
     */
38
    private $annotationReader;
39
40
    public function setUp()
41
    {
42
        $this->annotationReader = $this->createMock(Reader::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...otations\Reader::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\Common\Annotations\Reader> of property $annotationReader.

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...
43
44
        $this->mappingDriver = new MappingAnnotationDriver(
45
            $this->annotationReader
0 ignored issues
show
Documentation introduced by
$this->annotationReader is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\Annotations\Reader>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
        );
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function shouldReadAnnotations()
53
    {
54
        $someAnnotationA = new Service();
55
        $someAnnotationA->id = "some_service";
56
        $someAnnotationA->field = "foo";
0 ignored issues
show
Bug introduced by
The property field does not seem to exist in Addiks\RDMBundle\Mapping\Annotation\Service.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
58
        $someAnnotationB = new Service();
59
        $someAnnotationB->id = "other_service";
60
        $someAnnotationB->field = "bar";
61
        $someAnnotationB->lax = true;
62
63
        $someAnnotationC = new Choice();
64
        $someAnnotationC->column = "baz_column";
65
        $someAnnotationC->nullable = true;
66
        $someAnnotationC->choices = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('foo' => $someAnno...r' => $someAnnotationB) of type array<string,object<Addi...Annotation\\Service>"}> is incompatible with the declared type array<integer,object> of property $choices.

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...
67
            'foo' => $someAnnotationA,
68
            'bar' => $someAnnotationB,
69
        ];
70
71
        $someAnnotationD = new Choice();
72
        $someAnnotationD->column = "faz_column";
73
        $someAnnotationD->nullable = false;
74
        $someAnnotationD->choices = [
75
            'foo' => $someAnnotationA,
76
            'bar' => $someAnnotationB,
77
        ];
78
79
        /** @var string $entityClass */
80
        $entityClass = EntityExample::class;
81
82
        /** @var array<MappingInterface> $expectedFieldMappings */
83
        $expectedFieldMappings = [
84
            'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'foo'"),
85
            'bar' => new ServiceMapping("other_service", true, "in entity '{$entityClass}' on field 'bar'"),
86
            'baz' => new ChoiceMapping('baz_column', [
87
                'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'baz'"),
88
                'bar' => new ServiceMapping("other_service", true, "in entity '{$entityClass}' on field 'baz'"),
89
            ], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'baz'"),
90
            'faz' => new ChoiceMapping(new Column('faz_column', Type::getType('string'), [
91
                'notnull' => true,
92
                'length' => 255,
93
            ]), [
94
                'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'faz'"),
95
                'bar' => new ServiceMapping("other_service", true, "in entity '{$entityClass}' on field 'faz'"),
96
            ], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'faz'"),
97
        ];
98
99
        /** @var array<array<Service>> $annotationMap */
100
        $annotationMap = [
101
            'foo' => [$someAnnotationA],
102
            'bar' => [$someAnnotationB],
103
            'baz' => [$someAnnotationC],
104
            'faz' => [$someAnnotationD],
105
        ];
106
107
        $this->annotationReader->method('getPropertyAnnotations')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\Common\Annotations\Reader. Did you maybe mean getMethodAnnotation()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
108
            function (ReflectionProperty $propertyReflection) use ($annotationMap) {
109
                if (isset($annotationMap[$propertyReflection->getName()])) {
110
                    return $annotationMap[$propertyReflection->getName()];
111
                } else {
112
                    return [];
113
                }
114
            }
115
        ));
116
117
        /** @var EntityMapping $actualMapping */
118
        $actualMapping = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class);
119
120
        $this->assertEquals($expectedFieldMappings, $actualMapping->getFieldMappings());
121
    }
122
123
}
124