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->createMock('GuzzleHttp\Promise\Promise'); |
||
44 | |||
45 | $clientMock |
||
46 | ->method('requestAsync') |
||
47 | ->will($this->returnValue($promiseMock)); |
||
48 | |||
49 | $responseMock = $this->createMock('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() |
||
99 | |||
100 | /** |
||
101 | * @dataProvider errorFileProvider |
||
102 | * |
||
103 | * @param string $host import target host with protocol |
||
104 | * @param string $file file to import |
||
105 | * @param array $errors errors to check for (check valid case if none given) |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | public function testErrorFile($host, $file, $errors = []) |
||
110 | { |
||
111 | $clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
||
112 | |||
113 | $promiseMock = $this->createMock('GuzzleHttp\Promise\Promise'); |
||
114 | |||
115 | $clientMock |
||
116 | ->method('requestAsync') |
||
117 | ->will($this->returnValue($promiseMock)); |
||
118 | |||
119 | $responseMock = $this->createMock('Psr\Http\Message\ResponseInterface'); |
||
120 | |||
121 | $responseMock |
||
122 | ->method('getBody') |
||
123 | ->willReturn(json_encode((object) ["message" => "invalid"])); |
||
124 | |||
125 | $requestMock = $this->createMock('Psr\Http\Message\RequestInterface'); |
||
126 | $requestMock |
||
127 | ->method('getUri') |
||
128 | ->willReturn($host . '/core/app/test'); |
||
129 | |||
130 | $exceptionMock = $this->getMockBuilder('GuzzleHttp\Exception\RequestException') |
||
131 | ->setConstructorArgs(['Client error: 400', $requestMock, $responseMock]) |
||
132 | ->getMock(); |
||
133 | |||
134 | $exceptionMock |
||
135 | ->method('getRequest') |
||
136 | ->willReturn($requestMock); |
||
137 | |||
138 | $exceptionMock |
||
139 | ->method('getResponse') |
||
140 | ->willReturn($responseMock); |
||
141 | |||
142 | $promiseMock |
||
143 | ->method('then') |
||
144 | ->will( |
||
145 | $this->returnCallback( |
||
146 | function ($ok, $nok) use ($exceptionMock) { |
||
147 | return $nok($exceptionMock); |
||
148 | } |
||
149 | ) |
||
150 | ); |
||
151 | |||
152 | $sut = new ImportCommand( |
||
153 | $clientMock, |
||
154 | new Finder(), |
||
155 | new FrontMatter(), |
||
156 | new Parser(), |
||
157 | new VarCloner(), |
||
158 | new Dumper() |
||
159 | ); |
||
160 | |||
161 | $cmdTester = $this->getTester($sut, $file); |
||
162 | |||
163 | $this->assertContains('Loading data from ' . $file, $cmdTester->getDisplay()); |
||
164 | foreach ($errors as $error) { |
||
165 | $this->assertContains( |
||
166 | $error, |
||
167 | $cmdTester->getDisplay() |
||
168 | ); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return array[] |
||
174 | */ |
||
175 | public function errorFileProvider() |
||
196 | |||
197 | /** |
||
198 | * test rewriting of contents with --rewrite-host |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | public function testRewrite() |
||
203 | { |
||
204 | $clientMock = $this->getMockBuilder(self::CLIENT)->getMock(); |
||
205 | |||
206 | $promiseMock = $this->createMock('GuzzleHttp\Promise\Promise'); |
||
207 | |||
208 | $clientMock |
||
209 | ->method('requestAsync') |
||
210 | ->with( |
||
211 | $this->equalTo('PUT'), |
||
212 | $this->equalTo('http://example.com/core/module/test'), |
||
213 | $this->equalTo( |
||
214 | [ |
||
215 | 'json' => 'http://example.com/core/app/test', |
||
216 | 'upload' => false |
||
217 | ] |
||
218 | ) |
||
219 | ) |
||
220 | ->will($this->returnValue($promiseMock)); |
||
221 | |||
222 | $responseMock = $this->createMock('Psr\Http\Message\ResponseInterface'); |
||
223 | |||
224 | $responseMock |
||
225 | ->method('getHeader') |
||
226 | ->with('Link') |
||
227 | ->willReturn(['<http://example.com/core/module/test>; rel="self"']); |
||
228 | |||
229 | $promiseMock |
||
230 | ->method('then') |
||
231 | ->will( |
||
232 | $this->returnCallback( |
||
233 | function ($ok) use ($responseMock) { |
||
234 | $ok($responseMock); |
||
235 | } |
||
236 | ) |
||
237 | ); |
||
238 | |||
239 | $sut = new ImportCommand( |
||
240 | $clientMock, |
||
241 | new Finder(), |
||
242 | new FrontMatter(), |
||
243 | new Parser(), |
||
244 | new VarCloner(), |
||
245 | new Dumper() |
||
246 | ); |
||
247 | |||
248 | $cmdTester = $this->getTester( |
||
249 | $sut, |
||
250 | __DIR__ . '/fixtures/set-01/test-4.json', |
||
251 | [ |
||
252 | 'host' => 'http://example.com', |
||
253 | '--rewrite-host' => 'http://localhost' |
||
254 | ] |
||
255 | ); |
||
256 | $this->assertContains('Wrote <http://example.com/core/module/test>; rel="self"', $cmdTester->getDisplay()); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param ImportCommand $sut command under test |
||
261 | * @param string $file file to load |
||
262 | * @param array $args additional arguments |
||
263 | * |
||
264 | * @return CommandTester |
||
265 | */ |
||
266 | private function getTester(ImportCommand $sut, $file, array $args = []) |
||
291 | } |
||
292 |