Completed
Push — master ( 476384...cf7d6b )
by Taosikai
15:12
created

Tests/UploaderTest.php (4 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Slince\Upload\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Upload\FileInfo;
6
use Slince\Upload\Rule\SizeRule;
7
use Slince\Upload\Uploader;
8
9
class UploaderTest extends TestCase
10
{
11
    public function testGetter()
12
    {
13
        $uploader = new Uploader();
14
        $this->assertFalse($uploader->getOverride());
15
        $this->assertFalse($uploader->getIsRandName());
0 ignored issues
show
Deprecated Code introduced by
The method Slince\Upload\Uploader::getIsRandName() has been deprecated with message: Use "isRandName" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
16
        $this->assertFalse($uploader->isRandName());
17
18
        $uploader->setSavePath('./dst');
19
        $uploader->setOverride(true);
20
        $uploader->setRandName(true);
21
        $uploader->setIsRandName(true);
0 ignored issues
show
Deprecated Code introduced by
The method Slince\Upload\Uploader::setIsRandName() has been deprecated with message: Use "setRandName" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
        $this->assertEquals('./dst' . DIRECTORY_SEPARATOR, $uploader->getSavePath());
23
        $this->assertTrue($uploader->getOverride());
24
        $this->assertTrue($uploader->getIsRandName());
0 ignored issues
show
Deprecated Code introduced by
The method Slince\Upload\Uploader::getIsRandName() has been deprecated with message: Use "isRandName" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
25
        $this->assertTrue($uploader->isRandName());
26
    }
27
28
    public function testGetFilenameGenerator()
29
    {
30
        $uploader = new Uploader();
31
        $callback = function (FileInfo $fileInfo) {
32
            return '/dst/' . $fileInfo->getOriginName();
33
        };
34
35
        $this->assertFalse($callback === $uploader->getFilenameGenerator());
36
        $uploader->setFilenameGenerator($callback);
37
        $this->assertEquals($callback, $uploader->getFilenameGenerator());
38
    }
39
40
    public function testAddRule()
41
    {
42
        $uploader = new Uploader();
43
        $this->assertCount(1, $uploader->getRules());
44
        $uploader->addRule(new SizeRule(1000, 2000));
45
        $this->assertCount(2, $uploader->getRules());
46
    }
47
48
    public function testGenerateRandFilename()
49
    {
50
        $uploader = $this->createUploaderMock();
51
        $uploader->setSavePath(__DIR__ . '/Fixtures/dst');
52
        $uploader->setRandName(true);
53
        $uploader->setIsRandName(true);
0 ignored issues
show
Deprecated Code introduced by
The method Slince\Upload\Uploader::setIsRandName() has been deprecated with message: Use "setRandName" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
        $fileArray = [
55
            'name' => 'thumb.zip',
56
            'type' => 'application/x-zip-compressed',
57
            'tmp_name' => __DIR__ . '/Fixtures/foo.zip',
58
            'error' => 0,
59
            'size' => 105190,
60
        ];
61
        $fileInfo = $uploader->process($fileArray);
62
        $this->assertFalse($fileInfo->hasError());
63
        $this->assertNotEmpty($fileInfo->getPath());
64
        $this->assertNotContains($fileInfo->getOriginName(), $fileInfo->getPath());
65
    }
66
67
    public function testGenerateOriginFilename()
68
    {
69
        $uploader = $this->createUploaderMock();
70
        $uploader->setSavePath(__DIR__ . '/Fixtures/dst');
71
        $fileArray = [
72
            'name' => 'thumb.zip',
73
            'type' => 'application/x-zip-compressed',
74
            'tmp_name' => __DIR__ . '/Fixtures/foo.zip',
75
            'error' => 0,
76
            'size' => 105190,
77
        ];
78
        $fileInfo = $uploader->process($fileArray);
79
        $this->assertFalse($fileInfo->hasError());
80
        $this->assertContains($fileInfo->getOriginName(), $fileInfo->getPath());
81
    }
82
83
    public function testGenerateCustomFilename()
84
    {
85
        $uploader = $this->createUploaderMock();
86
        $uploader->setSavePath(__DIR__ . '/Fixtures/dst');
87
88
        $fileArray = [
89
            'name' => 'thumb.zip',
90
            'type' => 'application/x-zip-compressed',
91
            'tmp_name' => __DIR__ . '/Fixtures/foo.zip',
92
            'error' => 0,
93
            'size' => 105190,
94
        ];
95
96
        $uploader->setFilenameGenerator(function (FileInfo $fileInfo) {
97
            return 'foo-bar.ext';
98
        });
99
100
        $fileInfo = $uploader->process($fileArray);
101
        $this->assertFalse($fileInfo->hasError());
102
        $this->assertContains('foo-bar.ext', $fileInfo->getPath());
103
    }
104
105
    /**
106
     * @return Uploader
107
     */
108
    protected function createUploaderMock()
109
    {
110
        $mock =  $this->getMockBuilder(Uploader::class)
111
            ->setMethods(['moveUploadedFile'])
112
            ->getMock();
113
        $mock->expects($this->any())
114
            ->method('moveUploadedFile')
115
            ->will($this->returnValue(true));
116
        return $mock;
117
    }
118
}
119