Passed
Push — master ( 4fd14e...da3759 )
by Gerrit
02:04
created

shouldCreateCallArgumentFromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\Tests\Unit\Arguments\ArgumentFactory;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
15
use InvalidArgumentException;
16
use Addiks\SymfonyGenerics\Arguments\ArgumentCall;
17
use Addiks\SymfonyGenerics\Arguments\Argument;
18
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\EntityArgumentFactory;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use Addiks\SymfonyGenerics\Arguments\EntityArgument;
21
22
final class EntityArgumentFactoryTest extends TestCase
23
{
24
25
    /**
26
     * @var EntityArgumentFactory
27
     */
28
    private $factory;
29
30
    /**
31
     * @var ObjectManager
32
     */
33
    private $objectManager;
34
35
    /**
36
     * @var ArgumentFactory
37
     */
38
    private $argumentFactory;
39
40
    public function setUp()
41
    {
42
        $this->objectManager = $this->createMock(ObjectManager::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...e\ObjectManager::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\Common\Persistence\ObjectManager> of property $objectManager.

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...
43
        $this->argumentFactory = $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 $argumentFactory.

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->factory = new EntityArgumentFactory($this->objectManager, $this->argumentFactory);
0 ignored issues
show
Documentation introduced by
$this->objectManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\Persistence\ObjectManager>.

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...
Documentation introduced by
$this->argumentFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
46
    }
47
48
    /**
49
     * @test
50
     * @dataProvider dataProviderForShouldKnowIfUnderstandString
51
     */
52
    public function shouldKnowIfUnderstandString(bool $expectedResult, string $source)
53
    {
54
        /** @var bool $actualResult */
55
        $actualResult = $this->factory->understandsString($source);
56
57
        $this->assertEquals($expectedResult, $actualResult);
58
    }
59
60
    public function dataProviderForShouldKnowIfUnderstandString()
61
    {
62
        return array(
63
            [true, 'Foo#bar'],
64
            [false, 'Foo#'],
65
            [false, '#bar'],
66
            [false, '#'],
67
        );
68
    }
69
70
    /**
71
     * @test
72
     * @dataProvider dataProviderForShouldKnowIfUnderstandArray
73
     */
74
    public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source)
75
    {
76
        /** @var bool $actualResult */
77
        $actualResult = $this->factory->understandsArray($source);
78
79
        $this->assertEquals($expectedResult, $actualResult);
80
    }
81
82
    public function dataProviderForShouldKnowIfUnderstandArray()
83
    {
84
        return array(
85
            [true,  ['entity-class' => 'foo', 'entity-id' => 'bar']],
86
            [false, ['entity-id' => 'bar']],
87
            [false, ['entity-class' => 'foo']],
88
        );
89
    }
90
91
    /**
92
     * @test
93
     * @dataProvider dataProviderForShouldCreateCallArgumentFromString
94
     */
95
    public function shouldCreateCallArgumentFromString(
96
        ?EntityArgument $expectedResult,
97
        string $source,
98
        bool $shouldRejectCreation
99
    ) {
100
        if ($shouldRejectCreation) {
101
            $this->expectException(InvalidArgumentException::class);
102
103
        } else {
104
            $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class));
0 ignored issues
show
Bug introduced by
The method method() 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...
105
        }
106
107
        $actualResult = $this->factory->createArgumentFromString($source);
108
109
        $this->assertEquals($expectedResult, $actualResult);
110
    }
111
112
    public function dataProviderForShouldCreateCallArgumentFromString(): array
113
    {
114
        $this->setUp();
115
116
        return array(
117
            [new EntityArgument($this->objectManager, 'Foo', $this->createMock(Argument::class)), 'Foo#bar', false],
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
118
            [null, 'a#', true],
119
            [null, '#b', true],
120
            [null, '#', true],
121
        );
122
    }
123
124
    /**
125
     * @test
126
     * @dataProvider dataProviderForShouldCreateCallArgumentFromArray
127
     */
128
    public function shouldCreateCallArgumentFromArray(
129
        ?EntityArgument $expectedResult,
130
        array $source,
131
        bool $shouldRejectCreation
132
    ) {
133
        if ($shouldRejectCreation) {
134
            $this->expectException(InvalidArgumentException::class);
135
136
        } else {
137
            $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class));
0 ignored issues
show
Bug introduced by
The method method() 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...
138
            $this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class));
0 ignored issues
show
Bug introduced by
The method method() 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...
139
        }
140
141
        $actualResult = $this->factory->createArgumentFromArray($source);
142
143
        $this->assertEquals($expectedResult, $actualResult);
144
    }
145
146
    public function dataProviderForShouldCreateCallArgumentFromArray(): array
147
    {
148
        $this->setUp();
149
150
        return array(
151
            [null, [], true],
152
            [null, ['entity-id' => 'foo'], true],
153
            [null, ['entity-class' => 'bar'], true],
154
            [new EntityArgument($this->objectManager, 'Foo', $this->createMock(Argument::class)), [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
155
                'entity-class' => 'Foo',
156
                'entity-id' => 'bar'
157
            ], false],
158
        );
159
    }
160
161
}
162