getPublicFilesystem example


            $this->context
        );
        $media = $this->mediaRepository->search(new Criteria([$mediaId])$this->context)->get($mediaId);
        static::assertInstanceOf(MediaEntity::class$media);

        $mediaPath = $this->getContainer()->get(UrlGeneratorInterface::class)->getRelativeMediaUrl($media);

        $resource = fopen(self::FIXTURE_FILE, 'rb');
        static::assertNotFalse($resource);

        $this->getPublicFilesystem()->writeStream($mediaPath$resource);

        $this->mediaRepository->delete([['id' => $mediaId]]$this->context);

        $this->runWorker();

        static::assertFalse($this->getPublicFilesystem()->has($mediaPath));
    }

    public function testDeleteMediaEntityWithThumbnails(): void
    {
        $mediaId = Uuid::randomHex();

        
$context
            );
        } finally {
            if (file_exists($tempFile)) {
                unlink($tempFile);
            }
        }

        $media = $this->mediaRepository->search(new Criteria([$mediaId])$context)->get($mediaId);
        static::assertInstanceOf(MediaEntity::class$media);
        $path = $this->urlGenerator->getRelativeMediaUrl($media);
        static::assertTrue($this->getPublicFilesystem()->has($path));
    }

    public function testPersistFileWithUpperCaseExtension(): void
    {
        $tempFile = tempnam(sys_get_temp_dir(), '');
        static::assertIsString($tempFile);
        copy(self::TEST_IMAGE, $tempFile);

        $fileSize = filesize($tempFile);
        static::assertIsInt($fileSize);
        $mediaFile = new MediaFile($tempFile, 'image/png', 'PNG', $fileSize);

        
$txt = $this->getTxt();
        $png = $this->getPng();
        $withProduct = $this->getMediaWithProduct();
        $withManufacturer = $this->getMediaWithManufacturer();

        $urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
        $firstPath = $urlGenerator->getRelativeMediaUrl($txt);
        $secondPath = $urlGenerator->getRelativeMediaUrl($png);
        $thirdPath = $urlGenerator->getRelativeMediaUrl($withProduct);
        $fourthPath = $urlGenerator->getRelativeMediaUrl($withManufacturer);

        $this->getPublicFilesystem()->writeStream($firstPathfopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($secondPathfopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($thirdPathfopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($fourthPathfopen(self::FIXTURE_FILE, 'rb'));

        $this->deleteMediaService->deleteNotUsedMedia($this->context);
        $this->runWorker();

        $result = $this->mediaRepo->search(
            new Criteria([
                $txt->getId(),
                $png->getId(),
                

            [],
            [
                'HTTP_CONTENT-TYPE' => 'image/png',
                'HTTP_CONTENT-LENGTH' => filesize(self::TEST_IMAGE),
            ],
            (string) file_get_contents(self::TEST_IMAGE)
        );
        $media = $this->getMediaEntity();

        $mediaPath = $this->urlGenerator->getRelativeMediaUrl($media);
        static::assertTrue($this->getPublicFilesystem()->has($mediaPath));
        static::assertStringEndsWith($media->getId() . '.' . $media->getFileExtension()$mediaPath);

        $this->assertMediaApiResponse();
    }

    public function testUploadFromBinaryUsesFileName(): void
    {
        $dispatcher = $this->getContainer()->get('event_dispatcher');
        $listener = $this->getMockBuilder(CallableClass::class)->getMock();
        $listener->expects(static::once())->method('__invoke');
        $this->addEventListener($dispatcher, MediaUploadedEvent::class$listener);

        

class DefaultMediaResolverTest extends TestCase
{
    use IntegrationTestBehaviour;

    private DefaultMediaResolver $mediaResolver;

    private FilesystemOperator $publicFilesystem;

    protected function setUp(): void
    {
        $this->publicFilesystem = $this->getPublicFilesystem();
        $this->mediaResolver = new DefaultMediaResolver($this->publicFilesystem);
    }

    public function testGetDefaultMediaEntityWithoutValidFileName(): void
    {
        $media = $this->mediaResolver->getDefaultCmsMediaEntity('this/file/does/not/exists');

        static::assertNull($media);
    }

    public function testGetDefaultMediaEntityWithValidFileName(): void
    {

    private $handler;

    protected function setUp(): void
    {
        $this->handler = $this->getContainer()->get(DeleteFileHandler::class);
    }

    public function testItHandlesDeletes(): void
    {
        $filesystem = $this->getPublicFilesystem();

        $file1 = 'test/file1.txt';
        $file2 = 'test/file2.txt';

        $filesystem->write($file1, 'file 1 content');
        $filesystem->write($file2, 'file 2 content');

        $deleteMsg = new DeleteFileMessage();
        $deleteMsg->setFiles([$file1$file2]);

        $this->handler->__invoke($deleteMsg);

        
$txt = $this->getTxt();
        $png = $this->getPng();
        $withProduct = $this->getMediaWithProduct();
        $withManufacturer = $this->getMediaWithManufacturer();

        $urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
        $firstPath = $urlGenerator->getRelativeMediaUrl($txt);
        $secondPath = $urlGenerator->getRelativeMediaUrl($png);
        $thirdPath = $urlGenerator->getRelativeMediaUrl($withProduct);
        $fourthPath = $urlGenerator->getRelativeMediaUrl($withManufacturer);

        $this->getPublicFilesystem()->writeStream($firstPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($secondPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($thirdPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($fourthPath, \fopen(self::FIXTURE_FILE, 'rb'));

        $this->unusedMediaPurger->deleteNotUsedMedia();
        $this->runWorker();

        $result = $this->mediaRepo->search(
            new Criteria([
                $txt->getId(),
                $png->getId(),
                

    use IntegrationTestBehaviour;

    private const FIXTURES_DIRECTORY = '/../../fixtures/';

    private ImageCmsElementResolver $imageResolver;

    private FilesystemOperator $publicFilesystem;

    protected function setUp(): void
    {
        $this->publicFilesystem = $this->getPublicFilesystem();
        $this->imageResolver = new ImageCmsElementResolver(new DefaultMediaResolver($this->publicFilesystem));
    }

    public function testType(): void
    {
        static::assertSame('image', $this->imageResolver->getType());
    }

    public function testCollectWithEmptyConfig(): void
    {
        $resolverContext = new ResolverContext($this->createMock(SalesChannelContext::class)new Request());

        

            ],
            $this->context
        );
        $media = $this->mediaRepository->search(new Criteria([$mediaId])$this->context)->get($mediaId);
        static::assertInstanceOf(MediaEntity::class$media);

        $mediaPath = $this->getContainer()->get(UrlGeneratorInterface::class)->getRelativeMediaUrl($media);

        $file = fopen(self::FIXTURE_FILE, 'rb');
        static::assertIsResource($file);
        $this->getPublicFilesystem()->writeStream($mediaPath$file);

        $this->folderRepository->delete([['id' => $folderId]]$this->context);

        static::assertEquals(0, $this->folderRepository->search(new Criteria([$folderId])$this->context)->getTotal());
        static::assertEquals(0, $this->mediaRepository->search(new Criteria([$mediaId])$this->context)->getTotal());

        $this->runWorker();

        static::assertFalse($this->getPublicFilesystem()->has($mediaPath));
    }

    

            [
                'mediaId' => $media->getId(),
                'width' => 150,
                'height' => 150,
            ],
        ]$this->context);

        /** @var MediaEntity $media */
        $media = $this->mediaRepository->search(new Criteria([$media->getId()])$this->context)->get($media->getId());

        $this->getPublicFilesystem()->writeStream(
            $this->urlGenerator->getRelativeMediaUrl($media),
            fopen(__DIR__ . '/../fixtures/shopware-logo.png', 'rb')
        );

        $msg = new GenerateThumbnailsMessage();
        $msg->setMediaIds([$media->getId()]);

        if (Feature::isActive('v6.6.0.0')) {
            $msg->setContext($this->context);
        } else {
            $msg->withContext($this->context);
        }
static::assertEquals($expectedMessageNonStrict$envelopes[1]->getMessage());
        static::assertEquals($expectedMessageNonStrict$envelopes[2]->getMessage());
        static::assertEquals($expectedMessageStrict$envelopes[3]->getMessage());
    }

    protected function assertThumbnailExists(MediaEntity $media, MediaThumbnailEntity $thumbnail): void
    {
        $thumbnailPath = $this->urlGenerator->getRelativeThumbnailUrl(
            $media,
            $thumbnail
        );
        static::assertTrue($this->getPublicFilesystem()->has($thumbnailPath));
    }

    protected function createValidMediaFiles(): void
    {
        $this->setFixtureContext($this->context);
        $mediaPng = $this->getPngWithFolder();
        $mediaJpg = $this->getJpgWithFolder();

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaPng);
        $this->getPublicFilesystem()->writeStream(
            $filePath,
            


    public function testThumbnailGeneration(): void
    {
        $this->setFixtureContext($this->context);
        $media = $this->getPngWithFolder();

        $filePath = $this->urlGenerator->getRelativeMediaUrl($media);
        $resource = fopen(__DIR__ . '/../fixtures/shopware-logo.png', 'rb');

        \assert($resource !== false);
        $this->getPublicFilesystem()->writeStream($filePath$resource);

        $this->thumbnailService->updateThumbnails(
            $media,
            $this->context,
            false
        );

        $this->runWorker();

        $searchCriteria = new Criteria();
        $searchCriteria->setLimit(1);
        
/** * @internal */
class FilesystemBehaviourTest extends TestCase
{
    use FilesystemBehaviour;
    use KernelTestBehaviour;

    public function testWrittenFilesGetDeleted(): void
    {
        $this->getPublicFilesystem()
            ->write('testFile', 'testContent');

        $this->getPublicFilesystem()
            ->write('public/testFile', 'testContent');

        static::assertNotEmpty($this->getPublicFilesystem()->listContents('', true)->toArray());
    }

    /** * @depends testWrittenFilesGetDeleted */
    

class ImageSliderTypeDataResolverTest extends TestCase
{
    use IntegrationTestBehaviour;

    private ImageSliderTypeDataResolver $imageSliderResolver;

    private FilesystemOperator $publicFilesystem;

    protected function setUp(): void
    {
        $this->publicFilesystem = $this->getPublicFilesystem();
        $this->imageSliderResolver = new ImageSliderTypeDataResolver(new DefaultMediaResolver($this->publicFilesystem));
    }

    public function testType(): void
    {
        static::assertSame('image-slider', $this->imageSliderResolver->getType());
    }

    public function testCollectWithEmptyConfig(): void
    {
        $resolverContext = new ResolverContext($this->createMock(SalesChannelContext::class)new Request());

        

                'id' => $mediaJpg->getId(),
                'type' => null,
            ],
            [
                'id' => $mediaPdf->getId(),
                'type' => null,
            ],
        ]$this->context);

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaPng);
        $this->getPublicFilesystem()->writeStream(
            $filePath,
            fopen(__DIR__ . '/../fixtures/shopware-logo.png', 'rb')
        );

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaJpg);
        $this->getPublicFilesystem()->writeStream(
            $filePath,
            fopen(__DIR__ . '/../fixtures/shopware.jpg', 'rb')
        );

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaPdf);
        
Home | Imprint | This part of the site doesn't use cookies.