Completed
Push — master ( ed92ed...2c22d9 )
by Amir
30s
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\EntityId;
4
5
use Wikibase\DataModel\Entity\ItemIdParser;
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
	public function testGetIdParser_repositoryWithKnownMapping() {
19
		$dummyParser = new ItemIdParser();
20
		$idPrefixMapping = [
21
			'foo' => [ 'd' => 'de', 'e' => 'en', ],
22
		];
23
		$factory = new PrefixMappingEntityIdParserFactory( $dummyParser, $idPrefixMapping );
24
		$this->assertEquals(
25
			new PrefixMappingEntityIdParser( [ ''  => 'foo', 'd' => 'de', 'e' => 'en' ], $dummyParser ),
26
			$factory->getIdParser( 'foo' )
27
		);
28
	}
29
30
	public function testGetIdParser_repositoryWithoutKnownMapping() {
31
		$dummyParser = new ItemIdParser();
32
		$idPrefixMapping = [
33
			'foo' => [ 'd' => 'de', 'e' => 'en', ],
34
		];
35
		$factory = new PrefixMappingEntityIdParserFactory( $dummyParser, $idPrefixMapping );
36
		$this->assertEquals(
37
			new PrefixMappingEntityIdParser( [ '' => 'bar' ], $dummyParser ),
38
			$factory->getIdParser( 'bar' )
39
		);
40
	}
41
42
	public function testGetIdParser_noIdPrefixMappings() {
43
		$dummyParser = new ItemIdParser();
44
		$factory = new PrefixMappingEntityIdParserFactory( $dummyParser, [] );
45
		$this->assertEquals(
46
			new PrefixMappingEntityIdParser( [ '' => 'foo' ], $dummyParser ),
47
			$factory->getIdParser( 'foo' )
48
		);
49
	}
50
51
	public function testGivenNonStringRepository_exceptionIsThrown() {
52
		$factory = new PrefixMappingEntityIdParserFactory( new ItemIdParser(), [] );
53
		$this->setExpectedException( ParameterTypeException::class );
54
		$factory->getIdParser( 111 );
55
	}
56
57
	public function testGivenRepositoryIncludingColon_exceptionIsThrown() {
58
		$factory = new PrefixMappingEntityIdParserFactory( new ItemIdParser(), [] );
59
		$this->setExpectedException( ParameterAssertionException::class );
60
		$factory->getIdParser( 'en:' );
61
	}
62
63
	/**
64
	 * @dataProvider provideInvalidIdPrefixMapping
65
	 */
66
	public function testGivenInvalidIdPrefixMapping_exceptionIsThrown( array $mapping ) {
67
		$this->setExpectedException( ParameterAssertionException::class );
68
		new PrefixMappingEntityIdParserFactory( new ItemIdParser(), $mapping );
69
	}
70
71
	public function provideInvalidIdPrefixMapping() {
72
		return [
73
			'id prefix mapping values are not arrays' => [ [ 'foo' => 'bar' ] ],
74
			'non-string keys in id prefix mapping' => [ [ 0 => [ 'd' => 'wd' ] ] ],
75
			'non-string values in inner mapping' => [ [ 'foo' => [ 'd' => 123 ] ] ],
76
			'non-string keys in inner mapping' => [ [ 'foo' => [ 0 => 'wd' ] ] ],
77
			'keys containing colons in id prefix mapping' => [ [ 'fo:o' => [ 'd' => 'wd' ] ] ],
78
			'default prefix mapping differs from repository name' => [ [ 'foo' => [ '' => 'bar' ] ] ],
79
		];
80
	}
81
82
	public function testGetIdParserReusesTheInstanceOverMultitpleCalls() {
83
		$factory = new PrefixMappingEntityIdParserFactory( new ItemIdParser(), [] );
84
85
		$parserOne = $factory->getIdParser( 'foo' );
86
		$parserTwo = $factory->getIdParser( 'foo' );
87
88
		$this->assertSame( $parserOne, $parserTwo );
89
	}
90
91
}
92