xref: /web-bugs/tests/Unit/Utils/UploaderTest.php (revision f762db34)
1<?php declare(strict_types=1);
2
3namespace App\Tests\Unit\Utils;
4
5use PHPUnit\Framework\MockObject\MockObject;
6use PHPUnit\Framework\TestCase;
7use App\Utils\Uploader;
8
9class UploaderTest extends TestCase
10{
11    /** @var string */
12    private $fixturesDirectory = TEST_FIXTURES_DIRECTORY . '/files';
13
14    /**
15     * @dataProvider filesProvider
16     */
17    public function testUpload(string $validExtension, array $file): void
18    {
19        $_FILES = ['uploaded' => $file];
20
21        /** @var Uploader|MockObject $uploader */
22        $uploader = $this->getMockBuilder(Uploader::class)
23            ->setMethods(['isUploadedFile', 'moveUploadedFile'])
24            ->getMock();
25
26        $uploader->expects($this->once())
27                 ->method('isUploadedFile')
28                 ->will($this->returnValue(true));
29
30        $uploader->expects($this->once())
31                 ->method('moveUploadedFile')
32                 ->will($this->returnValue(true));
33
34        $uploader->setMaxFileSize(100 * 1024);
35        $uploader->setValidExtension($validExtension);
36        $uploader->setDir(TEST_VAR_DIRECTORY . '/uploads');
37        $tmpFile = $uploader->upload('uploaded');
38
39        $this->assertNotNull($tmpFile);
40    }
41
42    public function filesProvider(): array
43    {
44        return [
45            [
46                'txt',
47                [
48                    'name'     => 'patch.txt',
49                    'tmp_name' => $this->fixturesDirectory . '/patch.txt',
50                    'size'     => filesize($this->fixturesDirectory . '/patch.txt'),
51                    'error'    => UPLOAD_ERR_OK,
52                ]
53            ],
54        ];
55    }
56}
57