Completed
Push — master ( fe0371...1946fd )
by Simon
04:01
created

TaskTest::testGetAllPhotos()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 16
nc 1
nop 0
1
<?php namespace Tests;
2
3
use Crew\Unsplash\ArrayObject;
4
use Exception;
5
use PHPUnit_Framework_TestCase;
6
use Simondubois\UnsplashDownloader\Task;
7
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 testDownload() {
205
        // Assert download error
206
        $history = $this->getMock('Simondubois\UnsplashDownloader\History');
207
        $task = $this->getMock(
208
            'Simondubois\UnsplashDownloader\Task',
209
            ['getHistoryInstance', 'getPhotos', 'downloadAllPhotos'],
210
            [],
211
            '',
212
            false
213
        );
214
        $task->expects($this->once())->method('getHistoryInstance')->willReturn($history);
215
        $photos = new ArrayObject([], []);
216
        $task->expects($this->once())->method('getPhotos')->willReturn($photos);
217
        $task->expects($this->once())
218
            ->method('downloadAllPhotos')
219
            ->with($this->identicalTo($photos))
220
            ->willReturn(false);
221
        $task->__construct();
222
        $task->download();
223
224
        // Assert success
225
        $history = $this->getMock('Simondubois\UnsplashDownloader\History');
226
        $task = $this->getMock(
227
            'Simondubois\UnsplashDownloader\Task',
228
            ['getHistoryInstance', 'getPhotos', 'downloadAllPhotos'],
229
            [],
230
            '',
231
            false
232
        );
233
        $task->expects($this->once())->method('getHistoryInstance')->willReturn($history);
234
        $task->expects($this->once())->method('getPhotos')->willReturn($photos);
235
        $task->expects($this->once())
236
            ->method('downloadAllPhotos')
237
            ->with($this->identicalTo($photos))
238
            ->willReturn(false);
239
        $task->__construct();
240
        $task->download();
241
    }
242
243
    /**
244
     * Test Simondubois\UnsplashDownloader\Task::categories()
245
     */
246
    public function testCategories() {
247
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['listCategories']);
248
        $task->expects($this->once())->method('listCategories')->willReturn(true);
249
        $this->assertTrue($task->categories());
250
    }
251
252
    /**
253
     * Test Simondubois\UnsplashDownloader\Task::getPhotos()
254
     */
255
    public function testGetAllPhotos() {
256
        $quantity = 10;
257
        $photos = ['0123456789' => 'http://www.example.com'];
258
259
        // Instantiate proxy
260
        $unsplash = $this->mockUnsplashForGetPhotos();
261
        $unsplash->expects($this->once())
262
            ->method('allPhotos')
263
            ->with($this->identicalTo($quantity))
264
            ->willReturn($photos);
265
        $unsplash->expects($this->never())->method('photosInCategory');
266
        $unsplash->expects($this->never())->method('featuredPhotos');
267
268
        // Instantiate task
269
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']);
270
        $task->expects($this->exactly(2))->method('notify')->withConsecutive(
271
            [$this->identicalTo('Get photo list from unsplash... '), $this->identicalTo(null)],
272
            [$this->identicalTo('success.'.PHP_EOL), $this->identicalTo(Task::NOTIFY_INFO)]
273
        );
274
        $task->setQuantity($quantity);
275
        $this->assertEquals($photos, $task->getPhotos($unsplash));
276
    }
277
278
    /**
279
     * Test Simondubois\UnsplashDownloader\Task::getPhotos()
280
     */
281
    public function testGetPhotosInCategory() {
282
        $quantity = 10;
283
        $photos = ['0123456789' => 'http://www.example.com'];
284
285
        // Instantiate proxy
286
        $unsplash = $this->mockUnsplashForGetPhotos();
287
        $unsplash->expects($this->never())->method('allPhotos');
288
        $unsplash->expects($this->once())
289
            ->method('photosInCategory')
290
            ->with($this->identicalTo($quantity), $this->identicalTo(123))
291
            ->willReturn($photos);
292
        $unsplash->expects($this->never())->method('featuredPhotos');
293
294
        // Instantiate task
295
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']);
296
        $task->expects($this->exactly(2))->method('notify')->withConsecutive(
297
            [$this->identicalTo('Get photo list from unsplash... '), $this->identicalTo(null)],
298
            [$this->identicalTo('success.'.PHP_EOL), $this->identicalTo(Task::NOTIFY_INFO)]
299
        );
300
        $task->setQuantity($quantity);
301
        $task->setCategory(123);
302
        $this->assertEquals($photos, $task->getPhotos($unsplash));
303
    }
304
305
    /**
306
     * Test Simondubois\UnsplashDownloader\Task::getPhotos()
307
     */
308
    public function testGetFeaturedPhotos() {
309
        $quantity = 10;
310
        $photos = ['0123456789' => 'http://www.example.com'];
311
312
        // Instantiate proxy
313
        $unsplash = $this->mockUnsplashForGetPhotos();
314
        $unsplash->expects($this->never())->method('allPhotos');
315
        $unsplash->expects($this->never())->method('photosInCategory');
316
        $unsplash->expects($this->once())->method('featuredPhotos')
317
            ->with($this->identicalTo($quantity))->willReturn($photos);
318
319
        // Instantiate task
320
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']);
321
        $task->expects($this->exactly(2))->method('notify')->withConsecutive(
322
            [$this->identicalTo('Get photo list from unsplash... '), $this->identicalTo(null)],
323
            [$this->identicalTo('success.'.PHP_EOL), $this->identicalTo(Task::NOTIFY_INFO)]
324
        );
325
        $task->setQuantity($quantity);
326
        $task->setFeatured(true);
327
        $this->assertEquals($photos, $task->getPhotos($unsplash));
328
    }
