Skip to content

Commit 9c94dac

Browse files
committed
Count current backup in quantity cleaning.
The Quantity cleaner now counts the current backup so that the number of backup files does not exceed the quantity configured.
1 parent dfcc380 commit 9c94dac

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/Backup/Cleaner/Quantity.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,11 @@ public function cleanup(Target $target, Collector $collector, Result $result)
6262
{
6363
$files = $collector->getBackupFiles();
6464

65-
// backups exceed capacity?
66-
if (count($files) > $this->amount) {
65+
if ($this->isCapacityExceeded($files)) {
6766
// oldest backups first
6867
ksort($files);
6968

70-
// add one for current backup
71-
while (count($files) + 1 > $this->amount) {
69+
while ($this->isCapacityExceeded($files)) {
7270
$file = array_shift($files);
7371
$result->debug(sprintf('delete %s', $file->getPathname()));
7472
if (!$file->isWritable()) {
@@ -79,4 +77,18 @@ public function cleanup(Target $target, Collector $collector, Result $result)
7977
}
8078
}
8179
}
80+
81+
/**
82+
* Returns true when the capacity is exceeded.
83+
*
84+
* @return boolean
85+
*/
86+
private function isCapacityExceeded(array $files)
87+
{
88+
$totalFiles = count($files);
89+
$totalFilesPlusCurrentBackup = $totalFiles + 1;
90+
91+
return $totalFiles > 0
92+
&& $totalFilesPlusCurrentBackup > $this->amount;
93+
}
8294
}

tests/phpbu/Backup/Cleaner/QuantityTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,31 @@ public function testCleanupDeleteNoFile()
139139

140140
$cleaner->cleanup($targetStub, $collectorStub, $resultStub);
141141
}
142+
143+
/**
144+
* Tests Capacity::cleanup
145+
*/
146+
public function testCleanupDeleteFilesCountingCurrentBackup()
147+
{
148+
$fileList = $this->getFileMockList(
149+
array(
150+
array('size' => 100, 'shouldBeDeleted' => true),
151+
)
152+
);
153+
$resultStub = $this->getMockBuilder('\\phpbu\\App\\Result')
154+
->getMock();
155+
$collectorStub = $this->getMockBuilder('\\phpbu\\App\\Backup\\Collector')
156+
->disableOriginalConstructor()
157+
->getMock();
158+
$targetStub = $this->getMockBuilder('\\phpbu\\App\\Backup\\Target')
159+
->disableOriginalConstructor()
160+
->getMock();
161+
162+
$collectorStub->method('getBackupFiles')->willReturn($fileList);
163+
164+
$cleaner = new Quantity();
165+
$cleaner->setup(array('amount' => '1'));
166+
167+
$cleaner->cleanup($targetStub, $collectorStub, $resultStub);
168+
}
142169
}

0 commit comments

Comments
 (0)