1 | <?php namespace Tests; |
||
8 | class TaskTest extends PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * Mock History class and stub has() & put() methods |
||
13 | * @param mixed $has Value returned by has() method, null for no call to has() method. |
||
14 | * @param mixed $put Value returned by put() method, null for no call to put() method. |
||
15 | * @return object Mocked history |
||
16 | */ |
||
17 | private function mockHistory($has, $put) { |
||
18 | $methods = [ |
||
19 | 'has' => $has, |
||
20 | 'put' => $put, |
||
21 | ]; |
||
22 | |||
23 | $history = $this->getMock('Simondubois\UnsplashDownloader\History', array_keys($methods)); |
||
24 | |||
25 | foreach ($methods as $key => $value) { |
||
26 | $history->expects(is_null($value) ? $this->never() : $this->once()) |
||
27 | ->method($key) |
||
28 | ->willReturn($value); |
||
29 | } |
||
30 | |||
31 | return $history; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Mock Task class for downloadOnePhoto() method tests |
||
36 | * @param boolean $hasHistory Value returned by History::has() method, null for no call to History::has() method. |
||
37 | * @param null|bool $putHistory Value returned by History::put() method, null for no call to History::put() method. |
||
38 | * @param string $notificationStatus Status to pass to notify() method |
||
39 | * @param null|bool $copyFile Value returned by copyFile() method, null for no call to History::copyFile() method. |
||
40 | * @return object Mocked task |
||
41 | */ |
||
42 | private function mockTaskForDownloadOnePhoto($hasHistory, $putHistory, $notificationStatus, $copyFile) { |
||
43 | $task = $this->getMock( |
||
44 | 'Simondubois\UnsplashDownloader\Task', ['getHistoryInstance', 'notify', 'copyFile'], [], '', false |
||
45 | ); |
||
46 | |||
47 | $task->expects($this->once()) |
||
48 | ->method('getHistoryInstance') |
||
49 | ->willReturn($this->mockHistory($hasHistory, $putHistory)); |
||
50 | |||
51 | $task->expects($this->exactly(2))->method('notify')->withConsecutive( |
||
52 | [$this->stringContains('http://www.example.com'), $this->identicalTo(null)], |
||
53 | [$this->anything(), $this->identicalTo($notificationStatus)] |
||
54 | ); |
||
55 | |||
56 | $task->expects(is_null($copyFile) ? $this->never() : $this->once()) |
||
57 | ->method('copyFile') |
||
58 | ->with($this->identicalTo('http://www.example.com'), $this->identicalTo('destination/0123456789.jpg')) |
||
59 | ->willReturn($copyFile); |
||
60 | |||
61 | return $task; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Mock Unsplash class for getPhotos() tests |
||
66 | * @return object Mocked proxy |
||
67 | */ |
||
68 | public function mockUnsplashForGetPhotos() { |
||
69 | $unsplash = $this->getMock( |
||
70 | 'Simondubois\UnsplashDownloader\Unsplash', |
||
71 | ['allPhotos', 'photosInCategory', 'featuredPhotos'] |
||
72 | ); |
||
73 | |||
74 | return $unsplash; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Test Simondubois\UnsplashDownloader\Task::getNotificationCallback() |
||
79 | * & Simondubois\UnsplashDownloader\Task::setNotificationCallback() |
||
80 | */ |
||
81 | public function testNotificationCallback() { |
||
82 | // Instantiate task & custom value |
||
83 | $task = new Task(); |
||
84 | $notificationCallback = function($message, $level = null) { |
||
85 | return $level.':'.$message; |
||
86 | }; |
||
87 | |||
88 | // Assert default value |
||
89 | $this->assertTrue(is_callable($task->getNotificationCallback())); |
||
90 | |||
91 | // Assert custom value |
||
92 | $task->setNotificationCallback($notificationCallback); |
||
93 | $notificationCallback = $task->getNotificationCallback(); |
||
94 | $this->assertTrue(is_callable($notificationCallback)); |
||
95 | $this->assertEquals('level:message', call_user_func($notificationCallback, 'message', 'level')); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Test Simondubois\UnsplashDownloader\Task::getDestination() |
||
100 | * & Simondubois\UnsplashDownloader\Task::setDestination() |
||
101 | */ |
||
102 | public function testDestination() { |
||
103 | // Instantiate task & custom value |
||
104 | $task = new Task(); |
||
105 | $destination = 'destination'; |
||
106 | |||
107 | // Assert default value |
||
108 | $this->assertNull($task->getDestination()); |
||
109 | |||
110 | // Assert custom value |
||
111 | $task->setDestination($destination); |
||
112 | $this->assertEquals($destination, $task->getDestination()); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Test Simondubois\UnsplashDownloader\Task::getQuantity() |
||
117 | * & Simondubois\UnsplashDownloader\Task::setQuantity() |
||
118 | */ |
||
119 | public function testQuantity() { |
||
120 | // Instantiate task & custom value |
||
121 | $task = new Task(); |
||
122 | $quantity = 10; |
||
123 | |||
124 | // Assert default value |
||
125 | $this->assertNull($task->getQuantity()); |
||
126 | |||
127 | // Assert custom value |
||
128 | $task->setQuantity($quantity); |
||
129 | $this->assertEquals($quantity, $task->getQuantity()); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Test Simondubois\UnsplashDownloader\Task::getCategory() |
||
134 | * & Simondubois\UnsplashDownloader\Task::setCategory() |
||
135 | */ |
||
136 | public function testCategory() { |
||
137 | // Instantiate task & custom value |
||
138 | $task = new Task(); |
||
139 | $category = 1; |
||
140 | |||
141 | // Assert default value |
||
142 | $this->assertNull($task->getCategory()); |
||
143 | |||
144 | // Assert custom value |
||
145 | $task->setCategory($category); |
||
146 | $this->assertEquals($category, $task->getCategory()); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Test Simondubois\UnsplashDownloader\Task::getHistory() |
||
151 | * & Simondubois\UnsplashDownloader\Task::setHistory() |
||
152 | */ |
||
153 | public function testHistory() { |
||
154 | // Instantiate task & custom value |
||
155 | $task = new Task(); |
||
156 | $history = 'history'; |
||
157 | |||
158 | // Assert default value |
||
159 | $this->assertNull($task->getHistory()); |
||
160 | |||
161 | // Assert custom value |
||
162 | $task->setHistory($history); |
||
163 | $this->assertEquals($history, $task->getHistory()); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Test Simondubois\UnsplashDownloader\Task::getFeatured() |
||
168 | * & Simondubois\UnsplashDownloader\Task::setFeatured() |
||
169 | */ |
||
170 | public function testFeatured() { |
||
171 | // Instantiate task & custom value |
||
172 | $task = new Task(); |
||
173 | $featured = true; |
||
174 | |||
175 | // Assert default value |
||
176 | $this->assertNull($task->getFeatured()); |
||
177 | |||
178 | // Assert custom value |
||
179 | $task->setFeatured($featured); |
||
180 | $this->assertEquals($featured, $task->getFeatured()); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Test Simondubois\UnsplashDownloader\Task::notify() |
||
185 | */ |
||
186 | public function testNotify() { |
||
187 | // Instantiate task |
||
188 | $task = new Task(); |
||
189 | |||
190 | // Callback |
||
191 | $callback = $this->getMock('stdClass', ['callback']); |
||
192 | $callback->expects($this->once()) |
||
193 | ->method('callback') |
||
194 | ->with($this->identicalTo('message'), $this->identicalTo('level')); |
||
195 | |||
196 | // Assert |
||
197 | $task->setNotificationCallback([$callback, 'callback']); |
||
198 | $task->notify('message', 'level'); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Test Simondubois\UnsplashDownloader\Task::download() |
||
203 | */ |
||
204 | public function testDownloadError() { |
||
223 | |||
224 | /** |
||
225 | * Test Simondubois\UnsplashDownloader\Task::download() |
||
226 | */ |
||
227 | public function testDownloadSuccess() { |
||
246 | |||
247 | /** |
||
248 | * Test Simondubois\UnsplashDownloader\Task::categories() |
||
249 | */ |
||
250 | public function testCategories() { |
||
251 | $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['listCategories']); |
||
252 | $task->expects($this->once())->method('listCategories')->willReturn(true); |
||
253 | $this->assertTrue($task->categories()); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Test Simondubois\UnsplashDownloader\Task::getPhotos() |
||
258 | */ |
||
259 | public function testGetAllPhotos() { |
||
260 | $quantity = 10; |
||
261 | $photos = ['0123456789' => 'http://www.example.com']; |
||
262 | |||
263 | // Instantiate proxy |
||
264 | $unsplash = $this->mockUnsplashForGetPhotos(); |
||
265 | $unsplash->expects($this->once()) |
||
266 | ->method('allPhotos') |
||
267 | ->with($this->identicalTo($quantity)) |
||
268 | ->willReturn($photos); |
||
269 | $unsplash->expects($this->never())->method('photosInCategory'); |
||
270 | $unsplash->expects($this->never())->method('featuredPhotos'); |
||
271 | |||
272 | // Instantiate task |
||
273 | $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']); |
||
274 | $task->expects($this->exactly(2))->method('notify')->withConsecutive( |
||
275 | [$this->identicalTo('Get photo list from unsplash... '), $this->identicalTo(null)], |
||
276 | [$this->identicalTo('success.'.PHP_EOL), $this->identicalTo(Task::NOTIFY_INFO)] |
||
277 | ); |
||
278 | $task->setQuantity($quantity); |
||
279 | $this->assertEquals($photos, $task->getPhotos($unsplash)); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Test Simondubois\UnsplashDownloader\Task::getPhotos() |
||
284 | */ |
||
285 | public function testGetPhotosInCategory() { |
||
286 | $quantity = 10; |
||
287 | $photos = ['0123456789' => 'http://www.example.com']; |
||
288 | |||
289 | // Instantiate proxy |
||
290 | $unsplash = $this->mockUnsplashForGetPhotos(); |
||
291 | $unsplash->expects($this->never())->method('allPhotos'); |
||
292 | $unsplash->expects($this->once()) |
||
293 | ->method('photosInCategory') |
||
294 | ->with($this->identicalTo($quantity), $this->identicalTo(123)) |
||
295 | ->willReturn($photos); |
||
296 | $unsplash->expects($this->never())->method('featuredPhotos'); |
||
297 | |||
298 | // Instantiate task |
||
299 | $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']); |
||
300 | $task->expects($this->exactly(2))->method('notify')->withConsecutive( |
||
301 | [$this->identicalTo('Get photo list from unsplash... '), $this->identicalTo(null)], |
||
302 | [$this->identicalTo('success.'.PHP_EOL), $this->identicalTo(Task::NOTIFY_INFO)] |
||
303 | ); |
||
304 | $task->setQuantity($quantity); |
||
305 | $task->setCategory(123); |
||
306 | $this->assertEquals($photos, $task->getPhotos($unsplash)); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Test Simondubois\UnsplashDownloader\Task::getPhotos() |
||
311 | */ |
||
312 | public function testGetFeaturedPhotos() { |
||
313 | $quantity = 10; |
||
314 | $photos = ['0123456789' => 'http://www.example.com']; |
||
315 | |||
316 | // Instantiate proxy |
||
317 | $unsplash = $this->mockUnsplashForGetPhotos(); |
||
318 | $unsplash->expects($this->never())->method('allPhotos'); |
||
319 | $unsplash->expects($this->never())->method('photosInCategory'); |
||
320 | $unsplash->expects($this->once())->method('featuredPhotos') |
||
321 | ->with($this->identicalTo($quantity))->willReturn($photos); |
||
322 | |||
323 | // Instantiate task |
||
324 | $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']); |
||
325 | $task->expects($this->exactly(2))->method('notify')->withConsecutive( |
||
326 | [$this->identicalTo('Get photo list from unsplash... '), $this->identicalTo(null)], |
||
327 | [$this->identicalTo('success.'.PHP_EOL), $this->identicalTo(Task::NOTIFY_INFO)] |
||
328 | ); |
||
329 | $task->setQuantity($quantity); |
||
330 | $task->setFeatured(true); |
||
331 | $this->assertEquals($photos, $task->getPhotos($unsplash)); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Test Simondubois\UnsplashDownloader\Task::downloadAllPhotos() |
||
336 | */ |
||
337 | public function testFailedDownloadAllPhotos() { |
||
338 | // Prepare data |
||
339 | $quantity = 10; |
||
340 | $url = 'http://www.example.com'; |
||
341 | $photos = array_fill(0, $quantity, $url); |
||
342 | |||
343 | // Instantiate task |
||
344 | $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['downloadOnePhoto']); |
||
345 | $task->expects($this->exactly($quantity))->method('downloadOnePhoto')->withConsecutive( |
||
346 | [$this->identicalTo(0), $this->identicalTo($url)], [$this->identicalTo(1), $this->identicalTo($url)], |
||
347 | [$this->identicalTo(2), $this->identicalTo($url)], [$this->identicalTo(3), $this->identicalTo($url)], |
||
348 | [$this->identicalTo(4), $this->identicalTo($url)], [$this->identicalTo(5), $this->identicalTo($url)], |
||
349 | [$this->identicalTo(6), $this->identicalTo($url)], [$this->identicalTo(7), $this->identicalTo($url)], |
||
350 | [$this->identicalTo(8), $this->identicalTo($url)], [$this->identicalTo(9), $this->identicalTo($url)] |
||
351 | )->willReturn(false); |
||
352 | $task->setQuantity($quantity); |
||
353 | |||
354 | // Assert return value |
||
355 | $this->assertEquals(false, $task->downloadAllPhotos($photos)); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Test Simondubois\UnsplashDownloader\Task::downloadAllPhotos() |
||
360 | */ |
||
361 | public function testSuccessfulDownloadAllPhotos() { |
||
362 | // Prepare data |
||
363 | $quantity = 10; |
||
364 | $url = 'http://www.example.com'; |
||
365 | $photos = array_fill(0, $quantity, $url); |
||
366 | |||
367 | // Instantiate task |
||
368 | $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['downloadOnePhoto']); |
||
369 | $task->expects($this->exactly($quantity))->method('downloadOnePhoto')->withConsecutive( |
||
370 | [$this->identicalTo(0), $this->identicalTo($url)], [$this->identicalTo(1), $this->identicalTo($url)], |
||
371 | [$this->identicalTo(2), $this->identicalTo($url)], [$this->identicalTo(3), $this->identicalTo($url)], |
||
372 | [$this->identicalTo(4), $this->identicalTo($url)], [$this->identicalTo(5), $this->identicalTo($url)], |
||
373 | [$this->identicalTo(6), $this->identicalTo($url)], [$this->identicalTo(7), $this->identicalTo($url)], |
||
374 | [$this->identicalTo(8), $this->identicalTo($url)], [$this->identicalTo(9), $this->identicalTo($url)] |
||
375 | )->willReturn(true); |
||
376 | $task->setQuantity($quantity); |
||
377 | |||
378 | // Assert return value |
||
379 | $this->assertEquals(true, $task->downloadAllPhotos($photos)); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Test Simondubois\UnsplashDownloader\Task::downloadOnePhoto() |
||
384 | */ |
||
385 | public function testDownloadOnePhotoInHistory() { |
||
386 | // Initiate task |
||
387 | $task = $this->mockTaskForDownloadOnePhoto(true, null, Task::NOTIFY_COMMENT, null); |
||
388 | |||
389 | // Assert download photo in history |
||
390 | $task->__construct(); |
||
391 | $this->assertEquals(true, $task->downloadOnePhoto('0123456789', 'http://www.example.com')); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Test Simondubois\UnsplashDownloader\Task::downloadOnePhoto() |
||
396 | */ |
||
397 | public function testFailedDownloadOnePhoto() { |
||
398 | // Initiate task |
||
399 | $task = $this->mockTaskForDownloadOnePhoto(false, null, Task::NOTIFY_ERROR, false); |
||
400 | |||
401 | // Assert failed download |
||
402 | $task->__construct(); |
||
403 | $task->setDestination('destination'); |
||
404 | $this->assertEquals(false, $task->downloadOnePhoto('0123456789', 'http://www.example.com')); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Test Simondubois\UnsplashDownloader\Task::downloadOnePhoto() |
||
409 | */ |
||
410 | public function testSuccessfulDownloadOnePhoto() { |
||
419 | |||
420 | /** |
||
421 | * Test Simondubois\UnsplashDownloader\Task::listCategories() |
||
422 | */ |
||
423 | public function testListCategories() { |
||
424 | // Initiate custom values |
||
425 | $categories = [1 => 'First category', 2 => 'Second category']; |
||
426 | |||
427 | // Instantiate proxy |
||
428 | $unsplash = $this->getMock('Simondubois\UnsplashDownloader\Unsplash', ['allCategories']); |
||
429 | $unsplash->expects($this->once())->method('allCategories')->willReturn($categories); |
||
440 | } |
||
441 |