Passed
Push — master ( 5e2fb7...f2aa59 )
by Marius
01:47
created

StringValidatorTest::testValidate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace ValueValidators\Tests;
4
5
use PHPUnit_Framework_TestCase;
6
use ValueValidators\Error;
7
use ValueValidators\StringValidator;
8
9
/**
10
 * @covers ValueValidators\StringValidator
11
 *
12
 * @group ValueValidators
13
 * @group DataValueExtensions
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Thiemo Mättig
17
 */
18
class StringValidatorTest extends PHPUnit_Framework_TestCase {
19
20
	/**
21
	 * @dataProvider stringProvider
22
	 */
23
	public function testValidate( $value, array $options, $expectedError ) {
24
		$validator = new StringValidator();
25
		$validator->setOptions( $options );
26
		$result = $validator->validate( $value );
27
28
		$this->assertEquals(
29
			$expectedError === null ? [] : [ $expectedError ],
30
			$result->getErrors()
31
		);
32
	}
33
34
	public function stringProvider() {
35
		return [
36
			[
37
				'value' => null,
38
				'options' => [],
39
				'expectedErrors' => Error::newError( 'Not a string' )
40
			],
41
			[
42
				'value' => '',
43
				'options' => [],
44
				'expectedErrors' => null
45
			],
46
			[
47
				'value' => '',
48
				'options' => [ 'length' => 1 ],
49
				'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' )
50
			],
51
			[
52
				'value' => '1',
53
				'options' => [ 'length' => 1 ],
54
				'expectedErrors' => null
55
			],
56
			[
57
				'value' => '1',
58
				'options' => [ 'length' => 0 ],
59
				'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' )
60
			],
61
			[
62
				'value' => '',
63
				'options' => [ 'length' => 0 ],
64
				'expectedErrors' => null
65
			],
66
			[
67
				'value' => '',
68
				'options' => [ 'minlength' => 1 ],
69
				'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' )
70
			],
71
			[
72
				'value' => '1',
73
				'options' => [ 'minlength' => 1 ],
74
				'expectedErrors' => null
75
			],
76
			[
77
				'value' => '1',
78
				'options' => [ 'maxlength' => 0 ],
79
				'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' )
80
			],
81
			[
82
				'value' => '',
83
				'options' => [ 'maxlength' => 0 ],
84
				'expectedErrors' => null
85
			],
86
			[
87
				'value' => '1',
88
				'options' => [ 'regex' => '/^$/' ],
89
				'expectedErrors' => Error::newError( 'String does not match the regular expression /^$/' )
90
			],
91
			[
92
				'value' => '',
93
				'options' => [ 'regex' => '/^$/' ],
94
				'expectedErrors' => null
95
			],
96
		];
97
	}
98
99
}
100