Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Oct 25, 2023
1 parent 002fce6 commit 47c1013
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
16 changes: 8 additions & 8 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function moveFile(File $file): void
$path = $this->getPathname($file);

try {
$stream = ErrorHandler::wrap(static fn () => fopen($splFileInfo->getPathname(), 'r'));
$stream = ErrorHandler::wrap(static fn() => fopen($splFileInfo->getPathname(), 'r'));
} catch (ErrorException $exception) {
throw FileException::unableToMove($file, $exception);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public function read(File $file): string
$pathname = $this->getPathname($file);
if ($this->uploadFileMap->has($file)) {
try {
return ErrorHandler::wrap(static fn (): string => file_get_contents($pathname));
return ErrorHandler::wrap(static fn(): string => file_get_contents($pathname));
} catch (ErrorException $exception) {
throw FileException::unableToRead($file, $exception);
}
Expand All @@ -214,7 +214,7 @@ public function readStream(File $file)
$pathname = $this->getPathname($file);
if ($this->uploadFileMap->has($file)) {
try {
return ErrorHandler::wrap(static fn () => fopen($pathname, 'rb'));
return ErrorHandler::wrap(static fn() => fopen($pathname, 'rb'));
} catch (ErrorException $exception) {
throw FileException::unableToRead($file, $exception);
}
Expand All @@ -238,14 +238,14 @@ public function write(MutableFile $file, SplFileInfo $splFileInfo): void
$pathname = $this->getPathname($file);
if ($this->uploadFileMap->has($file)) {
try {
ErrorHandler::wrap(static fn (): bool => copy($splFileInfo->getRealPath(), $pathname));
ErrorHandler::wrap(static fn(): bool => copy($splFileInfo->getRealPath(), $pathname));
} catch (ErrorException $exception) {
throw FileException::unableToWrite($file, $exception);
}
clearstatcache(true, $pathname);
} else {
try {
$stream = ErrorHandler::wrap(static fn () => fopen($splFileInfo->getRealPath(), 'r'));
$stream = ErrorHandler::wrap(static fn() => fopen($splFileInfo->getRealPath(), 'r'));
} catch (ErrorException $exception) {
throw FileException::unableToWrite($file, $exception);
}
Expand Down Expand Up @@ -298,7 +298,7 @@ private function handleSplFile(SplFileInfo $file): SplFileInfo
$remoteFile = $file->openFile();
}

$tempFilename = ErrorHandler::wrap(fn (): string => tempnam($this->temporaryDirectory, 'file_manager'));
$tempFilename = ErrorHandler::wrap(fn(): string => tempnam($this->temporaryDirectory, 'file_manager'));
$file = new SplFileObject($tempFilename, 'r+');
while ($content = $remoteFile->fread(self::CHUNK_SIZE)) {
$file->fwrite($content);
Expand All @@ -314,7 +314,7 @@ private function handleSplFile(SplFileInfo $file): SplFileInfo
*/
private function hashFile(SplFileInfo $file): string
{
return ErrorHandler::wrap(fn (): string => hash_file($this->hashingAlgorithm, $file->getRealPath()));
return ErrorHandler::wrap(fn(): string => hash_file($this->hashingAlgorithm, $file->getRealPath()));
}

/**
Expand All @@ -324,7 +324,7 @@ private function getMimeTypeByFile(SplFileInfo $file): string
{
$mimeType = $this->mimeTypeDetector->detectMimeTypeFromFile($file->getRealPath());
if ($mimeType === null) {
throw new InvalidArgumentException('Failed to detect mimeType for ' . $file->getRealPath());
throw new InvalidArgumentException('Failed to detect mimeType for '.$file->getRealPath());
}

return $mimeType;
Expand Down
14 changes: 7 additions & 7 deletions tests/Validator/Constraint/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ class FileTest extends TestCase
{
public function testNormalizeBytes(): void
{
$file = new File(['maxSize' => 1000]);
$file = new File(maxSize: 1000);

self::assertSame(1000, $file->maxSize);
}

public function testNormalizeKb(): void
{
$file = new File(['maxSize' => '1k']);
$file = new File(maxSize: '1k');

self::assertSame(1000, $file->maxSize);
}

public function testNormalizeMb(): void
{
$file = new File(['maxSize' => '1M']);
$file = new File(maxSize: '1M');

self::assertSame(1000000, $file->maxSize);
}

public function testNormalizeKi(): void
{
$file = new File(['maxSize' => '1Ki']);
$file = new File(maxSize: '1Ki');

self::assertSame(1024, $file->maxSize);
}

public function testNormalizeMi(): void
{
$file = new File(['maxSize' => '1Mi']);
$file = new File(maxSize: '1Mi');

self::assertSame(1048576, $file->maxSize);
}
Expand All @@ -51,12 +51,12 @@ public function testInvalid(): void
$this->expectExceptionMessage('"1 gigabyte" is not a valid maximum size.');
$this->expectExceptionCode(0);

new File(['maxSize' => '1 gigabyte']);
new File(maxSize: '1 gigabyte');
}

public function testSingleMimeType(): void
{
$file = new File(['mimeTypes' => 'image/jpg']);
$file = new File(mimeTypes: 'image/jpg');

self::assertCount(1, $file->mimeTypes);
self::assertSame('image/jpg', $file->mimeTypes[0]);
Expand Down
38 changes: 10 additions & 28 deletions tests/Validator/Constraint/FileValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public function testNotValidValue(): void
$this->validator->validate(
new stdClass(),
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'maxSize' => 10000,
]
maxSize: 10000,
)
);
}
Expand All @@ -52,9 +50,7 @@ public function testNull(): void
$this->validator->validate(
null,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'maxSize' => 10000,
]
maxSize: 10000,
)
);
self::assertNoViolation();
Expand All @@ -67,9 +63,7 @@ public function testInvalidSize(): void
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'maxSize' => 100,
]
maxSize: 100,
)
);

Expand All @@ -88,9 +82,7 @@ public function testValidExactSize(): void
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'maxSize' => 1000,
]
maxSize: 1000,
)
);
self::assertNoViolation();
Expand All @@ -103,9 +95,7 @@ public function testValidSize(): void
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'maxSize' => 10000,
]
maxSize: 10000,
)
);
self::assertNoViolation();
Expand All @@ -118,9 +108,7 @@ public function testInvalidMimeType(): void
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'mimeTypes' => ['image/*', 'application/pdf'],
]
mimeTypes: ['image/*', 'application/pdf'],
)
);

Expand All @@ -139,19 +127,15 @@ public function testValidMimeType(): void
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'mimeTypes' => ['image/*', 'application/pdf'],
]
mimeTypes: ['image/*', 'application/pdf'],
)
);

$file = new File('filename', 1000, '12345', 'application/pdf');
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'mimeTypes' => ['image/*', 'application/pdf'],
]
mimeTypes: ['image/*', 'application/pdf'],
)
);

Expand All @@ -165,10 +149,8 @@ public function testInvalidSizeAndMimeType(): void
$this->validator->validate(
$file,
new \Arxy\FilesBundle\Validator\Constraint\File(
[
'maxSize' => 1000,
'mimeTypes' => ['image/*', 'application/pdf'],
]
maxSize: 1000,
mimeTypes: ['image/*', 'application/pdf'],
)
);

Expand Down

0 comments on commit 47c1013

Please sign in to comment.