1 | <?php |
||
23 | class ImportCommandTest extends \PHPUnit_Framework_TestCase |
||
24 | { |
||
25 | /** |
||
26 | * String Http Client Class. |
||
27 | */ |
||
28 | const CLIENT = 'Graviton\ImportExport\Service\HttpClient'; |
||
29 | |||
30 | /** |
||
31 | * @dataProvider uploadFileProvider |
||
32 | * |
||
33 | * @param string $host import target host with protocol |
||
34 | * @param string $file file to import |
||
35 | * @param string $path resulting path from file |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | public function testUploadFile($host, $file, $path) |
||
40 | { |
||
41 | $clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
||
42 | |||
43 | $promiseMock = $this->getMock('GuzzleHttp\Promise\Promise'); |
||
44 | |||
45 | $clientMock |
||
46 | ->method('requestAsync') |
||
47 | ->will($this->returnValue($promiseMock)); |
||
48 | |||
49 | $responseMock = $this->getMock('Psr\Http\Message\ResponseInterface'); |
||
50 | |||
51 | $responseMock |
||
52 | ->method('getHeader') |
||
53 | ->with('Link') |
||
54 | ->willReturn(['<' . $host . $path . '>; rel="self"']); |
||
55 | |||
56 | $promiseMock |
||
57 | ->method('then') |
||
58 | ->will( |
||
59 | $this->returnCallback( |
||
60 | function ($ok) use ($responseMock) { |
||
61 | $ok($responseMock); |
||
62 | } |
||
63 | ) |
||
64 | ); |
||
65 | |||
66 | $sut = new ImportCommand( |
||
67 | $clientMock, |
||
68 | new Finder(), |
||
69 | new FrontMatter(), |
||
70 | new Parser(), |
||
71 | new VarCloner(), |
||
72 | new Dumper() |
||
73 | ); |
||
74 | |||
75 | $cmdTester = $this->getTester($sut, $file); |
||
76 | |||
77 | $this->assertContains('Loading data from ' . $file, $cmdTester->getDisplay()); |
||
78 | $this->assertContains('Wrote <' . $host . $path . '>; rel="self"', $cmdTester->getDisplay()); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return array[] |
||
83 | */ |
||
84 | public function uploadFileProvider() |
||
94 | |||
95 | /** |
||
96 | * @return array[] |
||
97 | */ |
||
98 | public function uploadImageFileProvider() |
||
108 | |||
109 | /** |
||
110 | * @dataProvider errorFileProvider |
||
111 | * |
||
112 | * @param string $host import target host with protocol |
||
113 | * @param string $file file to import |
||
114 | * @param array $errors errors to check for (check valid case if none given) |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function testErrorFile($host, $file, $errors = []) |
||
180 | |||
181 | /** |
||
182 | * @return array[] |
||
183 | */ |
||
184 | public function errorFileProvider() |
||
205 | |||
206 | /** |
||
207 | * test rewriting of contents with --rewrite-host |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | public function testRewrite() |
||
212 | { |
||
213 | $clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
||
214 | |||
215 | $promiseMock = $this->getMock('GuzzleHttp\Promise\Promise'); |
||
216 | |||
217 | $clientMock |
||
218 | ->method('requestAsync') |
||
219 | ->with( |
||
220 | $this->equalTo('PUT'), |
||
221 | $this->equalTo('http://example.com/core/module/test'), |
||
222 | $this->equalTo( |
||
223 | [ |
||
224 | 'json' => 'http://example.com/core/app/test', |
||
225 | 'origin' => __DIR__.'/fixtures/set-01/test-4.json' |
||
226 | ] |
||
227 | ) |
||
228 | ) |
||
229 | ->will($this->returnValue($promiseMock)); |
||
230 | |||
231 | $responseMock = $this->getMock('Psr\Http\Message\ResponseInterface'); |
||
232 | |||
233 | $responseMock |
||
234 | ->method('getHeader') |
||
235 | ->with('Link') |
||
236 | ->willReturn(['<http://example.com/core/module/test>; rel="self"']); |
||
237 | |||
238 | $promiseMock |
||
239 | ->method('then') |
||
240 | ->will( |
||
241 | $this->returnCallback( |
||
242 | function ($ok) use ($responseMock) { |
||
243 | $ok($responseMock); |
||
244 | } |
||
245 | ) |
||
246 | ); |
||
247 | |||
248 | $sut = new ImportCommand( |
||
249 | $clientMock, |
||
250 | new Finder(), |
||
251 | new FrontMatter(), |
||
252 | new Parser(), |
||
253 | new VarCloner(), |
||
254 | new Dumper() |
||
255 | ); |
||
256 | |||
257 | $cmdTester = $this->getTester( |
||
258 | $sut, |
||
259 | __DIR__ . '/fixtures/set-01/test-4.json', |
||
260 | [ |
||
261 | 'host' => 'http://example.com', |
||
262 | '--rewrite-host' => 'http://localhost' |
||
263 | ] |
||
264 | ); |
||
265 | $this->assertContains('Wrote <http://example.com/core/module/test>; rel="self"', $cmdTester->getDisplay()); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param ImportCommand $sut command under test |
||
270 | * @param string $file file to load |
||
271 | * @param array $args additional arguments |
||
272 | * |
||
273 | * @return CommandTester |
||
274 | */ |
||
275 | private function getTester(ImportCommand $sut, $file, array $args = []) |
||
300 | } |
||
301 |