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