DownloadService example


    /** * @dataProvider dataProviderInvalidAccessToken */
    public function testInvalidAccessToken(ImportExportFileEntity $fileEntity, string $accessToken): void
    {
        static::expectException(InvalidFileAccessTokenException::class);
        static::expectExceptionMessage('Access to file denied due to invalid access token');
        /** @var StaticEntityRepository<EntityCollection<ImportExportFileEntity>> $fileRepository */
        $fileRepository = new StaticEntityRepository([new EntityCollection([$fileEntity])]);

        $downloadService = new DownloadService($this->createMock(FilesystemOperator::class)$fileRepository);
        $downloadService->createFileResponse(Context::createDefaultContext()$fileEntity->getId()$accessToken);
    }

    /** * @dataProvider dataProviderNotFoundFile */
    public function testNotFoundFile(ImportExportFileEntity $fileEntity, string $accessToken, string $fileId): void
    {
        static::expectException(FileNotFoundException::class);
        static::expectExceptionMessage(sprintf('Cannot find import/export file with id %s', $fileId));

        
$fileData = [
            'id' => Uuid::randomHex(),
            'originalName' => $asciiName . ' öäüß',
            'path' => 'test.csv',
            'expireDate' => new \DateTime(),
        ];
        $filesystem->write($fileData['path']$fileData['originalName']);
        $context = Context::createDefaultContext();
        $fileRepository->create([$fileData]$context);

        $downloadService = new DownloadService($filesystem$fileRepository);
        $accessToken = $downloadService->regenerateToken($context$fileData['id']);

        $response = $downloadService->createFileResponse($context$fileData['id']$accessToken);
        static::assertIsString($header = $response->headers->get('Content-Disposition'));
        static::assertStringContainsString($asciiName$header);

        $response->sendContent();
        $this->expectOutputString($fileData['originalName']);
    }

    public function testSlashFilename(): void
    {
Home | Imprint | This part of the site doesn't use cookies.