FileUploadAttributeMultipleFileTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 118
rs 10
c 4
b 1
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A getValidator() 0 6 1
A itShouldCheckIfHasLength() 0 5 1
A itShouldCheckIfIsBetween() 0 4 1
A itShouldCheckIfIsMimeType() 0 14 1
A itShouldCheckIfHasFileNameFormat() 0 11 1
A itShouldCheckIfHasValidUploadDirectory() 0 13 1
A itShouldCheckIfNotOverwritingExistingFile() 0 15 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 9/24/14
5
 * Time: 5:01 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tests\NilPortugues\Validator\Attribute\FileUpload;
12
13
use NilPortugues\Validator\Validator;
14
15
/**
16
 * Class FileUploadAttributeMultipleFileTest
17
 * @package Tests\NilPortugues\Validator\Attribute\FileUploadAttribute
18
 */
19
class FileUploadAttributeMultipleFileTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     *
23
     */
24
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
        $_FILES = [
27
            'image' => [
28
                'name'     => ['sample.png', 'sample.png', 'sample.png'],
29
                'type'     => ['image/png', 'image/png', 'image/png'],
30
                'tmp_name' => [
31
                    \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf',
32
                    \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf',
33
                    \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf',
34
                ],
35
                'error'    => [0, 0, 0],
36
                'size'     => [203868, 203868, 203868],
37
            ],
38
        ];
39
    }
40
41
    /**
42
     * @return \NilPortugues\Validator\Attribute\FileUpload\FileUploadAttribute
43
     */
44
    private function getValidator()
45
    {
46
        $validator = Validator::create();
47
48
        return $validator->isFileUpload('image');
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function itShouldCheckIfHasLength()
55
    {
56
        $this->assertTrue($this->getValidator()->hasLength(3)->validate('image'));
57
        $this->assertFalse($this->getValidator()->hasLength(2)->validate('image'));
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function itShouldCheckIfIsBetween()
64
    {
65
        $this->assertTrue($this->getValidator()->isBetween(0, 2, 'MB', true)->validate('image'));
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function itShouldCheckIfIsMimeType()
72
    {
73
        $this->assertTrue(
74
            $this->getValidator()
75
                ->isMimeType(['image/png', 'image/gif', 'image/jpg'])
76
                ->validate('image')
77
        );
78
79
        $this->assertFalse(
80
            $this->getValidator()
81
                ->isMimeType(['image/bmp'])
82
                ->validate('image')
83
        );
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function itShouldCheckIfHasFileNameFormat()
0 ignored issues
show
Coding Style introduced by
itShouldCheckIfHasFileNameFormat uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
90
    {
91
        $validator       = Validator::create();
92
        $stringValidator = $validator->isString('image')->isLowercase();
93
94
        $this->assertTrue($this->getValidator()->hasFileNameFormat($stringValidator)->validate('image'));
95
96
        $stringValidator         = $validator->isString('image')->isAlphanumeric();
97
        $_FILES['image']['name'] = '@sample.png';
98
        $this->assertFalse($this->getValidator()->hasFileNameFormat($stringValidator)->validate('image'));
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function itShouldCheckIfHasValidUploadDirectory()
105
    {
106
        $this->assertTrue(
107
            $this->getValidator()
108
                ->hasValidUploadDirectory(\realpath(\dirname(__FILE__)).'/resources/')
109
                ->validate('image')
110
        );
111
        $this->assertFalse(
112
            $this->getValidator()
113
                ->hasValidUploadDirectory(\realpath(\dirname(__FILE__)).'/not/')
114
                ->validate('image')
115
        );
116
    }
117
118
    /**
119
     * @test
120
     */
121
    public function itShouldCheckIfNotOverwritingExistingFile()
0 ignored issues
show
Coding Style introduced by
itShouldCheckIfNotOverwritingExistingFile uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
122
    {
123
        $this->assertFalse(
124
            $this->getValidator()
125
                ->notOverwritingExistingFile(\realpath(\dirname(__FILE__)).'/resources')
126
                ->validate('image')
127
        );
128
129
        $_FILES['image']['name'] = 'a.png';
130
        $this->assertTrue(
131
            $this->getValidator()
132
                ->notOverwritingExistingFile(\realpath(\dirname(__FILE__)).'/resources')
133
                ->validate('image')
134
        );
135
    }
136
}
137