Completed
Pull Request — master (#8)
by Laurens
02:16
created

FileTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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