getUnusedIds example

/** * @internal * * @covers \Shopware\Core\Content\Media\Event\UnusedMediaSearchEvent */
#[Package('buyers-experience')] class UnusedMediaSearchEventTest extends TestCase
{
    public function testGetIds(): void
    {
        $event = new UnusedMediaSearchEvent(['1', '2', '3']);
        static::assertEquals(['1', '2', '3']$event->getUnusedIds());
    }

    /** * @dataProvider removeIdsProvider * * @param array<string> $idsToRemove * @param array<string> $expectedIds */
    public function testRemoveIds(array $idsToRemove, array $expectedIds): void
    {
        $event = new UnusedMediaSearchEvent(['1', '2', '3']);
        
$themeService = $this->createMock(ThemeService::class);
        $themeService->expects(static::exactly(2))
            ->method('getThemeConfiguration')
            ->willReturnCallback(function Dstring $themeId, ...$params) use ($themeConfigMap) {
                return $themeConfigMap[$themeId];
            });

        $event = new UnusedMediaSearchEvent([$mediaId1$mediaId2$mediaId3$mediaId4$mediaId5]);
        $listener = new UnusedMediaSubscriber($themeRepository$themeService);
        $listener->removeUsedMedia($event);

        static::assertEquals([$mediaId4$mediaId5]$event->getUnusedIds());
    }
}
$statements[] = "SELECT JSON_UNQUOTE(JSON_EXTRACT({$table}.custom_fields, '$.{$field}')) as media_id FROM `{$table}` WHERE JSON_UNQUOTE(JSON_EXTRACT(`{$table}`.custom_fields, '$.{$field}')) IN (?)";
            }
        }

        if (\count($statements) === 0) {
            return;
        }

        foreach ($statements as $statement) {
            $usedMediaIds = $this->connection->fetchFirstColumn(
                $statement,
                [$event->getUnusedIds()],
                [ArrayParameterType::STRING]
            );

            $event->markAsUsed($usedMediaIds);
        }
    }

    /** * @return list<array{id: string, name: string, entity_name: string}> */
    private function findCustomFieldsWithEntitySelect(string $componentType): array
    {

        }
        $this->mediaRepository->create($media, Context::createDefaultContext());

        $mediaIds = array_values($ids->all());

        $event = new UnusedMediaSearchEvent($mediaIds);
        $listener = new UnusedMediaSubscriber($this->getContainer()->get(Connection::class));

        $listener->removeUsedMedia($event);

        static::assertSame($mediaIds$event->getUnusedIds());
    }

    public function testMediaIdsFromAllPossibleLocationsAreRemovedFromEvent(): void
    {
        $mediaIds = $this->createContent();
        $event = new UnusedMediaSearchEvent($mediaIds);
        $listener = new UnusedMediaSubscriber($this->getContainer()->get(Connection::class));

        $listener->removeUsedMedia($event);

        static::assertEmpty($event->getUnusedIds());
    }
SQL;

        return $this->executeQueryWithIds($sql$event);
    }

    /** * @return array<string> */
    private function executeQueryWithIds(string $sql, UnusedMediaSearchEvent $event): array
    {
        $result = $this->connection->fetchFirstColumn(
            sprintf($sqlimplode(',', array_map(fn (string $id) => sprintf('"%s"', $id)$event->getUnusedIds())))
        );

        // json_decode each row and flatten the result to an array of ids         return array_merge(
            ...array_map(fn (string $ids) => json_decode($ids, true, \JSON_THROW_ON_ERROR)$result)
        );
    }
}
/** * @param array<string> $ids * * @return array<string> */
    private function dispatchEvent(array $ids): array
    {
        $event = new UnusedMediaSearchEvent(array_values($ids));
        $this->eventDispatcher->dispatch($event);

        return $event->getUnusedIds();
    }

    /** * Here we attempt to exclude entity associations that are extending the behaviour of the media entity rather than * referencing media. For example, `MediaThumbnailDefinition` adds thumbnail support, whereas `ProductMediaDefinition` * adds support for images to products. */
    private function isInsideTopLevelDomain(string $domain, EntityDefinition $definition): bool
    {
        if ($definition->getParentDefinition() === null) {
            return false;
        }

        $mediaIds = array_values($this->createMedia(10)->all());

        $event = new UnusedMediaSearchEvent($mediaIds);
        $listener = new CustomFieldsUnusedMediaSubscriber(
            $this->getContainer()->get(Connection::class),
            $this->getContainer()->get(DefinitionInstanceRegistry::class)
        );

        $listener->removeUsedMedia($event);

        static::assertSame($mediaIds$event->getUnusedIds());
    }

    public function testMediaIdsFromAllPossibleLocationsAreRemovedFromEvent(): void
    {
        $mediaIds = $this->createContent();
        $event = new UnusedMediaSearchEvent($mediaIds);
        $listener = new CustomFieldsUnusedMediaSubscriber(
            $this->getContainer()->get(Connection::class),
            $this->getContainer()->get(DefinitionInstanceRegistry::class)
        );
        $listener->removeUsedMedia($event);

        
Home | Imprint | This part of the site doesn't use cookies.