Completed
Pull Request — master (#4)
by Laurens
02:47
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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B testGetFile() 0 27 1
A testDownload() 0 17 1
A testDownloadThrowsException() 0 7 1
A testUnZip() 0 16 2
A testCorruptUnZip() 0 6 1
1
<?php
2
3
namespace Tests\Werkspot\BingAdsApiBundle\Api\Helper;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Response;
9
use Mockery;
10
use PHPUnit_Framework_TestCase;
11
use Symfony\Component\Filesystem\Filesystem;
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
        $nonExistingFile = ASSETS_DIR . 'iDoNotExist.txt';
29
        $existingFile = ASSETS_DIR . 'report.csv';
30
        $onlineFile = 'http://google.com/test.txt';
31
        $file = ASSETS_DIR . 'example.txt';
32
33
        $mock = new MockHandler([new Response(200, [])]);
34
        $handler = HandlerStack::create($mock);
35
        $client = new Client(['handler' => $handler]);
36
        $fileHelper = new File($client);
37
38
        $result = $fileHelper->getFile($nonExistingFile);
39
        $this->assertFalse($result);
40
41
        $result = $fileHelper->getFile($existingFile);
42
        $this->assertEquals($existingFile, $result);
43
44
        $result = $fileHelper->getFile($existingFile, $file);
45
        $this->assertEquals($file, $result);
46
        $this->fileSystem->remove($file);
47
48
        $result = $fileHelper->getFile($onlineFile, $file);
49
        $this->assertEquals($file, $result);
50
51
        $this->fileSystem->remove($file);
52
    }
53
54
    public function testDownload()
55
    {
56
        $url = 'http://example.com';
57
        $file = ASSETS_DIR . 'example.txt';
58
59
        $clientMock = Mockery::mock(Client::class);
60
        $clientMock
61
            ->shouldReceive('request')
62
            ->with('GET', $url, ['sink' => $file])
63
            ->once()
64
            ->andReturn(new Response(200, [], 'test'));
65
66
        $fileHelper = new File($clientMock);
67
        $result = $fileHelper->download($url, $file);
68
69
        $this->assertEquals($file, $result);
70
    }
71
72
    /**
73
     * @expectedException \Exception
74
     */
75
    public function testDownloadThrowsException()
76
    {
77
        $url = 'http://example.com';
78
79
        $fileHelper = new File(new Client());
80
        $fileHelper->getFile($url);
81
    }
82
83
    public function testUnZip()
84
    {
85
        $file = ASSETS_DIR . 'test.zip';
86
87
        $this->fileSystem->copy(ASSETS_DIR . 'report.zip', $file);
88
89
        $fileHelper = new File();
90
        $files = $fileHelper->unZip($file);
91
92
        $this->assertEquals($files, [ASSETS_DIR . '0039202.csv']);
93
94
        //-- Remove the files created by this test
95
        foreach ($files as $file) {
96
            $this->fileSystem->remove($file);
97
        }
98
    }
99
100
    /**
101
     * @expectedException \Exception
102
     */
103
    public function testCorruptUnZip()
104
    {
105
        $file = ASSETS_DIR . 'corrupt.zip';
106
        $fileHelper = new File();
107
        $fileHelper->unZip($file, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a null|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
    }
109
}
110