Passed
Push — rm-common ( 88d3f9...2b80a9 )
by Jeroen De
04:49
created

ParserTestBase::testParseWithValidInputs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
namespace Tests\DataValues\Geo\Parsers;
4
5
use Comparable;
6
use DataValues\DataValue;
7
use ValueParsers\ParseException;
8
use ValueParsers\ValueParser;
9
10
/**
11
 * @deprecated
12
 * TODO: remove
13
 * This is a copy of ValueParserTestBase from DataValues Common,
14
 * created so we can stop depending on that comment even though we
15
 * did not refactor away the inheritance abuse here yet.
16
 *
17
 * @license GPL-2.0+
18
 */
19
abstract class ParserTestBase extends \PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @return array[]
23
	 */
24
	abstract public function validInputProvider();
25
26
	/**
27
	 * @return array[]
28
	 */
29
	abstract public function invalidInputProvider();
30
31
	/**
32
	 * @return ValueParser
33
	 */
34
	abstract protected function getInstance();
35
36
	/**
37
	 * @dataProvider validInputProvider
38
	 * @param mixed $value
39
	 * @param mixed $expected
40
	 * @param ValueParser|null $parser
41
	 */
42
	public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) {
43
		if ( $parser === null ) {
44
			$parser = $this->getInstance();
45
		}
46
47
		$this->assertSmartEquals( $expected, $parser->parse( $value ) );
48
	}
49
50
	/**
51
	 * @param DataValue|mixed $expected
52
	 * @param DataValue|mixed $actual
53
	 */
54
	private function assertSmartEquals( $expected, $actual ) {
55
		if ( $this->requireDataValue() || $expected instanceof Comparable ) {
56
			if ( $expected instanceof DataValue && $actual instanceof DataValue ) {
57
				$msg = "testing equals():\n"
58
					. preg_replace( '/\s+/', ' ', print_r( $actual->toArray(), true ) ) . " should equal\n"
59
					. preg_replace( '/\s+/', ' ', print_r( $expected->toArray(), true ) );
60
			} else {
61
				$msg = 'testing equals()';
62
			}
63
64
			$this->assertTrue( $expected->equals( $actual ), $msg );
65
		} else {
66
			$this->assertEquals( $expected, $actual );
67
		}
68
	}
69
70
	/**
71
	 * @dataProvider invalidInputProvider
72
	 * @param mixed $value
73
	 * @param ValueParser|null $parser
74
	 */
75
	public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) {
76
		if ( $parser === null ) {
77
			$parser = $this->getInstance();
78
		}
79
80
		$this->setExpectedException( ParseException::class );
81
		$parser->parse( $value );
82
	}
83
84
	/**
85
	 * Returns if the result of the parsing process should be checked to be a DataValue.
86
	 *
87
	 * @return bool
88
	 */
89
	protected function requireDataValue() {
90
		return true;
91
	}
92
93
}
94