Skip to content

Commit 5247956

Browse files
committed
test: conform Azure adapter tests to phpbu's mock pattern
Replace the in-file Testable* subclass doubles with phpbu's standard PHPUnit mock pattern (createPartialMock / getMockBuilder->onlyMethods), matching the sibling AmazonS3v3 tests. The azure-oss client is final, so the mocks target the adapter's own protected seams rather than the SDK client; coverage of the orchestration is preserved. Assert the values passed to the SDK seams via with() matchers instead of capturing internal state: the upload path, the list prefix and the deleted blob path are now verified, and the File fixture uses a nested blob name so pathname and filename are distinguishable. Also align the adapters with the AmazonS3v3 sibling: $client is now protected (Sync and File), and File\AzureBlob::deleteBlob() takes the blob path as an argument. Signed-off-by: Sebastian Mendel <sebastian.mendel@netresearch.de>
1 parent f98f01d commit 5247956

5 files changed

Lines changed: 108 additions & 173 deletions

File tree

src/Backup/File/AzureBlob.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AzureBlob extends Remote
2424
*
2525
* @var BlobContainerClient
2626
*/
27-
private $client;
27+
protected $client;
2828

2929
/**
3030
* AzureBlob constructor.
@@ -49,17 +49,19 @@ public function __construct(BlobContainerClient $client, Blob $blob)
4949
public function unlink()
5050
{
5151
try {
52-
$this->deleteBlob();
52+
$this->deleteBlob($this->pathname);
5353
} catch (\Exception $exception) {
5454
throw new Exception($exception->getMessage());
5555
}
5656
}
5757

5858
/**
5959
* Delete the blob from the container.
60+
*
61+
* @param string $path
6062
*/
61-
protected function deleteBlob(): void
63+
protected function deleteBlob(string $path): void
6264
{
63-
$this->client->getBlobClient($this->pathname)->delete();
65+
$this->client->getBlobClient($path)->delete();
6466
}
6567
}

src/Backup/Sync/AzureBlob.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class AzureBlob implements Simulator
3030
*
3131
* @var BlobContainerClient
3232
*/
33-
private $client;
34-
33+
protected $client;
3534

