Completed
Push — master ( 5e3d42...da4cd6 )
by Daniel
25s
created

PrefixMappingEntityIdParserFactoryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 71
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDummyEntityIdParser() 0 3 1
A testGetIdParser_repositoryWithKnownMapping() 0 11 1
A testGetIdParser_repositoryWithoutKnownMapping() 0 11 1
A testGivenNonStringRepository_exceptionIsThrown() 0 5 1
A testGivenRepositoryIncludingColon_exceptionIsThrown() 0 5 1
A testGetIdParser_noIdPrefixMappings() 0 8 1
A testGivenInvalidIdPrefixMapping_exceptionIsThrown() 0 4 1
A provideInvalidIdPrefixMapping() 0 10 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\EntityId;
4
5
use Wikibase\DataModel\Entity\EntityIdParser;
6
use Wikibase\DataModel\Services\EntityId\PrefixMappingEntityIdParser;
7
use Wikibase\DataModel\Services\EntityId\PrefixMappingEntityIdParserFactory;
8
use Wikimedia\Assert\ParameterAssertionException;
9
use Wikimedia\Assert\ParameterTypeException;
10
11
/**
12
 * @covers Wikibase\DataModel\Services\EntityId\PrefixMappingEntityIdParserFactory
13
 *
14
 * @license GPL-2.0+
15
 */
16
class PrefixMappingEntityIdParserFactoryTest extends \PHPUnit_Framework_TestCase {
17
18
	private function getDummyEntityIdParser() {
19
		return $this->getMock( EntityIdParser::class );
20
	}
21
22
	public function testGetIdParser_repositoryWithKnownMapping() {
23
		$dummyParser = $this->getDummyEntityIdParser();
24
		$idPrefixMapping = [
25
			'foo' => [ 'd' => 'de', 'e' => 'en', ],
26
		];
27
		$factory = new PrefixMappingEntityIdParserFactory( $dummyParser, $idPrefixMapping );
28
		$this->assertEquals(
29
			new PrefixMappingEntityIdParser( [ ''  => 'foo', 'd' => 'de', 'e' => 'en' ], $dummyParser ),
30
			$factory->getIdParser( 'foo' )
31
		);
32
	}
33
34
	public function testGetIdParser_repositoryWithoutKnownMapping() {
35
		$dummyParser = $this->getDummyEntityIdParser();
36
		$idPrefixMapping = [
37
			'foo' => [ 'd' => 'de', 'e' => 'en', ],
38
		];
39
		$factory = new PrefixMappingEntityIdParserFactory( $dummyParser, $idPrefixMapping );
40
		$this->assertEquals(
41
			new PrefixMappingEntityIdParser( [ '' => 'bar' ], $dummyParser ),
42
			$factory->getIdParser( 'bar' )
43
		);
44
	}
45
46
	public function testGetIdParser_noIdPrefixMappings() {
47
		$dummyParser = $this->getDummyEntityIdParser();
48
		$factory = new PrefixMappingEntityIdParserFactory( $dummyParser, [] );
49
		$this->assertEquals(
50
			new PrefixMappingEntityIdParser( [ '' => 'foo' ], $dummyParser ),
51
			$factory->getIdParser( 'foo' )
52
		);
53
	}
54
55
	public function testGivenNonStringRepository_exceptionIsThrown() {
56
		$factory = new PrefixMappingEntityIdParserFactory( $this->getDummyEntityIdParser(), [] );
57
		$this->setExpectedException( ParameterTypeException::class );
58
		$factory->getIdParser( 111 );
59
	}
60
61
	public function testGivenRepositoryIncludingColon_exceptionIsThrown() {
62
		$factory = new PrefixMappingEntityIdParserFactory( $this->getDummyEntityIdParser(), [] );
63
		$this->setExpectedException( ParameterAssertionException::class );
64
		$factory->getIdParser( 'en:' );
65
	}
66
67
	/**
68
	 * @dataProvider provideInvalidIdPrefixMapping
69
	 */
70
	public function testGivenInvalidIdPrefixMapping_exceptionIsThrown( array $mapping ) {
71
		$this->setExpectedException( ParameterAssertionException::class );
72
		new PrefixMappingEntityIdParserFactory( $this->getDummyEntityIdParser(), $mapping );
73
	}
74
75
	public function provideInvalidIdPrefixMapping() {
76
		return [
77
			'id prefix mapping values are not arrays' => [ [ 'foo' => 'bar' ] ],
78
			'non-string keys in id prefix mapping' => [ [ 0 => [ 'd' => 'wd' ] ] ],
79
			'non-string values in inner mapping' => [ [ 'foo' => [ 'd' => 123 ] ] ],
80
			'non-string keys in inner mapping' => [ [ 'foo' => [ 0 => 'wd' ] ] ],
81
			'keys containing colons in id prefix mapping' => [ [ 'fo:o' => [ 'd' => 'wd' ] ] ],
82
			'default prefix mapping differs from repository name' => [ [ 'foo' => [ '' => 'bar' ] ] ],
83
		];
84
	}
85
86
}
87