ValidatorFunctionMapTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A itShouldInvalidArgumentExceptionWhenFunctionNoInMap() 0 18 1
A itShouldThrowExceptionWhenErrorMessageDoesNotExist() 0 18 1
B itShouldFileUploadException() 0 27 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 9/18/14
5
 * Time: 8:57 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;
12
13
use NilPortugues\Validator\ValidatorFunctionMap;
14
15
/**
16
 * Class ValidatorFunctionMapTest
17
 * @package Tests\NilPortugues\Validator
18
 */
19
class ValidatorFunctionMapTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @test
23
     */
24
    public function itShouldInvalidArgumentExceptionWhenFunctionNoInMap()
25
    {
26
        $abstractValidator = $this
27
            ->getMockBuilder('NilPortugues\Validator\AbstractValidator')
28
            ->disableOriginalConstructor()
29
            ->getMock();
30
31
        $functionMapArray = [];
32
        $functionMap      = ValidatorFunctionMap::getInstance($abstractValidator, $functionMapArray);
33
        $errors           = [];
34
        $errorValues      = [];
35
36
        $this->setExpectedException(
37
            '\InvalidArgumentException',
38
            'Validator key \'a-function-name-that-doesnt-exist\' not found'
39
        );
40
        $functionMap->get('property', 'a-function-name-that-doesnt-exist', [], $errorValues, $errors);
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function itShouldThrowExceptionWhenErrorMessageDoesNotExist()
47
    {
48
        $abstractValidator = $this
49
            ->getMockBuilder('NilPortugues\Validator\AbstractValidator')
50
            ->disableOriginalConstructor()
51
            ->getMock();
52
53
        $functionMapArray = ['String::isAlpha' => '\NilPortugues\Validator\Validation\String\StringValidation::isAlpha'];
54
        $functionMap      = ValidatorFunctionMap::getInstance($abstractValidator, $functionMapArray);
55
        $errors           = [];
56
        $values           = ['@'];
57
58
        $this->setExpectedException(
59
            '\InvalidArgumentException',
60
            'Validator key \'string.is_alpha\' not found in error file'
61
        );
62
        $functionMap->get('property', 'StringAttribute::isAlpha', $values, $values, $errors);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function itShouldFileUploadException()
0 ignored issues
show
Coding Style introduced by
itShouldFileUploadException 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...
69
    {
70
        $_FILES = [
71
            'image' => [
72
                'name'     => 'sample.png',
73
                'type'     => 'image/png',
74
                'tmp_name' => \realpath(\dirname(__FILE__)).'/Traits/FileUpload/resources/phpGpKMlf',
75
                'error'    => 1,
76
                'size'     => '200003868',
77
            ],
78
        ];
79
80
        $abstractValidator = $this
81
            ->getMockBuilder('NilPortugues\Validator\AbstractValidator')
82
            ->disableOriginalConstructor()
83
            ->getMock();
84
85
        $functionMapArray = [
86
            'FileUploadAttribute::isBetween' => '\Tests\NilPortugues\Validator\Resources\DummyFileUpload::isBetween',
87
        ];
88
89
        $functionMap =  ValidatorFunctionMap::getInstance($abstractValidator, $functionMapArray);
90
        $values      = ['image', 0, 2, 'MB', true];
91
        $errors      = ['FileUpload::UPLOAD_ERR_INI_SIZE' => 'error message'];
92
93
        $this->assertFalse($functionMap->get('image', 'FileUploadAttribute::isBetween', $values, $values, $errors));
94
    }
95
}
96