Skip to content

Commit 666b83c

Browse files
gecolaytinect
andauthored
fix: GeneratorCompilerPass for ThumbnailService (#164)
Co-authored-by: tinect <[email protected]>
1 parent e8e97a3 commit 666b83c

File tree

9 files changed

+56
-16
lines changed

9 files changed

+56
-16
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
matrix: ${{ steps.matrix.outputs.matrix }}
1616
steps:
1717
- name: Checkout repository
18-
uses: actions/checkout@v2
18+
uses: actions/checkout@v4
1919

2020
- name: Get Shopware Version
2121
id: shopware-constraint
@@ -55,7 +55,7 @@ jobs:
5555
composer -V
5656
5757
- name: Checkout
58-
uses: actions/checkout@v3
58+
uses: actions/checkout@v4
5959
with:
6060
path: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }}
6161

@@ -65,7 +65,7 @@ jobs:
6565
composer create-placeholders
6666
php -d pcov.enabled=1 ${{ github.workspace }}/vendor/bin/phpunit --coverage-clover clover.xml --testsuite Unit
6767
68-
- uses: codecov/codecov-action@v3
68+
- uses: codecov/codecov-action@v5
6969
env:
7070
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
7171
with:
@@ -102,7 +102,7 @@ jobs:
102102
composer -V
103103
104104
- name: Checkout
105-
uses: actions/checkout@v3
105+
uses: actions/checkout@v4
106106
with:
107107
path: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }}
108108

@@ -112,7 +112,7 @@ jobs:
112112
composer create-placeholders
113113
php -d pcov.enabled=1 ${{ github.workspace }}/vendor/bin/phpunit --testsuite Integration
114114
115-
- uses: codecov/codecov-action@v3
115+
- uses: codecov/codecov-action@v5
116116
env:
117117
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
118118
with:

phpstan.baseline-generated-files.neon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ parameters:
1111
count: 1
1212
path: src/DependencyInjection/ThumbnailService.php
1313
reportUnmatched: false
14+
15+
-
16+
message: '#^Call to deprecated method#'
17+
count: 3
18+
path: src/DependencyInjection/ThumbnailService.php
19+
reportUnmatched: false
20+
21+
-
22+
message: '#^Only booleans are allowed in &&#'
23+
count: 1
24+
path: src/DependencyInjection/ThumbnailService.php
25+
reportUnmatched: false
26+
1427
-
1528
message: "#^Call to an undefined method Shopware\\\\Core\\\\Content\\\\Media\\\\Message\\\\GenerateThumbnailsMessage\\:\\:setContext\\(\\)\\.$#"
1629
count: 1

src/Controller/Api/TestController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
use Symfony\Component\HttpFoundation\JsonResponse;
2020
use Symfony\Component\HttpFoundation\Request;
2121
use Symfony\Component\Routing\Attribute\Route;
22+
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderCollection;
2223

