ImageUtilsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\tests\Unit\Service\File\Utils;
  3. use App\Entity\Core\File;
  4. use App\Service\File\Utils\ImageUtils;
  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\TestCase;
  10. class ImageUtilsTest extends TestCase
  11. {
  12. use TestToolsTrait;
  13. private DataManager $dataManager;
  14. private FilterManager $filterManager;
  15. public function setUp(): void
  16. {
  17. $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
  18. $this->filterManager = $this->getMockBuilder(FilterManager::class)->disableOriginalConstructor()->getMock();
  19. }
  20. public function getImageUtilsMockFor(string $methodName) {
  21. return $this->getMockBuilder(TestableImageUtils::class)
  22. ->setConstructorArgs([$this->dataManager, $this->filterManager])
  23. ->setMethodsExcept([$methodName])
  24. ->getMock();
  25. }
  26. /**
  27. * @see ImageUtils::getCroppingConfig()
  28. * @return void
  29. * @throws \ReflectionException
  30. */
  31. public function testGetCroppingConfig(): void {
  32. $imageUtils = $this->getImageUtilsMockFor('getCroppingConfig');
  33. $config = '{"width": 100, "height": 100, "x": 10, "y": 10}';
  34. $result =[
  35. 'filters' => [
  36. 'crop' => [
  37. 'size' => [100, 100],
  38. 'start' => [10, 10]
  39. ]
  40. ]
  41. ];
  42. $this->assertEquals($result, $this->invokeMethod($imageUtils, 'getCroppingConfig', [$config]));
  43. }
  44. /**
  45. * @see ImageUtils::cropImage()
  46. * @return void
  47. */
  48. public function testCropImageWithoutConfig()
  49. {
  50. $file = new File();
  51. $file->setPath('example.jpg');
  52. $file->setConfig('');
  53. $imageUtils = $this->getImageUtilsMockFor('cropImage');
  54. $binary = $this->createMock(Binary::class);
  55. $binary->method('getContent')->willReturn('mocked_binary_data');
  56. $this->dataManager->expects($this->once())
  57. ->method('find')
  58. ->with(ImageUtils::FILTER_CROP, $file->getPath())
  59. ->willReturn($binary);
  60. $result = $imageUtils->cropImage($file);
  61. $this->assertEquals('mocked_binary_data', $result);
  62. }
  63. /**
  64. * @see ImageUtils::cropImage()
  65. * @return void
  66. */
  67. public function testCropImageWithConfig()
  68. {
  69. $file = new File();
  70. $file->setPath('example.jpg');
  71. $file->setConfig('{"width": 100, "height": 100, "x": 10, "y": 10}');
  72. $config = [
  73. 'filters' => [
  74. 'crop' => [
  75. 'size' => [100, 100],
  76. 'start' => [10, 10]
  77. ]
  78. ]
  79. ];
  80. $imageUtils = $this->getImageUtilsMockFor('cropImage');
  81. $binary = $this->createMock(Binary::class);
  82. $binary->method('getContent')->willReturn('mocked_binary_data');
  83. $this->dataManager->expects($this->once())
  84. ->method('find')
  85. ->with(ImageUtils::FILTER_CROP, $file->getPath())
  86. ->willReturn($binary);
  87. $this->filterManager->expects($this->once())
  88. ->method('applyFilter')
  89. ->with($binary, ImageUtils::FILTER_CROP, $config)
  90. ->willReturn($binary);
  91. $result = $imageUtils->cropImage($file);
  92. $this->assertEquals('mocked_binary_data', $result);
  93. }
  94. }