77declare (strict_types=1 );
88
99namespace Magento \MediaGalleryRenditions \Test \Integration \Model ;
10+
1011use Magento \Framework \App \Filesystem \DirectoryList ;
12+ use Magento \Framework \Exception \FileSystemException ;
13+ use Magento \Framework \Exception \LocalizedException ;
1114use Magento \Framework \Filesystem ;
1215use Magento \Framework \Filesystem \Directory \WriteInterface ;
16+ use Magento \Framework \Filesystem \DriverInterface ;
1317use Magento \MediaGalleryRenditionsApi \Api \GenerateRenditionsInterface ;
1418use Magento \TestFramework \Helper \Bootstrap ;
19+ use Magento \MediaGalleryRenditions \Model \Config ;
1520use PHPUnit \Framework \TestCase ;
1621
1722class GenerateRenditionsTest extends TestCase
1823{
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-
2524 /**
2625 * @var GenerateRenditionsInterface
2726 */
@@ -32,28 +31,23 @@ class GenerateRenditionsTest extends TestCase
3231 */
3332 private $ mediaDirectory ;
3433
35- public static function setUpBeforeClass (): void
36- {
37- /** @var WriteInterface $mediaDirectory */
38- $ mediaDirectory = Bootstrap::getObjectManager ()->get (
39- Filesystem::class
40- )->getDirectoryWrite (
41- DirectoryList::MEDIA
42- );
43-
44- $ mediaDir = $ mediaDirectory ->getAbsolutePath ();
45-
46- $ fixtureDir = realpath (__DIR__ . '/../_files ' );
34+ /**
35+ * @var Config
36+ */
37+ private $ renditionSizeConfig ;
4738
48- copy ($ fixtureDir . self ::LARGE_SIZE_IMAGE , $ mediaDir . self ::LARGE_SIZE_IMAGE );
49- copy ($ fixtureDir . self ::MEDIUM_SIZE_IMAGE , $ mediaDir . self ::MEDIUM_SIZE_IMAGE );
50- }
39+ /**
40+ * @var DriverInterface
41+ */
42+ private $ driver ;
5143
5244 protected function setup (): void
5345 {
5446 $ this ->generateRenditions = Bootstrap::getObjectManager ()->get (GenerateRenditionsInterface::class);
55- $ this ->filesystem = Bootstrap::getObjectManager ()->get (Filesystem::class);
56- $ this ->mediaDirectory = $ this ->filesystem ->getDirectoryRead (DirectoryList::MEDIA );
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);
5751 }
5852
5953 public static function tearDownAfterClass (): void
@@ -64,47 +58,73 @@ public static function tearDownAfterClass(): void
6458 )->getDirectoryWrite (
6559 DirectoryList::MEDIA
6660 );
67- $ mediaDir = $ mediaDirectory ->getAbsolutePath ();
68- if ($ mediaDirectory ->isExist ($ mediaDir . self ::LARGE_SIZE_IMAGE )) {
69- $ mediaDirectory ->delete ($ mediaDir . self ::LARGE_SIZE_IMAGE );
70- }
71- if ($ mediaDirectory ->isExist ($ mediaDir . self ::MEDIUM_SIZE_IMAGE )) {
72- $ mediaDirectory ->delete ($ mediaDir . self ::MEDIUM_SIZE_IMAGE );
73- }
74- if ($ mediaDirectory ->isExist ($ mediaDir . self ::RENDITIONS_FOLDER_NAME )) {
75- $ mediaDirectory ->delete ($ mediaDir . self ::RENDITIONS_FOLDER_NAME );
61+ if ($ mediaDirectory ->isExist ($ mediaDirectory ->getAbsolutePath () . '/.renditions ' )) {
62+ $ mediaDirectory ->delete ($ mediaDirectory ->getAbsolutePath () . '/.renditions ' );
7663 }
7764 }
7865
7966 /**
8067 * @dataProvider renditionsImageProvider
8168 *
8269 * Test for generation of rendition images.
70+ *
71+ * @param array $paths
72+ * @param string $renditionPath
73+ * @param bool $isRenditionsGenerated
74+ * @throws LocalizedException
8375 */
84- public function testExecute (string $ path , string $ renditionPath , bool $ isRenditionsGenerated ): void
76+ public function testExecute (array $ paths , string $ renditionPath , bool $ isRenditionsGenerated ): void
8577 {
86- $ this ->generateRenditions ->execute ([$ path ]);
78+ $ this ->copyImage ($ paths );
79+ $ this ->generateRenditions ->execute ($ paths );
8780 $ expectedRenditionPath = $ this ->mediaDirectory ->getAbsolutePath ($ renditionPath );
8881 if ($ isRenditionsGenerated ) {
82+ list ($ imageWidth , $ imageHeight ) = getimagesize ($ expectedRenditionPath );
8983 $ this ->assertFileExists ($ expectedRenditionPath );
84+ $ this ->assertLessThanOrEqual (
85+ $ this ->renditionSizeConfig ->getWidth (),
86+ $ imageWidth ,
87+ 'Generated renditions image width should be less than or equal to original image '
88+ );
89+ $ this ->assertLessThanOrEqual (
90+ $ this ->renditionSizeConfig ->getHeight (),
91+ $ imageHeight ,
92+ 'Generated renditions image height should be less than or equal to original image '
93+ );
9094 } else {
9195 $ this ->assertFileDoesNotExist ($ expectedRenditionPath );
9296 }
9397 }
9498
99+ /**
100+ * @param array $paths
101+ * @throws FileSystemException
102+ */
103+ private function copyImage (array $ paths ): void
104+ {
105+ foreach ($ paths as $ path ) {
106+ $ imagePath = realpath (__DIR__ . '/../../_files ' . $ path );
107+ $ modifiableFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ path );
108+ $ this ->driver ->copy (
109+ $ imagePath ,
110+ $ modifiableFilePath
111+ );
112+ }
113+ }
114+
95115 /**
96116 * @return array
97117 */
98118 public function renditionsImageProvider (): array
99119 {
100120 return [
101121 'rendition_image_not_generated ' => [
102- 'path ' => '/magento_medium_image.jpg ' ,
122+ 'paths ' => [ '/magento_medium_image.jpg ' ] ,
103123 'renditionPath ' => ".renditions/magento_medium_image.jpg " ,
104124 'isRenditionsGenerated ' => false
105125 ],
106126 'rendition_image_generated ' => [
107- 'path ' => '/magento_large_image.jpg ' ,
127+ 'paths ' => [ '/magento_large_image.jpg ' ] ,
108128 'renditionPath ' => ".renditions/magento_large_image.jpg " ,
109129 'isRenditionsGenerated ' => true
110130 ]
0 commit comments