Test Failed
Pull Request — master (#130)
by
unknown
05:29
created

ValueParserTestBase::testParseWithValidInputs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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