3635
/**
3736
* Azure Blob Connection String

tests/phpbu/Backup/Collector/AzureBlobTest.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@
1010
use phpbu\App\Backup\Target;
1111
use PHPUnit\Framework\TestCase;
1212

13-
/**
14-
* Test double exposing a controllable blob listing seam so the real
15-
* AzureBlob collector logic runs without hitting the (final) Azure SDK client.
16-
*/
17-
class TestableAzureBlobCollector extends AzureBlob
18-
{
19-
/** @var \AzureOss\Storage\Blob\Models\Blob[] */
20-
public $blobs = [];
21-
22-
/** @var string|null */
23-
public $capturedPrefix = null;
24-
25-
protected function listBlobs(string $prefix): iterable
26-
{
27-
$this->capturedPrefix = $prefix;
28-
return $this->blobs;
29-
}
30-
}
31-
3213
/**
3314
* AzureBlob Collector test
3415
*
@@ -54,16 +35,23 @@ public function testCollector()
5435
$target = new Target($pathName, $filename, strtotime('2014-12-07 04:30:57'));
5536
$path = new Path($pathName, $time, false);
5637

57-
$collector = new TestableAzureBlobCollector($target, $path, $this->createContainerClient());
58-
$collector->blobs = [
38+
$blobs = [
5939
$this->createBlob('collector/static-dir/not-matching-2000-12-01-12_00.txt', '2000-12-01 12:00:00 +00:00'),
6040
$this->createBlob('collector/static-dir/foo-2000-12-01-12_00.txt', '2000-12-01 12:00:00 +00:00'),
6141
$this->createBlob($target->getPathname(), '2018-05-08 14:14:54 +00:00'),
6242
];
6343

44+
$collector = $this->getMockBuilder(AzureBlob::class)
45+
->setConstructorArgs([$target, $path, $this->createContainerClient()])
46+
->onlyMethods(['listBlobs'])
47+
->getMock();
48+
$collector->expects($this->once())
49+
->method('listBlobs')
50+
->with($this->equalTo('collector/static-dir/'))
51+
->willReturn($blobs);
52+
6453
$files = $collector->getBackupFiles();
6554

66-
$this->assertEquals('collector/static-dir/', $collector->capturedPrefix, 'blobs must be listed with the static path prefix');
6755
$this->assertCount(2, $files);
6856
$this->assertArrayHasKey('975672000-foo-2000-12-01-12_00.txt-0', $files);
6957
$this->assertEquals(
@@ -83,8 +71,13 @@ public function testNoBlobResult()
8371
$target = new Target($pathName, $filename, strtotime('2014-12-07 04:30:57'));
8472
$path = new Path('', $time, false);
8573

86-
$collector = new TestableAzureBlobCollector($target, $path, $this->createContainerClient());
87-
$collector->blobs = [];
74+
$collector = $this->getMockBuilder(AzureBlob::class)
75+
->setConstructorArgs([$target, $path, $this->createContainerClient()])
76+
->onlyMethods(['listBlobs'])
77+
->getMock();
78+
$collector->expects($this->once())
79+
->method('listBlobs')
80+
->willReturn([]);
8881

8982
$this->assertEquals([], $collector->getBackupFiles());
9083
}

tests/phpbu/Backup/File/AzureBlobTest.php

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,6 @@
88
use DateTimeImmutable;
99
use PHPUnit\Framework\TestCase;
1010

11-
/**
12-
* Test double exposing a controllable blob deletion seam so the real
13-
* AzureBlob logic runs without hitting the (final) Azure SDK client.
14-
*/
15-
class TestableAzureBlobFile extends AzureBlob
16-
{
17-
/** @var bool */
18-
public $deleted = false;
19-
20-
/** @var bool */
21-
public $deleteThrows = false;
22-
23-
/** @var string|null */
24-
public $deletedPathname = null;
25-
26-
protected function deleteBlob(): void
27-
{
28-
if ($this->deleteThrows) {
29-
throw new \Exception('delete failed');
30-
}
31-
$this->deleted = true;
32-
$this->deletedPathname = $this->pathname;
33-
}
34-
}
35-
3611
/**
3712
* AzureBlobTest
3813
*
@@ -52,17 +27,22 @@ class AzureBlobTest extends TestCase
5227
*/
5328
public function testCreateFileWithCorrectProperties()
5429
{
55-
$blob = $this->createBlob('dump.tar.gz', '2018-05-08 14:14:54.0 +00:00', 102102);
56-
$file = new TestableAzureBlobFile($this->createContainerClient(), $blob);
30+
$blob = $this->createBlob('collector/static-dir/dump.tar.gz', '2018-05-08 14:14:54.0 +00:00', 102102);
31+
32+
$file = $this->getMockBuilder(AzureBlob::class)
33+
->setConstructorArgs([$this->createContainerClient(), $blob])
34+
->onlyMethods(['deleteBlob'])
35+
->getMock();
36+
$file->expects($this->once())
37+
->method('deleteBlob')
38+
->with($this->equalTo('collector/static-dir/dump.tar.gz'));
5739

5840
$this->assertEquals('dump.tar.gz', $file->getFilename());
59-
$this->assertEquals('dump.tar.gz', $file->getPathname());
41+
$this->assertEquals('collector/static-dir/dump.tar.gz', $file->getPathname());
6042
$this->assertEquals(102102, $file->getSize());
6143
$this->assertEquals(1525788894, $file->getMTime());
6244

6345
$file->unlink();
64-
$this->assertTrue($file->deleted, 'blob should be deleted');
65-
$this->assertEquals('dump.tar.gz', $file->deletedPathname, 'the correct blob must be targeted for deletion');
6646
}
6747

6848
/**
@@ -72,9 +52,13 @@ public function testAzureBlobDeleteFailure()
7252
{
7353
$this->expectException('phpbu\App\Exception');
7454

75-
$blob = $this->createBlob('dump.tar.gz', '2018-05-08 14:14:54.0 +00:00', 102102);
76-
$file = new TestableAzureBlobFile($this->createContainerClient(), $blob);
77-
$file->deleteThrows = true;
55+
$blob = $this->createBlob('collector/static-dir/dump.tar.gz', '2018-05-08 14:14:54.0 +00:00', 102102);
56+
57+
$file = $this->getMockBuilder(AzureBlob::class)
58+
->setConstructorArgs([$this->createContainerClient(), $blob])
59+
->onlyMethods(['deleteBlob'])
60+
->getMock();
61+
$file->method('deleteBlob')->will($this->throwException(new \Exception()));
7862

7963
$file->unlink();
8064
}

0 commit comments

Comments
 (0)