Skip to content

Commit cd8b8d4

Browse files
Fix size diff check - issue #182
Some time ago the collectors where changed to return the current backup as well. That change was never reflected withing the size diff check. So the size diff check always checked the size difference against the current backup, which resulted in 0% change all the time.
1 parent 7f3bf04 commit cd8b8d4

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/Backup/Check/SizeDiffPreviousPercent.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,31 @@ class SizeDiffPreviousPercent implements Simulator
3434
*/
3535
public function pass(Target $target, $value, Local $collector, Result $result) : bool
3636
{
37+
$result->debug('checking size difference ' . $value . '%' . PHP_EOL);
38+
3739
// throws App\Exception if file doesn't exist
3840
$backupSize = $target->getSize();
3941
$history = $collector->getBackupFiles();
4042
$historyCount = count($history);
4143
$pass = true;
4244

43-
if ($historyCount > 0) {
44-
// oldest backups first
45-
ksort($history);
46-
$prevFile = array_pop($history);
47-
$prevSize = $prevFile->getSize();
48-
$diffPercent = Math::getDiffInPercent($backupSize, $prevSize);
45+
if ($historyCount > 1) {
46+
// latest backups first
47+
krsort($history);
48+
// grab the second backup in the history as it should be the previous one
49+
$previousBackup = $history[array_keys($history)[1]];
50+
$prevSize = $previousBackup->getSize();
51+
$diffPercent = Math::getDiffInPercent($backupSize, $prevSize);
4952

53+
$result->debug('size difference is ' . $diffPercent . '%' . PHP_EOL);
5054
$pass = $diffPercent < $value;
5155
}
5256

5357
return $pass;
5458
}
5559

5660
/**
57-
* Simulate the check execution.
61+
* Simulate the check execution
5862
*
5963
* @param \phpbu\App\Backup\Target $target
6064
* @param string $value

tests/phpbu/Backup/Check/SizeDiffPreviousPercentTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
namespace phpbu\App\Backup\Check;
33

4+
use phpbu\App\Backup\Collector\Local;
5+
use phpbu\App\Backup\Target;
6+
use phpbu\App\Result;
7+
48
/**
59
* SizeDiffPreviousPercentTest
610
*
@@ -19,19 +23,19 @@ class SizeDiffPreviousPercentTest extends \PHPUnit\Framework\TestCase
1923
*/
2024
public function testPass()
2125
{
22-
$resultStub = $this->createMock(\phpbu\App\Result::class);
23-
$collectorStub = $this->createMock(\phpbu\App\Backup\Collector\Local::class);
26+
$resultStub = $this->createMock(Result::class);
27+
$collectorStub = $this->createMock(Local::class);
2428
$collectorStub->expects($this->once())
2529
->method('getBackupFiles')
26-
->willReturn($this->getFileListMock([100, 500, 1000]));
27-
$targetStub = $this->createMock(\phpbu\App\Backup\Target::class);
30+
->willReturn($this->getFileListMock([100, 500, 1000, 1060]));
31+
$targetStub = $this->createMock(Target::class);
2832
$targetStub->method('getSize')->willReturn(1060);
2933

3034
$check = new SizeDiffPreviousPercent();
3135

3236
$this->assertTrue(
3337
$check->pass($targetStub, '10', $collectorStub, $resultStub),
34-
'size of stub should be about 900 - 1100'
38+
'size of 1060 should be in range of 900 - 1100'
3539
);
3640
}
3741

@@ -40,19 +44,19 @@ public function testPass()
4044
*/
4145
public function testFail()
4246
{
43-
$resultStub = $this->createMock(\phpbu\App\Result::class);
44-
$collectorStub = $this->createMock(\phpbu\App\Backup\Collector\Local::class);
47+
$resultStub = $this->createMock(Result::class);
48+
$collectorStub = $this->createMock(Local::class);
4549
$collectorStub->expects($this->once())
4650
->method('getBackupFiles')
47-
->willReturn($this->getFileListMock([100, 500, 1000]));
48-
$targetStub = $this->createMock(\phpbu\App\Backup\Target::class);
51+
->willReturn($this->getFileListMock([100, 500, 1000, 1060]));
52+
$targetStub = $this->createMock(Target::class);
4953
$targetStub->method('getSize')->willReturn(1060);
5054

5155
$check = new SizeDiffPreviousPercent();
5256

5357
$this->assertFalse(
5458
$check->pass($targetStub, '5', $collectorStub, $resultStub),
55-
'size of stub should be about 900 - 1100'
59+
'size of 1060 should not be in rage of 950 - 1050'
5660
);
5761
}
5862

@@ -61,9 +65,9 @@ public function testFail()
6165
*/
6266
public function testSimulate()
6367
{
64-
$collectorStub = $this->createMock(\phpbu\App\Backup\Collector\Local::class);
65-
$targetStub = $this->createMock(\phpbu\App\Backup\Target::class);
66-
$resultStub = $this->createMock(\phpbu\App\Result::class);
68+
$collectorStub = $this->createMock(Local::class);
69+
$targetStub = $this->createMock(Target::class);
70+
$resultStub = $this->createMock(Result::class);
6771
$resultStub->expects($this->once())->method('debug');
6872

6973
$check = new SizeDiffPreviousPercent();

0 commit comments

Comments
 (0)