createMedia example


    public function createPropertyOption($data)
    {
        $option = new PropertyOption();
        $option->setId($data['id']);
        $option->setName($data['name']);
        $option->setPosition((int) $data['position']);

        if ($data['media']) {
            $option->setMedia($this->createMedia($data['media']));
        }
        $option->addAttributes($this->createAttributes($data['attributes']));

        return $option;
    }

    /** * @param array $data * * @return Media */
    
static::markTestSkipped('JSON_OVERLAPS() function not supported on this database');
        }

        parent::setUp();

        $this->mediaRepository = $this->getContainer()->get('media.repository');
        $this->customFieldSetRepository = $this->getContainer()->get('custom_field_set.repository');
    }

    public function testMediaIdsAreNotRemovedWhenMediaIsNotReferenced(): void
    {
        $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());
    }

    
class UnusedMediaPurgerTest extends TestCase
{
    public function testGetNotUsedMediaOnlyAppliesValidAssociationsToCriteria(): void
    {
        $this->configureRegistry([
            'Media' => $mediaDefinition = $this->getMediaDefinition([]),
        ]);

        $id1 = Uuid::randomHex();
        $id2 = Uuid::randomHex();

        $media1 = $this->createMedia($id1);
        $media2 = $this->createMedia($id2);

        /** @var StaticEntityRepository<MediaCollection> $repo */
        $repo = new StaticEntityRepository(
            [
                function DCriteria $criteria, Context $context) use ($id1$id2) {
                    $filters = $criteria->getFilters();

                    self::assertCount(0, $filters);

                    return [$id1$id2];
                },
$commandTester->assertCommandIsSuccessful();
        static::assertStringContainsString('Are you sure that you want to delete unused media files?', $commandTester->getDisplay());
        static::assertStringContainsString('Successfully deleted 2 media files.', $commandTester->getDisplay());
    }

    public function testDryRunPrintsOutFilesToBeDeletedButDoesNotPerformDelete(): void
    {
        $service = $this->createMock(UnusedMediaPurger::class);

        $result = function D): \Generator {
            yield [$this->createMedia('File 1')];
            yield [$this->createMedia('File 2')];
        };

        $service->expects(static::once())
            ->method('getNotUsedMedia')
            ->willReturnCallback($result);

        $service->expects(static::never())
            ->method('deleteNotUsedMedia');

        $command = new DeleteNotUsedMediaCommand($service$this->createMock(Connection::class));

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