Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions src/Shared/Environment/EnvironmentReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
{
public function getProjectRoot(): string
{
$projectRoot = $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT)
?: $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT_ALIAS);
$projectRoot = $this->getEnvironment(@EnvironmentEnum::PROJECT_ROOT)
?: $this->getEnvironment(@EnvironmentEnum::PROJECT_ROOT_ALIAS);

if ($projectRoot === '') {
return $this->getcwd() ?: '';
Expand All @@ -22,9 +22,24 @@ public function getProjectRoot(): string
return rtrim($projectRoot, '\/');
}

public function getMaxFileSizeMegabytes(): string
public function getMaxFileSizeMegabytes(): int
{
return $this->getEnvironment(EnvironmentEnum::MAX_FILE_SIZE_MB);
$environment = EnvironmentEnum::MAX_FILE_SIZE_MB;
$maxFileSize = (int)$this->getEnvironment($environment);

if ($maxFileSize === 0) {
$maxFileSize = (int)$environment->getDefault();
}

/** @var int $maxFileSize */
$maxFileSize = min($maxFileSize, 1024);

return $maxFileSize;
}

public function getMaxFileSizeBytes(): int
{
return $this->getMaxFileSizeMegabytes() * 1_000_000;
}

private function getEnvironment(EnvironmentEnum $environment): string
Expand Down
4 changes: 3 additions & 1 deletion src/Shared/Environment/EnvironmentReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ interface EnvironmentReaderInterface
{
public function getProjectRoot(): string;

public function getMaxFileSizeMegabytes(): string;
public function getMaxFileSizeMegabytes(): int;

public function getMaxFileSizeBytes(): int;
}
4 changes: 1 addition & 3 deletions src/Shared/Validator/FileSizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
File "%s" ("%d" bytes) exceeds the maximum allowed size of "%d" bytes. Please split the file into smaller parts.
TEMPLATE;

private const int MAX_FILE_SIZE_BYTE_MULTIPLIER = 1_000_000;

public function __construct(private EnvironmentReaderInterface $environmentReader)
{
}
Expand Down Expand Up @@ -49,7 +47,7 @@ public function validate(string $path): ?ValidatorMessageTransfer

private function getMaxFileSizeBytes(): int
{
return (int)$this->environmentReader->getMaxFileSizeMegabytes() * self::MAX_FILE_SIZE_BYTE_MULTIPLIER;
return $this->environmentReader->getMaxFileSizeBytes();
}

protected function filesize(string $path): int|false
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/Shared/Environment/EnvironmentReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,26 @@ public function testProjectRootVariableIsNotSetAndWorkingDirectoryIsFailed(): vo
// Assert
$this->assertSame($expected, $actual);
}

#[TestDox('Environment variable max file size megabytes "$maxSizeMegabytes" is set with "$expected"')]
#[TestWith([false, 10])]
#[TestWith(['', 10])]
#[TestWith(['0', 10])]
#[TestWith(['test', 10])]
#[TestWith(['20', 20])]
#[TestWith(['200000', 1024])]
public function testGetMaxFileSizeMegabytes(string|bool $maxSizeMegabytes, int $expected): void
{
// Expect
$this->readerMock->expects($this->once())
->method('getenv')
->willReturn($maxSizeMegabytes)
->seal();

// Act
$actual = $this->readerMock->getMaxFileSizeMegabytes();

// Assert
$this->assertSame($expected, $actual);
}
}
8 changes: 4 additions & 4 deletions tests/unit/Shared/Validator/FileSizeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

final class FileSizeValidatorTest extends TestCase
{
private const string MAX_FILE_SIZE_MEGABYTES = '1';
private const int MAX_FILE_SIZE_BYTES = 10;

private FileSizeValidator&MockObject $validatorMock;

protected function setUp(): void
{
$environmentReaderStub = $this->createStub(EnvironmentReaderInterface::class);
$environmentReaderStub
->method('getMaxFileSizeMegabytes')
->willReturn(self::MAX_FILE_SIZE_MEGABYTES)
->method('getMaxFileSizeBytes')
->willReturn(self::MAX_FILE_SIZE_BYTES)
->seal();

$this->validatorMock = $this->getMockBuilder(FileSizeValidator::class)
Expand Down Expand Up @@ -56,7 +56,7 @@ public function testMaxFileSizeExceeded(): void
{
// Arrange
$path = '/some-path.txt';
$fileSize = (int)self::MAX_FILE_SIZE_MEGABYTES * 1_000_000 + 10;
$fileSize = self::MAX_FILE_SIZE_BYTES + 1;

// Expect
$this->validatorMock->expects($this->once())
Expand Down
Loading