FileUploadValidatorOneFileTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
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 100
rs 10
c 3
b 0
f 0

7 Methods

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