Passed
Push — master ( 8ff373...315bd9 )
by adam
01:44
created

DispatchingValueParserTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 62
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getParser() 0 14 2
A testGivenInvalidConstructorArguments_constructorThrowsException() 0 4 1
A invalidConstructorArgumentsProvider() 0 11 1
A testParse() 0 11 1
A testParseThrowsException() 0 11 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ValueParsers\Tests;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\TestCase;
9
use ValueParsers\DispatchingValueParser;
10
use ValueParsers\ParseException;
11
use ValueParsers\ValueParser;
12
13
/**
14
 * @covers \ValueParsers\DispatchingValueParser
15
 *
16
 * @group DataValue
17
 * @group DataValueExtensions
18
 * @group ValueParsers
19
 *
20
 * @license GPL-2.0-or-later
21
 * @author Thiemo Kreuz
22
 */
23
class DispatchingValueParserTest extends TestCase {
24
25
	private function getParser( $invocation ) : ValueParser {
26
		$mock = $this->createMock( ValueParser::class );
27
28
		$mock->expects( $invocation )
29
			->method( 'parse' )
30
			->will( $this->returnCallback( function( $value ) {
31
				if ( $value === 'invalid' ) {
32
					throw new ParseException( 'failed' );
33
				}
34
				return $value;
35
			} ) );
36
37
		return $mock;
38
	}
39
40
	/**
41
	 * @dataProvider invalidConstructorArgumentsProvider
42
	 */
43
	public function testGivenInvalidConstructorArguments_constructorThrowsException( $parsers, $format ) {
44
		$this->expectException( InvalidArgumentException::class );
45
		new DispatchingValueParser( $parsers, $format );
46
	}
47
48
	public function invalidConstructorArgumentsProvider() {
49
		$parsers = [
50
			$this->getParser( $this->never() ),
51
		];
52
53
		return [
54
			[ [], 'format' ],
55
			[ $parsers, null ],
56
			[ $parsers, '' ],
57
		];
58
	}
59
60
	public function testParse() {
61
		$parser = new DispatchingValueParser(
62
			[
63
				$this->getParser( $this->once() ),
64
				$this->getParser( $this->never() ),
65
			],
66
			'format'
67
		);
68
69
		$this->assertEquals( 'valid', $parser->parse( 'valid' ) );
70
	}
71
72
	public function testParseThrowsException() {
73
		$parser = new DispatchingValueParser(
74
			[
75
				$this->getParser( $this->once() ),
76
			],
77
			'format'
78
		);
79
80
		$this->expectException( ParseException::class );
81
		$parser->parse( 'invalid' );
82
	}
83
84
}
85