Skip to content

Commit

Permalink
Refactor tests for DownloadUtility.
Browse files Browse the repository at this point in the history
Add typehints.
  • Loading branch information
Warxcell committed May 29, 2021
1 parent 1cd8490 commit 7c05a12
Show file tree
Hide file tree
Showing 31 changed files with 208 additions and 193 deletions.
10 changes: 5 additions & 5 deletions src/EventListener/DoctrineORMListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public function postPersist(LifecycleEventArgs $eventArgs): void
}

public function preRemove(LifecycleEventArgs $eventArgs): void
{
$this->postRemove($eventArgs);
}

public function postRemove(LifecycleEventArgs $eventArgs): void
{
$entity = $eventArgs->getEntity();
$entityManager = $eventArgs->getEntityManager();
Expand All @@ -86,11 +91,6 @@ public function preRemove(LifecycleEventArgs $eventArgs): void
$this->handleEmbeddable($entityManager, $entity, $this->remove);
}

public function postRemove(LifecycleEventArgs $eventArgs): void
{
$this->preRemove($eventArgs);
}

public function onClear(): void
{
$this->manager->clear();
Expand Down
32 changes: 14 additions & 18 deletions src/Utility/DownloadUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Arxy\FilesBundle\ManagerInterface;
use Arxy\FilesBundle\Model\File;
use Arxy\FilesBundle\Model\MutableFile;
use DateTimeImmutable;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand All @@ -24,26 +23,9 @@ public function __construct(ManagerInterface $manager)
$this->manager = $manager;
}

private function createDisposition(File $file): string
{
if ($file instanceof DownloadableFile) {
return HeaderUtils::makeDisposition(
$file->isForceDownload() ? HeaderUtils::DISPOSITION_ATTACHMENT : HeaderUtils::DISPOSITION_INLINE,
$file->getName() ?? u($file->getOriginalFilename())->ascii()->toString()
);
} else {
return HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
u($file->getOriginalFilename())->ascii()->toString()
);
}
}

public function createResponse(File $file): StreamedResponse
{
$response = new StreamedResponse();
$disposition = $this->createDisposition($file);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', $file->getMimeType());
$response->setPublic();
$response->setEtag($file->getHash());
Expand All @@ -52,12 +34,26 @@ public function createResponse(File $file): StreamedResponse
$expireAt = $file->getExpireAt();
$response->setExpires($expireAt);
$response->setLastModified($file->getModifiedAt());

$contentDisposition = HeaderUtils::makeDisposition(
$file->isForceDownload() ? HeaderUtils::DISPOSITION_ATTACHMENT : HeaderUtils::DISPOSITION_INLINE,
$file->getName() ?? u($file->getOriginalFilename())->ascii()->toString()
);
} else {
$expireAt = new DateTimeImmutable("+30 days");
$response->setExpires($expireAt);
$response->setLastModified($file->getCreatedAt());

$contentDisposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
u($file->getOriginalFilename())->ascii()->toString()
);
}

$response->headers->set('Content-Length', $file->getSize());

$response->headers->set('Content-Disposition', $contentDisposition);

