forked from DataValues/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueParserTestBase.php
More file actions
107 lines (93 loc) · 2.3 KB
/
ValueParserTestBase.php
File metadata and controls
107 lines (93 loc) · 2.3 KB
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
<?php
namespace ValueParsers\Test;
use Comparable;
use DataValues\DataValue;
use PHPUnit_Framework_TestCase;
use ValueParsers\ValueParser;
/**
* Base for unit tests for ValueParser implementing classes.
*
* @since 0.1
*
* @group ValueParsers
* @group DataValueExtensions
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
abstract class ValueParserTestBase extends PHPUnit_Framework_TestCase {
/**
* @since 0.1
*
* @return array[]
*/
public abstract function validInputProvider();
/**
* @since 0.1
*
* @return array[]
*/
public abstract function invalidInputProvider();
/**
* @since 0.1
*
* @return ValueParser
*/
protected abstract function getInstance();
/**
* @since 0.1
*
* @dataProvider validInputProvider
* @param mixed $value
* @param mixed $expected
* @param ValueParser|null $parser
*/
public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) {
if ( $parser === null ) {
$parser = $this->getInstance();
}
$this->assertSmartEquals( $expected, $parser->parse( $value ) );
}
/**
* @param DataValue|mixed $expected
* @param DataValue|mixed $actual
*/
private function assertSmartEquals( $expected, $actual ) {
if ( $this->requireDataValue() || $expected instanceof Comparable ) {
if ( $expected instanceof DataValue && $actual instanceof DataValue ) {
$msg = "testing equals():\n"
. preg_replace( '/\s+/', ' ', print_r( $actual->toArray(), true ) ) . " should equal\n"
. preg_replace( '/\s+/', ' ', print_r( $expected->toArray(), true ) );
} else {
$msg = 'testing equals()';
}
$this->assertTrue( $expected->equals( $actual ), $msg );
} else {
$this->assertEquals( $expected, $actual );
}
}
/**
* @since 0.1
*
* @dataProvider invalidInputProvider
* @param mixed $value
* @param ValueParser|null $parser
*/
public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) {
if ( $parser === null ) {
$parser = $this->getInstance();
}
$this->setExpectedException( 'ValueParsers\ParseException' );
$parser->parse( $value );
}
/**
* Returns if the result of the parsing process should be checked to be a DataValue.
*
* @since 0.1
*
* @return bool
*/
protected function requireDataValue() {
return true;
}
}