|
1
|
|
|
<?php namespace Tests; |
|
2
|
|
|
|
|
3
|
|
|
use Exception; |
|
4
|
|
|
use PHPUnit_Framework_TestCase; |
|
5
|
|
|
use Simondubois\UnsplashDownloader\History; |
|
6
|
|
|
use org\bovigo\vfs\vfsStream; |
|
7
|
|
|
|
|
8
|
|
|
class HistoryTest extends PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Test Simondubois\UnsplashDownloader\Task::getPath() |
|
13
|
|
|
*/ |
|
14
|
|
|
public function testGetPath() { |
|
15
|
|
|
// Instantiate history |
|
16
|
|
|
$history = new History(); |
|
17
|
|
|
|
|
18
|
|
|
// Assert default value |
|
19
|
|
|
$this->assertNull($history->getPath()); |
|
20
|
|
|
|
|
21
|
|
|
// Assert custom value |
|
22
|
|
|
$path = 'history'; |
|
23
|
|
|
$history->load($path); |
|
24
|
|
|
$this->assertEquals($path, $history->getPath()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Test Simondubois\UnsplashDownloader\Task::save() |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testSaveBeforeLoad() { |
|
31
|
|
|
$history = new History(); |
|
32
|
|
|
$this->assertFalse($history->save()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Test Simondubois\UnsplashDownloader\Task::load() |
|
37
|
|
|
* & Simondubois\UnsplashDownloader\Task::loadContent() |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testLoadCalledTwice() { |
|
40
|
|
|
// Instantiate file system |
|
41
|
|
|
$root = vfsStream::setup('test')->url(); |
|
42
|
|
|
$existingFile = $root.'/existingFile'; |
|
43
|
|
|
$content = ['abc', 'def', '', 'ghi']; |
|
44
|
|
|
file_put_contents($existingFile, implode(PHP_EOL, $content)); |
|
45
|
|
|
|
|
46
|
|
|
// Assert first load |
|
47
|
|
|
$history = new History(); |
|
48
|
|
|
$this->assertTrue($history->load($existingFile)); |
|
49
|
|
|
|
|
50
|
|
|
// Assert second load |
|
51
|
|
|
try { |
|
52
|
|
|
$history->load($existingFile); |
|
53
|
|
|
} catch (Exception $exception) { |
|
54
|
|
|
$this->assertEquals($content, $history->getContent()); |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
$this->fail('An expected exception has not been raised.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Test Simondubois\UnsplashDownloader\Task::load() |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testLoadMissingFile() { |
|
64
|
|
|
// Instantiate file system |
|
65
|
|
|
$root = vfsStream::setup('test')->url(); |
|
66
|
|
|
$missingFile = $root.'/missingFile'; |
|
67
|
|
|
|
|
68
|
|
|
// Assert missing file |
|
69
|
|
|
$history = new History(); |
|
70
|
|
|
$this->assertFalse($history->load($missingFile)); |
|
71
|
|
|
$this->assertEmpty($history->getContent()); |
|
72
|
|
|
$this->assertFalse($history->save()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Test Simondubois\UnsplashDownloader\Task::load() |
|
77
|
|
|
* & Simondubois\UnsplashDownloader\Task::loadContent() |
|
78
|
|
|
* & Simondubois\UnsplashDownloader\Task::has() |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testSuccessfulLoad() { |
|
81
|
|
|
// Instantiate file system |
|
82
|
|
|
$root = vfsStream::setup('test')->url(); |
|
83
|
|
|
$existingFile = $root.'/existingFile'; |
|
84
|
|
|
$content = ['abc', 'def', '', 'ghi']; |
|
85
|
|
|
file_put_contents($existingFile, implode(PHP_EOL, $content)); |
|
86
|
|
|
|
|
87
|
|
|
// Assert successful load |
|
88
|
|
|
$history = new History(); |
|
89
|
|
|
$this->assertTrue($history->load($existingFile)); |
|
90
|
|
|
$this->assertEquals($content, $history->getContent()); |
|
91
|
|
|
$this->assertTrue($history->has('abc')); |
|
92
|
|
|
$this->assertTrue($history->has('def')); |
|
93
|
|
|
$this->assertTrue($history->has('')); |
|
94
|
|
|
$this->assertTrue($history->has('ghi')); |
|
95
|
|
|
$this->assertFalse($history->has('jkl')); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Test Simondubois\UnsplashDownloader\Task::load() |
|
100
|
|
|
* & Simondubois\UnsplashDownloader\Task::loadContent() |
|
101
|
|
|
* & Simondubois\UnsplashDownloader\Task::getContent() |
|
102
|
|
|
* & Simondubois\UnsplashDownloader\Task::has() |
|
103
|
|
|
* & Simondubois\UnsplashDownloader\Task::put() |
|
104
|
|
|
* & Simondubois\UnsplashDownloader\Task::save() |
|
105
|
|
|
* & Simondubois\UnsplashDownloader\Task::saveContent() |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testSuccessfulSave() { |
|
108
|
|
|
// Instantiate file system |
|
109
|
|
|
$root = vfsStream::setup('test')->url(); |
|
110
|
|
|
$existingFile = $root.'/existingFile'; |
|
111
|
|
|
$content = ['abc', 'def', '', 'ghi']; |
|
112
|
|
|
file_put_contents($existingFile, implode(PHP_EOL, $content)); |
|
113
|
|
|
|
|
114
|
|
|
// Instantiate history |
|
115
|
|
|
$history = new History(); |
|
116
|
|
|
$history->load($existingFile); |
|
117
|
|
|
|
|
118
|
|
|
// Assert put & has |
|
119
|
|
|
$history->put('jkl'); |
|
120
|
|
|
$content[] = 'jkl'; |
|
121
|
|
|
$this->assertTrue($history->has('jkl')); |
|
122
|
|
|
|
|
123
|
|
|
// Assert save |
|
124
|
|
|
$this->assertTrue($history->save()); |
|
125
|
|
|
$this->assertEquals($content, $history->getContent()); |
|
126
|
|
|
$this->assertEquals(implode(PHP_EOL, $content), file_get_contents($existingFile)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|