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