|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 9/18/14 |
|
5
|
|
|
* Time: 8:47 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\Numeric; |
|
12
|
|
|
|
|
13
|
|
|
use NilPortugues\Validator\Validator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class IntegerAttributeTest |
|
17
|
|
|
* @package Tests\NilPortugues\Validator\Attribute\Numeric |
|
18
|
|
|
*/ |
|
19
|
|
|
class IntegerAttributeTest extends \PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @return \NilPortugues\Validator\Attribute\Numeric\IntegerAttribute |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function getValidator() |
|
25
|
|
|
{ |
|
26
|
|
|
$validator = Validator::create(); |
|
27
|
|
|
|
|
28
|
|
|
return $validator->isInteger('propertyName'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
*/ |
|
34
|
|
|
public function itShouldCheckItIsInteger() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->assertTrue($this->getValidator()->validate(1)); |
|
37
|
|
|
$this->assertFalse($this->getValidator()->validate('a')); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
*/ |
|
43
|
|
|
public function itShouldCheckItIsNotZero() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->assertTrue($this->getValidator()->isNotZero()->validate(1)); |
|
46
|
|
|
$this->assertFalse($this->getValidator()->isNotZero()->validate(0)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
*/ |
|
52
|
|
|
public function itShouldCheckItIsPositive() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->assertTrue($this->getValidator()->isPositive()->validate(1)); |
|
55
|
|
|
$this->assertFalse($this->getValidator()->isPositive()->validate(-10)); |
|
56
|
|
|
$this->assertFalse($this->getValidator()->isPositive()->validate(0)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function itShouldCheckItIsPositiveOrZero() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertTrue($this->getValidator()->isPositiveOrZero()->validate(1)); |
|
65
|
|
|
$this->assertTrue($this->getValidator()->isPositiveOrZero()->validate(0)); |
|
66
|
|
|
$this->assertFalse($this->getValidator()->isPositiveOrZero()->validate(-10)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @test |
|
71
|
|
|
*/ |
|
72
|
|
|
public function itShouldCheckItIsNegative() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->assertTrue($this->getValidator()->isNegative()->validate(-10)); |
|
75
|
|
|
$this->assertFalse($this->getValidator()->isNegative()->validate(1)); |
|
76
|
|
|
$this->assertFalse($this->getValidator()->isNegative()->validate(0)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function itShouldCheckItIsNegativeOrZero() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertTrue($this->getValidator()->isNegativeOrZero()->validate(-10)); |
|
85
|
|
|
$this->assertTrue($this->getValidator()->isNegativeOrZero()->validate(0)); |
|
86
|
|
|
$this->assertFalse($this->getValidator()->isNegativeOrZero()->validate(1)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @test |
|
91
|
|
|
*/ |
|
92
|
|
|
public function itShouldCheckItIsBetween() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->assertTrue($this->getValidator()->isBetween(10, 20, false)->validate(13)); |
|
95
|
|
|
$this->assertTrue($this->getValidator()->isBetween(10, 20, true)->validate(10)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @test |
|
100
|
|
|
*/ |
|
101
|
|
|
public function itShouldCheckIsBetweenException() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->setExpectedException('\InvalidArgumentException'); |
|
104
|
|
|
$this->getValidator()->isBetween(20, 10, false)->validate(13); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @test |
|
109
|
|
|
*/ |
|
110
|
|
|
public function itShouldCheckIsOdd() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->assertTrue($this->getValidator()->isOdd()->validate(3)); |
|
113
|
|
|
$this->assertFalse($this->getValidator()->isOdd()->validate(2)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @test |
|
118
|
|
|
*/ |
|
119
|
|
|
public function itShouldCheckIsEven() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->assertTrue($this->getValidator()->isEven()->validate(2)); |
|
122
|
|
|
$this->assertFalse($this->getValidator()->isEven()->validate(3)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @test |
|
127
|
|
|
*/ |
|
128
|
|
|
public function itShouldCheckIsMultiple() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->assertTrue($this->getValidator()->isMultiple(2)->validate(4)); |
|
131
|
|
|
$this->assertFalse($this->getValidator()->isMultiple(2)->validate(3)); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|