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