Skip to content

Commit 0a01e2d

Browse files
Feedback changes
1 parent 58d40d3 commit 0a01e2d

File tree

4 files changed

+72
-75
lines changed

4 files changed

+72
-75
lines changed

MediaGalleryRenditions/Test/Integration/Model/GenerateRenditionsTest.php

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77
declare(strict_types=1);
88

99
namespace Magento\MediaGalleryRenditions\Test\Integration\Model;
10+
1011
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\Exception\FileSystemException;
13+
use Magento\Framework\Exception\LocalizedException;
1114
use Magento\Framework\Filesystem;
1215
use Magento\Framework\Filesystem\Directory\WriteInterface;
16+
use Magento\Framework\Filesystem\DriverInterface;
1317
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
1418
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\MediaGalleryRenditions\Model\Config;
1520
use PHPUnit\Framework\TestCase;
1621

1722
class 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
]

MediaGalleryRenditions/Test/Integration/Model/GetRenditionPathTest.php

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,35 @@
1111
use Magento\Framework\App\Filesystem\DirectoryList;
1212
use Magento\Framework\Filesystem;
1313
use Magento\Framework\Filesystem\Directory\WriteInterface;
14-
use Magento\Framework\Filesystem\Directory\ReadInterface;
14+
use Magento\Framework\Filesystem\DriverInterface;
1515
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
1616
use Magento\TestFramework\Helper\Bootstrap;
1717
use PHPUnit\Framework\TestCase;
1818

1919
class GetRenditionPathTest extends TestCase
2020
{
21-
private const MEDIUM_SIZE_IMAGE = '/magento_medium_image.jpg';
22-
23-
private const LARGE_SIZE_IMAGE = '/magento_large_image.jpg';
2421

2522
/**
2623
* @var GetRenditionPathInterface
2724
*/
2825
private $getRenditionPath;
2926

3027
/**
31-
* @var ReadInterface
28+
* @var WriteInterface
3229
*/
33-
protected static $mediaDir;
34-
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-
);
30+
private $mediaDirectory;
4331

44-
self::$mediaDir = $mediaDirectory->getAbsolutePath();
45-
46-
$fixtureDir = realpath(__DIR__ . '/../_files');
47-
48-
copy($fixtureDir . self::LARGE_SIZE_IMAGE, self::$mediaDir . self::LARGE_SIZE_IMAGE);
49-
copy($fixtureDir . self::MEDIUM_SIZE_IMAGE, self::$mediaDir . self::MEDIUM_SIZE_IMAGE);
50-
}
32+
/**
33+
* @var DriverInterface
34+
*/
35+
private $driver;
5136

5237
protected function setup(): void
5338
{
5439
$this->getRenditionPath = Bootstrap::getObjectManager()->get(GetRenditionPathInterface::class);
55-
}
56-
57-
public static function tearDownAfterClass(): void
58-
{
59-
/** @var WriteInterface $mediaDirectory */
60-
$mediaDirectory = Bootstrap::getObjectManager()->get(
61-
Filesystem::class
62-
)->getDirectoryWrite(
63-
DirectoryList::MEDIA
64-
);
65-
66-
if ($mediaDirectory->isExist(self::$mediaDir . self::LARGE_SIZE_IMAGE)) {
67-
$mediaDirectory->delete(self::$mediaDir . self::LARGE_SIZE_IMAGE);
68-
}
69-
if ($mediaDirectory->isExist(self::$mediaDir . self::MEDIUM_SIZE_IMAGE)) {
70-
$mediaDirectory->delete(self::$mediaDir . self::MEDIUM_SIZE_IMAGE);
71-
}
40+
$this->mediaDirectory = Bootstrap::getObjectManager()->get(Filesystem::class)
41+
->getDirectoryWrite(DirectoryList::MEDIA);
42+
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
7243
}
7344

7445
/**
@@ -78,6 +49,12 @@ public static function tearDownAfterClass(): void
7849
*/
7950
public function testExecute(string $path, string $expectedRenditionPath): void
8051
{
52+
$imagePath = realpath(__DIR__ . '/../../_files' . $path);
53+
$modifiableFilePath = $this->mediaDirectory->getAbsolutePath($path);
54+
$this->driver->copy(
55+
$imagePath,
56+
$modifiableFilePath
57+
);
8158
$getRenditionPath = $this->getRenditionPath->execute($path);
8259
$this->assertEquals($expectedRenditionPath, $getRenditionPath);
8360
}

MediaGalleryRenditions/Test/Integration/_files/magento_large_image.jpg renamed to MediaGalleryRenditions/Test/_files/magento_large_image.jpg

File renamed without changes.

MediaGalleryRenditions/Test/Integration/_files/magento_medium_image.jpg renamed to MediaGalleryRenditions/Test/_files/magento_medium_image.jpg

File renamed without changes.

0 commit comments

Comments
 (0)