diff --git a/phpstan.baseline-generated-files.neon b/phpstan.baseline-generated-files.neon new file mode 100644 index 0000000..b0d0ccd --- /dev/null +++ b/phpstan.baseline-generated-files.neon @@ -0,0 +1,98 @@ +parameters: + ignoreErrors: + - + message: "#^Result of && is always false\\.$#" + count: 1 + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#^Strict comparison using \\=\\=\\= between false and true will always evaluate to false\\.$#" + count: 1 + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + - + message: "#^Call to an undefined method Shopware\\\\Core\\\\Content\\\\Media\\\\Message\\\\GenerateThumbnailsMessage\\:\\:setContext\\(\\)\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: '#^Call to deprecated method#' + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#" + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#^Anonymous function should return array but returns mixed.#" + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#tag\\:v6\\.7\\.0 - reason\\:return-type-change - Return type will be native#" + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#^Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\FileSaver::__construct\\(\\) does not call parent constructor from Shopware\\\\Core\\\\Content\\\\Media\\\\File\\\\FileSaver\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Casting to string something that's already string\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Only booleans are allowed in a negated boolean, Shopware\\\\Core\\\\Content\\\\Media\\\\Aggregate\\\\MediaThumbnail\\\\MediaThumbnailCollection\\|null given\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false + + - + message: "#^Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService::__construct\\(\\) does not call parent constructor from Shopware\\\\Core\\\\Content\\\\Media\\\\Thumbnail\\\\ThumbnailService\\.$#" + count: 1 + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" + count: 4 + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#^Only booleans are allowed in a negated boolean, Shopware\\\\Core\\\\Content\\\\Media\\\\Aggregate\\\\MediaThumbnail\\\\MediaThumbnailCollection\\|null given\\.$#" + count: 1 + path: src/DependencyInjection/ThumbnailService.php + reportUnmatched: false + + - + message: "#^Parameter \\#4 \\$metadata of method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\FileSaver::updateMediaEntity\\(\\) expects array\\|null, array\\|null given\\.$#" + count: 1 + path: src/DependencyInjection/FileSaver.php + reportUnmatched: false diff --git a/phpstan.neon b/phpstan.neon index 4ea80c5..96d1591 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,19 +1,25 @@ +includes: + - phpstan.baseline-generated-files.neon + parameters: level: max paths: - src - tests ignoreErrors: + - - message: "#^Result of && is always false\\.$#" - count: 1 - path: src/DependencyInjection/ThumbnailService.php + message: "#Call to static method PHPUnit\\\\Framework\\\\Assert::assertIsIterable\\(\\) with array will always evaluate to true.#" reportUnmatched: false - - message: "#^Strict comparison using \\=\\=\\= between false and true will always evaluate to false\\.$#" - count: 1 - path: src/DependencyInjection/ThumbnailService.php + message: "#staticMethod.alreadyNarrowedType#" + path: tests/unit/Core/Media/MediaUrlGeneratorTest.php + reportUnmatched: false + + - + message: "#tag\\:v6\\.8\\.0 - reason:return-type-change - return type will be nullable#" + path: tests/integration/MediaUrlTest.php reportUnmatched: false - @@ -38,52 +44,64 @@ parameters: - message: "#possible param types, only#" reportUnmatched: false + - - message: "#^Call to an undefined method Shopware\\\\Core\\\\Content\\\\Media\\\\Message\\\\GenerateThumbnailsMessage\\:\\:setContext\\(\\)\\.$#" - count: 1 - path: src/DependencyInjection/FileSaver.php + message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\SystemConfigService\\\\StaticSystemConfigService#" reportUnmatched: false - - message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#" - path: src/DependencyInjection/ThumbnailService.php + message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\DataAbstractionLayer\\\\StaticEntityRepository#" + reportUnmatched: false - - message: "#^Anonymous function should return array but returns mixed.#" - path: src/DependencyInjection/ThumbnailService.php + message: '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#' reportUnmatched: false - - message: "#tag\\:v6\\.7\\.0 - reason\\:return-type-change - Return type will be native#" - path: src/DependencyInjection/ThumbnailService.php - reportUnmatched: false + message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php + + - + message: "#^Property Frosh\\\\ThumbnailProcessor\\\\Tests\\\\Integration\\\\MediaUrlTest.*does not accept object.*#" + path: tests/integration/MediaUrlTest.php - - message: '#^Call to deprecated method#' - path: src/DependencyInjection/FileSaver.php + message: "#Use AbstractMediaUrlGenerator instead#" reportUnmatched: false - - message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\SystemConfigService\\\\StaticSystemConfigService#" + message: "#^Strict comparison using === between array and null will always evaluate to false\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php reportUnmatched: false - - message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\DataAbstractionLayer\\\\StaticEntityRepository#" + message: "#^Call to function method_exists\\(\\) with 'PhpParser\\\\\\\\ParserFactory' and 'createForHostVersion' will always evaluate to true\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php reportUnmatched: false - - message: '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#' + message: "#^Access to undefined constant PhpParser\\\\ParserFactory::ONLY_PHP7\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php reportUnmatched: false - - message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" + message: "#^Call to an undefined method PhpParser\\\\ParserFactory::create\\(\\)\\.$#" count: 1 path: src/DependencyInjection/GeneratorCompilerPass.php + reportUnmatched: false - - message: "#^Property Frosh\\\\ThumbnailProcessor\\\\Tests\\\\Integration\\\\MediaUrlTest.*does not accept object.*#" - path: tests/integration/MediaUrlTest.php + message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\GeneratorCompilerPass::getPhpParser\\(\\) should return PhpParser\\\\Parser but returns mixed\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php + reportUnmatched: false - - message: "#Use AbstractMediaUrlGenerator instead#" + message: "#^Property PhpParser\\\\Node\\\\Stmt\\\\Namespace_::\\$stmts \\(array\\) does not accept array\\|null\\.$#" + count: 1 + path: src/DependencyInjection/GeneratorCompilerPass.php reportUnmatched: false diff --git a/src/Controller/Api/TestController.php b/src/Controller/Api/TestController.php index d5abb8b..0e23451 100644 --- a/src/Controller/Api/TestController.php +++ b/src/Controller/Api/TestController.php @@ -79,12 +79,12 @@ private function getProductFolderId(Context $context): string ->addAssociation('defaultFolder') ->setLimit(1); - $ids = $this->mediaFolderRepository + $id = $this->mediaFolderRepository ->searchIds($criteria, $context) - ->getIds(); + ->getIds()[0] ?? null; - if (!empty($ids[0]) && \is_string($ids[0])) { - return $ids[0]; + if (\is_string($id)) { + return $id; } throw new \RuntimeException('Media folder for product could not have been found!'); @@ -100,7 +100,7 @@ private function getSampleMedia(string $testFile): MediaEntity $pathInfo = pathinfo($testFile); $existingMedia = $this->getMediaById($pathInfo['filename'], $context); - if ($existingMedia) { + if ($existingMedia !== null) { return $existingMedia; } @@ -130,7 +130,7 @@ private function getSampleMedia(string $testFile): MediaEntity ); $existingMedia = $this->getMediaById($pathInfo['filename'], $context); - if ($existingMedia) { + if ($existingMedia !== null) { return $existingMedia; } diff --git a/src/Core/Media/MediaUrlGenerator.php b/src/Core/Media/MediaUrlGenerator.php index 7969720..3676f55 100644 --- a/src/Core/Media/MediaUrlGenerator.php +++ b/src/Core/Media/MediaUrlGenerator.php @@ -62,7 +62,6 @@ public function generate(array $paths): array private function canProcess(string $path): bool { $fileExtension = \pathinfo($path, \PATHINFO_EXTENSION); - \assert(\is_string($fileExtension)); return $this->canProcessFileExtension($fileExtension); } @@ -71,7 +70,7 @@ private function canProcessFileExtension(string $fileExtension): bool { $extensionsAllowList = $this->getExtensionsAllowList(); - if (empty($extensionsAllowList)) { + if ($extensionsAllowList === []) { return false; } @@ -101,7 +100,8 @@ private function getExtensionsAllowList(): array \explode( ',', (string) \preg_replace('/\s+/', '', \strtolower($extensionsAllowListConfig)) - ) + ), + static fn($value) => $value !== '' ) ); } @@ -111,7 +111,7 @@ private function getExtensionsAllowList(): array private function getWidth(string $maxWidth, UrlParams $value): string { - if ($value instanceof ExtendedUrlParams && !empty($value->width)) { + if ($value instanceof ExtendedUrlParams && $value->width !== null && $value->width !== 0) { return (string) $value->width; } diff --git a/src/Core/Media/MediaUrlLoader.php b/src/Core/Media/MediaUrlLoader.php index 464a392..506afc2 100644 --- a/src/Core/Media/MediaUrlLoader.php +++ b/src/Core/Media/MediaUrlLoader.php @@ -18,7 +18,7 @@ public function loaded(iterable $entities): void { $mapping = $this->map($entities); - if (empty($mapping)) { + if ($mapping === []) { return; } @@ -65,12 +65,12 @@ private function map(iterable $entities): array $mapped = []; foreach ($entities as $entity) { - if (!$entity->has('path') || empty($entity->get('path'))) { + if (!$entity->has('path') || $entity->get('path') === null || $entity->get('path') === '') { continue; } // don't generate private urls - if (!$entity->has('private') || $entity->get('private')) { + if (!$entity->has('private') || $entity->get('private') === true) { continue; } @@ -91,7 +91,7 @@ private function map(iterable $entities): array continue; } - if (!$thumbnail->has('path') || empty($thumbnail->get('path'))) { + if (!$thumbnail->has('path') || $thumbnail->get('path') === null || $thumbnail->get('path') === '') { continue; } diff --git a/src/DependencyInjection/GeneratorCompilerPass.php b/src/DependencyInjection/GeneratorCompilerPass.php index dfd5495..f7ba3ea 100644 --- a/src/DependencyInjection/GeneratorCompilerPass.php +++ b/src/DependencyInjection/GeneratorCompilerPass.php @@ -34,7 +34,7 @@ public function process(ContainerBuilder $container): void { $fileContents = $this->getFileContent(); - if (empty($fileContents)) { + if ($fileContents === null || $fileContents === '') { $this->removeReflectionClass(); return; @@ -71,7 +71,7 @@ public function process(ContainerBuilder $container): void $doc = '/**' . \PHP_EOL . 'THIS CLASS HAS BEEN GENERATED AUTOMATICALLY' . \PHP_EOL . '*/'; $existingDocs = $class->getDocComment()?->getText() ?? ''; - if (!empty($existingDocs)) { + if ($existingDocs !== '') { $doc .= \PHP_EOL . $existingDocs; } @@ -123,7 +123,7 @@ private function handleThumbnailService(NodeFinder $nodeFinder, array $ast): voi $updateThumbnailsNode = $this->getClassMethod($nodeFinder, 'updateThumbnails', $ast); $stmts = $updateThumbnailsNode->getStmts(); - if (empty($stmts)) { + if ($stmts === null || $stmts === []) { throw new \RuntimeException(\sprintf('Method %s in class %s is empty', 'updateThumbnails', $this->class)); } @@ -174,7 +174,7 @@ private function addUsesOfNamespace(?array &$stmts, string $namespace): void $filePath = $this->getFileName(); $files = glob(\dirname($filePath) . '/*.php'); - if (empty($files)) { + if ($files === false || $files === []) { return; } @@ -188,7 +188,7 @@ private function addUsesOfNamespace(?array &$stmts, string $namespace): void $uses[] = new UseUse(new Name($class)); } - if (empty($uses)) { + if ($uses === []) { return; } @@ -197,7 +197,13 @@ private function addUsesOfNamespace(?array &$stmts, string $namespace): void private function getFileContent(): ?string { - return file_get_contents($this->getFileName()) ?: null; + $content = file_get_contents($this->getFileName()); + + if (\is_string($content)) { + return $content; + } + + return null; } private function getFileName(): string @@ -206,7 +212,7 @@ private function getFileName(): string $fileName = $reflectionClass->getFileName(); - if (empty($fileName)) { + if ($fileName === false) { throw new \RuntimeException(\sprintf('Cannot get fileName of class %s', $this->class)); } @@ -231,7 +237,7 @@ private function getClassName(): string { $lastOccur = strrchr($this->class, '\\'); - if (empty($lastOccur)) { + if ($lastOccur === false || $lastOccur === '') { throw new \RuntimeException(\sprintf('Cannot determine className from %s', $this->class)); } diff --git a/src/EventListener/ThumbnailSizesChangedListener.php b/src/EventListener/ThumbnailSizesChangedListener.php index f2171cc..98f92a1 100644 --- a/src/EventListener/ThumbnailSizesChangedListener.php +++ b/src/EventListener/ThumbnailSizesChangedListener.php @@ -36,14 +36,14 @@ public function onThumbnailSizeChanged(EntityWrittenEvent $event): void foreach ($event->getWriteResults() as $writeResult) { $mediaFolderConfigurationId = $writeResult->getPayload()['mediaFolderConfigurationId'] ?? null; - if (!empty($mediaFolderConfigurationId) && \is_string($mediaFolderConfigurationId)) { + if ($mediaFolderConfigurationId !== '' && \is_string($mediaFolderConfigurationId)) { $updatedMediaFolderConfigurationIds[] = $mediaFolderConfigurationId; } } $updatedMediaFolderConfigurationIds = \array_unique($updatedMediaFolderConfigurationIds); - if (empty($updatedMediaFolderConfigurationIds)) { + if ($updatedMediaFolderConfigurationIds === []) { return; } diff --git a/src/Migration/Migration1686772873AddActiveConfig.php b/src/Migration/Migration1686772873AddActiveConfig.php index 421c763..e1523df 100644 --- a/src/Migration/Migration1686772873AddActiveConfig.php +++ b/src/Migration/Migration1686772873AddActiveConfig.php @@ -16,7 +16,7 @@ public function update(Connection $connection): void { $currentPluginVersion = $this->getPluginVersion($connection); - if (empty($currentPluginVersion)) { + if ($currentPluginVersion === null || $currentPluginVersion === '') { return; } diff --git a/src/Service/ConfigReader.php b/src/Service/ConfigReader.php index fd058f1..64baee0 100644 --- a/src/Service/ConfigReader.php +++ b/src/Service/ConfigReader.php @@ -66,7 +66,7 @@ private function isActive(mixed $config): bool return true; } - return $this->isTestActive() || !empty($config); + return $this->isTestActive() || ($config !== false && $config !== '0' && $config !== 0); } private function isTestActive(): bool diff --git a/src/Service/ThumbnailUrlTemplate.php b/src/Service/ThumbnailUrlTemplate.php index 3329f0c..8eb35c3 100644 --- a/src/Service/ThumbnailUrlTemplate.php +++ b/src/Service/ThumbnailUrlTemplate.php @@ -13,21 +13,28 @@ public function __construct( public function getUrl(string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $mediaUpdatedAt): string { + $timestamp = $mediaUpdatedAt !== null ? (string) $mediaUpdatedAt->getTimestamp() : '0'; + return str_replace( ['{mediaUrl}', '{mediaPath}', '{width}', '{mediaUpdatedAt}'], - [$mediaUrl, $mediaPath, $width, $mediaUpdatedAt?->getTimestamp() ?: '0'], + [$mediaUrl, $mediaPath, $width, $timestamp], $this->getPattern() ); } private function getPattern(): string { - if ($this->pattern) { + if (isset($this->pattern)) { return $this->pattern; } + $this->pattern = '{mediaUrl}/{mediaPath}?width={width}'; + $pattern = $this->configReader->getConfig('ThumbnailPattern'); - $this->pattern = $pattern && \is_string($pattern) ? $pattern : '{mediaUrl}/{mediaPath}?width={width}'; + + if (\is_string($pattern) && $pattern !== '') { + $this->pattern = $pattern; + } return $this->pattern; } diff --git a/tests/TestBootstraper.php b/tests/TestBootstraper.php index bcef83d..d5f7ecd 100644 --- a/tests/TestBootstraper.php +++ b/tests/TestBootstraper.php @@ -5,18 +5,6 @@ use Shopware\Core\TestBootstrapper; $pluginName = 'FroshPlatformThumbnailProcessor'; -$testBootstrapperPluginDevDocker = '/opt/share/shopware/tests/TestBootstrapper.php'; - -if (\is_file($testBootstrapperPluginDevDocker)) { - $testBootstrapper = require $testBootstrapperPluginDevDocker; - - return $testBootstrapper - ->setLoadEnvFile(true) - ->setForceInstallPlugins(true) - ->addActivePlugins($pluginName) - ->bootstrap() - ->getClassLoader(); -} $paths = [ '../../../../src/Core/TestBootstrapper.php', diff --git a/tests/integration/MediaUrlTest.php b/tests/integration/MediaUrlTest.php index 7f4fabb..2bab839 100644 --- a/tests/integration/MediaUrlTest.php +++ b/tests/integration/MediaUrlTest.php @@ -36,20 +36,15 @@ class MediaUrlTest extends TestCase protected function setUp(): void { - /** @var ContainerInterface $container */ - $container = $this->getContainer(); + $container = $this::getContainer(); $urlGenerator = $container->get(AbstractMediaUrlGenerator::class); - \assert($urlGenerator instanceof AbstractMediaUrlGenerator); $this->mediaRepository = $container->get('media.repository'); - \assert($this->mediaRepository instanceof EntityRepository); $this->generateThumbnailsCommand = $container->get(GenerateThumbnailsCommand::class); - \assert($this->generateThumbnailsCommand instanceof GenerateThumbnailsCommand); $this->systemConfigService = $container->get(SystemConfigService::class); - \assert($this->systemConfigService instanceof SystemConfigService); $this->context = Context::createDefaultContext(); } @@ -62,8 +57,12 @@ public function testMediaUrlWithInactiveConfigResultsInOriginalMedia(): void static::assertMatchesRegularExpression('/http:\/\/localhost:8000\/media\/_test\/pngFileWithExtensionAndFolder\.png\?(\d+|ts=\d+)/', $media->getUrl()); $folderName = null; - if (\is_array($fixture['mediaFolder']) && !empty($fixture['mediaFolder']['name'])) { - $folderName = $fixture['mediaFolder']['name']; + + if (\is_array($fixture['mediaFolder'])) { + $name = $fixture['mediaFolder']['name'] ?? null; + if ($name !== null && $name !== '') { + $folderName = $name; + } } static::assertNotEmpty($folderName); @@ -119,8 +118,11 @@ public function testMediaUrlWithActiveConfig(): void static::assertStringEndsWith('pngFileWithExtensionAndFolder.png?width=3000', $media->getUrl()); $folderName = null; - if (\is_array($fixture['mediaFolder']) && !empty($fixture['mediaFolder']['name'])) { - $folderName = $fixture['mediaFolder']['name']; + if (\is_array($fixture['mediaFolder'])) { + $name = $fixture['mediaFolder']['name'] ?? null; + if ($name !== null && $name !== '') { + $folderName = $name; + } } static::assertNotEmpty($folderName); diff --git a/tests/unit/Core/Media/MediaUrlGeneratorTest.php b/tests/unit/Core/Media/MediaUrlGeneratorTest.php index e78c946..d69abf6 100644 --- a/tests/unit/Core/Media/MediaUrlGeneratorTest.php +++ b/tests/unit/Core/Media/MediaUrlGeneratorTest.php @@ -21,11 +21,11 @@ public function testGenerateWithUrlParam(): void { $decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class); $thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class); - $thumbnailUrlTemplate->expects(static::once()) + $thumbnailUrlTemplate->expects($this->once()) ->method('getUrl') ->willReturn('https://localhost/media/123.jpg?width=3000'); $filesystem = $this->createMock(PrefixFilesystem::class); - $filesystem->expects(static::once()) + $filesystem->expects($this->once()) ->method('publicUrl') ->willReturn('https://localhost'); @@ -37,7 +37,7 @@ public function testGenerateWithUrlParam(): void 'ExtensionsAllowList' => 'jpg', 'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}', ]; - $configReader->expects(static::any()) + $configReader->expects($this->any()) ->method('getConfig') ->willReturnCallback(function ($key) use ($defaultPluginConfig) { return $defaultPluginConfig[$key] ?? null; @@ -68,11 +68,11 @@ public function testGenerateWithExtendedUrlParam(): void { $decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class); $thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class); - $thumbnailUrlTemplate->expects(static::once()) + $thumbnailUrlTemplate->expects($this->once()) ->method('getUrl') ->willReturn('https://localhost/media/123.jpg?width=100'); $filesystem = $this->createMock(PrefixFilesystem::class); - $filesystem->expects(static::once()) + $filesystem->expects($this->once()) ->method('publicUrl') ->willReturn('https://localhost'); @@ -84,7 +84,7 @@ public function testGenerateWithExtendedUrlParam(): void 'ExtensionsAllowList' => 'jpg', 'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}', ]; - $configReader->expects(static::any()) + $configReader->expects($this->any()) ->method('getConfig') ->willReturnCallback(function ($key) use ($defaultPluginConfig) { return $defaultPluginConfig[$key] ?? null; @@ -120,16 +120,16 @@ public function testGenerateWithExtendedUrlParam(): void public function testGenerateWithInactiveConfig(): void { $decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class); - $decoratedMediaUrlGenerator->expects(static::once()) + $decoratedMediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn(['https://localhost/media/123.jpg']); $thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class); - $thumbnailUrlTemplate->expects(static::never()) + $thumbnailUrlTemplate->expects($this->never()) ->method('getUrl') ->willReturn('https://localhost/media/123.jpg?width=3000'); $filesystem = $this->createMock(PrefixFilesystem::class); - $filesystem->expects(static::never()) + $filesystem->expects($this->never()) ->method('publicUrl') ->willReturn('https://localhost'); @@ -141,7 +141,7 @@ public function testGenerateWithInactiveConfig(): void 'ExtensionsAllowList' => 'jpg', 'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}', ]; - $configReader->expects(static::any()) + $configReader->expects($this->any()) ->method('getConfig') ->willReturnCallback(function ($key) use ($defaultPluginConfig) { return $defaultPluginConfig[$key] ?? null; @@ -174,16 +174,16 @@ public function testGenerateWithInactiveConfig(): void public function testGenerateWithNotAllowedExtensionResultsInOriginal(string $allowList): void { $decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class); - $decoratedMediaUrlGenerator->expects(static::once()) + $decoratedMediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn(['https://localhost/media/123.jpg']); $thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class); - $thumbnailUrlTemplate->expects(static::never()) + $thumbnailUrlTemplate->expects($this->never()) ->method('getUrl') ->willReturn('https://localhost/media/123.jpg?width=3000'); $filesystem = $this->createMock(PrefixFilesystem::class); - $filesystem->expects(static::once()) + $filesystem->expects($this->once()) ->method('publicUrl') ->willReturn('https://localhost'); @@ -195,7 +195,7 @@ public function testGenerateWithNotAllowedExtensionResultsInOriginal(string $all 'ExtensionsAllowList' => $allowList, 'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}', ]; - $configReader->expects(static::any()) + $configReader->expects($this->any()) ->method('getConfig') ->willReturnCallback(function ($key) use ($defaultPluginConfig) { return $defaultPluginConfig[$key] ?? null; @@ -226,11 +226,11 @@ public function testMultipleGenerationsUseCachedExtensionAllowList(): void { $decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class); $thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class); - $thumbnailUrlTemplate->expects(static::exactly(2)) + $thumbnailUrlTemplate->expects($this->exactly(2)) ->method('getUrl') ->willReturn('https://localhost/media/123.jpg?width=100'); $filesystem = $this->createMock(PrefixFilesystem::class); - $filesystem->expects(static::once()) + $filesystem->expects($this->once()) ->method('publicUrl') ->willReturn('https://localhost'); @@ -242,7 +242,7 @@ public function testMultipleGenerationsUseCachedExtensionAllowList(): void 'ExtensionsAllowList' => 'jpg', 'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}', ]; - $configReader->expects(static::exactly(3)) + $configReader->expects($this->exactly(3)) ->method('getConfig') ->willReturnCallback(function ($key) use ($defaultPluginConfig) { return $defaultPluginConfig[$key] ?? null; diff --git a/tests/unit/Core/Media/MediaUrlLoaderTest.php b/tests/unit/Core/Media/MediaUrlLoaderTest.php index eb3c508..5709a47 100644 --- a/tests/unit/Core/Media/MediaUrlLoaderTest.php +++ b/tests/unit/Core/Media/MediaUrlLoaderTest.php @@ -5,6 +5,7 @@ use Frosh\ThumbnailProcessor\Core\Media\MediaUrlGenerator; use Frosh\ThumbnailProcessor\Core\Media\MediaUrlLoader; use PHPUnit\Framework\TestCase; +use Shopware\Core\Framework\DataAbstractionLayer\Entity; use Shopware\Core\Framework\DataAbstractionLayer\PartialEntity; use Shopware\Core\Framework\Uuid\Uuid; @@ -15,7 +16,7 @@ public function testLoaded(): void $id = Uuid::randomHex(); $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::once()) + $mediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn([$id => 'https://example.com/a0/image.jpg']); @@ -40,7 +41,7 @@ public function testLoadedWithMissingPrivate(): void $id = Uuid::randomHex(); $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::never()) + $mediaUrlGenerator->expects($this->never()) ->method('generate') ->willReturn([$id => 'https://example.com/a0/image.jpg']); @@ -63,7 +64,7 @@ public function testLoadedWithMissingUrlResult(): void $id = Uuid::randomHex(); $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::once()) + $mediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn([]); @@ -87,7 +88,7 @@ public function testLoadedWithMissingPathResult(): void $id = Uuid::randomHex(); $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::never()) + $mediaUrlGenerator->expects($this->never()) ->method('generate'); $mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); @@ -107,7 +108,7 @@ public function testLoadedWithMissingPathResult(): void public function testLoadedWithThumbnailHavingUrls(): void { $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::once()) + $mediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn([ '1' => 'https://example.com/a0/image.jpg?width=100', @@ -134,6 +135,7 @@ public function testLoadedWithThumbnailHavingUrls(): void $thumbnails = $entity->get('thumbnails'); static::assertIsArray($thumbnails); + static::assertInstanceOf(Entity::class, $thumbnails[0]); static::assertFalse($thumbnails[0]->has('url')); $mediaUrlLoader->loaded([$entity]); @@ -142,6 +144,7 @@ public function testLoadedWithThumbnailHavingUrls(): void $thumbnails = $entity->get('thumbnails'); static::assertIsArray($thumbnails); + static::assertInstanceOf(Entity::class, $thumbnails[0]); static::assertTrue($thumbnails[0]->has('url')); static::assertSame('https://example.com/a0/thumbnailimage.jpg?width=100', $thumbnails[0]->get('url')); } @@ -149,7 +152,7 @@ public function testLoadedWithThumbnailHavingUrls(): void public function testLoadedWithThumbnailHavingMaxWidthWithMissingWidth(): void { $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::once()) + $mediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn([ '1' => 'https://example.com/a0/image.jpg?width=100', @@ -174,6 +177,7 @@ public function testLoadedWithThumbnailHavingMaxWidthWithMissingWidth(): void $thumbnails = $entity->get('thumbnails'); static::assertIsArray($thumbnails); + static::assertInstanceOf(Entity::class, $thumbnails[0]); static::assertFalse($thumbnails[0]->has('url')); $mediaUrlLoader->loaded([$entity]); @@ -182,6 +186,7 @@ public function testLoadedWithThumbnailHavingMaxWidthWithMissingWidth(): void $thumbnails = $entity->get('thumbnails'); static::assertIsArray($thumbnails); + static::assertInstanceOf(Entity::class, $thumbnails[0]); static::assertTrue($thumbnails[0]->has('url')); static::assertSame('https://example.com/a0/thumbnailimage.jpg?width=100', $thumbnails[0]->get('url')); } @@ -189,7 +194,7 @@ public function testLoadedWithThumbnailHavingMaxWidthWithMissingWidth(): void public function testLoadedWithThumbnailHavingNoUrl(): void { $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::once()) + $mediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn([ '1' => 'https://example.com/a0/image.jpg', @@ -213,6 +218,7 @@ public function testLoadedWithThumbnailHavingNoUrl(): void $thumbnails = $entity->get('thumbnails'); static::assertIsArray($thumbnails); + static::assertInstanceOf(Entity::class, $thumbnails[0]); static::assertFalse($thumbnails[0]->has('url')); $mediaUrlLoader->loaded([$entity]); @@ -221,6 +227,7 @@ public function testLoadedWithThumbnailHavingNoUrl(): void $thumbnails = $entity->get('thumbnails'); static::assertIsArray($thumbnails); + static::assertInstanceOf(Entity::class, $thumbnails[0]); static::assertFalse($thumbnails[0]->has('url')); } @@ -230,7 +237,7 @@ public function testLoadedWithThumbnailHavingNoUrl(): void public function testLoadedWithNotIterableThumbnail(mixed $thumbnail): void { $mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); - $mediaUrlGenerator->expects(static::once()) + $mediaUrlGenerator->expects($this->once()) ->method('generate') ->willReturn([ '1' => 'https://example.com/a0/image.jpg', diff --git a/tests/unit/Service/ThumbnailUrlTemplateTest.php b/tests/unit/Service/ThumbnailUrlTemplateTest.php index 80e7ba9..baffc63 100644 --- a/tests/unit/Service/ThumbnailUrlTemplateTest.php +++ b/tests/unit/Service/ThumbnailUrlTemplateTest.php @@ -15,14 +15,15 @@ class ThumbnailUrlTemplateTest extends TestCase public function testGetUrl(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $date): void { $configReader = $this->createMock(ConfigReader::class); - $configReader->expects(static::once()) + $configReader->expects($this->once()) ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&updatedAt={mediaUpdatedAt}&uff'); $class = new ThumbnailUrlTemplate($configReader); $url = $class->getUrl($mediaUrl, $mediaPath, $width, $date); + $timestamp = $date !== null ? $date->getTimestamp() : '0'; - static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s&uff', $mediaUrl, $mediaPath, $width, $date?->getTimestamp() ?: '0'), $url); + static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s&uff', $mediaUrl, $mediaPath, $width, $timestamp), $url); } /** @@ -31,14 +32,15 @@ public function testGetUrl(?string $salesChannelId, string $mediaUrl, string $me public function testGetUrlWithoutSetConfig(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $date): void { $configReader = $this->createMock(ConfigReader::class); - $configReader->expects(static::once()) + $configReader->expects($this->once()) ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&updatedAt={mediaUpdatedAt}'); $class = new ThumbnailUrlTemplate($configReader); $url = $class->getUrl($mediaUrl, $mediaPath, $width, $date); + $timestamp = $date !== null ? $date->getTimestamp() : '0'; - static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s', $mediaUrl, $mediaPath, $width, $date?->getTimestamp() ?: '0'), $url); + static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s', $mediaUrl, $mediaPath, $width, $timestamp), $url); } /** @@ -47,7 +49,7 @@ public function testGetUrlWithoutSetConfig(?string $salesChannelId, string $medi public function testGetUrlGetPatternOnce(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $date): void { $configReader = $this->createMock(ConfigReader::class); - $configReader->expects(static::once()) + $configReader->expects($this->once()) ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&updatedAt={mediaUpdatedAt}'); $class = new ThumbnailUrlTemplate($configReader); @@ -55,8 +57,9 @@ public function testGetUrlGetPatternOnce(?string $salesChannelId, string $mediaU $class->getUrl($mediaUrl, $mediaPath, $width, $date); $url = $class->getUrl($mediaUrl, $mediaPath, $width, $date); + $timestamp = $date !== null ? $date->getTimestamp() : '0'; - static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s', $mediaUrl, $mediaPath, $width, $date?->getTimestamp() ?: '0'), $url); + static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s', $mediaUrl, $mediaPath, $width, $timestamp), $url); } /**