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