Completed
Push — master ( cffbb8...5e3d42 )
by Daniel
32s
created

PrefixMappingEntityIdParserTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegularParser() 0 3 1
A testGivenOnlyDefaultPrefixMapping_prefixIsAddedToIdSerialization() 0 9 1
A provideValidIdSerialization() 0 9 1
A testGivenPrefixIsMapped_mappedOneIsUsed() 0 9 1
A provideMappedSerializationId() 0 6 1
A testGivenPrefixIsNotMapped_defaultPrefixIsAddedToIdSerialization() 0 6 1
A testEntityIdParsingExceptionsAreNotCaught() 0 9 1
A testGivenInvalidPrefixMapping_exceptionIsThrown() 0 5 1
A provideInvalidPrefixMapping() 0 8 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\EntityId;
4
5
use Wikibase\DataModel\Entity\BasicEntityIdParser;
6
use Wikibase\DataModel\Entity\EntityIdParser;
7
use Wikibase\DataModel\Entity\EntityIdParsingException;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\DataModel\Services\EntityId\PrefixMappingEntityIdParser;
12
use Wikimedia\Assert\ParameterAssertionException;
13
14
/**
15
 * @covers Wikibase\DataModel\Services\EntityId\PrefixMappingEntityIdParser
16
 *
17
 * @license GPL-2.0+
18
 */
19
class PrefixMappingEntityIdParserTest extends \PHPUnit_Framework_TestCase {
20
21
	private function getRegularParser() {
22
		return new BasicEntityIdParser();
23
	}
24
25
	/**
26
	 * @dataProvider provideValidIdSerialization
27
	 */
28
	public function testGivenOnlyDefaultPrefixMapping_prefixIsAddedToIdSerialization(
29
		array $prefixMapping, $input, EntityId $expectedId
30
	) {
31
		$parser = new PrefixMappingEntityIdParser(
32
			$prefixMapping,
33
			$this->getRegularParser()
34
		);
35
		$this->assertEquals( $expectedId, $parser->parse( $input ) );
36
	}
37
38
	public function provideValidIdSerialization() {
39
		return [
40
			[ [ '' => 'wikidata' ], 'Q1337', new ItemId( 'wikidata:Q1337' ) ],
41
			[ [ '' => '' ], 'Q1337', new ItemId( 'Q1337' ) ],
42
			[ [ '' => 'foo' ], 'P123', new PropertyId( 'foo:P123' ) ],
43
			[ [ '' => 'foo' ], 'bar:Q1337', new ItemId( 'foo:bar:Q1337' ) ],
44
			[ [ '' => 'foo' ], 'foo:Q1337', new ItemId( 'foo:foo:Q1337' ) ],
45
		];
46
	}
47
48
	/**
49
	 * @dataProvider provideMappedSerializationId
50
	 */
51
	public function testGivenPrefixIsMapped_mappedOneIsUsed(
52
		array $prefixMapping,
53
		$foreignId,
54
		EntityId $expectedEntityId
55
	) {
56
		$regularParser = $this->getRegularParser();
57
		$parser = new PrefixMappingEntityIdParser( $prefixMapping, $regularParser );
58
		$this->assertEquals( $expectedEntityId, $parser->parse( $foreignId ) );
59
	}
60
61
	public function provideMappedSerializationId() {
62
		return [
63
			[ [ '' => 'foo', 'bar' => 'wd' ], 'bar:Q1337', new ItemId( 'wd:Q1337' ) ],
64
			[ [ '' => 'foo', 'bar' => 'wd' ], 'bar:x:Q1337', new ItemId( 'wd:x:Q1337' ) ],
65
		];
66
	}
67
68
	public function testGivenPrefixIsNotMapped_defaultPrefixIsAddedToIdSerialization() {
69
		$expectedId = new ItemId( 'foo:x:bar:Q1337' );
70
		$regularParser = $this->getRegularParser();
71
		$parser = new PrefixMappingEntityIdParser( [ '' => 'foo', 'bar' => 'wd' ], $regularParser );
72
		$this->assertEquals( $expectedId, $parser->parse( 'x:bar:Q1337' ) );
73
	}
74
75
	public function testEntityIdParsingExceptionsAreNotCaught() {
76
		$regularParser = $this->getMock( EntityIdParser::class );
77
		$regularParser->expects( $this->any() )
78
			->method( 'parse' )
79
			->will( $this->throwException( new EntityIdParsingException() ) );
80
		$parser = new PrefixMappingEntityIdParser( [ '' => 'wikidata' ], $regularParser );
81
		$this->setExpectedException( EntityIdParsingException::class );
82
		$parser->parse( 'QQQ' );
83
	}
84
85
	/**
86
	 * @dataProvider provideInvalidPrefixMapping
87
	 */
88
	public function testGivenInvalidPrefixMapping_exceptionIsThrown( array $prefixMapping ) {
89
		$regularParser = $this->getMock( EntityIdParser::class );
90
		$this->setExpectedException( ParameterAssertionException::class );
91
		new PrefixMappingEntityIdParser( $prefixMapping, $regularParser );
92
	}
93
94
	public function provideInvalidPrefixMapping() {
95
		return [
96
			'no empty-string key in prefix mapping' => [ [] ],
97
			'non-string values in prefix mapping' => [ [ '' => 123 ] ],
98
			'non-string keys in prefix mapping' => [ [ '' => 'foo', 0 => 'wd' ] ],
99
			'prefix mapping values containing colons' => [ [ '' => 'wikidata:' ] ],
100
		];
101
	}
102
103
}
104