FileUtilsTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Utils;
  4. use App\Entity\Core\File;
  5. use App\Service\Utils\FileUtils;
  6. use Path\Path;
  7. use PHPUnit\Framework\MockObject\MockObject;
  8. use PHPUnit\Framework\TestCase;
  9. use Symfony\Component\Config\FileLocator;
  10. class TestableFileUtils extends FileUtils {
  11. public function makePath(string $path): Path
  12. {
  13. return parent::makePath($path);
  14. }
  15. public function makeFileLocator(array $directories): FileLocator
  16. {
  17. return parent::makeFileLocator($directories);
  18. }
  19. }
  20. class FileUtilsTest extends TestCase
  21. {
  22. private function getMockFor(string $methodName): TestableFileUtils|MockObject
  23. {
  24. return $this
  25. ->getMockBuilder(TestableFileUtils::class)
  26. ->setMethodsExcept([$methodName])
  27. ->getMock();
  28. }
  29. public function testGetMimeTypeFromExt() {
  30. $fileUtils = $this->getMockFor('getMimeTypeFromExt');
  31. $this->assertEquals(
  32. 'image/jpeg',
  33. $fileUtils->getMimeTypeFromExt('jpg')
  34. );
  35. $this->assertEquals(
  36. 'image/png',
  37. $fileUtils->getMimeTypeFromExt('.png')
  38. );
  39. $this->assertEquals(
  40. null,
  41. $fileUtils->getMimeTypeFromExt('.unknown')
  42. );
  43. }
  44. public function testGetMimeTypeFromFilename() {
  45. $fileUtils = $this->getMockFor('guessMimeTypeFromFilename');
  46. $fileUtils->method('getMimeTypeFromExt')->with('png')->willReturn('image/png');
  47. $this->assertEquals(
  48. 'image/png',
  49. $fileUtils->guessMimeTypeFromFilename('my_file.png')
  50. );
  51. }
  52. public function testIsImage() {
  53. $fileUtils = $this->getMockFor('isImage');
  54. // has a registered mimetype
  55. $file1 = $this->getMockBuilder(File::class)->getMock();
  56. $file1->method('getMimeType')->willReturn('image/png');
  57. // has no registered mimetype
  58. $file2 = $this->getMockBuilder(File::class)->getMock();
  59. $file2->method('getMimeType')->willReturn(null);
  60. $file2->method('getName')->willReturn('my_file.png');
  61. // has no registered mimetype and is not an image
  62. $file3 = $this->getMockBuilder(File::class)->getMock();
  63. $file3->method('getMimeType')->willReturn(null);
  64. $file3->method('getName')->willReturn('my_file.pdf');
  65. $fileUtils->method('guessMimeTypeFromFilename')->willReturnMap([
  66. ['my_file.png', 'image/png'],
  67. ['my_file.pdf', 'document/pdf'],
  68. ]);
  69. $this->assertTrue($fileUtils->isImage($file1));
  70. $this->assertTrue($fileUtils->isImage($file2));
  71. $this->assertFalse($fileUtils->isImage($file3));
  72. }
  73. public function testGetTempFilename() {
  74. $fileUtils = $this->getMockFor('getTempFilename');
  75. $tempDirPath = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  76. $tempDirPath->expects($this->once())->method('mkdir')->with(511, true);
  77. $fileUtils->method('makePath')->willReturn($tempDirPath);
  78. $this->assertMatchesRegularExpression(
  79. '/^.*\/var\/tmp\/\w{13}\.jpg$/',
  80. $fileUtils->getTempFilename('jpg')
  81. );
  82. }
  83. public function testGetTempFilenameWithPrefix() {
  84. $fileUtils = $this->getMockFor('getTempFilename');
  85. $tempDirPath = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  86. $tempDirPath->expects($this->once())->method('mkdir')->with(511, true);
  87. $fileUtils->method('makePath')->willReturn($tempDirPath);
  88. $this->assertMatchesRegularExpression(
  89. '/^.*\/var\/tmp\/foo\w{13}\.jpg$/',
  90. $fileUtils->getTempFilename('jpg', 'foo')
  91. );
  92. }
  93. public function testRmIfExist() {
  94. $fileUtils = $this->getMockFor('rmIfExist');
  95. $myPath = '/path/to/something';
  96. $path = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  97. $fileUtils->method('makePath')->with($myPath)->willReturn($path);
  98. $path->expects($this->once())->method('remove_p');
  99. $fileUtils->rmIfExist($myPath);
  100. }
  101. public function testGetFileContent() {
  102. $fileUtils = $this->getMockFor('getFileContent');
  103. $myPath = '/path/to/something';
  104. $path = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  105. $fileUtils->method('makePath')->with($myPath)->willReturn($path);
  106. $path->expects($this->once())->method('getContent')->willReturn('my content');
  107. $this->assertEquals('my content', $fileUtils->getFileContent($myPath));
  108. }
  109. public function testLocate() {
  110. $fileUtils = $this->getMockFor('locate');
  111. $fileLocator = $this->getMockBuilder(FileLocator::class)->disableOriginalConstructor()->getMock();
  112. $fileLocator
  113. ->expects($this->once())
  114. ->method('locate')
  115. ->with('my_file.pdf', null, false)
  116. ->willReturn(['/my/dir/my_file.pdf']);
  117. $fileUtils->method('makeFileLocator')->with(['/my/dir'])->willReturn($fileLocator);
  118. $this->assertEquals(
  119. '/my/dir/my_file.pdf',
  120. $fileUtils->locate(['/my/dir'], 'my_file.pdf')
  121. );
  122. }
  123. public function testLocateNotFound() {
  124. $fileUtils = $this->getMockFor('locate');
  125. $fileLocator = $this->getMockBuilder(FileLocator::class)->disableOriginalConstructor()->getMock();
  126. $fileLocator
  127. ->expects($this->once())
  128. ->method('locate')
  129. ->with('my_file.pdf', null, false)
  130. ->willReturn([]);
  131. $fileUtils->method('makeFileLocator')->with(['/my/dir'])->willReturn($fileLocator);
  132. $this->assertEquals(
  133. null,
  134. $fileUtils->locate(['/my/dir'], 'my_file.pdf')
  135. );
  136. }
  137. public function testList() {
  138. $fileUtils = $this->getMockFor('list');
  139. $path = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  140. $fileUtils->method('makePath')->with('/home')->willReturn($path);
  141. $child1 = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  142. $child1->method('__toString')->willReturn('/home/my_file.pdf');
  143. $child2 = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  144. $child2->method('__toString')->willReturn('/home/my_other_file.pdf');
  145. $path
  146. ->expects($this->once())
  147. ->method('glob')
  148. ->with('*')
  149. ->willReturn([$child1, $child2]);
  150. $this->assertEquals(
  151. ['/home/my_file.pdf', '/home/my_other_file.pdf'],
  152. $fileUtils->list('/home')
  153. );
  154. }
  155. public function testListWithGlob() {
  156. $fileUtils = $this->getMockFor('list');
  157. $path = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  158. $fileUtils->method('makePath')->with('/home')->willReturn($path);
  159. $child1 = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  160. $child1->method('__toString')->willReturn('/home/my_file.pdf');
  161. $child2 = $this->getMockBuilder(Path::class)->disableOriginalConstructor()->getMock();
  162. $child2->method('__toString')->willReturn('/home/my_other_file.pdf');
  163. $path
  164. ->expects($this->once())
  165. ->method('glob')
  166. ->with('*.pdf')
  167. ->willReturn([$child1, $child2]);
  168. $this->assertEquals(
  169. ['/home/my_file.pdf', '/home/my_other_file.pdf'],
  170. $fileUtils->list('/home', '*.pdf')
  171. );
  172. }
  173. }