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