testGetIdParser_repositoryWithoutKnownMapping()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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