Passed
Push — master ( 356755...14835f )
by Gerrit
04:25
created

ListValueResolverTest::shouldResolveValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\ValueResolver\ListValueResolver;
15
use Addiks\RDMBundle\Mapping\ListMappingInterface;
16
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
17
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
18
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
19
use Doctrine\DBAL\Schema\Column;
20
use Addiks\RDMBundle\Mapping\MappingInterface;
21
22
final class ListValueResolverTest extends TestCase
23
{
24
25
    /**
26
     * @var ListValueResolver
27
     */
28
    private $valueResolver;
29
30
    /**
31
     * @var ValueResolverInterface
32
     */
33
    private $entryValueResolver;
34
35
    public function setUp()
36
    {
37
        $this->entryValueResolver = $this->createMock(ValueResolverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...solverInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...ValueResolverInterface> of property $entryValueResolver.

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...
38
39
        $this->valueResolver = new ListValueResolver($this->entryValueResolver);
0 ignored issues
show
Documentation introduced by
$this->entryValueResolver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
40
    }
41
42
    /**
43
     * @test
44
     */
45
    public function shouldResolveValue()
46
    {
47
        /** @var ListMappingInterface $listMapping */
48
        $listMapping = $this->createMock(ListMappingInterface::class);
49
50
        /** @var HydrationContextInterface $context */
51
        $context = $this->createMock(HydrationContextInterface::class);
52
53
        /** @var Column $column */
54
        $column = $this->createMock(Column::class);
55
        $column->method('getName')->willReturn('some_column');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Schema\Column>.

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...
56
57
        $listMapping->method('getDBALColumn')->willReturn($column);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...g\ListMappingInterface>.

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...
58
59
        /** @var array $dataFromAdditionalColumns */
60
        $dataFromAdditionalColumns = [
61
            'some_column' => '{"a":"LOREM","b":"IPSUM","c":"DOLOR","d":"SIT","e":"AMET"}',
62
        ];
63
64
        $this->entryValueResolver->method('resolveValue')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
65
            function ($mapping, $context, $data) {
66
                return strtolower($data['']);
67
            }
68
        ));
69
70
        /** @var mixed $expectedResult */
71
        $expectedResult = [
72
            'a' => 'lorem',
73
            'b' => 'ipsum',
74
            'c' => 'dolor',
75
            'd' => 'sit',
76
            'e' => 'amet'
77
        ];
78
79
        /** @var mixed $actualResult */
80
        $actualResult = $this->valueResolver->resolveValue(
81
            $listMapping,
82
            $context,
83
            $dataFromAdditionalColumns
84
        );
85
86
        $this->assertEquals($expectedResult, $actualResult);
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function shouldRevertValue()
93
    {
94
        /** @var ListMappingInterface $listMapping */
95
        $listMapping = $this->createMock(ListMappingInterface::class);
96
97
        /** @var HydrationContextInterface $context */
98
        $context = $this->createMock(HydrationContextInterface::class);
99
100
        /** @var Column $column */
101
        $column = $this->createMock(Column::class);
102
        $column->method('getName')->willReturn('some_column');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Schema\Column>.

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...
103
104
        $listMapping->method('getDBALColumn')->willReturn($column);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...g\ListMappingInterface>.

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...
105
106
        $this->entryValueResolver->method('revertValue')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
107
            function ($mapping, $context, $line) {
108
                return ['' => strtoupper($line)];
109
            }
110
        ));
111
112
        /** @var mixed $valueFromEntityField */
113
        $valueFromEntityField = [
114
            'lorem',
115
            'ipsum',
116
            'dolor',
117
            'sit',
118
            'amet'
119
        ];
120
121
        /** @var mixed $expectedResult */
122
        $expectedResult = [
123
            'some_column' => '["LOREM","IPSUM","DOLOR","SIT","AMET"]',
124
        ];
125
126
        /** @var mixed $actualResult */
127
        $actualResult = $this->valueResolver->revertValue(
128
            $listMapping,
129
            $context,
130
            $valueFromEntityField
131
        );
132
133
        $this->assertEquals($expectedResult, $actualResult);
134
    }
135
136
    /**
137
     * @test
138
     */
139 View Code Duplication
    public function shouldNotRevertValueOnNonListMapping()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        /** @var MappingInterface $listMapping */
142
        $listMapping = $this->createMock(MappingInterface::class);
143
144
        /** @var HydrationContextInterface $context */
145
        $context = $this->createMock(HydrationContextInterface::class);
146
147
        /** @var mixed $valueFromEntityField */
148
        $valueFromEntityField = [];
149
150
        $this->assertEmpty($this->valueResolver->revertValue(
151
            $listMapping,
152
            $context,
153
            $valueFromEntityField
154
        ));
155
    }
156
157
    /**
158
     * @test
159
     */
160 View Code Duplication
    public function shouldAssertValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $this->assertNull($this->valueResolver->assertValue(
163
            $this->createMock(ListMappingInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...appingInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
164
            $this->createMock(HydrationContextInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...ontextInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...rationContextInterface>.

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...
165
            [],
166
            null
167
        ));
168
    }
169
170
    /**
171
     * @test
172
     */
173
    public function shouldThrowExceptionOnFailedAssertion()
174
    {
175
        $this->expectException(FailedRDMAssertionException::class);
176
177
        $this->valueResolver->assertValue(
178
            $this->createMock(ListMappingInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...appingInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\Mapping\MappingInterface>.

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...
179
            $this->createMock(HydrationContextInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...ontextInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...rationContextInterface>.

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...
180
            [],
181
            "Lorem ipsum!"
182
        );
183
    }
184
185
}
186