2324
#[Route(defaults: ['_routeScope' => ['api']])]
2425
class TestController
2526
{
2627
public const REQUEST_ATTRIBUTE_TEST_ACTIVE = 'FroshPlatformThumbnailProcessorTestActive';
2728
public const TEST_FILE_PATH = __DIR__ . '/../../Resources/data/froshthumbnailprocessortestimage.jpg';
2829

30+
/**
31+
* @param EntityRepository<MediaCollection> $mediaRepository
32+
* @param EntityRepository<MediaFolderCollection> $mediaFolderRepository
33+
*/
2934
public function __construct(
3035
private readonly AbstractMediaUrlGenerator $urlGenerator,
3136
private readonly EntityRepository $mediaRepository,
@@ -143,7 +148,6 @@ private function getMediaById(string $fileName, Context $context): ?MediaEntity
143148
// we use the fileName filter to add backward compatibility
144149
$criteria->addFilter(new EqualsFilter('fileName', $fileName));
145150

146-
/** @var MediaCollection $entities */
147151
$entities = $this->mediaRepository->search($criteria, $context)->getEntities();
148152

149153
return $entities->first();

src/DependencyInjection/GeneratorCompilerPass.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,19 @@ private function handleThumbnailService(NodeFinder $nodeFinder, array $ast): voi
113113
} catch (\RuntimeException $e) {
114114
if ($e->getMessage() === 'Method createThumbnailsForSizes in class Shopware\Core\Content\Media\Thumbnail\ThumbnailService is missing') {
115115
$generateAndSaveNode = $this->getClassMethod($nodeFinder, 'generateAndSave', $ast);
116-
$this->handleGenerateAndSaveNode($generateAndSaveNode);
116+
117+
// when the internal 'isSameDimension' method is no longer available, we need to add the 'mediaThumbnailSizeId' field
118+
$addMediaThumbnailSizeId = true;
119+
try {
120+
$this->getClassMethod($nodeFinder, 'isSameDimension', $ast);
121+
$addMediaThumbnailSizeId = false;
122+
} catch (\RuntimeException $e) {
123+
if ($e->getMessage() !== 'Method isSameDimension in class Shopware\Core\Content\Media\Thumbnail\ThumbnailService is missing') {
124+
throw $e;
125+
}
126+
}
127+
128+
$this->handleGenerateAndSaveNode($generateAndSaveNode, $addMediaThumbnailSizeId);
117129
} else {
118130
throw $e;
119131
}
@@ -269,7 +281,7 @@ private function handleCreateThumbnailsForSizes(ClassMethod $createThumbnailsFor
269281
return $savedThumbnails;');
270282
}
271283

272-
private function handleGenerateAndSaveNode(ClassMethod $generateAndSaveNode): void
284+
private function handleGenerateAndSaveNode(ClassMethod $generateAndSaveNode, bool $addMediaThumbnailSizeId): void
273285
{
274286
// we don't need to generate the files, so we just return the array
275287
$generateAndSaveNode->stmts = $this->getPhpParser()
@@ -284,15 +296,13 @@ private function handleGenerateAndSaveNode(ClassMethod $generateAndSaveNode): vo
284296
throw MediaException::mediaTypeNotLoaded($media->getId());
285297
}
286298
287-
$mapped = [];
288299
foreach ($sizes as $size) {
289300
$id = Uuid::randomHex();
290301
291-
$mapped[$size->getId()] = $id;
292-
293302
$records[] = [
294303
\'id\' => $id,
295304
\'mediaId\' => $media->getId(),
305+
' . ($addMediaThumbnailSizeId ? '\'mediaThumbnailSizeId\' => $size->getId(),' : '') . '
296306
\'width\' => $size->getWidth(),
297307
\'height\' => $size->getHeight(),
298308
];

src/EventListener/ThumbnailSizesChangedListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Frosh\ThumbnailProcessor\EventListener;
44

5-
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
65
use Shopware\Core\Content\Media\Aggregate\MediaFolderConfigurationMediaThumbnailSize\MediaFolderConfigurationMediaThumbnailSizeDefinition;
76
use Shopware\Core\Content\Media\Commands\GenerateThumbnailsCommand;
87
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
@@ -12,9 +11,13 @@
1211
use Symfony\Component\Console\Input\ArrayInput;
1312
use Symfony\Component\Console\Output\NullOutput;
1413
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderCollection;
1515

1616
class ThumbnailSizesChangedListener implements EventSubscriberInterface
1717
{
18+
/**
19+
* @param EntityRepository<MediaFolderCollection> $mediaFolderRepository
20+
*/
1821
public function __construct(
1922
private readonly GenerateThumbnailsCommand $generateThumbnailsCommand,
2023
private readonly EntityRepository $mediaFolderRepository,
@@ -52,7 +55,6 @@ public function onThumbnailSizeChanged(EntityWrittenEvent $event): void
5255

5356
$result = $this->mediaFolderRepository->search($criteria, $event->getContext());
5457

55-
/** @var MediaFolderEntity $entity */
5658
foreach ($result->getEntities() as $entity) {
5759
$parameters = [];
5860

src/Service/SalesChannelIdDetector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
use Shopware\Core\PlatformRequest;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpFoundation\RequestStack;
13+
use Shopware\Core\Content\ProductExport\ProductExportCollection;
1314

1415
class SalesChannelIdDetector
1516
{
17+
/**
18+
* @param EntityRepository<ProductExportCollection> $productExportRepository
19+
*/
1620
public function __construct(
1721
private readonly RequestStack $requestStack,
1822
private readonly EntityRepository $productExportRepository

tests/integration/MediaUrlTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailCollection;
88
use Shopware\Core\Content\Media\Commands\GenerateThumbnailsCommand;
99
use Shopware\Core\Content\Media\Core\Application\AbstractMediaUrlGenerator;
10+
use Shopware\Core\Content\Media\MediaCollection;
1011
use Shopware\Core\Content\Media\MediaEntity;
1112
use Shopware\Core\Content\Test\Media\MediaFixtures;
1213
use Shopware\Core\Framework\Context;
@@ -18,14 +19,16 @@
1819
use Shopware\Core\System\SystemConfig\SystemConfigService;
1920
use Symfony\Component\Console\Input\ArrayInput;
2021
use Symfony\Component\Console\Output\NullOutput;
21-
use Symfony\Component\DependencyInjection\ContainerInterface;
2222

2323
class MediaUrlTest extends TestCase
2424
{
2525
use IntegrationTestBehaviour;
2626
use MediaFixtures;
2727
use QueueTestBehaviour;
2828

29+
/**
30+
* @var EntityRepository<MediaCollection>
31+
*/
2932
private EntityRepository $mediaRepository;
3033

3134
private GenerateThumbnailsCommand $generateThumbnailsCommand;
@@ -90,8 +93,8 @@ public function testMediaUrlWithInactiveConfigResultsInOriginalMedia(): void
9093

9194
$mediaResult = $this->mediaRepository->search($searchCriteria, $this->context);
9295

93-
/** @var MediaEntity $updatedMedia */
9496
$updatedMedia = $mediaResult->getEntities()->first();
97+
static::assertInstanceOf(MediaEntity::class, $updatedMedia);
9598

9699
$thumbnails = $updatedMedia->getThumbnails();
97100
static::assertInstanceOf(MediaThumbnailCollection::class, $thumbnails);
@@ -150,8 +153,8 @@ public function testMediaUrlWithActiveConfig(): void
150153

151154
$mediaResult = $this->mediaRepository->search($searchCriteria, $this->context);
152155

153-
/** @var MediaEntity $updatedMedia */
154156
$updatedMedia = $mediaResult->getEntities()->first();
157+
static::assertInstanceOf(MediaEntity::class, $updatedMedia);
155158

156159
$thumbnails = $updatedMedia->getThumbnails();
157160
static::assertInstanceOf(MediaThumbnailCollection::class, $thumbnails);

tests/unit/EventListener/ThumbnailSizesChangedListenerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testOnThumbnailSizeChanged(): void
4848
$mediaFolderCollection = new MediaFolderCollection();
4949
$mediaFolderCollection->add($mediaFolderEntity);
5050

51+
/** @var StaticEntityRepository<MediaFolderCollection> */
5152
$mediaFolderRepository = new StaticEntityRepository([$mediaFolderCollection]);
5253

5354
$thumbnailSizesChangedListener = new ThumbnailSizesChangedListener(

tests/unit/Service/SalesChannelIdDetectorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Frosh\ThumbnailProcessor\Tests\Unit\Service;
44

55
use Frosh\ThumbnailProcessor\Service\SalesChannelIdDetector;
6+
use PHPUnit\Framework\MockObject\MockObject;
67
use PHPUnit\Framework\TestCase;
78
use Shopware\Core\Content\ProductExport\ProductExportCollection;
89
use Shopware\Core\Content\ProductExport\ProductExportEntity;
@@ -90,6 +91,7 @@ public function testGetSalesChannelIdProductExport(): void
9091
$productExportCollection = new ProductExportCollection();
9192
$productExportCollection->add($productExportEntity);
9293

94+
/** @var StaticEntityRepository<ProductExportCollection> */
9395
$productExportRepository = new StaticEntityRepository([$productExportCollection]);
9496

9597
$class = new SalesChannelIdDetector($requestStack, $productExportRepository);
@@ -106,6 +108,7 @@ public function testGetSalesChannelIdProductExportWithoutFileNameAndAccessKey():
106108
],
107109
));
108110

111+
/** @var StaticEntityRepository<ProductExportCollection>&MockObject */
109112
$productExportRepository = $this->createMock(StaticEntityRepository::class);
110113
$productExportRepository->expects(static::never())->method('search');
111114

0 commit comments

Comments
 (0)