|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueValidators\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use ValueValidators\DimensionValidator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers ValueValidators\DimensionValidator |
|
10
|
|
|
* |
|
11
|
|
|
* @group ValueValidators |
|
12
|
|
|
* @group DataValueExtensions |
|
13
|
|
|
* |
|
14
|
|
|
* @licence GNU GPL v2+ |
|
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
16
|
|
|
*/ |
|
17
|
|
|
class DimensionValidatorTest extends PHPUnit_Framework_TestCase { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var DimensionValidator |
|
21
|
|
|
*/ |
|
22
|
|
|
private $validator; |
|
23
|
|
|
|
|
24
|
|
|
protected function setUp() { |
|
25
|
|
|
$this->validator = new DimensionValidator(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testWhenAutoIsNotAllowed_autoIsNotValid() { |
|
29
|
|
|
$this->validator->setAllowAuto( false ); |
|
30
|
|
|
$this->assertIsNotValid( 'auto' ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function assertIsNotValid( $value ) { |
|
34
|
|
|
$this->assertFalse( $this->validator->validate( $value )->isValid() ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testWhenAutoIsAllowed_autoIsValid() { |
|
38
|
|
|
$this->validator->setAllowAuto( true ); |
|
39
|
|
|
$this->assertIsValid( 'auto' ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function assertIsValid( $value ) { |
|
43
|
|
|
$this->assertTrue( $this->validator->validate( $value )->isValid() ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testUsingDefaultSettings_pxIsAllowed() { |
|
47
|
|
|
$this->assertIsValid( '100px' ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testUsingDefaultSettings_NoUnitIsAllowed() { |
|
51
|
|
|
$this->assertIsValid( '100' ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testUsingDefaultSettings_trailingNewlineIsInvalid() { |
|
55
|
|
|
$this->assertIsNotValid( "100\n" ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testGivenUpperBound_valueUnderIsValid() { |
|
59
|
|
|
$this->validator->setUpperBound( 100 ); |
|
60
|
|
|
$this->assertIsValid( '99' ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testGivenUpperBound_valueEqualIsValid() { |
|
64
|
|
|
$this->validator->setUpperBound( 100 ); |
|
65
|
|
|
$this->assertIsValid( '100' ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testGivenUpperBound_valueOverIsInvalid() { |
|
69
|
|
|
$this->validator->setUpperBound( 100 ); |
|
70
|
|
|
$this->assertIsNotValid( '101' ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testUsingDefaultSettings_percentageIsNotValid() { |
|
74
|
|
|
$this->assertIsNotValid( '50%' ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testWhenPercentageInUnitList_percentageValid() { |
|
78
|
|
|
$this->validator->setAllowedUnits( array( 'px', '%' ) ); |
|
79
|
|
|
$this->assertIsValid( '50%' ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testGivenLowerPercentageBound_valueOverIsValid() { |
|
83
|
|
|
$this->validator->setAllowedUnits( array( '%' ) ); |
|
84
|
|
|
$this->validator->setMinPercentage( 50 ); |
|
85
|
|
|
$this->assertIsValid( '51%' ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testGivenLowerPercentageBound_valueEqualIsValid() { |
|
89
|
|
|
$this->validator->setAllowedUnits( array( '%' ) ); |
|
90
|
|
|
$this->validator->setMinPercentage( 50 ); |
|
91
|
|
|
$this->assertIsValid( '50%' ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testGivenLowerPercentageBound_valueUnderIsNotValid() { |
|
95
|
|
|
$this->validator->setAllowedUnits( array( '%' ) ); |
|
96
|
|
|
$this->validator->setMinPercentage( 50 ); |
|
97
|
|
|
$this->assertIsNotValid( '49%' ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testInvalidValuesAreInvalid() { |
|
101
|
|
|
$this->assertIsNotValid( '' ); |
|
102
|
|
|
$this->assertIsNotValid( 'a' ); |
|
103
|
|
|
$this->assertIsNotValid( '1a' ); |
|
104
|
|
|
$this->assertIsNotValid( '1px2' ); |
|
105
|
|
|
$this->assertIsNotValid( 'a1px' ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|