summaryrefslogtreecommitdiff
path: root/www/wiki/vendor/param-processor/param-processor/tests/Integration/Definitions/ParamDefinitionTest.php
blob: 64ac9004239b7a33ca263e254e62443431180257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

namespace ParamProcessor\Tests\Integration\Definitions;

use ParamProcessor\IParamDefinition;
use ParamProcessor\Options;
use ParamProcessor\PackagePrivate\Param;
use ParamProcessor\ParamDefinition;
use ParamProcessor\ParamDefinitionFactory;
use PHPUnit\Framework\TestCase;

/**
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
abstract class ParamDefinitionTest extends TestCase {

	/**
	 * Returns a list of arrays that hold values to test handling of.
	 * Each array holds the following unnamed elements:
	 * - value (mixed, required)
	 * - valid (boolean, required)
	 * - expected (mixed, optional)
	 *
	 * ie array( '42', true, 42 )
	 *
	 * @since 0.1
	 *
	 * @param boolean $stringlyTyped
	 *
	 * @return array
	 */
	public abstract function valueProvider( $stringlyTyped = true );

	public abstract function getType();

	public function getDefinitions() {
		$params = [];

		$params['empty'] = [];

		$params['values'] = [
			'values' => [ 'foo', '1', '0.1', 'yes', 1, 0.1 ]
		];

		return $params;
	}

	public function definitionProvider() {
		$definitions = $this->getDefinitions();

		foreach ( $definitions as &$definition ) {
			$definition['type'] = $this->getType();
		}

		return $definitions;
	}

	public function getEmptyInstance() {
		return ParamDefinitionFactory::singleton()->newDefinitionFromArray( [
			'name' => 'empty',
			'message' => 'test-empty',
			'type' => $this->getType(),
		] );
	}

	public function instanceProvider() {
		$definitions = [];

		foreach ( $this->definitionProvider() as $name => $definition ) {
			if ( !array_key_exists( 'message', $definition ) ) {
				$definition['message'] = 'test-' . $name;
			}

			$definition['name'] = $name;
			$definitions[] = [ ParamDefinitionFactory::singleton()->newDefinitionFromArray( $definition ) ];
		}

		return $definitions;
	}

	/**
	 * @dataProvider instanceProvider
	 */
	public function testGetType( IParamDefinition $definition ) {
		$this->assertEquals( $this->getType(), $definition->getType() );
	}

	/**
	 * @dataProvider instanceProvider
	 */
	public function testValidate( IParamDefinition $definition ) {
		foreach ( [ true, false ] as $stringlyTyped ) {
			$values = $this->valueProvider( $stringlyTyped );
			$options = new Options();
			$options->setRawStringInputs( $stringlyTyped );

			foreach ( $values[$definition->getName()] as $data ) {
				list( $input, $valid, ) = $data;

				$param = new Param( $definition );
				$param->setUserValue( $definition->getName(), $input, $options );
				$definitions = [];
				$param->process( $definitions, [], $options );

				$this->assertEquals(
					$valid,
					$param->getErrors() === [],
					'The validation process should ' . ( $valid ? '' : 'not ' ) . 'pass'
				);
			}
		}

		$this->assertTrue( true );
	}

	/**
	 * @dataProvider instanceProvider
	 */
	public function testFormat( IParamDefinition $sourceDefinition ) {
		$values = $this->valueProvider();
		$options = new Options();

		foreach ( $values[$sourceDefinition->getName()] as $data ) {
			$definition = clone $sourceDefinition;

			list( $input, $valid, ) = $data;

			$param = new Param( $definition );
			$param->setUserValue( $definition->getName(), $input, $options );

			if ( $valid && array_key_exists( 2, $data ) ) {
				$defs = [];
				$param->process( $defs, [], $options );

				$this->assertEquals(
					$data[2],
					$param->getValue()
				);
			}
		}

		$this->assertTrue( true );
	}

	protected function validate( ParamDefinition $definition, $testValue, $validity, Options $options = null ) {
		$def = clone $definition;
		$options = $options === null ? new Options() : $options;

		$param = new Param( $def );
		$param->setUserValue( $def->getName(), $testValue, $options );

		$defs = [];
		$param->process( $defs, [], $options );

		$this->assertEquals( $validity, $param->getErrors() === [] );
	}

	public function testConstructingWithoutMessageLeadsToDefaultMessage() {
		$this->assertSame(
			'validator-message-nodesc',
			( new ParamDefinition( 'type', 'name' ) )->getMessage()
		);
	}

}