CleanTempFilesTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace App\Tests\Service\Cron\Job;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Core\File;
  5. use App\Repository\Access\AccessRepository;
  6. use App\Repository\Core\FileRepository;
  7. use App\Service\Cron\Job\CleanTempFiles;
  8. use App\Service\Cron\UI\CronUIInterface;
  9. use App\Service\Storage\LocalStorage;
  10. use App\Service\Utils\DatesUtils;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\Common\Collections\Criteria;
  14. use Doctrine\ORM\AbstractQuery;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Doctrine\ORM\Query;
  17. use Doctrine\ORM\Query\Expr;
  18. use Doctrine\ORM\QueryBuilder;
  19. use PHPUnit\Framework\MockObject\MockObject;
  20. use PHPUnit\Framework\TestCase;
  21. use Psr\Log\LoggerInterface;
  22. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  23. use Symfony\Contracts\Service\Attribute\Required;
  24. class TestableCleanTempFile extends CleanTempFiles {
  25. public function listFilesToDelete(): Collection { return parent::listFilesToDelete(); }
  26. public function deleteFiles(Collection $files): void { parent::deleteFiles($files); }
  27. public function purgeDb(bool $commit = true): void { parent::purgeDb($commit); }
  28. }
  29. class CleanTempFilesTest extends TestCase
  30. {
  31. private CronUIInterface|MockObject $ui;
  32. private MockObject|LoggerInterface $logger;
  33. private FileRepository|MockObject $fileRepository;
  34. private EntityManagerInterface|MockObject $em;
  35. private AccessRepository|MockObject $accessRepository;
  36. private LocalStorage|MockObject $storage;
  37. public function setUp(): void
  38. {
  39. $this->ui = $this->getMockBuilder(CronUIInterface::class)->disableOriginalConstructor()->getMock();
  40. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  41. $this->fileRepository = $this->getMockBuilder(FileRepository::class)->disableOriginalConstructor()->getMock();
  42. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  43. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  44. $this->storage = $this->getMockBuilder(LocalStorage::class)->disableOriginalConstructor()->getMock();
  45. }
  46. private function getMockFor(string $method) {
  47. $cleanTempFiles = $this->getMockBuilder(TestableCleanTempFile::class)
  48. ->setConstructorArgs([$this->fileRepository, $this->em, $this->accessRepository, $this->storage])
  49. ->setMethodsExcept([$method, 'setUI', 'setLoggerInterface'])
  50. ->getMock();
  51. $cleanTempFiles->setUI($this->ui);
  52. $cleanTempFiles->setLoggerInterface($this->logger);
  53. return $cleanTempFiles;
  54. }
  55. public function testPreview(): void
  56. {
  57. $cleanTempFiles = $this->getMockFor('preview');
  58. $file1 = $this->getMockBuilder(File::class)->getMock();
  59. $file1->method('getPath')->willReturn('/foo');
  60. $file2 = $this->getMockBuilder(File::class)->getMock();
  61. $file2->method('getPath')->willReturn('/bar');
  62. $file3 = $this->getMockBuilder(File::class)->getMock();
  63. $file3->method('getPath')->willReturn('/foo/bar');
  64. $cleanTempFiles->method('listFilesToDelete')->willReturn(new ArrayCollection([$file1, $file2, $file3]));
  65. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  66. ["3 temporary files to be removed"],
  67. ["> Printing the first and last 50 :"],
  68. [" * /foo"],
  69. [" * /bar"],
  70. [" * /foo/bar"]
  71. );
  72. $cleanTempFiles->expects(self::once())->method('purgeDb')->with(false);
  73. $cleanTempFiles->preview();
  74. }
  75. public function testExecute(): void {
  76. $cleanTempFiles = $this->getMockFor('execute');
  77. $files = new ArrayCollection([
  78. $this->getMockBuilder(File::class)->getMock(),
  79. $this->getMockBuilder(File::class)->getMock(),
  80. $this->getMockBuilder(File::class)->getMock()
  81. ]);
  82. $cleanTempFiles->method('listFilesToDelete')->willReturn($files);
  83. $cleanTempFiles->expects(self::once())->method('deleteFiles')->with($files);
  84. $cleanTempFiles->expects(self::once())->method('purgeDb')->with();
  85. $cleanTempFiles->execute();
  86. }
  87. public function testListFilesToDelete(): void {
  88. $cleanTempFiles = $this->getMockFor('listFilesToDelete');
  89. DatesUtils::setFakeDatetime('2022-01-08 12:00:00');
  90. $this->ui->expects(self::once())->method('print')->with(
  91. 'List temporary files created before 2022-01-01T12:00:00+00:00'
  92. );
  93. $files = new ArrayCollection([
  94. $this->getMockBuilder(File::class)->getMock(),
  95. $this->getMockBuilder(File::class)->getMock(),
  96. $this->getMockBuilder(File::class)->getMock()
  97. ]);
  98. $this->fileRepository
  99. ->expects(self::once())
  100. ->method('matching')
  101. ->with(
  102. self::callback(static function (Criteria $c) {
  103. return $c instanceof Criteria; // TODO: trouver un moyen de tester le critère de façon plus précise
  104. })
  105. )->willReturn($files);
  106. $this->assertEquals(
  107. $files,
  108. $cleanTempFiles->listFilesToDelete()
  109. );
  110. }
  111. public function testDeleteFiles(): void
  112. {
  113. $cleanTempFiles = $this->getMockFor('deleteFiles');
  114. $file1 = $this->getMockBuilder(File::class)->getMock();
  115. $file2 = $this->getMockBuilder(File::class)->getMock();
  116. $file3 = $this->getMockBuilder(File::class)->getMock();
  117. $files = new ArrayCollection([$file1, $file2, $file3]);
  118. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  119. ['3 temporary files to be removed'],
  120. ['Deleting files...'],
  121. ['3 files deleted'],
  122. );
  123. $author = $this->getMockBuilder(Access::class)->getMock();
  124. $this->accessRepository->method('find')->with(10984)->willReturn($author);
  125. $this->ui->expects(self::exactly(4))->method('progress')->withConsecutive([0, 3], [1, 3], [2, 3], [3, 3]);
  126. $this->storage
  127. ->expects(self::exactly(3))
  128. ->method('delete')
  129. ->withConsecutive(
  130. [$file1, $author],
  131. [$file2, $author],
  132. [$file3, $author]
  133. );
  134. $cleanTempFiles->deleteFiles($files);
  135. }
  136. public function testDeleteFilesUnknownAuthor(): void
  137. {
  138. $cleanTempFiles = $this->getMockFor('deleteFiles');
  139. $file1 = $this->getMockBuilder(File::class)->getMock();
  140. $file2 = $this->getMockBuilder(File::class)->getMock();
  141. $file3 = $this->getMockBuilder(File::class)->getMock();
  142. $files = new ArrayCollection([$file1, $file2, $file3]);
  143. $this->accessRepository->method('find')->with(10984)->willReturn(null);
  144. $this->ui->expects(self::never())->method('progress');
  145. $this->storage->expects(self::never())->method('delete');
  146. $this->expectException(\RuntimeException::class);
  147. $this->expectExceptionMessage('Access 10984 could not be found');
  148. $cleanTempFiles->deleteFiles($files);
  149. }
  150. public function testDeleteFilesDeletionError(): void
  151. {
  152. $cleanTempFiles = $this->getMockFor('deleteFiles');
  153. $file1 = $this->getMockBuilder(File::class)->getMock();
  154. $file1->method('getId')->willReturn(1);
  155. $file2 = $this->getMockBuilder(File::class)->getMock();
  156. $file2->method('getId')->willReturn(2);
  157. $files = new ArrayCollection([$file1, $file2]);
  158. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  159. ['2 temporary files to be removed'],
  160. ['Deleting files...'],
  161. ['ERROR : foo'],
  162. ['1 files deleted'],
  163. );
  164. $author = $this->getMockBuilder(Access::class)->getMock();
  165. $this->accessRepository->method('find')->with(10984)->willReturn($author);
  166. $this->ui->expects(self::exactly(3))->method('progress')->withConsecutive([0, 2], [1, 2], [2, 2]);
  167. $this->storage
  168. ->expects(self::exactly(2))
  169. ->method('delete')
  170. ->willReturnCallback(static function ($file, $author): File {
  171. if ($file->getId() === 1) {
  172. throw new \RuntimeException('foo');
  173. }
  174. return $file;
  175. });
  176. $cleanTempFiles->deleteFiles($files);
  177. }
  178. public function testPurgeDb(): void {
  179. $cleanTempFiles = $this->getMockFor('purgeDb');
  180. DatesUtils::setFakeDatetime('2022-01-01 12:00:00');
  181. $this->ui->expects(self::exactly(2))->method('print')->withConsecutive(
  182. ['Purge DB from records of files deleted before 2021-01-01T12:00:00+00:00'],
  183. ['DB purged - 3 records permanently deleted'],
  184. );
  185. $qb = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
  186. $this->em->expects(self::once())->method('createQueryBuilder')->willReturn($qb);
  187. $exprBuilder = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  188. $qb->method('expr')->willReturn($exprBuilder);
  189. $cmp1 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  190. $exprBuilder->expects(self::once())->method('eq')->with('f.isTemporaryFile', 1)->willReturn($cmp1);
  191. $cmp2 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  192. $exprBuilder->expects(self::once())->method('lt')->with('f.updateDate', ':maxDate')->willReturn($cmp2);
  193. $qb->expects(self::once())->method('delete')->with('File', 'f')->willReturnSelf();
  194. $qb->method('where')->with($cmp1)->willReturnSelf();
  195. $qb->method('andWhere')->with($cmp2)->willReturnSelf();
  196. $qb->method('setParameter')->with('maxDate', DatesUtils::new('2021-01-01T12:00:00+00:00'))->willReturnSelf();
  197. $q = $this->getMockBuilder(AbstractQuery::class)->disableOriginalConstructor()->getMock();
  198. $qb->method('getQuery')->willReturn($q);
  199. $q->method('getResult')->willReturn(3);
  200. $this->em->expects(self::once())->method('commit');
  201. $cleanTempFiles->purgeDb();
  202. }
  203. public function testPurgeDbNoCommit(): void {
  204. $cleanTempFiles = $this->getMockFor('purgeDb');
  205. DatesUtils::setFakeDatetime('2022-01-01 12:00:00');
  206. $this->ui->expects(self::exactly(2))->method('print')->withConsecutive(
  207. ['Purge DB from records of files deleted before 2021-01-01T12:00:00+00:00'],
  208. ['DB purged - 3 records would be permanently deleted'],
  209. );
  210. $qb = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();
  211. $this->em->expects(self::once())->method('createQueryBuilder')->willReturn($qb);
  212. $exprBuilder = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  213. $qb->method('expr')->willReturn($exprBuilder);
  214. $cmp1 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  215. $exprBuilder->expects(self::once())->method('eq')->with('f.isTemporaryFile', 1)->willReturn($cmp1);
  216. $cmp2 = $this->getMockBuilder(Expr::class)->disableOriginalConstructor()->getMock();
  217. $exprBuilder->expects(self::once())->method('lt')->with('f.updateDate', ':maxDate')->willReturn($cmp2);
  218. $qb->expects(self::once())->method('delete')->with('File', 'f')->willReturnSelf();
  219. $qb->method('where')->willReturnSelf();
  220. $qb->method('andWhere')->willReturnSelf();
  221. $qb->method('setParameter')->willReturnSelf();
  222. $q = $this->getMockBuilder(AbstractQuery::class)->disableOriginalConstructor()->getMock();
  223. $qb->method('getQuery')->willReturn($q);
  224. $q->method('getResult')->willReturn(3);
  225. $this->em->expects(self::never())->method('commit');
  226. $this->em->expects(self::once())->method('rollback');
  227. $cleanTempFiles->purgeDb(false);
  228. }
  229. }