Spaces:
No application file
No application file
File size: 3,095 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
<?php
namespace Mautic\ReportBundle\Tests\Model;
use Mautic\ReportBundle\Model\ReportCleanup;
use Mautic\ReportBundle\Scheduler\Model\FileHandler;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class ReportCleanupTest extends TestCase
{
private MockObject|FileHandler $fileHandler;
private ReportCleanup $cleanup;
protected function setUp(): void
{
$this->fileHandler = $this->createMock(FileHandler::class);
$this->cleanup = new ReportCleanup($this->fileHandler);
}
public function testCleanupAll(): void
{
$reportIds = [11, 13, 33];
$reportsDir = sys_get_temp_dir().'/csv_reports';
if (false === file_exists($reportsDir)) {
mkdir($reportsDir);
}
$filePaths = [];
foreach ($reportIds as $reportId) {
$filePath = $this->getFilePath($reportsDir, $reportId);
$filePaths[] = $filePath;
$days = ReportCleanup::KEEP_FILE_DAYS + 1;
// this report shouldn't be deleted
if (33 === $reportId) {
$days = ReportCleanup::KEEP_FILE_DAYS - 1;
}
$modifiedDate = time() - (86400 * $days);
$this->createTmpFile($filePath, $modifiedDate);
}
$this->fileHandler->expects($this->once())
->method('getCompressedCsvFileForReportDir')
->willReturn($reportsDir);
$this->fileHandler->expects($this->exactly(2))
->method('delete')
->willReturnOnConsecutiveCalls($filePaths[0], $filePaths[1]);
$this->cleanup->cleanupAll();
foreach ($filePaths as $filePath) {
unlink($filePath);
}
rmdir($reportsDir);
}
public function testCleanup(): void
{
$reportId = 9;
$reportsDir = sys_get_temp_dir().'/csv_reports';
if (false === file_exists($reportsDir)) {
mkdir($reportsDir);
}
$filePath = $this->getFilePath($reportsDir, $reportId);
$days = ReportCleanup::KEEP_FILE_DAYS + 1;
$modifiedDate = time() - (86400 * $days);
$this->createTmpFile($filePath, $modifiedDate);
$this->fileHandler->expects($this->once())
->method('deleteCompressedCsvFileForReportId')
->with($reportId);
$this->fileHandler->expects($this->once())
->method('getPathToCompressedCsvFileForReportId')
->with($reportId)
->willReturn($filePath);
$this->cleanup->cleanup($reportId);
unlink($filePath);
rmdir($reportsDir);
}
private function createTmpFile(string $filePath, ?int $modifiedDate = null, string $content = ''): string
{
file_put_contents($filePath, $content);
if (null !== $modifiedDate) {
touch($filePath, $modifiedDate);
}
return $filePath;
}
private function getFilePath(string $reportsDir, int $reportId): string
{
return $reportsDir.'/'."report_{$reportId}.zip";
}
}
|