329
330
    /**
331
     * Test Simondubois\UnsplashDownloader\Task::downloadAllPhotos()
332
     */
333
    public function testFailedDownloadAllPhotos() {
334
        // Prepare data
335
        $quantity = 10;
336
        $url = 'http://www.example.com';
337
        $photos = array_fill(0, $quantity, $url);
338
339
        // Instantiate task
340
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['downloadOnePhoto']);
341
        $task->expects($this->exactly($quantity))->method('downloadOnePhoto')->withConsecutive(
342
            [$this->identicalTo(0), $this->identicalTo($url)], [$this->identicalTo(1), $this->identicalTo($url)],
343
            [$this->identicalTo(2), $this->identicalTo($url)], [$this->identicalTo(3), $this->identicalTo($url)],
344
            [$this->identicalTo(4), $this->identicalTo($url)], [$this->identicalTo(5), $this->identicalTo($url)],
345
            [$this->identicalTo(6), $this->identicalTo($url)], [$this->identicalTo(7), $this->identicalTo($url)],
346
            [$this->identicalTo(8), $this->identicalTo($url)], [$this->identicalTo(9), $this->identicalTo($url)]
347
        )->willReturn(false);
348
        $task->setQuantity($quantity);
349
350
        // Assert return value
351
        $this->assertEquals(false, $task->downloadAllPhotos($photos));
352
    }
353
354
    /**
355
     * Test Simondubois\UnsplashDownloader\Task::downloadAllPhotos()
356
     */
357
    public function testSuccessfulDownloadAllPhotos() {
358
        // Prepare data
359
        $quantity = 10;
360
        $url = 'http://www.example.com';
361
        $photos = array_fill(0, $quantity, $url);
362
363
        // Instantiate task
364
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['downloadOnePhoto']);
365
        $task->expects($this->exactly($quantity))->method('downloadOnePhoto')->withConsecutive(
366
                [$this->identicalTo(0), $this->identicalTo($url)], [$this->identicalTo(1), $this->identicalTo($url)],
367
                [$this->identicalTo(2), $this->identicalTo($url)], [$this->identicalTo(3), $this->identicalTo($url)],
368
                [$this->identicalTo(4), $this->identicalTo($url)], [$this->identicalTo(5), $this->identicalTo($url)],
369
                [$this->identicalTo(6), $this->identicalTo($url)], [$this->identicalTo(7), $this->identicalTo($url)],
370
                [$this->identicalTo(8), $this->identicalTo($url)], [$this->identicalTo(9), $this->identicalTo($url)]
371
            )->willReturn(true);
372
        $task->setQuantity($quantity);
373
374
        // Assert return value
375
        $this->assertEquals(true, $task->downloadAllPhotos($photos));
376
    }
377
378
    /**
379
     * Test Simondubois\UnsplashDownloader\Task::downloadOnePhoto()
380
     */
381
    public function testDownloadOnePhotoInHistory() {
382
        // Initiate task
383
        $task = $this->mockTaskForDownloadOnePhoto(true, null, Task::NOTIFY_COMMENT, null);
384
385
        // Assert download photo in history
386
        $task->__construct();
387
        $this->assertEquals(true, $task->downloadOnePhoto('0123456789', 'http://www.example.com'));
388
    }
389
390
    /**
391
     * Test Simondubois\UnsplashDownloader\Task::downloadOnePhoto()
392
     */
393
    public function testFailedDownloadOnePhoto() {
394
        // Initiate task
395
        $task = $this->mockTaskForDownloadOnePhoto(false, null, Task::NOTIFY_ERROR, false);
396
397
        // Assert failed download
398
        $task->__construct();
399
        $task->setDestination('destination');
400
        $this->assertEquals(false, $task->downloadOnePhoto('0123456789', 'http://www.example.com'));
401
    }
402
403
    /**
404
     * Test Simondubois\UnsplashDownloader\Task::downloadOnePhoto()
405
     */
406
    public function testSuccessfulDownloadOnePhoto() {
407
        // Initiate task
408
        $task = $this->mockTaskForDownloadOnePhoto(false, true, Task::NOTIFY_INFO, true);
409
410
        // Assert successful download
411
        $task->__construct();
412
        $task->setDestination('destination');
413
        $this->assertEquals(true, $task->downloadOnePhoto('0123456789', 'http://www.example.com'));
414
    }
415
416
    /**
417
     * Test Simondubois\UnsplashDownloader\Task::listCategories()
418
     */
419
    public function testListCategories() {
420
        // Initiate custom values
421
        $categories = [1 => 'First category', 2 => 'Second category'];
422
423
        // Instantiate proxy
424
        $unsplash = $this->getMock('Simondubois\UnsplashDownloader\Unsplash', ['allCategories']);
425
        $unsplash->expects($this->once())->method('allCategories')->willReturn($categories);
426
427
        // Instantiate task
428
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['notify']);
429
        $task->expects($this->exactly(3))->method('notify')->withConsecutive(
430
            [$this->identicalTo('Unsplash categories :'.PHP_EOL)],
431
            [$this->identicalTo("\t1 => First category".PHP_EOL)],
432
            [$this->identicalTo("\t2 => Second category".PHP_EOL)]
433
        );
434
        $task->listCategories($unsplash);
435
    }
436
}
437