ImageUtilsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\tests\Unit\Service\File\Utils;
  3. use App\Entity\Core\File;
  4. use App\Service\File\Utils\FileUtils;
  5. use App\Tests\Unit\TestToolsTrait;
  6. use Liip\ImagineBundle\Imagine\Data\DataManager;
  7. use Liip\ImagineBundle\Imagine\Filter\FilterManager;
  8. use Liip\ImagineBundle\Model\Binary;
  9. use PHPUnit\Framework\MockObject\MockObject;
  10. use PHPUnit\Framework\TestCase;
  11. use Psr\Log\LoggerInterface;
  12. class ImageUtilsTest extends TestCase
  13. {
  14. use TestToolsTrait;
  15. private MockObject | DataManager $dataManager;
  16. private MockObject | FilterManager $filterManager;
  17. private MockObject | LoggerInterface $logger;
  18. public function setUp(): void
  19. {
  20. $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
  21. $this->filterManager = $this->getMockBuilder(FilterManager::class)->disableOriginalConstructor()->getMock();
  22. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  23. }
  24. public function getImageUtilsMockFor(string $methodName) {
  25. return $this->getMockBuilder(FileUtils::class)
  26. ->setConstructorArgs([$this->dataManager, $this->filterManager, $this->logger])
  27. ->setMethodsExcept([$methodName])
  28. ->getMock();
  29. }
  30. /**
  31. * @return void
  32. * @throws \ReflectionException
  33. *@see FileUtils::getCroppingConfig()
  34. */
  35. public function testGetCroppingConfig(): void {
  36. $imageUtils = $this->getImageUtilsMockFor('getCroppingConfig');
  37. $config = '{"width": 100, "height": 100, "x": 10, "y": 10}';
  38. $result =[
  39. 'filters' => [
  40. 'crop' => [
  41. 'size' => [100, 100],
  42. 'start' => [10, 10]
  43. ]
  44. ]
  45. ];
  46. $this->assertEquals($result, $this->invokeMethod($imageUtils, 'getCroppingConfig', [$config]));
  47. }
  48. /**
  49. * @return void
  50. *@see FileUtils::formatImage()
  51. */
  52. public function testFormatImageWithoutConfig()
  53. {
  54. $file = $this->getMockBuilder(File::class)->getMock();
  55. $file->method('getPath')->willReturn('example.jpg');
  56. $file->method('getConfig')->willReturn(null);
  57. $imageUtils = $this->getImageUtilsMockFor('formatImage');
  58. $binary = $this->createMock(Binary::class);
  59. $binary->method('getContent')->willReturn('mocked_binary_data');
  60. $this->dataManager->expects($this->once())
  61. ->method('find')
  62. ->with(FileUtils::FILTER_CROP, $file->getPath())
  63. ->willReturn($binary);
  64. $result = $imageUtils->formatImage($file);
  65. $this->assertEquals('mocked_binary_data', $result);
  66. }
  67. /**
  68. * @return void
  69. *@see FileUtils::formatImage()
  70. */
  71. public function testFormatImageWithConfig()
  72. {
  73. $file = $this->getMockBuilder(File::class)->getMock();
  74. $file->method('getPath')->willReturn('example.jpg');
  75. $file->method('getConfig')->willReturn('{"width": 100, "height": 100, "x": 10, "y": 10}');
  76. $config = [
  77. 'filters' => [
  78. 'crop' => [
  79. 'size' => [100, 100],
  80. 'start' => [10, 10]
  81. ]
  82. ]
  83. ];
  84. $imageUtils = $this->getImageUtilsMockFor('formatImage');
  85. $binary = $this->createMock(Binary::class);
  86. $binary->method('getContent')->willReturn('mocked_binary_data');
  87. $this->dataManager->expects($this->once())
  88. ->method('find')
  89. ->with(FileUtils::FILTER_CROP, $file->getPath())
  90. ->willReturn($binary);
  91. $this->filterManager->expects($this->once())
  92. ->method('applyFilter')
  93. ->with($binary, FileUtils::FILTER_CROP, $config)
  94. ->willReturn($binary);
  95. $this->logger->expects($this->never())->method('error');
  96. $result = $imageUtils->formatImage($file);
  97. $this->assertEquals('mocked_binary_data', $result);
  98. }
  99. /**
  100. * @return void
  101. *@see FileUtils::formatImage()
  102. */
  103. public function testFormatImageWithErrorConfig()
  104. {
  105. $file = $this->getMockBuilder(File::class)->getMock();
  106. $file->method('getPath')->willReturn('example.jpg');
  107. $file->method('getConfig')->willReturn('{"hei": 100, "x": 10, "y": 10}');
  108. $imageUtils = $this->getImageUtilsMockFor('formatImage');
  109. $binary = $this->createMock(Binary::class);
  110. $binary->method('getContent')->willReturn('mocked_binary_data');
  111. $this->dataManager->expects($this->once())
  112. ->method('find')
  113. ->with(FileUtils::FILTER_CROP, $file->getPath())
  114. ->willReturn($binary);
  115. $this->logger->expects($this->exactly(2))
  116. ->method('error')
  117. ;
  118. $result = $imageUtils->formatImage($file);
  119. $this->assertEquals('mocked_binary_data', $result);
  120. }
  121. /**
  122. * @return void
  123. *@see FileUtils::isImage()
  124. */
  125. public function testIsImage()
  126. {
  127. $file = $this->getMockBuilder(File::class)->getMock();
  128. $file->method('getMimeType')->willReturn('image/jpeg');
  129. $imageUtils = $this->getImageUtilsMockFor('isImage');
  130. $this->assertTrue($imageUtils->isImage($file));
  131. }
  132. /**
  133. * @return void
  134. * @see FileUtils::isImage()
  135. */
  136. public function testIsNotImage()
  137. {
  138. $file = $this->getMockBuilder(File::class)->getMock();
  139. $file->method('getMimeType')->willReturn('application/pdf');
  140. $imageUtils = $this->getImageUtilsMockFor('isImage');
  141. $this->assertFalse($imageUtils->isImage($file));
  142. }
  143. }