Skip to content

Commit 2062357

Browse files
jbouzekrisebastianfeldmann
authored andcommitted
azure blob unit tests
1 parent 450d394 commit 2062357

File tree

5 files changed

+536
-3
lines changed

5 files changed

+536
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"vlucas/phpdotenv": "~2.4",
5757
"google/apiclient":"^2.0",
5858
"php-opencloud/openstack": "^3.0",
59-
"arhitector/yandex": "^2.0"
59+
"arhitector/yandex": "^2.0",
60+
"microsoft/azure-storage-blob": "~1.4"
6061
},
6162
"suggest": {
6263
"sebastianfeldmann/ftp": "Require ~0.9 to sync to an FTP server",

src/Backup/Sync/AzureBlob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function sync(Target $target, Result $result)
142142
*
143143
* @return \MicrosoftAzure\Storage\Blob\BlobRestProxy
144144
*/
145-
private function createClient() : BlobRestProxy
145+
protected function createClient() : BlobRestProxy
146146
{
147147
return BlobRestProxy::createBlobService($this->connectionString);
148148
}
@@ -230,7 +230,7 @@ private function upload(Target $target, BlobRestProxy $blobRestProxy)
230230
* @return resource
231231
* @throws \phpbu\App\Backup\Sync\Exception
232232
*/
233-
private function getFileHandle($path, $mode)
233+
protected function getFileHandle($path, $mode)
234234
{
235235
$handle = fopen($path, $mode);
236236
if (!is_resource($handle)) {
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
namespace phpbu\App\Backup\Collector;
3+
4+
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
5+
use MicrosoftAzure\Storage\Blob\Models\ListBlobsResult;
6+
use phpbu\App\Backup\Path;
7+
use phpbu\App\Backup\Target;
8+
use phpbu\App\Util;
9+
10+
/**
11+
* AzureBlob Collector test
12+
*
13+
* @package phpbu
14+
* @subpackage tests
15+
* @author Sebastian Feldmann <sebastian@phpbu.de>
16+
* @author Jonathan Bouzekri <jonathan.bouzekri@gmail.com>
17+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
18+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
19+
* @link http://www.phpbu.de/
20+
* @since Class available since Release 5.2.7
21+
*/
22+
class AzureBlobTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/**
25+
* Test Azure Blob collector
26+
*/
27+
public function testCollector()
28+
{
29+
$time = time();
30+
$path = 'collector/static-dir/';
31+
$filename = 'foo-%Y-%m-%d-%H_%i.txt';
32+
$target = new Target($path, $filename, strtotime('2014-12-07 04:30:57'));
33+
$path = new Path($path, $time, false);
34+
$azureBlob = $this->getMockBuilder(\MicrosoftAzure\Storage\Blob\BlobRestProxy::class)
35+
->disableOriginalConstructor()
36+
->setMethods(['listBlobs'])
37+
->getMock();
38+
39+
$azureBlobContents = ListBlobsResult::create(
40+
[
41+
"@attributes" => [
42+
"ServiceEndpoint" => "https://accountname.blob.core.windows.net/",
43+
"ContainerName" => "mycontainer"
44+
],
45+
"Prefix" => $path->getPath(),
46+
"MaxResults" => 10,
47+
"Blobs" => [
48+
"Blob" => [
49+
[
50+
"Name" => 'collector/static-dir/not-matching-2000-12-01-12_00.txt',
51+
"Properties" => [
52+
"Creation-Time" => "Fri, 01 Dec 2000 12:00:00 GMT",
53+
"Last-Modified" => "Fri, 01 Dec 2000 12:00:00 GMT",
54+
"Etag" => "0x8D702FF4A6EFED0",
55+
"Content-Length" => "100",
56+
"Content-Type" => "application/octet-stream",
57+
"Content-Encoding" => NULL,
58+
"Content-Language" => NULL,
59+
"Content-MD5" => "4zrnbmTxOxLksK3+1ulT8g==",
60+
"Cache-Control" => NULL,
61+
"Content-Disposition" => NULL,
62+
"BlobType" => "BlockBlob",
63+
"AccessTier" =>"Hot",
64+
"AccessTierInferred" => "true",
65+
"LeaseStatus" => "unlocked",
66+
"LeaseState" => "available",
67+
"ServerEncrypted" => "true"
68+
]
69+
],
70+
[
71+
"Name" => 'collector/static-dir/foo-2000-12-01-12_00.txt',
72+
"Properties" => [
73+
"Creation-Time" => "Fri, 01 Dec 2000 12:00:00 GMT",
74+
"Last-Modified" => "Fri, 01 Dec 2000 12:00:00 GMT",
75+
"Etag" => "0x8D702FF4A6EFED0",
76+
"Content-Length" => "100",
77+
"Content-Type" => "application/octet-stream",
78+
"Content-Encoding" => NULL,
79+
"Content-Language" => NULL,
80+
"Content-MD5" => "4zrnbmTxOxLksK3+1ulT8g==",
81+
"Cache-Control" => NULL,
82+
"Content-Disposition" => NULL,
83+
"BlobType" => "BlockBlob",
84+
"AccessTier" =>"Hot",
85+
"AccessTierInferred" => "true",
86+
"LeaseStatus" => "unlocked",
87+
"LeaseState" => "available",
88+
"ServerEncrypted" => "true"
89+
]
90+
],
91+
[
92+
"Name" => $target->getPathname(), // Current backup file
93+
"Properties" => [
94+
"Creation-Time" => "Fri, 08 May 2018 14:14:54 GMT",
95+
"Last-Modified" => "Fri, 08 May 2018 14:14:54 GMT",
96+
"Etag" => "0x8D702FF4A6EFED0",
97+
"Content-Length" => "100",
98+
"Content-Type" => "application/octet-stream",
99+
"Content-Encoding" => NULL,
100+
"Content-Language" => NULL,
101+
"Content-MD5" => "4zrnbmTxOxLksK3+1ulT8g==",
102+
"Cache-Control" => NULL,
103+
"Content-Disposition" => NULL,
104+
"BlobType" => "BlockBlob",
105+
"AccessTier" =>"Hot",
106+
"AccessTierInferred" => "true",
107+
"LeaseStatus" => "unlocked",
108+
"LeaseState" => "available",
109+
"ServerEncrypted" => "true"
110+
]
111+
]
112+
]
113+
],
114+
"NextMarker" => NULL
115+
]
116+
);
117+
118+
// Firstly mock listObjects without wrong or non existed contents key to
119+
// make sure it returns empty array of files
120+
$azureBlob->expects($this->once())
121+
->method('listBlobs')
122+
->with($this->equalTo('mycontainer'), $this->isInstanceOf(ListBlobsOptions::class))
123+
->willReturn($azureBlobContents);
124+
125+
$collector = new AzureBlob($target, $path, $azureBlob, 'mycontainer');
126+
$this->assertAttributeEquals($azureBlob, 'client', $collector);
127+
$this->assertAttributeEquals('mycontainer', 'containerName', $collector);
128+
$this->assertAttributeEquals($target, 'target', $collector);
129+
$this->assertAttributeEquals(null, 'files', $collector);
130+
$this->assertAttributeEquals(
131+
Util\Path::datePlaceholdersToRegex($target->getFilenameRaw()),
132+
'fileRegex',
133+
$collector
134+
);
135+
$files = $collector->getBackupFiles();
136+
137+
$this->assertCount(2, $files);
138+
$this->assertArrayHasKey('975672000-foo-2000-12-01-12_00.txt-0', $files);
139+
$this->assertEquals(
140+
'foo-2000-12-01-12_00.txt',
141+
$files['975672000-foo-2000-12-01-12_00.txt-0']->getFilename()
142+
);
143+
}
144+
145+
public function testNoBlobResult()
146+
{
147+
$time = time();
148+
$path = '/collector/static-dir/';
149+
$filename = 'foo-%Y-%m-%d-%H_%i.txt';
150+
$target = new Target($path, $filename, strtotime('2014-12-07 04:30:57'));
151+
$path = new Path('', $time, false);
152+
$azureBlob = $this->getMockBuilder(\MicrosoftAzure\Storage\Blob\BlobRestProxy::class)
153+
->disableOriginalConstructor()
154+
->setMethods(['listBlobs'])
155+
->getMock();
156+
157+
$azureBlobContents = ListBlobsResult::create(
158+
[
159+
"@attributes" => [
160+
"ServiceEndpoint" => "https://accountname.blob.core.windows.net/",
161+
"ContainerName" => "mycontainer"
162+
],
163+
"Prefix" => $path,
164+
"MaxResults" => 10,
165+
"Blobs" => [
166+
"Blob" => []
167+
],
168+
"NextMarker" => NULL
169+
]
170+
);
171+
172+
$azureBlob->expects($this->exactly(1))
173+
->method('listBlobs')
174+
->with($this->equalTo('mycontainer'), $this->isInstanceOf(ListBlobsOptions::class))
175+
->willReturn($azureBlobContents);
176+
177+
178+
$collector1 = new AzureBlob($target, $path, $azureBlob, 'mycontainer');
179+
$this->assertEquals([], $collector1->getBackupFiles());
180+
}
181+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
namespace phpbu\App\Backup\File;
3+
4+
use MicrosoftAzure\Storage\Blob\Models\Blob;
5+
use MicrosoftAzure\Storage\Blob\Models\BlobProperties;
6+
7+
/**
8+
* AzureBlobTest
9+
*
10+
* @package phpbu
11+
* @subpackage tests
12+
* @author Sebastian Feldmann <sebastian@phpbu.de>
13+
* @author Jonathan Bouzekri <jonathan.bouzekri@gmail.com>
14+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
15+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
16+
* @link http://www.phpbu.de/
17+
* @since Class available since Release 5.2.7
18+
*/
19+
class AzureBlobTest extends \PHPUnit\Framework\TestCase
20+
{
21+
/**
22+
* Test creating file and handle removing
23+
*/
24+
public function testCreateFileWithCorrectProperties()
25+
{
26+
$azureBlob = $this->getMockBuilder(\MicrosoftAzure\Storage\Blob\BlobRestProxy::class)
27+
->disableOriginalConstructor()
28+
->setMethods(['deleteBlob'])
29+
->getMock();
30+
31+
$azureBlob->expects($this->once())
32+
->method('deleteBlob')
33+
->with('mycontainer', 'dump.tar.gz');
34+
35+
$blobProps = new BlobProperties();
36+
$blobProps->setLastModified(new \DateTime('2018-05-08 14:14:54.0 +00:00'));
37+
$blobProps->setContentLength(102102);
38+
39+
$blob = new Blob();
40+
$blob->setProperties($blobProps);
41+
$blob->setName('dump.tar.gz');
42+
43+
$file = new AzureBlob($azureBlob, 'mycontainer', $blob);
44+
$this->assertEquals('dump.tar.gz', $file->getFilename());
45+
$this->assertEquals('dump.tar.gz', $file->getPathname());
46+
$this->assertEquals(102102, $file->getSize());
47+
$this->assertEquals(1525788894, $file->getMTime());
48+
$this->assertAttributeEquals('mycontainer', 'containerName', $file);
49+
50+
$file->unlink();
51+
$this->assertTrue(true, 'no exception should occur');
52+
}
53+
54+
/**
55+
* Tests AmazonS3V3::unlink
56+
*/
57+
public function testAzureBlobDeleteFailure()
58+
{
59+
$this->expectException('phpbu\App\Exception');
60+
$azureBlob = $this->getMockBuilder(\MicrosoftAzure\Storage\Blob\BlobRestProxy::class)
61+
->disableOriginalConstructor()
62+
->setMethods(['deleteBlob'])
63+
->getMock();
64+
65+
$azureBlob->expects($this->once())
66+
->method('deleteBlob')
67+
->with('mycontainer', 'dump.tar.gz')
68+
->will($this->throwException(new \Exception));
69+
70+
$blobProps = new BlobProperties();
71+
$blobProps->setLastModified(new \DateTime('2018-05-08 14:14:54.0 +00:00'));
72+
$blobProps->setContentLength(102102);
73+
74+
$blob = new Blob();
75+
$blob->setProperties($blobProps);
76+
$blob->setName('dump.tar.gz');
77+
78+
$file = new AzureBlob($azureBlob, 'mycontainer', $blob);
79+
$file->unlink();
80+
}
81+
}

0 commit comments

Comments
 (0)