FileUploadValidatorMultipleFileTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 89
rs 10
c 3
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A itShouldCheckIfHasLength() 0 5 1
A itShouldCheckIfIsBetween() 0 4 1
A itShouldCheckIfIsMimeType() 0 5 1
A itShouldCheckIfHasFileNameFormat() 0 11 1
A itShouldCheckIfHasValidUploadDirectory() 0 7 1
A itShouldCheckIfNotOverwritingExistingFile() 0 11 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\Validation\FileUpload;
12
13
use NilPortugues\Validator\Validation\FileUpload\FileUploadValidation;
14
use NilPortugues\Validator\Validator;
15
16
class FileUploadValidatorMultipleFileTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     *
20
     */
21
    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...
22
    {
23
        $_FILES = [
24
            'image' => [
25
                'name'     => ['sample.png', 'sample.png', 'sample.png'],
26
                'type'     => ['image/png', 'image/png', 'image/png'],
27
                'tmp_name' => [
28
                    \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf',
29
                    \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf',
30
                    \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf',
31
                ],
32
                'error'    => [],
33
                'size'     => [203868, 203868, 203868],
34
            ],
35
        ];
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function itShouldCheckIfHasLength()
42
    {
43
        $this->assertTrue(FileUploadValidation::hasLength('image', 3));
44
        $this->assertFalse(FileUploadValidation::hasLength('image', 2));
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function itShouldCheckIfIsBetween()
51
    {
52
        $this->assertTrue(FileUploadValidation::isBetween('image', 0, 2, 'MB', true));
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function itShouldCheckIfIsMimeType()
59
    {
60
        $this->assertTrue(FileUploadValidation::isMimeType('image', ['image/png', 'image/gif', 'image/jpg']));
61
        $this->assertFalse(FileUploadValidation::isMimeType('image', ['image/bmp']));
62
    }
63
64
    /**
65
     * @test
66
     */
67
    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...
68
    {
69
        $validator       = Validator::create();
70
        $stringValidator = $validator->isString('image')->isLowercase();
71
72
        $this->assertTrue(FileUploadValidation::hasFileNameFormat('image', $stringValidator));
73
74
        $stringValidator         = $validator->isString('image')->isAlphanumeric();
75
        $_FILES['image']['name'] = '@sample.png';
76
        $this->assertFalse(FileUploadValidation::hasFileNameFormat('image', $stringValidator));
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function itShouldCheckIfHasValidUploadDirectory()
83
    {
84
        $this->assertTrue(
85
            FileUploadValidation::hasValidUploadDirectory('image', \realpath(\dirname(__FILE__)).'/resources/')
86
        );
87
        $this->assertFalse(FileUploadValidation::hasValidUploadDirectory('image', \realpath(\dirname(__FILE__)).'/not/'));
88
    }
89
90
    /**
91
     * @test
92
     */
93
    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...
94
    {
95
        $this->assertFalse(
96
            FileUploadValidation::notOverwritingExistingFile('image', \realpath(\dirname(__FILE__)).'/resources')
97
        );
98
99
        $_FILES['image']['name'] = 'a.png';
100
        $this->assertTrue(
101
            FileUploadValidation::notOverwritingExistingFile('image', \realpath(\dirname(__FILE__)).'/resources')
102
        );
103
    }
104
}
105