1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueParsers\Test; |
4
|
|
|
|
5
|
|
|
use Comparable; |
6
|
|
|
use DataValues\DataValue; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use ValueParsers\ParserOptions; |
9
|
|
|
use ValueParsers\ValueParser; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Base for unit tests for ValueParser implementing classes. |
13
|
|
|
* |
14
|
|
|
* @since 0.1 |
15
|
|
|
* |
16
|
|
|
* @group ValueParsers |
17
|
|
|
* @group DataValueExtensions |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0-or-later |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
abstract class ValueParserTestCase extends TestCase { |
23
|
|
|
|
24
|
|
|
protected const NON_VALID_CASES = [ |
25
|
|
|
array( true ), |
26
|
|
|
array( false ), |
27
|
|
|
array( null ), |
28
|
|
|
array( 4.2 ), |
29
|
|
|
array( array() ), |
30
|
|
|
array( 42 ) |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @since 0.1 |
35
|
|
|
* |
36
|
|
|
* @return array[] |
37
|
|
|
*/ |
38
|
|
|
abstract public function validInputProvider(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @since 0.1 |
42
|
|
|
* |
43
|
|
|
* @return array[] |
44
|
|
|
*/ |
45
|
|
|
abstract public function invalidInputProvider(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @since 0.1 |
49
|
|
|
* |
50
|
|
|
* @return ValueParser |
51
|
|
|
*/ |
52
|
|
|
abstract protected function getInstance(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @since 0.1 |
56
|
|
|
* |
57
|
|
|
* @dataProvider validInputProvider |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @param mixed $expected |
60
|
|
|
* @param ValueParser|null $parser |
61
|
|
|
*/ |
62
|
|
|
public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) { |
63
|
|
|
if ( $parser === null ) { |
64
|
|
|
$parser = $this->getInstance(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->assertSmartEquals( $expected, $parser->parse( $value ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param DataValue|mixed $expected |
72
|
|
|
* @param DataValue|mixed $actual |
73
|
|
|
*/ |
74
|
|
|
private function assertSmartEquals( $expected, $actual ) { |
75
|
|
|
if ( $this->requireDataValue() || $expected instanceof Comparable ) { |
|
|
|
|
76
|
|
|
if ( $expected instanceof DataValue && $actual instanceof DataValue ) { |
77
|
|
|
$msg = "testing equals():\n" |
78
|
|
|
. preg_replace( '/\s+/', ' ', print_r( $actual->toArray(), true ) ) . " should equal\n" |
79
|
|
|
. preg_replace( '/\s+/', ' ', print_r( $expected->toArray(), true ) ); |
80
|
|
|
} else { |
81
|
|
|
$msg = 'testing equals()'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->assertTrue( $expected->equals( $actual ), $msg ); |
85
|
|
|
} else { |
86
|
|
|
$this->assertEquals( $expected, $actual ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @since 0.1 |
92
|
|
|
* |
93
|
|
|
* @dataProvider invalidInputProvider |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @param ValueParser|null $parser |
96
|
|
|
*/ |
97
|
|
|
public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) { |
98
|
|
|
if ( $parser === null ) { |
99
|
|
|
$parser = $this->getInstance(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->expectException( 'ValueParsers\ParseException' ); |
103
|
|
|
$parser->parse( $value ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns if the result of the parsing process should be checked to be a DataValue. |
108
|
|
|
* |
109
|
|
|
* @since 0.1 |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
protected function requireDataValue() { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSetAndGetOptions() { |
118
|
|
|
$parser = $this->getInstance(); |
119
|
|
|
|
120
|
|
|
$parser->setOptions( new ParserOptions() ); |
121
|
|
|
|
122
|
|
|
$this->assertEquals( new ParserOptions(), $parser->getOptions() ); |
123
|
|
|
|
124
|
|
|
$options = new ParserOptions(); |
125
|
|
|
$options->setOption( '~=[,,_,,]:3', '~=[,,_,,]:3' ); |
126
|
|
|
|
127
|
|
|
$parser->setOptions( $options ); |
128
|
|
|
|
129
|
|
|
$this->assertEquals( $options, $parser->getOptions() ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|