Completed
Push — master ( 47ba96...4fd14e )
by Gerrit
02:18
created

shouldKnowWhatStringsToUnderstand()   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 2
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\AdditionalDataArgumentFactory;
15
use Addiks\SymfonyGenerics\Arguments\ArgumentContextInterface;
16
use InvalidArgumentException;
17
use Addiks\SymfonyGenerics\Arguments\AdditionalDataArgument;
18
19
final class AdditionalDataArgumentFactoryTest extends TestCase
20
{
21
22
    /**
23
     * @var AdditionalDataArgumentFactory
24
     */
25
    private $factory;
26
27
    /**
28
     * @var ArgumentContextInterface
29
     */
30
    private $context;
31
32
    public function setUp()
33
    {
34
        $this->context = $this->createMock(ArgumentContextInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...ontextInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...gumentContextInterface> of property $context.

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...
35
36
        $this->factory = new AdditionalDataArgumentFactory($this->context);
0 ignored issues
show
Documentation introduced by
$this->context is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...gumentContextInterface>.

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...
37
    }
38
39
    /**
40
     * @test
41
     * @dataProvider dataproviderForShouldKnowWhatStringsToUnderstand
42
     */
43
    public function shouldKnowWhatStringsToUnderstand(bool $expectedResult, string $source)
44
    {
45
        /** @var bool $actualResult */
46
        $actualResult = $this->factory->understandsString($source);
47
48
        $this->assertEquals($expectedResult, $actualResult);
49
    }
50
51
    public function dataproviderForShouldKnowWhatStringsToUnderstand(): array
52
    {
53
        return array(
54
            [true,  '%foo%'],
55
            [true,  '%bar%'],
56
            [true,  '%baz%'],
57
            [false, 'foo'],
58
            [false, '%foo'],
59
            [false, 'foo%'],
60
            [false, '$foo'],
61
            [false, ''],
62
        );
63
    }
64
65
    /**
66
     * @test
67
     * @dataProvider dataproviderForShouldKnowWhatArraysToUnderstand
68
     */
69
    public function shouldKnowWhatArraysToUnderstand(bool $expectedResult, array $source)
70
    {
71
        /** @var bool $actualResult */
72
        $actualResult = $this->factory->understandsArray($source);
73
74
        $this->assertEquals($expectedResult, $actualResult);
75
    }
76
77
    public function dataproviderForShouldKnowWhatArraysToUnderstand(): array
78
    {
79
        return array(
80
            [true,  ['type' => 'additional-data', 'key' => 'foo']],
81
            [true,  ['type' => 'additional-data', 'key' => 'bar']],
82
            [true,  ['type' => 'additional-data', 'key' => 'a']],
83
            [true,  ['type' => 'additional-data', 'key' => 'aaaaaaaaaaaaaaaaaa']],
84
            [false, ['type' => 'additional-data', 'key' => null]],
85
            [false, ['type' => 'additional-NULL', 'key' => 'foo']],
86
            [false, ['type' => 'additional-data', 'ley' => 'foo']],
87
            [false, ['type' => 'additional-data']],
88
            [false, ['key' => 'foo']],
89
        );
90
    }
91
92
    /**
93
     * @test
94
     * @dataProvider dataproviderForShouldCreateArgumentFromString
95
     */
96 View Code Duplication
    public function shouldCreateArgumentFromString($expectedResult, string $source, bool $shouldReject)
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...
97
    {
98
        if ($shouldReject) {
99
            $this->expectException(InvalidArgumentException::class);
100
        }
101
102
        /** @var object $actualResult */
103
        $actualResult = $this->factory->createArgumentFromString($source);
104
105
        $this->assertEquals($expectedResult, $actualResult);
106
    }
107
108
    public function dataproviderForShouldCreateArgumentFromString(): array
109
    {
110
        $this->setUp();
111
112
        return array(
113
            [new AdditionalDataArgument('foo', $this->context), '%foo%', false],
114
            [new AdditionalDataArgument('lorem-ipsum', $this->context), '%lorem-ipsum%', false],
115
            [new AdditionalDataArgument('a', $this->context), '%a%', false],
116
            [null, '%%', true],
117
            [null, 'foo%', true],
118
            [null, '%foo', true],
119
            [null, 'foo', true],
120
        );
121
    }
122
123
    /**
124
     * @test
125
     * @dataProvider dataproviderForShouldCreateArgumentFromArray
126
     */
127 View Code Duplication
    public function shouldCreateArgumentFromArray($expectedResult, array $source, bool $shouldReject)
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...
128
    {
129
        if ($shouldReject) {
130
            $this->expectException(InvalidArgumentException::class);
131
        }
132
133
        /** @var object $actualResult */
134
        $actualResult = $this->factory->createArgumentFromArray($source);
135
136
        $this->assertEquals($expectedResult, $actualResult);
137
    }
138
139
    public function dataproviderForShouldCreateArgumentFromArray(): array
140
    {
141
        $this->setUp();
142
143
        return array(
144
            [new AdditionalDataArgument('foo', $this->context), ['type' => 'additional-data', 'key' => 'foo'], false],
145
            [new AdditionalDataArgument('lorem-ipsum', $this->context), ['type' => 'additional-data', 'key' => 'lorem-ipsum'], false],
146
            [new AdditionalDataArgument('a', $this->context), ['type' => 'additional-data', 'key' => 'a'], false],
147
            [null, ['type' => 'additional-data', 'key' => null], true],
148
            [null, ['type' => 'additional-data', ], true],
149
            [null, ['key' => null], true],
150
        );
151
    }
152
153
}
154