Completed
Push — master ( da3759...8e04a1 )
by Gerrit
02:15
created

ArgumentFactoryLazyLoadProxyTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
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\SymfonyGenerics\Arguments\ArgumentFactory;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactoryLazyLoadProxy;
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use Psr\Container\ContainerInterface;
17
use Addiks\SymfonyGenerics\Arguments\Argument;
18
use InvalidArgumentException;
19
20
final class ArgumentFactoryLazyLoadProxyTest extends TestCase
21
{
22
23
    /**
24
     * @var ArgumentFactoryLazyLoadProxy
25
     */
26
    private $factory;
27
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @var ArgumentFactory
35
     */
36
    private $loadedArgumentFactory;
37
38
    public function setUp()
39
    {
40
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Psr\C...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Psr\Container\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...
41
        $this->loadedArgumentFactory = $this->createMock(ArgumentFactory::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...ArgumentFactory::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...actory\ArgumentFactory> of property $loadedArgumentFactory.

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...
42
43
        $this->factory = new ArgumentFactoryLazyLoadProxy($this->container, 'some-service-id');
0 ignored issues
show
Documentation introduced by
$this->container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\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...
44
    }
45
46
    /**
47
     * @test
48
     * @dataProvider dataProviderShouldAskInnerFactoryIfUnderstandsString
49
     */
50 View Code Duplication
    public function shouldAskInnerFactoryIfUnderstandsString(bool $expectedResult, string $source)
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...
51
    {
52
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\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
            $this->equalTo('some-service-id')
54
        )->willReturn($this->loadedArgumentFactory);
55
56
        $this->loadedArgumentFactory->expects($this->once())->method('understandsString')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
57
            $this->equalTo($source)
58
        )->willReturn($expectedResult);
59
60
        /** @var bool $actualResult */
61
        $actualResult = $this->factory->understandsString($source);
62
63
        $this->assertEquals($expectedResult, $actualResult);
64
    }
65
66 View Code Duplication
    public function dataProviderShouldAskInnerFactoryIfUnderstandsString()
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...
67
    {
68
        return array(
69
            [true, 'foo'],
70
            [false, 'foo'],
71
        );
72
    }
73
74
    /**
75
     * @test
76
     * @dataProvider dataProviderShouldAskInnerFactoryIfUnderstandsArray
77
     */
78 View Code Duplication
    public function shouldAskInnerFactoryIfUnderstandsArray(bool $expectedResult, array $source)
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...
79
    {
80
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\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...
81
            $this->equalTo('some-service-id')
82
        )->willReturn($this->loadedArgumentFactory);
83
84
        $this->loadedArgumentFactory->expects($this->once())->method('understandsArray')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
85
            $this->equalTo($source)
86
        )->willReturn($expectedResult);
87
88
        /** @var bool $actualResult */
89
        $actualResult = $this->factory->understandsArray($source);
90
91
        $this->assertEquals($expectedResult, $actualResult);
92
    }
93
94 View Code Duplication
    public function dataProviderShouldAskInnerFactoryIfUnderstandsArray()
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...
95
    {
96
        return array(
97
            [true, ['foo']],
98
            [false, ['foo']],
99
        );
100
    }
101
102
    /**
103
     * @test
104
     * @dataProvider dataProviderShouldCreateArgumentFromStringUsingInnerFactory
105
     */
106 View Code Duplication
    public function shouldCreateArgumentFromStringUsingInnerFactory(Argument $expectedResult, string $source)
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...
107
    {
108
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\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...
109
            $this->equalTo('some-service-id')
110
        )->willReturn($this->loadedArgumentFactory);
111
112
        $this->loadedArgumentFactory->expects($this->once())->method('createArgumentFromString')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
113
            $this->equalTo($source)
114
        )->willReturn($expectedResult);
115
116
        /** @var mixed $actualResult */
117
        $actualResult = $this->factory->createArgumentFromString($source);
118
119
        $this->assertEquals($expectedResult, $actualResult);
120
    }
121
122 View Code Duplication
    public function dataProviderShouldCreateArgumentFromStringUsingInnerFactory()
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...
123
    {
124
        return array(
125
            [$this->createMock(Argument::class), 'foo'],
126
            [$this->createMock(Argument::class), 'foo'],
127
        );
128
    }
129
130
    /**
131
     * @test
132
     * @dataProvider dataProviderShouldCreateArgumentFromArrayUsingInnerFactory
133
     */
134 View Code Duplication
    public function shouldCreateArgumentFromArrayUsingInnerFactory(Argument $expectedResult, array $source)
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...
135
    {
136
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\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...
137
            $this->equalTo('some-service-id')
138
        )->willReturn($this->loadedArgumentFactory);
139
140
        $this->loadedArgumentFactory->expects($this->once())->method('createArgumentFromArray')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
141
            $this->equalTo($source)
142
        )->willReturn($expectedResult);
143
144
        /** @var mixed $actualResult */
145
        $actualResult = $this->factory->createArgumentFromArray($source);
146
147
        $this->assertEquals($expectedResult, $actualResult);
148
    }
149
150 View Code Duplication
    public function dataProviderShouldCreateArgumentFromArrayUsingInnerFactory()
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...
151
    {
152
        return array(
153
            [$this->createMock(Argument::class), ['foo']],
154
            [$this->createMock(Argument::class), ['foo']],
155
        );
156
    }
157
158
    /**
159
     * @test
160
     */
161
    public function shouldRejectInvalidService()
162
    {
163
        $this->expectException(InvalidArgumentException::class);
164
        $this->expectExceptionMessage(sprintf(
165
            "Expected service 'some-service-id' to be instance of '%s'!",
166
            ArgumentFactory::class
167
        ));
168
169
        $container = $this->createMock(ContainerInterface::class);
170
        $container->expects($this->once())->method('get')->with(
171
            $this->equalTo('some-service-id')
172
        )->willReturn(null);
173
174
        $factory = new ArgumentFactoryLazyLoadProxy($container, 'some-service-id');
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\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...
175
        $factory->createArgumentFromArray([]);
176
    }
177
178
}
179