-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileSizeValidatorTest.php
More file actions
75 lines (60 loc) · 2.15 KB
/
FileSizeValidatorTest.php
File metadata and controls
75 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Picamator\Tests\Unit\TransferObject\Shared\Validator;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Picamator\TransferObject\Shared\Environment\EnvironmentReaderInterface;
use Picamator\TransferObject\Shared\Validator\FileSizeValidator;
final class FileSizeValidatorTest extends TestCase
{
private const int MAX_FILE_SIZE_BYTES = 10;
private FileSizeValidator&MockObject $validatorMock;
protected function setUp(): void
{
$environmentReaderStub = $this->createStub(EnvironmentReaderInterface::class);
$environmentReaderStub
->method('getMaxFileSizeBytes')
->willReturn(self::MAX_FILE_SIZE_BYTES)
->seal();
$this->validatorMock = $this->getMockBuilder(FileSizeValidator::class)
->setConstructorArgs([$environmentReaderStub])
->onlyMethods(['filesize'])
->getMock();
}
#[TestDox('Unknown file size')]
public function testUnknownFileSize(): void
{
// Arrange
$path = '/some-path.txt';
// Expect
$this->validatorMock->expects($this->once())
->method('filesize')
->with($path)
->willReturn(false)
->seal();
// Act
$actual = $this->validatorMock->validate($path);
// Assert
$this->assertNotNull($actual);
$this->assertStringContainsString('Failed to get file size', $actual->errorMessage);
}
#[TestDox('Max file size exceeded')]
public function testMaxFileSizeExceeded(): void
{
// Arrange
$path = '/some-path.txt';
$fileSize = self::MAX_FILE_SIZE_BYTES + 1;
// Expect
$this->validatorMock->expects($this->once())
->method('filesize')
->with($path)
->willReturn($fileSize)
->seal();
// Act
$actual = $this->validatorMock->validate($path);
// Assert
$this->assertNotNull($actual);
$this->assertStringContainsString('exceeds the maximum allowed size', $actual->errorMessage);
}
}