Completed
Push — master ( 1f339d...13b514 )
by Gerrit
04:45
created

shouldNotAssertIfAlwaysLaxModeIsActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
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\RDMBundle\Tests\Mapping;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Mapping\ServiceMapping;
15
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
18
use Addiks\RDMBundle\Tests\Hydration\ServiceExample;
19
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
20
21
final class ServiceMappingTest extends TestCase
22
{
23
24
    /**
25
     * @var ServiceMapping
26
     */
27
    private $serviceMapping;
28
29
    /**
30
     * @var ContainerInterface
31
     */
32
    private $container;
33
34
    /**
35
     * @var ServiceMapping
36
     */
37
    private $defaultServiceMapping;
38
39
    public function setUp(
40
        bool $isLax = true,
41
        string $serviceId = "some_service_id"
42
    ) {
43
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
44
45
        $this->serviceMapping = new ServiceMapping(
46
            $this->container,
0 ignored issues
show
Documentation introduced by
$this->container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

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...
47
            $serviceId,
48
            $isLax,
49
            "some origin"
50
        );
51
52
        $this->defaultServiceMapping = new ServiceMapping(
53
            $this->container,
0 ignored issues
show
Documentation introduced by
$this->container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

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...
54
            "some_default_service_id"
55
        );
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function shouldKnowItsServiceId()
62
    {
63
        $this->assertEquals("some_service_id", $this->serviceMapping->getServiceId());
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function shouldKnowItsOrigin()
70
    {
71
        $this->assertEquals("some origin", $this->serviceMapping->describeOrigin());
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function shouldHaveNoColumns()
78
    {
79
        $this->assertEquals([], $this->serviceMapping->collectDBALColumns());
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function shouldKnowIfLaxOrNot()
86
    {
87
        $this->assertEquals(true, $this->serviceMapping->isLax());
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function shouldBeNotLaxByDefault()
94
    {
95
        $this->assertEquals(false, $this->defaultServiceMapping->isLax());
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function shouldNotKnowItsOriginByDefault()
102
    {
103
        $this->assertEquals("unknown", $this->defaultServiceMapping->describeOrigin());
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function shouldResolveToService()
110
    {
111
        $this->setUp(false, "some_service");
112
113
        $expectedService = new ServiceExample("lorem", 123);
114
115
        $this->container->method('has')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
116
            ['some_service', true],
117
        ]));
118
119
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
120
            ['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $expectedService],
121
        ]));
122
123
        /** @var mixed $actualValue */
124
        $actualService = $this->serviceMapping->resolveValue(
125
            $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...
126
            []
127
        );
128
129
        $this->assertSame($expectedService, $actualService);
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function shouldNotRevertService()
136
    {
137
        $this->assertEquals([], $this->serviceMapping->revertValue(
138
            $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...
139
            null
140
        ));
141
    }
142
143
    /**
144
     * @test
145
     */
146
    public function shouldAssertThatCorrectServiceWasSet()
147
    {
148
        $this->setUp(false, "some_service");
149
150
        $service = new ServiceExample("lorem", 123);
151
        $otherService = new ServiceExample("ipsum", 456);
152
153
        $this->container->method('has')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
154
            ['some_service', true],
155
        ]));
156
157
        $this->container->method('get')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
158
            ['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $service],
159
        ]));
160
161
        $this->expectException(FailedRDMAssertionExceptionInterface::class);
162
163
        /** @var HydrationContextInterface $context */
164
        $context = $this->createMock(HydrationContextInterface::class);
165
        $context->method('getEntityClass')->willReturn(EntityExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...rationContextInterface>.

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...
166
167
        $this->serviceMapping->assertValue($context, [], $otherService);
168
    }
169
170
    /**
171
     * @test
172
     */
173 View Code Duplication
    public function shouldNotAssertLaxFieldMappings()
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...
174
    {
175
        $this->setUp(true);
176
177
        $this->container->expects($this->never())->method('has');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
178
        $this->container->expects($this->never())->method('get');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
179
180
        /** @var HydrationContextInterface $context */
181
        $context = $this->createMock(HydrationContextInterface::class);
182
        $context->method('getEntityClass')->willReturn(EntityExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...rationContextInterface>.

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...
183
184
        $service = new ServiceExample("lorem", 123);
185
186
        $this->serviceMapping->assertValue($context, [], $service);
187
    }
188
189
    /**
190
     * @test
191
     */
192
    public function shouldNotAssertIfAlwaysLaxModeIsActive()
193
    {
194
        $this->setUp(false);
195
196
        $this->container->expects($this->never())->method('has');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
197
        $this->container->expects($this->never())->method('get');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
198
199
        /** @var HydrationContextInterface $context */
200
        $context = $this->createMock(HydrationContextInterface::class);
201
        $context->method('getEntityClass')->willReturn(EntityExample::class);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...rationContextInterface>.

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...
202
203
        /** @var mixed $service */
204
        $service = new ServiceExample("lorem", 123);
205
206
        ServiceMapping::setAlwaysLax(true);
207
208
        $this->serviceMapping->assertValue($context, [], $service);
209
210
        ServiceMapping::setAlwaysLax(false);
211
    }
212
213
    /**
214
     * @test
215
     */
216 View Code Duplication
    public function shouldWakeUpInnerMapping()
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...
217
    {
218
        /** @var ContainerInterface $container */
219
        $container = $this->createMock(ContainerInterface::class);
220
221
        /** @var HydrationContextInterface $context */
222
        $context = $this->createMock(HydrationContextInterface::class);
223
224
        $container->method("has")->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
225
        $container->method("get")->willReturn("Foo bar baz");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
226
227
        $this->serviceMapping->wakeUpMapping($container);
228
229
        $this->assertSame("Foo bar baz", $this->serviceMapping->resolveValue($context, []));
230
    }
231
232
}
233