Passed
Push — master ( 761e95...f1e6ba )
by Jeroen De
05:40
created

ParamDefinitionTest::getDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ParamProcessor\Tests\Integration\Definitions;
4
5
use ParamProcessor\IParamDefinition;
6
use ParamProcessor\Options;
7
use ParamProcessor\PackagePrivate\Param;
8
use ParamProcessor\ParamDefinition;
9
use ParamProcessor\ParamDefinitionFactory;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
abstract class ParamDefinitionTest extends TestCase {
17
18
	/**
19
	 * Returns a list of arrays that hold values to test handling of.
20
	 * Each array holds the following unnamed elements:
21
	 * - value (mixed, required)
22
	 * - valid (boolean, required)
23
	 * - expected (mixed, optional)
24
	 *
25
	 * ie array( '42', true, 42 )
26
	 *
27
	 * @since 0.1
28
	 *
29
	 * @param boolean $stringlyTyped
30
	 *
31
	 * @return array
32
	 */
33
	public abstract function valueProvider( $stringlyTyped = true );
34
35
	public abstract function getType();
36
37
	public function getDefinitions() {
38
		$params = [];
39
40
		$params['empty'] = [];
41
42
		$params['values'] = [
43
			'values' => [ 'foo', '1', '0.1', 'yes', 1, 0.1 ]
44
		];
45
46
		return $params;
47
	}
48
49
	public function definitionProvider() {
50
		$definitions = $this->getDefinitions();
51
52
		foreach ( $definitions as &$definition ) {
53
			$definition['type'] = $this->getType();
54
		}
55
56
		return $definitions;
57
	}
58
59
	public function getEmptyInstance() {
60
		return ParamDefinitionFactory::singleton()->newDefinitionFromArray( [
0 ignored issues
show
Deprecated Code introduced by
The method ParamProcessor\ParamDefinitionFactory::singleton() has been deprecated with message: since 1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
61
			'name' => 'empty',
62
			'message' => 'test-empty',
63
			'type' => $this->getType(),
64
		] );
65
	}
66
67
	public function instanceProvider() {
68
		$definitions = [];
69
70
		foreach ( $this->definitionProvider() as $name => $definition ) {
71
			if ( !array_key_exists( 'message', $definition ) ) {
72
				$definition['message'] = 'test-' . $name;
73
			}
74
75
			$definition['name'] = $name;
76
			$definitions[] = [ ParamDefinitionFactory::singleton()->newDefinitionFromArray( $definition ) ];
0 ignored issues
show
Deprecated Code introduced by
The method ParamProcessor\ParamDefinitionFactory::singleton() has been deprecated with message: since 1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77
		}
78
79
		return $definitions;
80
	}
81
82
	/**
83
	 * @dataProvider instanceProvider
84
	 */
85
	public function testGetType( IParamDefinition $definition ) {
86
		$this->assertEquals( $this->getType(), $definition->getType() );
87
	}
88
89
	/**
90
	 * @dataProvider instanceProvider
91
	 */
92
	public function testValidate( IParamDefinition $definition ) {
93
		foreach ( [ true, false ] as $stringlyTyped ) {
94
			$values = $this->valueProvider( $stringlyTyped );
95
			$options = new Options();
96
			$options->setRawStringInputs( $stringlyTyped );
97
98
			foreach ( $values[$definition->getName()] as $data ) {
99
				list( $input, $valid, ) = $data;
100
101
				$param = new Param( $definition );
102
				$param->setUserValue( $definition->getName(), $input, $options );
103
				$definitions = [];
104
				$param->process( $definitions, [], $options );
105
106
				$this->assertEquals(
107
					$valid,
108
					$param->getErrors() === [],
109
					'The validation process should ' . ( $valid ? '' : 'not ' ) . 'pass'
110
				);
111
			}
112
		}
113
114
		$this->assertTrue( true );
115
	}
116
117
	/**
118
	 * @dataProvider instanceProvider
119
	 */
120
	public function testFormat( IParamDefinition $sourceDefinition ) {
121
		$values = $this->valueProvider();
122
		$options = new Options();
123
124
		foreach ( $values[$sourceDefinition->getName()] as $data ) {
125
			$definition = clone $sourceDefinition;
126
127
			list( $input, $valid, ) = $data;
128
129
			$param = new Param( $definition );
130
			$param->setUserValue( $definition->getName(), $input, $options );
131
132
			if ( $valid && array_key_exists( 2, $data ) ) {
133
				$defs = [];
134
				$param->process( $defs, [], $options );
135
136
				$this->assertEquals(
137
					$data[2],
138
					$param->getValue()
139
				);
140
			}
141
		}
142
143
		$this->assertTrue( true );
144
	}
145
146
	protected function validate( ParamDefinition $definition, $testValue, $validity, Options $options = null ) {
147
		$def = clone $definition;
148
		$options = $options === null ? new Options() : $options;
149
150
		$param = new Param( $def );
151
		$param->setUserValue( $def->getName(), $testValue, $options );
152
153
		$defs = [];
154
		$param->process( $defs, [], $options );
155
156
		$this->assertEquals( $validity, $param->getErrors() === [] );
157
	}
158
159
	public function testConstructingWithoutMessageLeadsToDefaultMessage() {
160
		$this->assertSame(
161
			'validator-message-nodesc',
162
			( new ParamDefinition( 'type', 'name' ) )->getMessage()
163
		);
164
	}
165
166
}