FileUtilsTest.php 7.5 KB

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