dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock(); $this->filterManager = $this->getMockBuilder(FilterManager::class)->disableOriginalConstructor()->getMock(); $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock(); } public function getImageUtilsMockFor(string $methodName) { return $this->getMockBuilder(ImageUtils::class) ->setConstructorArgs([$this->dataManager, $this->filterManager, $this->logger]) ->setMethodsExcept([$methodName]) ->getMock(); } /** * @see ImageUtils::getCroppingConfig() * @return void * @throws \ReflectionException */ public function testGetCroppingConfig(): void { $imageUtils = $this->getImageUtilsMockFor('getCroppingConfig'); $config = '{"width": 100, "height": 100, "x": 10, "y": 10}'; $result =[ 'filters' => [ 'crop' => [ 'size' => [100, 100], 'start' => [10, 10] ] ] ]; $this->assertEquals($result, $this->invokeMethod($imageUtils, 'getCroppingConfig', [$config])); } /** * @see ImageUtils::formatImage() * @return void */ public function testFormatImageWithoutConfig() { $file = $this->getMockBuilder(File::class)->getMock(); $file->method('getPath')->willReturn('example.jpg'); $file->method('getConfig')->willReturn(null); $imageUtils = $this->getImageUtilsMockFor('formatImage'); $binary = $this->createMock(Binary::class); $binary->method('getContent')->willReturn('mocked_binary_data'); $this->dataManager->expects($this->once()) ->method('find') ->with(ImageUtils::FILTER_CROP, $file->getPath()) ->willReturn($binary); $result = $imageUtils->formatImage($file); $this->assertEquals('mocked_binary_data', $result); } /** * @see ImageUtils::formatImage() * @return void */ public function testFormatImageWithConfig() { $file = $this->getMockBuilder(File::class)->getMock(); $file->method('getPath')->willReturn('example.jpg'); $file->method('getConfig')->willReturn('{"width": 100, "height": 100, "x": 10, "y": 10}'); $config = [ 'filters' => [ 'crop' => [ 'size' => [100, 100], 'start' => [10, 10] ] ] ]; $imageUtils = $this->getImageUtilsMockFor('formatImage'); $binary = $this->createMock(Binary::class); $binary->method('getContent')->willReturn('mocked_binary_data'); $this->dataManager->expects($this->once()) ->method('find') ->with(ImageUtils::FILTER_CROP, $file->getPath()) ->willReturn($binary); $this->filterManager->expects($this->once()) ->method('applyFilter') ->with($binary, ImageUtils::FILTER_CROP, $config) ->willReturn($binary); $this->logger->expects($this->never())->method('error'); $result = $imageUtils->formatImage($file); $this->assertEquals('mocked_binary_data', $result); } /** * @see ImageUtils::formatImage() * @return void */ public function testFormatImageWithErrorConfig() { $file = $this->getMockBuilder(File::class)->getMock(); $file->method('getPath')->willReturn('example.jpg'); $file->method('getConfig')->willReturn('{"hei": 100, "x": 10, "y": 10}'); $imageUtils = $this->getImageUtilsMockFor('formatImage'); $binary = $this->createMock(Binary::class); $binary->method('getContent')->willReturn('mocked_binary_data'); $this->dataManager->expects($this->once()) ->method('find') ->with(ImageUtils::FILTER_CROP, $file->getPath()) ->willReturn($binary); $this->logger->expects($this->exactly(2)) ->method('error') ; $result = $imageUtils->formatImage($file); $this->assertEquals('mocked_binary_data', $result); } /** * @see ImageUtils::isImage() * @return void */ public function testIsImage() { $file = $this->getMockBuilder(File::class)->getMock(); $file->method('getMimeType')->willReturn('image/jpeg'); $imageUtils = $this->getImageUtilsMockFor('isImage'); $this->assertTrue($imageUtils->isImage($file)); } /** * @see ImageUtils::isImage() * @return void */ public function testIsNotImage() { $file = $this->getMockBuilder(File::class)->getMock(); $file->method('getMimeType')->willReturn('application/pdf'); $imageUtils = $this->getImageUtilsMockFor('isImage'); $this->assertFalse($imageUtils->isImage($file)); } }