$stream = $this->manager->readStream($file);
$response->setCallback(
static function () use ($stream): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/DownloadableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Arxy\FilesBundle\Utility;

use Arxy\FilesBundle\Entity\MutableFile;
use Arxy\FilesBundle\Model\DecoratedFile;
use Arxy\FilesBundle\Model\File;
use Arxy\FilesBundle\Model\MutableFile;
use DateTimeImmutable;
use DateTimeInterface;

Expand Down
4 changes: 2 additions & 2 deletions tests/AbstractModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class AbstractModelFactoryTest extends TestCase
{
public function testCreate()
public function testCreate(): void
{
$factory = new AbstractModelFactory(File::class);
$file = $factory->create(new \SplFileInfo(__DIR__.'/files/image1.jpg'), 'name', 12345, 'hash', 'mimeType');
Expand All @@ -21,7 +21,7 @@ public function testCreate()
self::assertSame('mimeType', $file->getMimeType());
}

public function testInvalidClass()
public function testInvalidClass(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/MigrateNamingStrategyCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function setUp(): void
$this->command = new MigrateNamingStrategyCommand($this->migrator, $this->repository);
}

public function testExecute()
public function testExecute(): void
{
$file1 = new File('filename', 125, '098f6bcd4621d373cade4e832627b4f6', 'image/jpeg');
$file1->setId(1);
Expand Down
32 changes: 16 additions & 16 deletions tests/DelegatingManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ protected function setUp(): void
);
}

public function testZeroManagers()
public function testZeroManagers(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You should pass at least one manager!');

new DelegatingManager([]);
}

public function testRead()
public function testRead(): void
{
$file1 = new File('original_filename.jpg', 125, '098f6bcd4621d373cade4e832627b4f6', 'image/jpeg');
$file2 = new File2('original_filename.jpg', 125, '098f6bcd4621d373cade4e832627b4f6', 'image/jpeg');
Expand All @@ -55,7 +55,7 @@ public function testRead()
self::assertSame('manager2Read', $this->manager->read($file2));
}

public function testReadStream()
public function testReadStream(): void
{
$file1 = new File('original_filename.jpg', 125, '098f6bcd4621d373cade4e832627b4f6', 'image/jpeg');
$file2 = new File2('original_filename.jpg', 125, '098f6bcd4621d373cade4e832627b4f6', 'image/jpeg');
Expand All @@ -67,7 +67,7 @@ public function testReadStream()
self::assertSame('manager2Read', $this->manager->readStream($file2));
}

public function testGetPathname()
public function testGetPathname(): void
{
$file1 = new File('original_filename.jpg', 125, '1234567', 'image/jpeg');
$file1->setId(1);
Expand All @@ -92,7 +92,7 @@ public function testGetPathname()
);
}

public function testWrite()
public function testWrite(): void
{
$file1 = new File('original_filename.jpg', 125, '1234567', 'image/jpeg');

Expand All @@ -101,7 +101,7 @@ public function testWrite()
$this->manager->write($file1, 'test');
}

public function testWriteStream()
public function testWriteStream(): void
{
$file1 = new File('original_filename.jpg', 125, '1234567', 'image/jpeg');
$stream = fopen('data://text/plain,test', 'r');
Expand All @@ -111,61 +111,61 @@ public function testWriteStream()
$this->manager->writeStream($file1, $stream);
}

