Completed
Push — master ( 4d3c9c...4a4bf4 )
by Gerrit
02:32
created

shouldAssertThatCorrectServiceWasSet()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 1
cc 1
eloc 13
nc 1
nop 0
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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\ValueResolver\ServiceValueResolver;
15
use Addiks\RDMBundle\Tests\Hydration\ServiceExample;
16
use Addiks\RDMBundle\Mapping\ServiceMappingInterface;
17
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
20
21
final class ServiceValueResolverTest extends TestCase
22
{
23
24
    /**
25
     * @var ServiceValueResolver
26
     */
27
    private $valueResolver;
28
29
    /**
30
     * @var ContainerInterface
31
     */
32
    private $container;
33
34
    public function setUp()
35
    {
36
        $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...
37
38
        $this->valueResolver = new ServiceValueResolver($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...
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function shouldResolveAValue()
45
    {
46
        $expectedService = new ServiceExample("lorem", 123);
47
48
        $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...
49
            ['some_service', true],
50
        ]));
51
52
        $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...
53
            ['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $expectedService],
54
        ]));
55
56
        /** @var ServiceMappingInterface $fieldMapping */
57
        $fieldMapping = $this->createMock(ServiceMappingInterface::class);
58
59
        $fieldMapping->method('getServiceId')->willReturn("some_service");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...erviceMappingInterface>.

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...
60
61
        /** @var mixed $actualValue */
62
        $actualService = $this->valueResolver->resolveValue(
63
            $fieldMapping,
64
            $this->createMock(EntityExample::class),
65
            []
66
        );
67
68
        $this->assertSame($expectedService, $actualService);
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function shouldRevertValue()
75
    {
76
        /** @var ServiceMappingInterface $fieldMapping */
77
        $fieldMapping = $this->createMock(ServiceMappingInterface::class);
78
79
        $this->assertEquals([], $this->valueResolver->revertValue(
80
            $fieldMapping,
81
            $this->createMock(EntityExample::class),
82
            null
83
        ));
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function shouldAssertThatCorrectServiceWasSet()
90
    {
91
        /** @var ServiceMappingInterface $fieldMapping */
92
        $fieldMapping = $this->createMock(ServiceMappingInterface::class);
93
94
        $fieldMapping->method('isLax')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...erviceMappingInterface>.

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...
95
        $fieldMapping->method('getServiceId')->willReturn("some_service");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...erviceMappingInterface>.

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...
96
97
        $entity = new EntityExample();
98
99
        $service = new ServiceExample("lorem", 123);
100
        $otherService = new ServiceExample("ipsum", 456);
101
102
        $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...
103
            ['some_service', true],
104
        ]));
105
106
        $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...
107
            ['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $service],
108
        ]));
109
110
        $this->expectException(FailedRDMAssertionExceptionInterface::class);
111
112
        $this->valueResolver->assertValue($fieldMapping, $entity, [], $otherService);
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function shouldNotAssertLaxFieldMappings()
119
    {
120
        /** @var ServiceMappingInterface $fieldMapping */
121
        $fieldMapping = $this->createMock(ServiceMappingInterface::class);
122
123
        $fieldMapping->method('isLax')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...erviceMappingInterface>.

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...
124
125
        $fieldMapping->expects($this->never())->method('getServiceId');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\RDMBundle\...erviceMappingInterface>.

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...
126
127
        $entity = new EntityExample();
128
129
        $service = new ServiceExample("lorem", 123);
130
131
        $this->valueResolver->assertValue($fieldMapping, $entity, [], $service);
132
    }
133
134
}
135