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\RequestFileArgumentFactory; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\RequestFileArgument; |
18
|
|
|
|
19
|
|
|
final class RequestFileArgumentFactoryTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RequestFileArgumentFactory |
24
|
|
|
*/ |
25
|
|
|
private $factory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RequestStack |
29
|
|
|
*/ |
30
|
|
|
private $requestStack; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->requestStack = $this->createMock(RequestStack::class); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$this->factory = new RequestFileArgumentFactory($this->requestStack); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
* @dataProvider dataProviderForShouldKnowIfUnderstandsString |
42
|
|
|
*/ |
43
|
|
|
public function shouldKnowIfUnderstandsString(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 dataProviderForShouldKnowIfUnderstandsString() |
52
|
|
|
{ |
53
|
|
|
return array( |
54
|
|
|
[true, '$files.foo'], |
55
|
|
|
[true, '$files.f'], |
56
|
|
|
[true, '$files.foo.bar'], |
57
|
|
|
[false, '$files.'], |
58
|
|
|
[false, '$file.foo'], |
59
|
|
|
[false, 'files.foo'], |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
* @dataProvider dataProviderForShouldKnowIfUnderstandsArray |
66
|
|
|
*/ |
67
|
|
|
public function shouldKnowIfUnderstandsArray(bool $expectedResult, array $source) |
68
|
|
|
{ |
69
|
|
|
/** @var bool $actualResult */ |
70
|
|
|
$actualResult = $this->factory->understandsArray($source); |
71
|
|
|
|
72
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function dataProviderForShouldKnowIfUnderstandsArray() |
76
|
|
|
{ |
77
|
|
|
return array( |
78
|
|
|
[true, ['type' => 'request-file', 'key' => 'foo']], |
79
|
|
|
[true, ['type' => 'request-file', 'key' => 'bar']], |
80
|
|
|
[true, ['type' => 'request-file', 'key' => 'f']], |
81
|
|
|
[true, ['type' => 'request-file', 'key' => '']], |
82
|
|
|
[false, ['type' => 'request-file', ]], |
83
|
|
|
[false, ['key' => 'foo']], |
84
|
|
|
[false, ['type' => 'request-blah', 'key' => 'foo']], |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
* @dataProvider dataProviderForShouldCreateArgumentFromString |
91
|
|
|
*/ |
92
|
|
|
public function shouldCreateArgumentFromString($expectedResult, string $source, bool $expectException) |
93
|
|
|
{ |
94
|
|
|
if ($expectException) { |
95
|
|
|
$this->expectException(InvalidArgumentException::class); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$actualResult = $this->factory->createArgumentFromString($source); |
99
|
|
|
|
100
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function dataProviderForShouldCreateArgumentFromString() |
104
|
|
|
{ |
105
|
|
|
$this->setUp(); |
106
|
|
|
|
107
|
|
|
return array( |
108
|
|
|
[new RequestFileArgument($this->requestStack, 'foo', 'content'), '$files.foo', false], |
109
|
|
|
[new RequestFileArgument($this->requestStack, 'foo', 'asd'), '$files.foo.asd', false], |
110
|
|
|
[new RequestFileArgument($this->requestStack, 'bar', 'asd'), '$files.bar.asd', false], |
111
|
|
|
[new RequestFileArgument($this->requestStack, '', 'content'), '$files.', false], |
112
|
|
|
[null, '$file.foo', true], |
113
|
|
|
[null, 'files.foo', true], |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
* @dataProvider dataProviderForShouldCreateArgumentFromArray |
120
|
|
|
*/ |
121
|
|
|
public function shouldCreateArgumentFromArray($expectedResult, array $source, bool $expectException) |
122
|
|
|
{ |
123
|
|
|
if ($expectException) { |
124
|
|
|
$this->expectException(InvalidArgumentException::class); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$actualResult = $this->factory->createArgumentFromArray($source); |
128
|
|
|
|
129
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function dataProviderForShouldCreateArgumentFromArray() |
133
|
|
|
{ |
134
|
|
|
$this->setUp(); |
135
|
|
|
|
136
|
|
|
return array( |
137
|
|
|
[new RequestFileArgument($this->requestStack, 'foo', 'content'), ['key' => 'foo'], false], |
138
|
|
|
[new RequestFileArgument($this->requestStack, 'foo', 'asd'), ['key' => 'foo', 'property' => 'asd'], false], |
139
|
|
|
[new RequestFileArgument($this->requestStack, 'bar', 'asd'), ['key' => 'bar', 'property' => 'asd'], false], |
140
|
|
|
[new RequestFileArgument($this->requestStack, '', 'content'), ['key' => ''], false], |
141
|
|
|
[null, [], true], |
142
|
|
|
[null, ['property' => 'asd'], true], |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
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..