|
| 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 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Filesystem; |
| 12 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 13 | +use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class GenerateRenditionsTest extends TestCase |
| 18 | +{ |
| 19 | + private const MEDIUM_SIZE_IMAGE = '/magento_medium_image.jpg'; |
| 20 | + |
| 21 | + private const LARGE_SIZE_IMAGE = '/magento_large_image.jpg'; |
| 22 | + |
| 23 | + private const RENDITIONS_FOLDER_NAME = '.renditions'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var GenerateRenditionsInterface |
| 27 | + */ |
| 28 | + private $generateRenditions; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var GetRenditionPathInterface |
| 32 | + */ |
| 33 | + private $mediaDirectory; |
| 34 | + |
| 35 | + protected static $mediaDir; |
| 36 | + |
| 37 | + public static function setUpBeforeClass(): void |
| 38 | + { |
| 39 | + /** @var WriteInterface $mediaDirectory */ |
| 40 | + $mediaDirectory = Bootstrap::getObjectManager()->get( |
| 41 | + Filesystem::class |
| 42 | + )->getDirectoryWrite( |
| 43 | + DirectoryList::MEDIA |
| 44 | + ); |
| 45 | + |
| 46 | + self::$mediaDir = $mediaDirectory->getAbsolutePath(); |
| 47 | + |
| 48 | + $fixtureDir = realpath(__DIR__ . '/../_files'); |
| 49 | + |
| 50 | + copy($fixtureDir . self::LARGE_SIZE_IMAGE, self::$mediaDir . self::LARGE_SIZE_IMAGE); |
| 51 | + copy($fixtureDir . self::MEDIUM_SIZE_IMAGE, self::$mediaDir . self::MEDIUM_SIZE_IMAGE); |
| 52 | + } |
| 53 | + |
| 54 | + protected function setup(): void |
| 55 | + { |
| 56 | + $this->generateRenditions = Bootstrap::getObjectManager()->get(GenerateRenditionsInterface::class); |
| 57 | + $this->filesystem = Bootstrap::getObjectManager()->get(Filesystem::class); |
| 58 | + $this->mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); |
| 59 | + } |
| 60 | + |
| 61 | + public static function tearDownAfterClass(): void |
| 62 | + { |
| 63 | + /** @var WriteInterface $mediaDirectory */ |
| 64 | + $mediaDirectory = Bootstrap::getObjectManager()->get( |
| 65 | + Filesystem::class |
| 66 | + )->getDirectoryWrite( |
| 67 | + DirectoryList::MEDIA |
| 68 | + ); |
| 69 | + |
| 70 | + if ($mediaDirectory->isExist(self::$mediaDir . self::LARGE_SIZE_IMAGE)) { |
| 71 | + $mediaDirectory->delete(self::$mediaDir . self::LARGE_SIZE_IMAGE); |
| 72 | + } |
| 73 | + if ($mediaDirectory->isExist(self::$mediaDir . self::MEDIUM_SIZE_IMAGE)) { |
| 74 | + $mediaDirectory->delete(self::$mediaDir . self::MEDIUM_SIZE_IMAGE); |
| 75 | + } |
| 76 | + if ($mediaDirectory->isExist(self::$mediaDir . self::RENDITIONS_FOLDER_NAME)) { |
| 77 | + $mediaDirectory->delete(self::$mediaDir . self::RENDITIONS_FOLDER_NAME); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test for generation of rendition images. |
| 83 | + */ |
| 84 | + public function testExecute(): void |
| 85 | + { |
| 86 | + $this->generateRenditions->execute([self::LARGE_SIZE_IMAGE, self::MEDIUM_SIZE_IMAGE]); |
| 87 | + $this->assertFileExists($this->mediaDirectory->getAbsolutePath(self::RENDITIONS_FOLDER_NAME . self::LARGE_SIZE_IMAGE)); |
| 88 | + $this->assertFileDoesNotExist($this->mediaDirectory->getAbsolutePath(self::RENDITIONS_FOLDER_NAME . self::MEDIUM_SIZE_IMAGE)); |
| 89 | + } |
| 90 | +} |
0 commit comments