Failed Conditions
Pull Request — master (#8)
by Laurens
02:20
created

FileTest::testIsZipFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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 Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException;
12
use Werkspot\BingAdsApiBundle\Api\Helper\File;
13
14
class FileTest extends PHPUnit_Framework_TestCase
15
{
16
    private $fileSystem;
17
18
    /**
19
     * FileTest constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->fileSystem = new Filesystem();
24
    }
25
26
    public function testGetFile()
27
    {
28
        $existingFile = ASSETS_DIR . 'report.csv';
29
        $onlineFile = 'http://google.com/test.txt';
30
        $file = ASSETS_DIR . 'example.txt';
31
32
        $mock = new MockHandler([new Response(200, [])]);
33
        $handler = HandlerStack::create($mock);
34
        $client = new Client(['handler' => $handler]);
35
        $fileHelper = new File($client);
36
37
        $result = $fileHelper->copyFile($existingFile);
38
        $this->assertEquals($existingFile, $result);
39
40
        $result = $fileHelper->copyFile($existingFile, $file);
41
        $this->assertEquals($file, $result);
42
        $this->fileSystem->remove($file);
43
44
        $result = $fileHelper->copyFile($onlineFile, $file);
45
        $this->assertEquals($file, $result);
46
47
        $this->fileSystem->remove($file);
48
49
    }
50
51
    /**
52
     * @expectedException \Werkspot\BingAdsApiBundle\Api\Exceptions\FileNotCopiedException
53
     */
54
    public function testGetNonExistingFile()
55
    {
56
        $mock = new MockHandler([new Response(200, [])]);
57
        $handler = HandlerStack::create($mock);
58
        $client = new Client(['handler' => $handler]);
59
        $fileHelper = new File($client);
60
61
        $nonExistingFile = ASSETS_DIR . 'iDoNotExist.txt';
62
        $result = $fileHelper->copyFile($nonExistingFile);
63
        $this->assertFalse($result);
64
    }
65
66
    public function testDownload()
67
    {
68
        $url = 'http://example.com';
69
        $file = ASSETS_DIR . 'example.txt';
70
71
        $clientMock = Mockery::mock(Client::class);
72
        $clientMock
73
            ->shouldReceive('request')
74
            ->with('GET', $url, ['sink' => $file])
75
            ->once()
76
            ->andReturn(new Response(200, [], 'test'));
77
78
        $fileHelper = new File($clientMock);
79
        $result = $fileHelper->download($url, $file);
80
81
        $this->assertEquals($file, $result);
82
    }
83
84
    /**
85
     * @expectedException \Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException
86
     */
87
    public function testDownloadThrowsException()
88
    {
89
        $url = 'http://example.com';
90
91
        $fileHelper = new File(new Client());
92
        $fileHelper->copyFile($url);
93
    }
94
95
    public function testIsZipFile()
96
    {
97
        $fileHelper = new File(new Client());
98
        $this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'example.txt'));
99
        $this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'corrupt.zip'));
100
        $this->assertTrue($fileHelper->isHealthyZipFile(ASSETS_DIR . 'report.zip'));
101
    }
102
103
    public function testUnZip()
104
    {
105
        $file = ASSETS_DIR . 'test.zip';
106
107
        $this->fileSystem->copy(ASSETS_DIR . 'report.zip', $file);
108
109
        $fileHelper = new File();
110
        $files = $fileHelper->unZip($file);
111
112
        $this->assertEquals($files, [ASSETS_DIR . '0039202.csv']);
113
114
        //-- Remove the files created by this test
115
        foreach ($files as $file) {
116
            $this->fileSystem->remove($file);
117
        }
118
    }
119
120
    /**
121
     * @expectedException \Exception
122
     */
123
    public function testCorruptUnZip()
124
    {
125
        $file = ASSETS_DIR . 'corrupt.zip';
126
        $fileHelper = new File();
127
        $fileHelper->unZip($file, null, false);
128
    }
129
}
130