|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\MediaGalleryRenditions\Test\Integration\Model; |
| 10 | + |
| 11 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 12 | +use Magento\Framework\Exception\FileSystemException; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
| 14 | +use Magento\Framework\Filesystem; |
| 15 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 16 | +use Magento\Framework\Filesystem\DriverInterface; |
| 17 | +use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\MediaGalleryRenditions\Model\Config; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +class GenerateRenditionsTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var GenerateRenditionsInterface |
| 26 | + */ |
| 27 | + private $generateRenditions; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var WriteInterface |
| 31 | + */ |
| 32 | + private $mediaDirectory; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Config |
| 36 | + */ |
| 37 | + private $renditionSizeConfig; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var DriverInterface |
| 41 | + */ |
| 42 | + private $driver; |
| 43 | + |
| 44 | + protected function setup(): void |
| 45 | + { |
| 46 | + $this->generateRenditions = Bootstrap::getObjectManager()->get(GenerateRenditionsInterface::class); |
| 47 | + $this->mediaDirectory = Bootstrap::getObjectManager()->get(Filesystem::class) |
| 48 | + ->getDirectoryWrite(DirectoryList::MEDIA); |
| 49 | + $this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class); |
| 50 | + $this->renditionSizeConfig = Bootstrap::getObjectManager()->get(Config::class); |
| 51 | + } |
| 52 | + |
| 53 | + public static function tearDownAfterClass(): void |
| 54 | + { |
| 55 | + /** @var WriteInterface $mediaDirectory */ |
| 56 | + $mediaDirectory = Bootstrap::getObjectManager()->get( |
| 57 | + Filesystem::class |
| 58 | + )->getDirectoryWrite( |
| 59 | + DirectoryList::MEDIA |
| 60 | + ); |
| 61 | + if ($mediaDirectory->isExist($mediaDirectory->getAbsolutePath() . '/.renditions')) { |
| 62 | + $mediaDirectory->delete($mediaDirectory->getAbsolutePath() . '/.renditions'); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @dataProvider renditionsImageProvider |
| 68 | + * |
| 69 | + * Test for generation of rendition images. |
| 70 | + * |
| 71 | + * @param string $path |
| 72 | + * @param string $renditionPath |
| 73 | + * @param bool $isRenditionsGenerated |
| 74 | + * @throws LocalizedException |
| 75 | + */ |
| 76 | + public function testExecute(string $path, string $renditionPath, bool $isRenditionsGenerated): void |
| 77 | + { |
| 78 | + $this->copyImage($path); |
| 79 | + $this->generateRenditions->execute([$path]); |
| 80 | + $expectedRenditionPath = $this->mediaDirectory->getAbsolutePath($renditionPath); |
| 81 | + if (!$isRenditionsGenerated) { |
| 82 | + $this->assertFileDoesNotExist($expectedRenditionPath); |
| 83 | + return; |
| 84 | + } |
| 85 | + list($imageWidth, $imageHeight) = getimagesize($expectedRenditionPath); |
| 86 | + $this->assertFileExists($expectedRenditionPath); |
| 87 | + $this->assertLessThanOrEqual( |
| 88 | + $this->renditionSizeConfig->getWidth(), |
| 89 | + $imageWidth, |
| 90 | + 'Generated renditions image width should be less than or equal to configured value' |
| 91 | + ); |
| 92 | + $this->assertLessThanOrEqual( |
| 93 | + $this->renditionSizeConfig->getHeight(), |
| 94 | + $imageHeight, |
| 95 | + 'Generated renditions image height should be less than or equal to configured value' |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param array $paths |
| 101 | + * @throws FileSystemException |
| 102 | + */ |
| 103 | + private function copyImage(string $path): void |
| 104 | + { |
| 105 | + $imagePath = realpath(__DIR__ . '/../../_files' . $path); |
| 106 | + $modifiableFilePath = $this->mediaDirectory->getAbsolutePath($path); |
| 107 | + $this->driver->copy( |
| 108 | + $imagePath, |
| 109 | + $modifiableFilePath |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return array |
| 115 | + */ |
| 116 | + public function renditionsImageProvider(): array |
| 117 | + { |
| 118 | + return [ |
| 119 | + 'rendition_image_not_generated' => [ |
| 120 | + 'paths' => '/magento_medium_image.jpg', |
| 121 | + 'renditionPath' => ".renditions/magento_medium_image.jpg", |
| 122 | + 'isRenditionsGenerated' => false |
| 123 | + ], |
| 124 | + 'rendition_image_generated' => [ |
| 125 | + 'paths' => '/magento_large_image.jpg', |
| 126 | + 'renditionPath' => ".renditions/magento_large_image.jpg", |
| 127 | + 'isRenditionsGenerated' => true |
| 128 | + ] |
| 129 | + ]; |
| 130 | + } |
| 131 | +} |
0 commit comments