1 | <?php |
||
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 | } |
||
228 |