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); |
|
|
|
|
43
|
|
|
$this->argumentFactory = $this->createMock(ArgumentFactory::class); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->factory = new EntityArgumentFactory($this->objectManager, $this->argumentFactory); |
|
|
|
|
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)); |
|
|
|
|
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], |
|
|
|
|
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)); |
|
|
|
|
138
|
|
|
$this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class)); |
|
|
|
|
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)), [ |
|
|
|
|
155
|
|
|
'entity-class' => 'Foo', |
156
|
|
|
'entity-id' => 'bar' |
157
|
|
|
], false], |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
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..