Spaces:
No application file
No application file
File size: 5,372 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
<?php
declare(strict_types=1);
namespace Mautic\ReportBundle\Tests\Scheduler\Model;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\FilePathResolver;
use Mautic\CoreBundle\Helper\FileProperties;
use Mautic\ReportBundle\Entity\Report;
use Mautic\ReportBundle\Exception\FileTooBigException;
use Mautic\ReportBundle\Scheduler\Model\FileHandler;
use PHPUnit\Framework\MockObject\MockObject;
class FileHandlerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject|FilePathResolver
*/
private MockObject $filePathResolver;
/**
* @var MockObject|FileProperties
*/
private MockObject $fileProperties;
/**
* @var MockObject|CoreParametersHelper
*/
private MockObject $coreParametersHelper;
private FileHandler $fileHandler;
protected function setUp(): void
{
$this->filePathResolver = $this->createMock(FilePathResolver::class);
$this->fileProperties = $this->createMock(FileProperties::class);
$this->coreParametersHelper = $this->createMock(CoreParametersHelper::class);
$this->fileHandler = new FileHandler(
$this->filePathResolver,
$this->fileProperties,
$this->coreParametersHelper
);
}
public function testFileCanBeAttachedIfTrue(): void
{
$filePath = 'file/path.csv';
$fileSize = 1000;
$fileLimit = 5000;
$this->fileProperties->expects($this->once())
->method('getFileSize')
->with($filePath)
->willReturn($fileSize);
$this->coreParametersHelper->expects($this->once())
->method('get')
->with('report_export_max_filesize_in_bytes')
->willReturn($fileLimit);
$this->fileHandler->fileCanBeAttached($filePath);
}
public function testFileCanBeAttachedIfFalse(): void
{
$filePath = 'file/path.csv';
$fileSize = 10000;
$fileLimit = 5000;
$this->fileProperties->expects($this->once())
->method('getFileSize')
->with($filePath)
->willReturn($fileSize);
$this->coreParametersHelper->expects($this->once())
->method('get')
->with('report_export_max_filesize_in_bytes')
->willReturn($fileLimit);
$this->expectException(FileTooBigException::class);
$this->fileHandler->fileCanBeAttached($filePath);
}
public function testZipIt(): void
{
$tmpFilePath = $this->createTmpFile();
$tmpZipFilePath = $this->fileHandler->zipIt($tmpFilePath);
$this->assertFileExists($tmpZipFilePath);
unlink($tmpFilePath);
unlink($tmpZipFilePath);
}
public function testGetPathToCompressedCsvFileForReport(): void
{
$report = $this->createMock(Report::class);
$report->expects($this->once())
->method('getId')
->willReturn(33);
$this->coreParametersHelper->expects($this->once())
->method('get')
->with('report_temp_dir')
->willReturn('/some/path');
$filePath = $this->fileHandler->getPathToCompressedCsvFileForReport($report);
$this->assertSame('/some/path/csv_reports/report_33.zip', $filePath);
}
public function testMoveZipToPermanentLocation(): void
{
$report = $this->createMock(Report::class);
$filePath = 'file/path.csv';
$report->expects($this->once())
->method('getId')
->willReturn(33);
$this->coreParametersHelper->expects($this->once())
->method('get')
->with('report_temp_dir')
->willReturn('/some/path');
$this->filePathResolver->expects($this->once())
->method('delete')
->with('/some/path/csv_reports/report_33.zip');
$this->filePathResolver->expects($this->once())
->method('createDirectory')
->with('/some/path/csv_reports');
$this->filePathResolver->expects($this->once())
->method('move')
->with($filePath, '/some/path/csv_reports/report_33.zip');
$this->fileHandler->moveZipToPermanentLocation($report, $filePath);
}
public function testDeleteCompressedCsvFileForReportId(): void
{
$reportId = 33;
$tempDir = sys_get_temp_dir();
$reportsDir = $tempDir.'/csv_reports';
$fileName = "report_{$reportId}.zip";
$filePath = $reportsDir.'/'.$fileName;
if (!file_exists($reportsDir)) {
mkdir($reportsDir);
}
$this->createTmpFile('csv_reports/'.$fileName);
$this->coreParametersHelper->expects($this->once())
->method('get')
->with('report_temp_dir')
->willReturn($tempDir);
$this->filePathResolver->expects($this->once())
->method('delete')
->with($filePath);
$this->fileHandler->deleteCompressedCsvFileForReportId($reportId);
unlink($filePath);
rmdir($reportsDir);
}
private function createTmpFile(string $name = 'test.csv', string $content = ''): string
{
$filePath = sys_get_temp_dir().'/'.$name;
file_put_contents($filePath, $content);
return $filePath;
}
}
|