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() |
|
|
|
|
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
|
|
|
|
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: