1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* check import command |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ImportExportTest\Command; |
7
|
|
|
|
8
|
|
|
use Graviton\ImportExport\Command\ImportCommand; |
9
|
|
|
use Graviton\ImportExportTest\Util\TestUtils; |
10
|
|
|
use Symfony\Component\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Finder\Finder; |
14
|
|
|
use Symfony\Component\Yaml\Parser; |
15
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
16
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper as Dumper; |
17
|
|
|
use Webuni\FrontMatter\FrontMatter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
22
|
|
|
* @link http://swisscom.ch |
23
|
|
|
*/ |
24
|
|
|
class ImportCommandTest extends \PHPUnit_Framework_TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* String Http Client Class. |
28
|
|
|
*/ |
29
|
|
|
const CLIENT = 'Graviton\ImportExport\Service\HttpClient'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider uploadFileProvider |
33
|
|
|
* |
34
|
|
|
* @param string $host import target host with protocol |
35
|
|
|
* @param string $file file to import |
36
|
|
|
* @param string $path resulting path from file |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function testUploadFile($host, $file, $path) |
41
|
|
|
{ |
42
|
|
|
$clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
43
|
|
|
|
44
|
|
|
$responseMock = $this->createMock('Psr\Http\Message\ResponseInterface'); |
45
|
|
|
|
46
|
|
|
$responseMock |
47
|
|
|
->method('getHeader') |
48
|
|
|
->with('Link') |
49
|
|
|
->willReturn(['<' . $host . $path . '>; rel="self"']); |
50
|
|
|
|
51
|
|
|
$clientMock |
52
|
|
|
->method('request') |
53
|
|
|
->will($this->returnValue($responseMock)); |
54
|
|
|
|
55
|
|
|
$logger = TestUtils::getTestingLogger(); |
56
|
|
|
|
57
|
|
|
$sut = new ImportCommand( |
58
|
|
|
$logger, |
59
|
|
|
$clientMock, |
60
|
|
|
new Finder(), |
61
|
|
|
new FrontMatter(), |
62
|
|
|
new Parser(), |
63
|
|
|
new VarCloner(), |
64
|
|
|
new Dumper() |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$cmdTester = $this->getTester($sut, $file); |
68
|
|
|
$display = TestUtils::getFullStringFromLog($logger->getHandlers()[0]); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->assertContains('Loading data from ' . $file, $display); |
71
|
|
|
$this->assertContains('Wrote <' . $host . $path . '>; rel="self"', $display); |
72
|
|
|
$this->assertEquals(0, $cmdTester->getStatusCode()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array[] |
77
|
|
|
*/ |
78
|
|
|
public function uploadFileProvider() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
'basic valid file' => [ |
82
|
|
|
'http://localhost', |
83
|
|
|
__DIR__ . '/fixtures/set-01/test-2.json', |
84
|
|
|
'/core/app/test', |
85
|
|
|
], |
86
|
|
|
'basic valid image file' => [ |
87
|
|
|
'http://localhost', |
88
|
|
|
__DIR__ . '/fixtures/file', |
89
|
|
|
'/core/app/test', |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @dataProvider errorFileProvider |
96
|
|
|
* |
97
|
|
|
* @param string $host import target host with protocol |
98
|
|
|
* @param string $file file to import |
99
|
|
|
* @param array $errors errors to check for (check valid case if none given) |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function testErrorFile($host, $file, $errors = []) |
104
|
|
|
{ |
105
|
|
|
$clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
106
|
|
|
|
107
|
|
|
$responseMock = $this->createMock('Psr\Http\Message\ResponseInterface'); |
108
|
|
|
|
109
|
|
|
$responseMock |
110
|
|
|
->method('getBody') |
111
|
|
|
->willReturn(json_encode((object) ["message" => "invalid"])); |
112
|
|
|
$responseMock |
113
|
|
|
->method('getHeader') |
114
|
|
|
->with('Link') |
115
|
|
|
->willReturn(['<' . $host . $file . '>; rel="self"']); |
116
|
|
|
|
117
|
|
|
$requestMock = $this->createMock('Psr\Http\Message\RequestInterface'); |
118
|
|
|
$requestMock |
119
|
|
|
->method('getUri') |
120
|
|
|
->willReturn($host . '/core/app/test'); |
121
|
|
|
|
122
|
|
|
$exceptionMock = $this->getMockBuilder('GuzzleHttp\Exception\RequestException') |
123
|
|
|
->setConstructorArgs(['Client error: 400', $requestMock, $responseMock]) |
124
|
|
|
->getMock(); |
125
|
|
|
|
126
|
|
|
$exceptionMock |
127
|
|
|
->method('getRequest') |
128
|
|
|
->willReturn($requestMock); |
129
|
|
|
|
130
|
|
|
$exceptionMock |
131
|
|
|
->method('getResponse') |
132
|
|
|
->willReturn($responseMock); |
133
|
|
|
|
134
|
|
|
$clientMock |
135
|
|
|
->method('request') |
136
|
|
|
->willThrowException($exceptionMock); |
137
|
|
|
|
138
|
|
|
$logger = TestUtils::getTestingLogger(); |
139
|
|
|
|
140
|
|
|
$sut = new ImportCommand( |
141
|
|
|
$logger, |
142
|
|
|
$clientMock, |
143
|
|
|
new Finder(), |
144
|
|
|
new FrontMatter(), |
145
|
|
|
new Parser(), |
146
|
|
|
new VarCloner(), |
147
|
|
|
new Dumper() |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$cmdTester = $this->getTester($sut, $file); |
151
|
|
|
$display = TestUtils::getFullStringFromLog($logger->getHandlers()[0]); |
|
|
|
|
152
|
|
|
|
153
|
|
|
$this->assertContains('Loading data from ' . $file, $display); |
154
|
|
|
foreach ($errors as $error) { |
155
|
|
|
$this->assertContains( |
156
|
|
|
$error, |
157
|
|
|
$display.' - '.$cmdTester->getDisplay() |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
$this->assertEquals(1, $cmdTester->getStatusCode()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array[] |
165
|
|
|
*/ |
166
|
|
|
public function errorFileProvider() |
167
|
|
|
{ |
168
|
|
|
return [ |
169
|
|
|
'invalid file (server side)' => [ |
170
|
|
|
'http://localhost', |
171
|
|
|
__DIR__ . '/fixtures/set-01/test.json', |
172
|
|
|
[ |
173
|
|
|
'Failed to write <http://localhost/core/app/test> from \'' . |
174
|
|
|
__DIR__ . '/fixtures/set-01/test.json\' with message \'Client error: 400\'', |
175
|
|
|
'"message": "invalid"', |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
'missing target in file (user error)' => [ |
179
|
|
|
'http://localhost', |
180
|
|
|
__DIR__ . '/fixtures/set-01/test-3.json', |
181
|
|
|
[ |
182
|
|
|
'Missing target in \'' . __DIR__ . '/fixtures/set-01/test-3.json\'', |
183
|
|
|
], |
184
|
|
|
] |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* test rewriting of contents with --rewrite-host |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
public function testRewrite() |
194
|
|
|
{ |
195
|
|
|
$responseMock = $this->createMock('Psr\Http\Message\ResponseInterface'); |
196
|
|
|
|
197
|
|
|
$responseMock |
198
|
|
|
->method('getHeader') |
199
|
|
|
->with('Link') |
200
|
|
|
->willReturn(['<http://example.com/core/module/test>; rel="self"']); |
201
|
|
|
|
202
|
|
|
$clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
203
|
|
|
|
204
|
|
|
$clientMock |
205
|
|
|
->method('request') |
206
|
|
|
->with( |
207
|
|
|
$this->equalTo('PUT'), |
208
|
|
|
$this->equalTo('http://example.com/core/module/test'), |
209
|
|
|
$this->equalTo( |
210
|
|
|
[ |
211
|
|
|
'json' => 'http://example.com/core/app/test', |
212
|
|
|
'upload' => false |
213
|
|
|
] |
214
|
|
|
) |
215
|
|
|
) |
216
|
|
|
->will($this->returnValue($responseMock)); |
217
|
|
|
|
218
|
|
|
$logger = TestUtils::getTestingLogger(); |
219
|
|
|
|
220
|
|
|
$sut = new ImportCommand( |
221
|
|
|
$logger, |
222
|
|
|
$clientMock, |
223
|
|
|
new Finder(), |
224
|
|
|
new FrontMatter(), |
225
|
|
|
new Parser(), |
226
|
|
|
new VarCloner(), |
227
|
|
|
new Dumper() |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
$cmdTester = $this->getTester( |
231
|
|
|
$sut, |
232
|
|
|
__DIR__ . '/fixtures/set-01/test-4.json', |
233
|
|
|
[ |
234
|
|
|
'host' => 'http://example.com', |
235
|
|
|
'--rewrite-host' => 'http://localhost' |
236
|
|
|
] |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$display = TestUtils::getFullStringFromLog($logger->getHandlers()[0]); |
|
|
|
|
240
|
|
|
|
241
|
|
|
$this->assertContains('Wrote <http://example.com/core/module/test>; rel="self"', $display); |
242
|
|
|
$this->assertEquals(0, $cmdTester->getStatusCode()); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param ImportCommand $sut command under test |
247
|
|
|
* @param string $file file to load |
248
|
|
|
* @param array $args additional arguments |
249
|
|
|
* |
250
|
|
|
* @return CommandTester |
251
|
|
|
*/ |
252
|
|
|
private function getTester(ImportCommand $sut, $file, array $args = []) |
253
|
|
|
{ |
254
|
|
|
$app = new Application(); |
255
|
|
|
$app->add($sut); |
256
|
|
|
|
257
|
|
|
$cmd = $app->find('g:i'); |
258
|
|
|
$cmdTester = new CommandTester($cmd); |
259
|
|
|
$cmdTester->execute( |
260
|
|
|
array_merge( |
261
|
|
|
[ |
262
|
|
|
'command' => $cmd->getName(), |
263
|
|
|
'host' => 'http://localhost', |
264
|
|
|
'file' => [ |
265
|
|
|
$file |
266
|
|
|
], |
267
|
|
|
], |
268
|
|
|
$args |
269
|
|
|
), |
270
|
|
|
[ |
271
|
|
|
'decorated' => true, |
272
|
|
|
'verbosity' => OutputInterface::VERBOSITY_VERBOSE, |
273
|
|
|
] |
274
|
|
|
); |
275
|
|
|
return $cmdTester; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.