Skip to content

Commit 88070ea

Browse files
committed
improved BackuManager::index() method, also regarding the correct files sorting. This also solves a small bug for
the `rotate()` method (which precisely affects `index()`)
1 parent 9fd0118 commit 88070ea

File tree

6 files changed

+24
-30
lines changed

6 files changed

+24
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97));
77
* little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97));
88
* improved `BackuManager::index()` method, also regarding the correct files sorting. This also solves a small bug for
9-
the `rotate()` method (which precisely affects `index()`);
9+
the `rotate()` method (which precisely affects `index()`). The `index()` method now returns a collection of arrays (
10+
and no longer a collection of `Entity`);
1011
* some testing methods that have been missing for a long time have been added;
1112
* the `BackupTrait::getDriverName()` method can no longer be static;
1213
* removed (old and useless) `BaseCommandTestCase` class;

psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,4 @@
1616
<code>empty($this-&gt;Driver)</code>
1717
</TypeDoesNotContainType>
1818
</file>
19-
<file src="src/Utility/BackupManager.php">
20-
<TooManyTemplateParams occurrences="1">
21-
<code>\Cake\Collection\CollectionInterface&lt;\Cake\ORM\Entity&gt;</code>
22-
</TooManyTemplateParams>
23-
</file>
2419
</files>

src/Command/IndexCommand.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Cake\Console\ConsoleIo;
2020
use Cake\Console\ConsoleOptionParser;
2121
use Cake\I18n\Number;
22-
use Cake\ORM\Entity;
2322
use DatabaseBackup\Console\Command;
2423
use DatabaseBackup\Utility\BackupManager;
2524

@@ -63,11 +62,11 @@ public function execute(Arguments $args, ConsoleIo $io): void
6362
__d('database_backup', 'Size'),
6463
__d('database_backup', 'Datetime'),
6564
];
66-
$cells = $backups->map(fn(Entity $backup): array => $backup
67-
->set('compression', $backup->get('compression') ?: '')
68-
->set('datetime', $backup->get('datetime')->nice())
69-
->set('size', Number::toReadableSize($backup->get('size')))
70-
->toArray());
65+
$cells = $backups->map(fn(array $backup): array => [
66+
'compression' => $backup['compression'] ?: '',
67+
'datetime' => $backup['datetime']->nice(),
68+
'size' => Number::toReadableSize($backup['size']),
69+
] + $backup);
7170
$io->helper('table')->output(array_merge([$headers], $cells->toList()));
7271
}
7372
}

src/Command/RotateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function execute(Arguments $args, ConsoleIo $io): void
6767
}
6868

6969
foreach ($files as $file) {
70-
$io->verbose(__d('database_backup', 'Backup `{0}` has been deleted', $file->get('filename')));
70+
$io->verbose(__d('database_backup', 'Backup `{0}` has been deleted', $file['filename']));
7171
}
7272

7373
$io->success(__d('database_backup', 'Deleted backup files: {0}', count($files)));

src/Utility/BackupManager.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Cake\Core\Configure;
2121
use Cake\I18n\FrozenTime;
2222
use Cake\Mailer\Mailer;
23-
use Cake\ORM\Entity;
2423
use DatabaseBackup\BackupTrait;
2524
use LogicException;
2625
use Symfony\Component\Finder\Finder;
@@ -65,7 +64,7 @@ public static function deleteAll(): array
6564

6665
/**
6766
* Returns a list of database backups
68-
* @return \Cake\Collection\CollectionInterface<\Cake\ORM\Entity> Backups
67+
* @return \Cake\Collection\CollectionInterface Array of backups
6968
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupManager-utility#index
7069
*/
7170
public static function index(): CollectionInterface
@@ -77,16 +76,13 @@ public static function index(): CollectionInterface
7776
->sortByModifiedTime()
7877
->reverseSorting();
7978

80-
return (new Collection($Finder))->map(function (SplFileInfo $File): Entity {
81-
$filename = $File->getFilename();
82-
83-
return new Entity(compact('filename') + [
84-
'extension' => self::getExtension($filename),
85-
'compression' => self::getCompression($filename),
86-
'size' => $File->getSize(),
87-
'datetime' => FrozenTime::createFromTimestamp($File->getMTime()),
88-
]);
89-
})->compile(false);
79+
return (new Collection($Finder))->map(fn(SplFileInfo $File): array => [
80+
'filename' => $File->getFilename(),
81+
'extension' => self::getExtension($File->getFilename()),
82+
'compression' => self::getCompression($File->getFilename()),
83+
'size' => $File->getSize(),
84+
'datetime' => FrozenTime::createFromTimestamp($File->getMTime()),
85+
])->compile(false);
9086
}
9187

9288
/**

tests/TestCase/Utility/BackupManagerTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use Cake\Core\Configure;
1919
use Cake\I18n\FrozenTime;
20-
use Cake\ORM\Entity;
2120
use Cake\TestSuite\EmailTrait;
2221
use DatabaseBackup\TestSuite\TestCase;
2322
use DatabaseBackup\Utility\BackupExport;
@@ -111,9 +110,9 @@ public function testIndex(): void
111110

112111
//Checks for properties of each backup object
113112
foreach ($files as $file) {
114-
$this->assertInstanceOf(Entity::class, $file);
115-
$this->assertGreaterThan(0, $file->get('size'));
116-
$this->assertInstanceOf(FrozenTime::class, $file->get('datetime'));
113+
$this->assertIsArray($file);
114+
$this->assertGreaterThan(0, $file['size']);
115+
$this->assertInstanceOf(FrozenTime::class, $file['datetime']);
117116
}
118117
}
119118

@@ -139,7 +138,11 @@ public function testRotate(): void
139138
$this->assertSame(['bzip2', 'gzip'], $filesAfterRotate->extract('compression')->toList());
140139

141140
//Gets the difference
142-
$diff = array_udiff($initialFiles->toList(), $filesAfterRotate->toList(), fn(Entity $first, Entity $second): int => strcmp($first->get('filename'), $second->get('filename')));
141+
$diff = array_udiff(
142+
$initialFiles->toList(),
143+
$filesAfterRotate->toList(),
144+
fn(array $first, array $second): int => strcmp($first['filename'], $second['filename'])
145+
);
143146

144147
//Again, only 1 backup was deleted. The difference is the same
145148
$this->assertCount(1, $diff);

0 commit comments

Comments
 (0)