1 | <?php |
||
13 | use Werkspot\BingAdsApiBundle\Api\Helper\File; |
||
14 | |||
15 | class FileTest extends PHPUnit_Framework_TestCase |
||
16 | { |
||
17 | public function testGetFile() |
||
18 | { |
||
19 | $fileSystem = $this->getFileSystem(); |
||
20 | $existingFile = ASSETS_DIR . 'report.csv'; |
||
21 | $onlineFile = 'http://google.com/test.txt'; |
||
22 | $file = ASSETS_DIR . 'example.txt'; |
||
23 | |||
24 | $mock = new MockHandler([new Response(200, [])]); |
||
25 | $handler = HandlerStack::create($mock); |
||
26 | $client = new Client(['handler' => $handler]); |
||
27 | $fileHelper = $this->getFileHelper($client); |
||
28 | |||
29 | $result = $fileHelper->copyFile($existingFile); |
||
30 | $this->assertEquals($existingFile, $result); |
||
31 | |||
32 | $result = $fileHelper->copyFile($existingFile, $file); |
||
33 | $this->assertEquals($file, $result); |
||
34 | $fileSystem->remove($file); |
||
35 | |||
36 | $result = $fileHelper->copyFile($onlineFile, $file); |
||
37 | $this->assertEquals($file, $result); |
||
38 | |||
39 | $fileSystem->remove($file); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @expectedException \Werkspot\BingAdsApiBundle\Api\Exceptions\FileNotCopiedException |
||
44 | */ |
||
45 | public function testGetNonExistingFile() |
||
46 | { |
||
47 | $mock = new MockHandler([new Response(200, [])]); |
||
48 | $handler = HandlerStack::create($mock); |
||
49 | $client = new Client(['handler' => $handler]); |
||
50 | $fileHelper = $this->getFileHelper($client); |
||
51 | |||
52 | $nonExistingFile = ASSETS_DIR . 'iDoNotExist.txt'; |
||
53 | $result = $fileHelper->copyFile($nonExistingFile); |
||
54 | $this->assertFalse($result); |
||
55 | } |
||
56 | |||
57 | public function testDownload() |
||
58 | { |
||
59 | $url = 'http://example.com'; |
||
60 | $file = ASSETS_DIR . 'example.txt'; |
||
61 | |||
62 | $clientMock = Mockery::mock(Client::class); |
||
63 | $clientMock |
||
64 | ->shouldReceive('request') |
||
65 | ->with('GET', $url, ['sink' => $file]) |
||
66 | ->once() |
||
67 | ->andReturn(new Response(200, [], 'test')); |
||
68 | |||
69 | $fileHelper = $this->getFileHelper($clientMock); |
||
70 | $result = $fileHelper->download($url, $file); |
||
71 | |||
72 | $this->assertEquals($file, $result); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @expectedException \Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException |
||
77 | */ |
||
78 | public function testDownloadThrowsException() |
||
79 | { |
||
80 | $url = 'http://example.com'; |
||
81 | |||
82 | $fileHelper = $this->getFileHelper(); |
||
83 | $fileHelper->copyFile($url); |
||
84 | } |
||
85 | |||
86 | public function testIsZipFile() |
||
87 | { |
||
88 | $fileHelper = $this->getFileHelper(); |
||
89 | $this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'example.txt')); |
||
90 | $this->assertFalse($fileHelper->isHealthyZipFile(ASSETS_DIR . 'corrupt.zip')); |
||
91 | $this->assertTrue($fileHelper->isHealthyZipFile(ASSETS_DIR . 'report.zip')); |
||
92 | } |
||
93 | |||
94 | public function testUnZip() |
||
95 | { |
||
96 | $fileSystem = $this->getFileSystem(); |
||
97 | |||
98 | $file = ASSETS_DIR . 'test.zip'; |
||
99 | |||
100 | $fileSystem->copy(ASSETS_DIR . 'report.zip', $file); |
||
101 | |||
102 | $fileHelper = $this->getFileHelper(); |
||
103 | $files = $fileHelper->unZip($file); |
||
104 | |||
105 | $this->assertEquals($files, [ASSETS_DIR . '0039202.csv']); |
||
106 | |||
107 | //-- Remove the files created by this test |
||
108 | foreach ($files as $file) { |
||
109 | $fileSystem->remove($file); |
||
229 |