|
| 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 | +} |
0 commit comments