1
|
|
|
<?php |
2
|
|
|
namespace Tests\Werkspot\BingAdsApiBundle\Api\Helper; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use GuzzleHttp\Handler\MockHandler; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use Mockery; |
9
|
|
|
use PHPUnit_Framework_TestCase; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
use Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException; |
13
|
|
|
use Werkspot\BingAdsApiBundle\Api\Helper\File; |
14
|
|
|
|
15
|
|
|
class FileTest extends PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testGetFile() |
18
|
|
|
{ |
19
|
|
|
$fileSystem = $this->getFileSystem(); |
20
|
|
|
$existingFile = ASSETS_DIR . 'report.csv'; |
21
|
|
|
$onlineFile = 'http://google.com/test.txt'; |
22
|
|
|
$file = ASSETS_DIR . 'example.txt'; |
23
|
|
|
|
24
|
|
|
$mock = new MockHandler([new Response(200, [])]); |
25
|
|
|
$handler = HandlerStack::create($mock); |
26
|
|
|
$client = new Client(['handler' => $handler]); |
27
|
|
|
$fileHelper = $this->getFileHelper($client); |
28
|
|
|
|
29
|
|
|
$result = $fileHelper->copyFile($existingFile); |
30
|
|
|
$this->assertEquals($existingFile, $result); |
31
|
|
|
|
32
|
|
|
$result = $fileHelper->copyFile($existingFile, $file); |
33
|
|
|
$this->assertEquals($file, $result); |
34
|
|
|
$fileSystem->remove($file); |
35
|
|
|
|
36
|
|
|
$result = $fileHelper->copyFile($onlineFile, $file); |
37
|
|
|
$this->assertEquals($file, $result); |
38
|
|
|
|
39
|
|
|
$fileSystem->remove($file); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @expectedException \Werkspot\BingAdsApiBundle\Api\Exceptions\FileNotCopiedException |
45
|
|
|
*/ |
46
|
|
|
public function testGetNonExistingFile() |
47
|
|
|
{ |
48
|
|
|
$mock = new MockHandler([new Response(200, [])]); |
49
|
|
|
$handler = HandlerStack::create($mock); |
50
|
|
|
$client = new Client(['handler' => $handler]); |
51
|
|
|
$fileHelper = $this->getFileHelper($client); |
52
|
|
|
|
53
|
|
|
$nonExistingFile = ASSETS_DIR . 'iDoNotExist.txt'; |
54
|
|
|
$result = $fileHelper->copyFile($nonExistingFile); |
55
|
|
|
$this->assertFalse($result); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testDownload() |
59
|
|
|
{ |
60
|
|
|
$url = 'http://example.com'; |
61
|
|
|
$file = ASSETS_DIR . 'example.txt'; |
62
|
|
|
|
63
|
|
|
$clientMock = Mockery::mock(Client::class); |
64
|
|
|
$clientMock |
65
|
|
|
->shouldReceive('request') |
66
|
|
|
->with('GET', $url, ['sink' => $file]) |
67
|
|
|
->once() |
68
|
|
|
->andReturn(new Response(200, [], 'test')); |
69
|
|
|
|
70
|
|
|
$fileHelper = $this->getFileHelper($clientMock); |
71
|
|
|
$result = $fileHelper->download($url, $file); |
72
|
|
|
|
73
|
|
|
$this->assertEquals($file, $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException |
78
|
|
|
*/ |
79
|
|
|
public function testDownloadThrowsException() |
80
|
|
|
{ |
81
|
|
|
$url = 'http://example.com'; |
82
|
|
|
|
83
|
|
|
$fileHelper = $this->getFileHelper(); |
84
|
|
|
$fileHelper->copyFile($url); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testIsZipFile() |
88
|
|
|
{ |
89
|
|
|
$fileHelper = $this->getFileHelper(); |
90
|
|
|
$this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'example.txt')); |
91
|
|
|
$this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'corrupt.zip')); |
92
|
|
|
$this->assertTrue($fileHelper->isHealthyZipFile(ASSETS_DIR . 'report.zip')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testUnZip() |
96
|
|
|
{ |
97
|
|
|
$fileSystem = $this->getFileSystem(); |
98
|
|
|
|
99
|
|
|
$file = ASSETS_DIR . 'test.zip'; |
100
|
|
|
|
101
|
|
|
$fileSystem->copy(ASSETS_DIR . 'report.zip', $file); |
102
|
|
|
|
103
|
|
|
$fileHelper = $this->getFileHelper(); |
104
|
|
|
$files = $fileHelper->unZip($file); |
105
|
|
|
|
106
|
|
|
$this->assertEquals($files, [ASSETS_DIR . '0039202.csv']); |
107
|
|
|
|
108
|
|
|
//-- Remove the files created by this test |
109
|
|
|
foreach ($files as $file) { |
110
|
|
|
$fileSystem->remove($file); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @expectedException \Exception |
116
|
|
|
*/ |
117
|
|
|
public function testCorruptUnZip() |
118
|
|
|
{ |
119
|
|
|
$file = ASSETS_DIR . 'corrupt.zip'; |
120
|
|
|
$fileHelper = $this->getFileHelper(); |
121
|
|
|
$fileHelper->unZip($file, null, false); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @dataProvider getTestClearCacheData |
126
|
|
|
* |
127
|
|
|
* @param string $file |
128
|
|
|
* @param int $removeTimes |
129
|
|
|
*/ |
130
|
|
|
public function testClearCache($file, $removeTimes) |
131
|
|
|
{ |
132
|
|
|
$fileSystemMock = Mockery::mock(Filesystem::class); |
133
|
|
|
$fileSystemMock |
134
|
|
|
->shouldReceive('remove') |
135
|
|
|
->times($removeTimes); |
136
|
|
|
|
137
|
|
|
$fileHelper = $this->getFileHelper(null, $fileSystemMock); |
138
|
|
|
$fileHelper->clearCache($file); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getTestClearCacheData() |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
'string - file' => [ |
145
|
|
|
'file' => '/tmp/someFile.txt', |
146
|
|
|
'removeTimes' => 1 |
147
|
|
|
], |
148
|
|
|
'array - files' => [ |
149
|
|
|
'file' => ['/tmp/someFile1.txt', '/tmp/someFile2.txt','/tmp/someFile3.txt',], |
150
|
|
|
'removeTimes' => 3 |
151
|
|
|
], |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testClearCacheDir() |
156
|
|
|
{ |
157
|
|
|
$path = '/tmp/'; |
158
|
|
|
$numberOfFiles = count((new Finder())->files()->in($path)); |
159
|
|
|
$fileSystemMock = Mockery::mock(Filesystem::class); |
160
|
|
|
$fileSystemMock |
161
|
|
|
->shouldReceive('remove') |
162
|
|
|
->times($numberOfFiles); |
163
|
|
|
|
164
|
|
|
$fileHelper = $this->getFileHelper(null, $fileSystemMock); |
165
|
|
|
$fileHelper->clearCache($path); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testMoveFirstFile() |
169
|
|
|
{ |
170
|
|
|
$file = '/tmp/newFile.txt'; |
171
|
|
|
$arrayFiles = ['/tmp/oldFile.txt']; |
172
|
|
|
$fileSystemMock = Mockery::mock(Filesystem::class); |
173
|
|
|
$fileSystemMock |
174
|
|
|
->shouldReceive('rename') |
175
|
|
|
->withArgs([$arrayFiles[0], $file]) |
176
|
|
|
->andReturn($file) |
177
|
|
|
->once(); |
178
|
|
|
|
179
|
|
|
$fileHelper = $this->getFileHelper(null, $fileSystemMock); |
180
|
|
|
$fileHelper->moveFirstFile($arrayFiles, $file); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testReadFileLinesIntoArray() |
184
|
|
|
{ |
185
|
|
|
$file = ASSETS_DIR . 'report.csv'; |
186
|
|
|
$expectedData = file($file); |
187
|
|
|
|
188
|
|
|
$fileHelper = $this->getFileHelper(); |
189
|
|
|
$this->assertEquals($expectedData, $fileHelper->readFileLinesIntoArray($file)); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testWriteLinesToFile() |
194
|
|
|
{ |
195
|
|
|
$file = ASSETS_DIR . 'writeTest.txt'; |
196
|
|
|
$data = [ |
197
|
|
|
0 => "first Line\n", |
198
|
|
|
1 => "second Line\n", |
199
|
|
|
2 => "third Line\n", |
200
|
|
|
3 => "fourth Line\n", |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
$fileHelper = $this->getFileHelper(); |
204
|
|
|
$fileHelper->writeLinesToFile($data, $file); |
205
|
|
|
$this->assertEquals($data, file($file)); |
206
|
|
|
|
207
|
|
|
$this->getFileSystem()->remove($file); |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param Client|null $client |
213
|
|
|
* @param Filesystem|null $filesystem |
214
|
|
|
* @param Finder|null $finder |
215
|
|
|
* |
216
|
|
|
* @return File |
217
|
|
|
*/ |
218
|
|
|
private function getFileHelper(Client $client = null, Filesystem $filesystem = null, Finder $finder = null ) |
219
|
|
|
{ |
220
|
|
|
$client = ($client == null) ? new Client() : $client; |
221
|
|
|
$filesystem = ($filesystem == null) ? new Filesystem() : $filesystem; |
222
|
|
|
$finder = ($finder == null) ? new Finder() : $finder; |
223
|
|
|
|
224
|
|
|
return new File($client, $filesystem, $finder); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
private function getFileSystem() |
228
|
|
|
{ |
229
|
|
|
return new Filesystem(); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|