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

StringValidatorTest::stringProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 64
rs 9.3956
cc 1
eloc 50
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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