| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- namespace App\Tests\Unit\Service\Cron\Job;
- use App\Entity\Core\File;
- use App\Enum\Core\FileStatusEnum;
- use App\Repository\Core\FileRepository;
- use App\Service\Cron\Job\CleanTempFiles;
- use App\Service\Cron\UI\CronUIInterface;
- use App\Service\Utils\DatesUtils;
- use Doctrine\DBAL\Connection;
- use Doctrine\ORM\EntityManagerInterface;
- use Doctrine\ORM\Query;
- use Doctrine\ORM\Query\Expr;
- use Doctrine\ORM\Query\Expr\Comparison;
- use Doctrine\ORM\QueryBuilder;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\LoggerInterface;
- class TestableCleanTempFile extends CleanTempFiles
- {
- public function listFilesToDelete(\DateTime $maxDate): array
- {
- return parent::listFilesToDelete($maxDate);
- }
- public function deleteFiles(array $files): void
- {
- parent::deleteFiles($files);
- }
- public function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void
- {
- parent::getQueryConditions($queryBuilder, $maxDate);
- }
- }
- class CleanTempFilesTest extends TestCase
- {
- private CronUIInterface|MockObject $ui;
- private MockObject|LoggerInterface $logger;
- private FileRepository|MockObject $fileRepository;
- private Connection|MockObject $connection;
- private EntityManagerInterface|MockObject $em;
- public function setUp(): void
- {
- $this->ui = $this->getMockBuilder(CronUIInterface::class)->disableOriginalConstructor()->getMock();
- $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
- $this->fileRepository = $this->getMockBuilder(FileRepository::class)->disableOriginalConstructor()->getMock();
- $this->connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
- $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
- }
- private function getMockFor(string $method): MockObject|TestableCleanTempFile
- {
- $cleanTempFiles = $this->getMockBuilder(TestableCleanTempFile::class)
- ->setConstructorArgs([$this->connection, $this->fileRepository, $this->em])
- ->setMethodsExcept([$method, 'setUI', 'setLoggerInterface'])
- ->getMock();
- $cleanTempFiles->setUI($this->ui);
- $cleanTempFiles->setLoggerInterface($this->logger);
- return $cleanTempFiles;
- }
- public function testPreview(): void
- {
- DatesUtils::setFakeDatetime('2022-01-08 00:00:00');
- $maxDate = DatesUtils::new();
- $maxDate->sub(new \DateInterval('P60D'));
- $cleanTempFiles = $this->getMockFor('preview');
- $file1 = $this->getMockBuilder(File::class)->getMock();
- $file1->method('getSlug')->willReturn('/foo');
- $file2 = $this->getMockBuilder(File::class)->getMock();
- $file2->method('getSlug')->willReturn('/bar');
- $file3 = $this->getMockBuilder(File::class)->getMock();
- $file3->method('getSlug')->willReturn('/foo/bar');
- $cleanTempFiles->method('listFilesToDelete')->with($maxDate)->willReturn([$file1, $file2, $file3]);
- $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
- ['3 temporary files to be removed'],
- ['> Printing the first and last 50 :'],
- [' * /foo'],
- [' * /bar'],
- [' * /foo/bar']
- );
- $cleanTempFiles->preview();
- }
- public function testExecute(): void
- {
- DatesUtils::setFakeDatetime('2022-01-08 00:00:00');
- $maxDate = DatesUtils::new();
- $maxDate->sub(new \DateInterval('P60D'));
- $cleanTempFiles = $this->getMockFor('execute');
- $files = [
- $this->getMockBuilder(File::class)->getMock(),
- $this->getMockBuilder(File::class)->getMock(),
- $this->getMockBuilder(File::class)->getMock(),
- ];
- $cleanTempFiles->method('listFilesToDelete')->with($maxDate)->willReturn($files);
- $cleanTempFiles->expects(self::once())->method('deleteFiles')->with($files);
- $cleanTempFiles->execute();
- }
- public function testListFilesToDelete(): void
- {
- $cleanTempFiles = $this->getMockFor('listFilesToDelete');
- DatesUtils::setFakeDatetime('2023-05-01 00:00:00');
- $maxDate = DatesUtils::new();
- // Mock la méthode getQueryBuilder()
- $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->fileRepository->method('createQueryBuilder')->with('f')->willReturn($queryBuilder);
- // S'attend à ce que la méthode select() soit appelée
- $queryBuilder->expects($this->once())
- ->method('select');
- // S'attend à ce que la méthode getQueryConditions() soit appelée avec $maxDate
- $cleanTempFiles->expects($this->once())
- ->method('getQueryConditions')
- ->with($queryBuilder, $maxDate);
- // Mock la méthode getQuery() et getResult() pour renvoyer un tableau vide
- $query = $this->getMockBuilder(Query::class)
- ->disableOriginalConstructor()
- ->getMock();
- $query->expects($this->once())
- ->method('getResult')
- ->willReturn([]);
- $queryBuilder->expects($this->once())
- ->method('getQuery')
- ->willReturn($query);
- // Appeler la méthode listFilesToDelete()
- $result = $cleanTempFiles->listFilesToDelete($maxDate);
- // Vérifier que la méthode getResult() a été appelée sur la requête
- $this->assertEquals([], $result);
- }
- public function testDeleteFiles(): void
- {
- $cleanTempFiles = $this->getMockFor('deleteFiles');
- $file1 = $this->getMockBuilder(File::class)->getMock();
- $file2 = $this->getMockBuilder(File::class)->getMock();
- $file3 = $this->getMockBuilder(File::class)->getMock();
- $files = [$file1, $file2, $file3];
- // Set expectations for connection
- $this->connection->expects($this->once())->method('setAutoCommit')->with(false);
- $this->connection->expects($this->exactly(3))->method('beginTransaction');
- $this->connection->expects($this->exactly(3))->method('commit');
- $this->connection->expects($this->never())->method('rollback');
- // Set expectations for file status changes
- $file1->expects($this->once())->method('setStatus')->with(FileStatusEnum::DELETION_REQUESTED);
- $file2->expects($this->once())->method('setStatus')->with(FileStatusEnum::DELETION_REQUESTED);
- $file3->expects($this->once())->method('setStatus')->with(FileStatusEnum::DELETION_REQUESTED);
- // Set expectations for entity manager operations
- $this->em->expects($this->exactly(3))->method('persist')->withConsecutive([$file1], [$file2], [$file3]);
- $this->em->expects($this->exactly(3))->method('flush');
- // Set expectations for UI progress calls
- $this->ui->expects($this->exactly(4))->method('progress');
- // Set expectations for logger calls
- $this->logger->expects($this->exactly(3))->method('info')->withConsecutive(
- ['3 temporary files to be marked for deletion'],
- ['Marking files for deletion...'],
- ['3 files marked for deletion']
- );
- $cleanTempFiles->deleteFiles($files);
- }
- public function testDeleteFilesWithNonBlockingErrors(): void
- {
- $cleanTempFiles = $this->getMockFor('deleteFiles');
- $file1 = $this->getMockBuilder(File::class)->getMock();
- $file2 = $this->getMockBuilder(File::class)->getMock();
- $files = [$file1, $file2];
- // Set expectations for connection
- $this->connection->expects($this->once())->method('setAutoCommit')->with(false);
- $this->connection->expects($this->exactly(2))->method('beginTransaction');
- $this->connection->expects($this->never())->method('commit');
- $this->connection->expects($this->exactly(2))->method('rollback');
- // Set expectations for file status changes
- $file1->expects($this->once())->method('setStatus')->with(FileStatusEnum::DELETION_REQUESTED);
- $file2->expects($this->once())->method('setStatus')->with(FileStatusEnum::DELETION_REQUESTED);
- // Set expectations for entity manager operations that throw errors
- $this->em->expects($this->exactly(2))->method('persist')->withConsecutive([$file1], [$file2]);
- $this->em->expects($this->exactly(2))->method('flush')
- ->willReturnOnConsecutiveCalls(
- $this->throwException(new \RuntimeException('Some error')),
- $this->throwException(new \InvalidArgumentException('Some other error'))
- );
- // Set expectations for UI progress calls
- $this->ui->expects($this->exactly(3))->method('progress');
- // Set expectations for logger calls
- $this->logger->expects($this->exactly(3))->method('info')->withConsecutive(
- ['2 temporary files to be marked for deletion'],
- ['Marking files for deletion...'],
- ['0 files marked for deletion']
- );
- $this->logger->expects($this->exactly(2))->method('error')
- ->withConsecutive(
- ['ERROR : Some error'],
- ['ERROR : Some other error']
- );
- $cleanTempFiles->deleteFiles($files);
- }
- public function testDeleteFilesWithBlockingError(): void
- {
- $cleanTempFiles = $this->getMockFor('deleteFiles');
- $file1 = $this->getMockBuilder(File::class)->getMock();
- $files = [$file1];
- // Set expectations for connection
- $this->connection->expects($this->once())->method('setAutoCommit')->with(false);
- $this->connection->expects($this->once())->method('beginTransaction');
- $this->connection->expects($this->never())->method('commit');
- $this->connection->expects($this->once())->method('rollback');
- // Set expectations for file status changes
- $file1->expects($this->once())->method('setStatus')->with(FileStatusEnum::DELETION_REQUESTED);
- // Set expectations for entity manager operations that throw blocking error
- $this->em->expects($this->once())->method('persist')->with($file1);
- $this->em->expects($this->once())->method('flush')
- ->willThrowException(new \Exception('Some unknown error'));
- // Set expectations for UI progress calls
- $this->ui->expects($this->exactly(2))->method('progress');
- // Set expectations for logger calls
- $this->logger->expects($this->exactly(2))->method('info')->withConsecutive(
- ['1 temporary files to be marked for deletion'],
- ['Marking files for deletion...']
- );
- $this->expectException(\Exception::class);
- $this->expectExceptionMessage('Some unknown error');
- $cleanTempFiles->deleteFiles($files);
- }
- public function testGetQueryConditions(): void
- {
- DatesUtils::setFakeDatetime('2022-01-08 00:00:00');
- $cleanTempFiles = $this->getMockFor('getQueryConditions');
- $maxDate = DatesUtils::new();
- $maxDate->sub(new \DateInterval('P60D'));
- $queryBuilder = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
- $expr = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
- $queryBuilder->method('expr')->willReturn($expr);
- $cmp1 = $this->getMockBuilder(Comparison::class)->disableOriginalConstructor()->getMock();
- $cmp2 = $this->getMockBuilder(Comparison::class)->disableOriginalConstructor()->getMock();
- $cmp3 = $this->getMockBuilder(Comparison::class)->disableOriginalConstructor()->getMock();
- $expr->expects(self::exactly(2))->method('eq')->willReturnMap(
- [
- ['f.isTemporaryFile', ':temporaryTrue', $cmp1],
- ['f.status', ':status', $cmp2],
- ]
- );
- $expr->expects(self::once())->method('lt')->with('f.createDate', ':maxDate')->willReturn($cmp3);
- $expr->expects(self::once())->method('isNull')->with('f.createDate')->willReturn('f.createDate is null');
- $orX1 = $this->getMockBuilder(Expr\Orx::class)->disableOriginalConstructor()->getMock();
- $orX2 = $this->getMockBuilder(Expr\Orx::class)->disableOriginalConstructor()->getMock();
- $expr->expects(self::exactly(2))->method('orX')->willReturnMap(
- [
- [$cmp1, $cmp2, $orX1],
- [$cmp3, 'f.createDate is null', $orX2],
- ]
- );
- $queryBuilder
- ->expects(self::exactly(2))
- ->method('andWhere')
- ->withConsecutive(
- [$orX1],
- [$orX2],
- )
- ->willReturnSelf();
- $queryBuilder
- ->expects(self::exactly(3))
- ->method('setParameter')
- ->withConsecutive(
- ['temporaryTrue', true],
- ['status', FileStatusEnum::DELETED],
- ['maxDate', '2021-11-09'],
- )
- ->willReturnSelf();
- $cleanTempFiles->getQueryConditions($queryBuilder, $maxDate);
- }
- }
|