Passed
Push — stringTitleTests ( 5b765d )
by no
02:13
created

StringValidatorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 82
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidate() 0 10 2
A stringProvider() 0 64 1
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 ? array() : array( $expectedError ),
30
			$result->getErrors()
31
		);
32
	}
33
34
	public function stringProvider() {
35
		return array(
36
			array(
37
				'value' => null,
38
				'options' => array(),
39
				'expectedErrors' => Error::newError( 'Not a string' )
40
			),
41
			array(
42
				'value' => '',
43
				'options' => array(),
44
				'expectedErrors' => null
45
			),
46
			array(
47
				'value' => '',
48
				'options' => array( 'length' => 1 ),
49
				'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' )
50
			),
51
			array(
52
				'value' => '1',
53
				'options' => array( 'length' => 1 ),
54
				'expectedErrors' => null
55
			),
56
			array(
57
				'value' => '1',
58
				'options' => array( 'length' => 0 ),
59
				'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' )
60
			),
61
			array(
62
				'value' => '',
63
				'options' => array( 'length' => 0 ),
64
				'expectedErrors' => null
65
			),
66
			array(
67
				'value' => '',
68
				'options' => array( 'minlength' => 1 ),
69
				'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' )
70
			),
71
			array(
72
				'value' => '1',
73
				'options' => array( 'minlength' => 1 ),
74
				'expectedErrors' => null
75
			),
76
			array(
77
				'value' => '1',
78
				'options' => array( 'maxlength' => 0 ),
79
				'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' )
80
			),
81
			array(
82
				'value' => '',
83
				'options' => array( 'maxlength' => 0 ),
84
				'expectedErrors' => null
85
			),
86
			array(
87
				'value' => '1',
88
				'options' => array( 'regex' => '/^$/' ),
89
				'expectedErrors' => Error::newError( 'String does not match the regular expression /^$/' )
90
			),
91
			array(
92
				'value' => '',
93
				'options' => array( 'regex' => '/^$/' ),
94
				'expectedErrors' => null
95
			),
96
		);
97
	}
98
99
}
100