FieldMappingTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 40
c 1
b 0
f 1
dl 0
loc 134
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldResolveFieldValue() 0 22 1
A shouldRevertFieldValue() 0 13 1
A shouldCollectDBALColumns() 0 3 1
A setUp() 0 5 1
A shouldStoreDBALColumn() 0 3 1
A shouldStoreOrigin() 0 3 1
A createEntityManagerMock() 0 16 1
A shouldWakeUpInnerMapping() 0 6 1
A shouldAssertFieldValue() 0 11 1
1
<?php
2
/**
3
 * Copyright (C) 2017  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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Mapping\FieldMapping;
15
use Doctrine\DBAL\Schema\Column;
16
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Doctrine\DBAL\Connection;
19
use Doctrine\DBAL\Platforms\AbstractPlatform;
20
use Doctrine\DBAL\Types\Type;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
final class FieldMappingTest extends TestCase
24
{
25
26
    /**
27
     * @var FieldMapping
28
     */
29
    private $fieldMapping;
30
31
    /**
32
     * @var Column
33
     */
34
    private $dbalColumn;
35
36
    public function setUp(): void
37
    {
38
        $this->dbalColumn = $this->createMock(Column::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...L\Schema\Column::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\DBAL\Schema\Column of property $dbalColumn.

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...
39
40
        $this->fieldMapping = new FieldMapping($this->dbalColumn, "some origin");
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function shouldStoreDBALColumn()
47
    {
48
        $this->assertSame($this->dbalColumn, $this->fieldMapping->getDBALColumn());
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function shouldStoreOrigin()
55
    {
56
        $this->assertSame("some origin", $this->fieldMapping->describeOrigin());
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function shouldCollectDBALColumns()
63
    {
64
        $this->assertSame([$this->dbalColumn], $this->fieldMapping->collectDBALColumns());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function shouldResolveFieldValue()
71
    {
72
        /** @var HydrationContextInterface $context */
73
        $context = $this->createMock(HydrationContextInterface::class);
74
        $context->method('getEntityClass')->willReturn(EntityExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Addiks\RDMBundle\Hydrati...drationContextInterface. ( Ignorable by Annotation )

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

74
        $context->/** @scrutinizer ignore-call */ 
75
                  method('getEntityClass')->willReturn(EntityExample::class);

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...
Bug introduced by
The type Addiks\RDMBundle\Tests\Mapping\EntityExample was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
        $context->method('getEntityManager')->willReturn($this->createEntityManagerMock());
76
77
        /** @var string $expectedResult */
78
        $expectedResult = 'bar';
79
80
        $this->dbalColumn->method('getName')->willReturn('foo');
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\DBAL\Schema\Column. ( Ignorable by Annotation )

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

80
        $this->dbalColumn->/** @scrutinizer ignore-call */ 
81
                           method('getName')->willReturn('foo');

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...
81
        $this->dbalColumn->method('getType')->willReturn(Type::getType('string'));
82
83
        /** @var mixed $actualResult */
84
        $actualResult = $this->fieldMapping->resolveValue(
85
            $context,
86
            [
87
                'foo' => $expectedResult
88
            ]
89
        );
90
91
        $this->assertSame($expectedResult, $actualResult);
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function shouldRevertFieldValue()
98
    {
99
        /** @var HydrationContextInterface $context */
100
        $context = $this->createMock(HydrationContextInterface::class);
101
        $context->method('getEntityClass')->willReturn(EntityExample::class);
102
        $context->method('getEntityManager')->willReturn($this->createEntityManagerMock());
103
104
        $this->dbalColumn->method('getName')->willReturn('foo');
105
        $this->dbalColumn->method('getType')->willReturn(Type::getType('string'));
106
107
        $this->assertEquals(
108
            ['foo' => "Lorem ipsum"],
109
            $this->fieldMapping->revertValue($context, "Lorem ipsum")
110
        );
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function shouldAssertFieldValue()
117
    {
118
        /** @var HydrationContextInterface $context */
119
        $context = $this->createMock(HydrationContextInterface::class);
120
        $context->method('getEntityClass')->willReturn(EntityExample::class);
121
        $context->method('getEntityManager')->willReturn($this->createEntityManagerMock());
122
123
        $this->assertSame(null, $this->fieldMapping->assertValue(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fieldMapping->ass...ontext, array(), 'Foo') targeting Addiks\RDMBundle\Mapping...dMapping::assertValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
124
            $context,
125
            [],
126
            "Foo"
127
        ));
128
    }
129
130
    private function createEntityManagerMock(): EntityManagerInterface
131
    {
132
        /** @var EntityManagerInterface $entityManager */
133
        $entityManager = $this->createMock(EntityManagerInterface::class);
134
135
        /** @var Connection $connection */
136
        $connection = $this->createMock(Connection::class);
137
138
        $entityManager->method('getConnection')->willReturn($connection);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\EntityManagerInterface. ( Ignorable by Annotation )

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

138
        $entityManager->/** @scrutinizer ignore-call */ 
139
                        method('getConnection')->willReturn($connection);

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...
139
140
        /** @var AbstractPlatform $platform */
141
        $platform = $this->createMock(AbstractPlatform::class);
142
143
        $connection->method('getDatabasePlatform')->willReturn($platform);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\DBAL\Connection. ( Ignorable by Annotation )

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

143
        $connection->/** @scrutinizer ignore-call */ 
144
                     method('getDatabasePlatform')->willReturn($platform);

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...
144
145
        return $entityManager;
146
    }
147
148
    /**
149
     * @test
150
     */
151
    public function shouldWakeUpInnerMapping()
152
    {
153
        /** @var ContainerInterface $container */
154
        $container = $this->createMock(ContainerInterface::class);
155
156
        $this->assertNull($this->fieldMapping->wakeUpMapping($container));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fieldMapping->wakeUpMapping($container) targeting Addiks\RDMBundle\Mapping...apping::wakeUpMapping() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
    }
158
159
}
160