public function testNoManagerForFileRead()
public function testNoManagerForFileRead(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->read(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testNoManagerForFileReadStream()
public function testNoManagerForFileReadStream(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->readStream(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testNoManagerForFileWrite()
public function testNoManagerForFileWrite(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->readStream(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testNoManagerForFileWriteStream()
public function testNoManagerForFileWriteStream(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->readStream(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testNoManagerForFilePathname()
public function testNoManagerForFilePathname(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->getPathname(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testNoManagerForFileMove()
public function testNoManagerForFileMove(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->moveFile(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testNoManagerForRemove()
public function testNoManagerForRemove(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No manager for Arxy\FilesBundle\Tests\File3');
$this->manager->remove(new File3('original_filename.jpg', 125, '1234567', 'image/jpeg'));
}

public function testGetClass()
public function testGetClass(): void
{
self::assertEquals($this->manager1->getClass(), $this->manager->getClass());
}

public function testMainManager()
public function testMainManager(): void
{
$forUpload = new \SplFileInfo(__DIR__.'/files/image1.jpg');
$uploadedFile = new File2('original_filename.jpg', 125, '1234567', 'image/jpeg');
Expand All @@ -186,7 +186,7 @@ public function testMainManager()
self::assertSame($uploadedFile, $actualFile);
}

public function testClear()
public function testClear(): void
{
$manager1 = $this->createMock(ManagerInterface::class);
$manager1->expects($this->once())->method('clear');
Expand Down
9 changes: 4 additions & 5 deletions tests/Form/Type/FileTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ protected function setUp(): void
parent::setUp();
}

protected function getExtensions()
protected function getExtensions(): array
{
$type = new FileType($this->manager);

return [
new PreloadedExtension([$type], []),

];
}

protected function getTypeExtensions()
protected function getTypeExtensions(): array
{
return [
new FormTypeHttpFoundationExtension(),
];
}

public function testSingleUpload()
public function testSingleUpload(): void
{
$uploadedFile = new UploadedFile(__DIR__.'/../../files/image1.jpg', 'image1.jpg');

Expand All @@ -61,7 +60,7 @@ public function testSingleUpload()
self::assertSame($file, $actual);
}

public function testMultipleUpload()
public function testMultipleUpload(): void
{
$uploadedFile1 = new UploadedFile(__DIR__.'/../../files/image1.jpg', 'image1.jpg');
$uploadedFile2 = new UploadedFile(__DIR__.'/../../files/image2.jpg', 'image2.jpg');
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/LiipImagine/FileFilterPathResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setUp(): void
$this->entityManager->flush();
}

public function testFilter()
public function testFilter(): void
{
$pathResolver = self::$container->get(FileFilterPathResolver::class);
assert($pathResolver instanceof FileFilterPathResolver);
Expand All @@ -46,7 +46,7 @@ public function testFilter()
);
}

public function testInvalidInstancePassed()
public function testInvalidInstancePassed(): void
{
$pathResolver = self::$container->get(FileFilterPathResolver::class);
assert($pathResolver instanceof FileFilterPathResolver);
Expand Down
6 changes: 3 additions & 3 deletions tests/Functional/Preview/PreviewGeneratorMessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected static function getBundles(): array
return [];
}

public function testImagePreview()
public function testImagePreview(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/image1.jpg'));
assert($file instanceof FileWithPreview);
Expand All @@ -45,7 +45,7 @@ public function testImagePreview()
// self::assertSame('image1_preview.jpg', $file->getPreview()->getOriginalFilename());
}

public function testPreviewWrite()
public function testPreviewWrite(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/image1.jpg'));
assert($file instanceof FileWithPreview);
Expand All @@ -61,7 +61,7 @@ public function testPreviewWrite()
self::assertNotSame($preview1, $file->getPreview());
}

public function testPreviewWriteStream()
public function testPreviewWriteStream(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/image1.jpg'));
assert($file instanceof FileWithPreview);
Expand Down
8 changes: 4 additions & 4 deletions tests/Functional/Preview/PreviewGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected static function getBundles(): array
return [];
}

public function testImagePreview()
public function testImagePreview(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/image1.jpg'));
assert($file instanceof FileWithPreview);
Expand All @@ -45,7 +45,7 @@ public function testImagePreview()
// self::assertSame('image1_preview.jpg', $file->getPreview()->getOriginalFilename());
}

public function testPreviewWrite()
public function testPreviewWrite(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/image1.jpg'));
assert($file instanceof FileWithPreview);
Expand All @@ -61,7 +61,7 @@ public function testPreviewWrite()
self::assertNotSame($preview1, $file->getPreview());
}

public function testPreviewWriteStream()
public function testPreviewWriteStream(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/image1.jpg'));
assert($file instanceof FileWithPreview);
Expand All @@ -79,7 +79,7 @@ public function testPreviewWriteStream()
self::assertNotSame($preview1, $file->getPreview());
}

public function testNotSupportedFile()
public function testNotSupportedFile(): void
{
$file = $this->manager->upload(new SplFileInfo(__DIR__.'/../../files/lorem-ipsum.pdf'));
assert($file instanceof FileWithPreview);
Expand Down
Loading

0 comments on commit 7c05a12

Please sign in to comment.