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\Helper\File; |
12
|
|
|
|
13
|
|
|
class FileTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
private $fileSystem; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* FileTest constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->fileSystem = new Filesystem(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGetFile() |
26
|
|
|
{ |
27
|
|
|
$nonExistingFile = ASSETS_DIR . 'iDoNotExist.txt'; |
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($nonExistingFile); |
38
|
|
|
$this->assertFalse($result); |
39
|
|
|
|
40
|
|
|
$result = $fileHelper->copyFile($existingFile); |
41
|
|
|
$this->assertEquals($existingFile, $result); |
42
|
|
|
|
43
|
|
|
$result = $fileHelper->copyFile($existingFile, $file); |
44
|
|
|
$this->assertEquals($file, $result); |
45
|
|
|
$this->fileSystem->remove($file); |
46
|
|
|
|
47
|
|
|
$result = $fileHelper->copyFile($onlineFile, $file); |
48
|
|
|
$this->assertEquals($file, $result); |
49
|
|
|
|
50
|
|
|
$this->fileSystem->remove($file); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testDownload() |
54
|
|
|
{ |
55
|
|
|
$url = 'http://example.com'; |
56
|
|
|
$file = ASSETS_DIR . 'example.txt'; |
57
|
|
|
|
58
|
|
|
$clientMock = Mockery::mock(Client::class); |
59
|
|
|
$clientMock |
60
|
|
|
->shouldReceive('request') |
61
|
|
|
->with('GET', $url, ['sink' => $file]) |
62
|
|
|
->once() |
63
|
|
|
->andReturn(new Response(200, [], 'test')); |
64
|
|
|
|
65
|
|
|
$fileHelper = new File($clientMock); |
66
|
|
|
$result = $fileHelper->download($url, $file); |
67
|
|
|
|
68
|
|
|
$this->assertEquals($file, $result); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @expectedException \Exception |
73
|
|
|
*/ |
74
|
|
|
public function testDownloadThrowsException() |
75
|
|
|
{ |
76
|
|
|
$url = 'http://example.com'; |
77
|
|
|
|
78
|
|
|
$fileHelper = new File(new Client()); |
79
|
|
|
$fileHelper->copyFile($url); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testIsZipFile() |
83
|
|
|
{ |
84
|
|
|
$fileHelper = new File(new Client()); |
85
|
|
|
$this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'example.txt')); |
86
|
|
|
$this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'corrupt.zip')); |
87
|
|
|
$this->assertTrue($fileHelper->isHealthyZipFile(ASSETS_DIR . 'report.zip')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testUnZip() |
91
|
|
|
{ |
92
|
|
|
$file = ASSETS_DIR . 'test.zip'; |
93
|
|
|
|
94
|
|
|
$this->fileSystem->copy(ASSETS_DIR . 'report.zip', $file); |
95
|
|
|
|
96
|
|
|
$fileHelper = new File(); |
97
|
|
|
$files = $fileHelper->unZip($file); |
98
|
|
|
|
99
|
|
|
$this->assertEquals($files, [ASSETS_DIR . '0039202.csv']); |
100
|
|
|
|
101
|
|
|
//-- Remove the files created by this test |
102
|
|
|
foreach ($files as $file) { |
103
|
|
|
$this->fileSystem->remove($file); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @expectedException \Exception |
109
|
|
|
*/ |
110
|
|
|
public function testCorruptUnZip() |
111
|
|
|
{ |
112
|
|
|
$file = ASSETS_DIR . 'corrupt.zip'; |
113
|
|
|
$fileHelper = new File(); |
114
|
|
|
$fileHelper->unZip($file, null, false); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|