Skip to content

Commit 84a6296

Browse files
authored
Merge pull request #1762 from konarshankar07/integration-test-for-renditions-services--task-1751
Integration test coverage for rendition image generation
2 parents cdaec92 + 931a010 commit 84a6296

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\WriteInterface;
14+
use Magento\Framework\Filesystem\DriverInterface;
15+
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class GetRenditionPathTest extends TestCase
20+
{
21+
22+
/**
23+
* @var GetRenditionPathInterface
24+
*/
25+
private $getRenditionPath;
26+
27+
/**
28+
* @var WriteInterface
29+
*/
30+
private $mediaDirectory;
31+
32+
/**
33+
* @var DriverInterface
34+
*/
35+
private $driver;
36+
37+
protected function setup(): void
38+
{
39+
$this->getRenditionPath = Bootstrap::getObjectManager()->get(GetRenditionPathInterface::class);
40+
$this->mediaDirectory = Bootstrap::getObjectManager()->get(Filesystem::class)
41+
->getDirectoryWrite(DirectoryList::MEDIA);
42+
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
43+
}
44+
45+
/**
46+
* @dataProvider getImageProvider
47+
*
48+
* Test for getting a rendition path.
49+
*/
50+
public function testExecute(string $path, string $expectedRenditionPath): void
51+
{
52+
$imagePath = realpath(__DIR__ . '/../../_files' . $path);
53+
$modifiableFilePath = $this->mediaDirectory->getAbsolutePath($path);
54+
$this->driver->copy(
55+
$imagePath,
56+
$modifiableFilePath
57+
);
58+
$this->assertEquals($expectedRenditionPath, $this->getRenditionPath->execute($path));
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function getImageProvider(): array
65+
{
66+
return [
67+
'return_original_path' => [
68+
'path' => '/magento_medium_image.jpg',
69+
'expectedRenditionPath' => '/magento_medium_image.jpg'
70+
],
71+
'return_rendition_path' => [
72+
'path' => '/magento_large_image.jpg',
73+
'expectedRenditionPath' => '.renditions/magento_large_image.jpg'
74+
]
75+
];
76+
}
77+
}
54 KB
Loading
34.2 KB
Loading

0 commit comments

Comments
 (0)