1
|
|
|
<?php namespace Tests; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use org\bovigo\vfs\vfsStream; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use Simondubois\UnsplashDownloader\Validate; |
8
|
|
|
|
9
|
|
|
class ValidateTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::quantity() |
13
|
|
|
*/ |
14
|
|
|
public function testNotNumericQuantity() { |
15
|
|
|
$validate = new Validate(); |
16
|
|
|
|
17
|
|
|
$exceptionCode = null; |
18
|
|
|
try { |
19
|
|
|
$validate->quantity('abc'); |
20
|
|
|
} catch (InvalidArgumentException $exception) { |
21
|
|
|
$exceptionCode = $exception->getCode(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->assertEquals(Validate::ERROR_QUANTITY_NOTNUMERIC, $exceptionCode); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::quantity() |
30
|
|
|
*/ |
31
|
|
|
public function testNotPositiveQuantity() { |
32
|
|
|
$validate = new Validate(); |
33
|
|
|
|
34
|
|
|
$exceptionCode = null; |
35
|
|
|
try { |
36
|
|
|
$validate->quantity('-1'); |
37
|
|
|
} catch (InvalidArgumentException $exception) { |
38
|
|
|
$exceptionCode = $exception->getCode(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->assertEquals(Validate::ERROR_QUANTITY_NOTPOSITIVE, $exceptionCode); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::quantity() |
47
|
|
|
*/ |
48
|
|
|
public function testTooHighQuantity() { |
49
|
|
|
$validate = new Validate(); |
50
|
|
|
|
51
|
|
|
$exceptionCode = null; |
52
|
|
|
try { |
53
|
|
|
$validate->quantity('101'); |
54
|
|
|
} catch (InvalidArgumentException $exception) { |
55
|
|
|
$exceptionCode = $exception->getCode(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->assertEquals(Validate::ERROR_QUANTITY_TOOHIGH, $exceptionCode); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::quantity() |
64
|
|
|
*/ |
65
|
|
|
public function testValidQuantity() { |
66
|
|
|
$validate = new Validate(); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(1, $validate->quantity('1')); |
69
|
|
|
$this->assertEquals(10, $validate->quantity('10')); |
70
|
|
|
$this->assertEquals(100, $validate->quantity('100')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::destination() |
75
|
|
|
*/ |
76
|
|
|
public function testNotDirectoryDestination() { |
77
|
|
|
$validate = new Validate(); |
78
|
|
|
|
79
|
|
|
$root = vfsStream::setup('test')->url(); |
80
|
|
|
$existingFile = $root.'/existingFile'; |
81
|
|
|
touch($existingFile); |
82
|
|
|
|
83
|
|
|
$exceptionCode = null; |
84
|
|
|
try { |
85
|
|
|
$validate->destination($existingFile); |
86
|
|
|
} catch (InvalidArgumentException $exception) { |
87
|
|
|
$exceptionCode = $exception->getCode(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->assertEquals(Validate::ERROR_DESTINATION_NOTDIR, $exceptionCode); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::destination() |
95
|
|
|
*/ |
96
|
|
|
public function testMissingFolderDestination() { |
97
|
|
|
$validate = new Validate(); |
98
|
|
|
|
99
|
|
|
$root = vfsStream::setup('test')->url(); |
100
|
|
|
$missingFolder = $root.'/missingFolder'; |
101
|
|
|
|
102
|
|
|
$exceptionCode = null; |
103
|
|
|
try { |
104
|
|
|
$validate->destination($missingFolder); |
105
|
|
|
} catch (InvalidArgumentException $exception) { |
106
|
|
|
$exceptionCode = $exception->getCode(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->assertEquals(Validate::ERROR_DESTINATION_NOTDIR, $exceptionCode); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::destination() |
114
|
|
|
*/ |
115
|
|
|
public function testUnwritableDestination() { |
116
|
|
|
$validate = new Validate(); |
117
|
|
|
|
118
|
|
|
$root = vfsStream::setup('test')->url(); |
119
|
|
|
$unwritableFolder = $root.'/unwritableFolder'; |
120
|
|
|
mkdir($unwritableFolder, 0000); |
121
|
|
|
|
122
|
|
|
$exceptionCode = null; |
123
|
|
|
try { |
124
|
|
|
$validate->destination($unwritableFolder); |
125
|
|
|
} catch (InvalidArgumentException $exception) { |
126
|
|
|
$exceptionCode = $exception->getCode(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->assertEquals(Validate::ERROR_DESTINATION_UNWRITABLE, $exceptionCode); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::destination() |
134
|
|
|
*/ |
135
|
|
|
public function testSuccessfulDestination() { |
136
|
|
|
$validate = new Validate(); |
137
|
|
|
|
138
|
|
|
$root = vfsStream::setup('test')->url(); |
139
|
|
|
$existingFolder = $root.'/existingFolder'; |
140
|
|
|
mkdir($existingFolder); |
141
|
|
|
|
142
|
|
|
$this->assertEquals($existingFolder, $validate->destination($existingFolder)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::history() |
147
|
|
|
*/ |
148
|
|
|
public function testNotFileHistory() { |
149
|
|
|
$validate = new Validate(); |
150
|
|
|
|
151
|
|
|
$root = vfsStream::setup('test')->url(); |
152
|
|
|
$existingFolder = $root.'/existingFolder'; |
153
|
|
|
mkdir($existingFolder); |
154
|
|
|
|
155
|
|
|
$exceptionCode = null; |
156
|
|
|
try { |
157
|
|
|
$validate->history($existingFolder); |
158
|
|
|
} catch (InvalidArgumentException $exception) { |
159
|
|
|
$exceptionCode = $exception->getCode(); |
160
|
|
|
} |
161
|
|
|
$this->assertEquals(Validate::ERROR_HISTORY_NOTFILE, $exceptionCode); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::history() |
166
|
|
|
*/ |
167
|
|
|
public function testUnwritableHistory() { |
168
|
|
|
$validate = new Validate(); |
169
|
|
|
|
170
|
|
|
$root = vfsStream::setup('test')->url(); |
171
|
|
|
$unwritableFolder = $root.'/unwritableFolder'; |
172
|
|
|
mkdir($unwritableFolder, 0000); |
173
|
|
|
$unwritableFile = $unwritableFolder.'/unwritableFile'; |
174
|
|
|
|
175
|
|
|
$exceptionCode = null; |
176
|
|
|
try { |
177
|
|
|
$validate->history($unwritableFile); |
178
|
|
|
} catch (InvalidArgumentException $exception) { |
179
|
|
|
$exceptionCode = $exception->getCode(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->assertEquals(Validate::ERROR_HISTORY_NOTRW, $exceptionCode); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::history() |
187
|
|
|
*/ |
188
|
|
|
public function testValidHistory() { |
189
|
|
|
$validate = new Validate(); |
190
|
|
|
|
191
|
|
|
$root = vfsStream::setup('test')->url(); |
192
|
|
|
$existingFile = $root.'/existingFile'; |
193
|
|
|
$missingFile = $root.'/missingFile'; |
194
|
|
|
touch($existingFile); |
195
|
|
|
|
196
|
|
|
$this->assertNull($validate->history(null)); |
197
|
|
|
$this->assertEquals($existingFile, $validate->history($existingFile)); |
198
|
|
|
$this->assertEquals($missingFile, $validate->history($missingFile)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Test Simondubois\UnsplashDownloader\Validate::category() |
203
|
|
|
*/ |
204
|
|
|
public function testValidCategory() { |
205
|
|
|
$validate = new Validate(); |
206
|
|
|
|
207
|
|
|
$this->assertNull($validate->category(null)); |
208
|
|
|
$this->assertEquals(1, $validate->category('1')); |
209
|
|
|
$this->assertEquals(0, $validate->category('abc')); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|