Completed
Push — master ( cb0578...28965e )
by Jeroen De
07:11 queued 04:27
created

tests/phpunit/Definitions/ParamDefinitionTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ParamProcessor\Tests\Definitions;
4
5
use ParamProcessor\Options;
6
use ParamProcessor\ParamDefinition;
7
use ParamProcessor\IParamDefinition;
8
use ParamProcessor\Param;
9
use ParamProcessor\ParamDefinitionFactory;
10
11
/**
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
abstract class ParamDefinitionTest extends \PHPUnit_Framework_TestCase {
16
17
	/**
18
	 * Returns a list of arrays that hold values to test handling of.
19
	 * Each array holds the following unnamed elements:
20
	 * - value (mixed, required)
21
	 * - valid (boolean, required)
22
	 * - expected (mixed, optional)
23
	 *
24
	 * ie array( '42', true, 42 )
25
	 *
26
	 * @since 0.1
27
	 *
28
	 * @param boolean $stringlyTyped
29
	 *
30
	 * @return array
31
	 */
32
	public abstract function valueProvider( $stringlyTyped = true );
0 ignored issues
show
The abstract declaration must precede the visibility declaration
Loading history...
33
34
	public abstract function getType();
0 ignored issues
show
The abstract declaration must precede the visibility declaration
Loading history...
35
36
	public function getDefinitions() {
37
		$params = [];
38
39
		$params['empty'] = [];
40
41
		$params['values'] = [
42
			'values' => [ 'foo', '1', '0.1', 'yes', 1, 0.1 ]
43
		];
44
45
		return $params;
46
	}
47
48
	public function definitionProvider() {
49
		$definitions = $this->getDefinitions();
50
51
		foreach ( $definitions as &$definition ) {
52
			$definition['type'] = $this->getType();
53
		}
54
55
		return $definitions;
56
	}
57
58
	public function getEmptyInstance() {
59
		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...
60
			'name' => 'empty',
61
			'message' => 'test-empty',
62
			'type' => $this->getType(),
63
		] );
64
	}
65
66
	public function instanceProvider() {
67
		$definitions = [];
68
69
		foreach ( $this->definitionProvider() as $name => $definition ) {
70
			if ( !array_key_exists( 'message', $definition ) ) {
71
				$definition['message'] = 'test-' . $name;
72
			}
73
74
			$definition['name'] = $name;
75
			$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...
76
		}
77
78
		return $definitions;
79
	}
80
81
	/**
82
	 * @dataProvider instanceProvider
83
	 */
84
	public function testGetType( IParamDefinition $definition )  {
85
		$this->assertEquals( $this->getType(), $definition->getType() );
86
	}
87
88
	/**
89
	 * @dataProvider instanceProvider
90
	 */
91
	public function testValidate( IParamDefinition $definition ) {
92
		foreach ( [ true, false ] as $stringlyTyped ) {
93
			$values = $this->valueProvider( $stringlyTyped );
94
			$options = new Options();
95
			$options->setRawStringInputs( $stringlyTyped );
96
97
			foreach ( $values[$definition->getName()] as $data ) {
98
				list( $input, $valid, ) = $data;
99
100
				$param = new Param( $definition );
101
				$param->setUserValue( $definition->getName(), $input, $options );
102
				$definitions = [];
103
				$param->process( $definitions, [], $options );
104
105
				$this->assertEquals(
106
					$valid,
107
					$param->getErrors() === [],
108
					'The validation process should ' . ( $valid ? '' : 'not ' ) . 'pass'
109
				);
110
			}
111
		}
112
113
		$this->assertTrue( true );
114
	}
115
116
	/**
117
	 * @dataProvider instanceProvider
118
	 */
119
	public function testFormat( IParamDefinition $sourceDefinition ) {
120
		$values = $this->valueProvider();
121
		$options = new Options();
122
123
		foreach ( $values[$sourceDefinition->getName()] as $data ) {
124
			$definition = clone $sourceDefinition;
125
126
			list( $input, $valid, ) = $data;
127
128
			$param = new Param( $definition );
129
			$param->setUserValue( $definition->getName(), $input, $options );
130
131
			if ( $valid && array_key_exists( 2, $data ) ) {
132
				$defs = [];
133
				$param->process( $defs, [], $options );
134
135
				$this->assertEquals(
136
					$data[2],
137
					$param->getValue()
138
				);
139
			}
140
		}
141
142
		$this->assertTrue( true );
143
	}
144
145
	protected function validate( ParamDefinition $definition, $testValue, $validity, Options $options = null ) {
146
		$def = clone $definition;
147
		$options = $options === null ? new Options() : $options;
148
149
		$param = new Param( $def );
150
		$param->setUserValue( $def->getName(), $testValue, $options );
151
152
		$defs = [];
153
		$param->process( $defs, [], $options );
154
155
		$this->assertEquals( $validity, $param->getErrors() === [] );
156
	}
157
158
}