Completed
Push — master ( 769627...07ac62 )
by Marius
8s
created

EntityIdLabelFormatterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validProvider() 0 13 1
A testParseWithValidArguments() 0 8 1
A getLabelLookup() 0 15 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\EntityId;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Services\EntityId\EntityIdLabelFormatter;
10
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookupException;
11
use Wikibase\DataModel\Services\Lookup\LabelLookup;
12
use Wikibase\DataModel\Term\Term;
13
14
/**
15
 * @covers Wikibase\DataModel\Services\EntityId\EntityIdLabelFormatter
16
 *
17
 * @license GPL-2.0+
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Daniel Kinzler
20
 * @author Katie Filbert < [email protected] >
21
 */
22
class EntityIdLabelFormatterTest extends PHPUnit_Framework_TestCase {
23
24
	/**
25
	 * @return array
26
	 */
27
	public function validProvider() {
28
		$argLists = [];
29
30
		$argLists[] = [ new ItemId( 'Q42' ), 'es', 'foo' ];
31
32
		$argLists[] = [ new ItemId( 'Q9001' ), 'en', 'Q9001' ];
33
34
		$argLists[] = [ new PropertyId( 'P9001' ), 'en', 'P9001' ];
35
36
		$argLists['unresolved-redirect'] = [ new ItemId( 'Q23' ), 'en', 'Q23' ];
37
38
		return $argLists;
39
	}
40
41
	/**
42
	 * @dataProvider validProvider
43
	 *
44
	 * @param EntityId $entityId
45
	 * @param string $languageCode
46
	 * @param string $expectedString
47
	 */
48
	public function testParseWithValidArguments( EntityId $entityId, $languageCode, $expectedString ) {
49
		$formatter = new EntityIdLabelFormatter( $this->getLabelLookup( $languageCode ) );
50
51
		$formattedValue = $formatter->formatEntityId( $entityId );
52
53
		$this->assertInternalType( 'string', $formattedValue );
54
		$this->assertEquals( $expectedString, $formattedValue );
55
	}
56
57
	/**
58
	 * @param string $languageCode
59
	 *
60
	 * @return LabelLookup
61
	 */
62
	private function getLabelLookup( $languageCode ) {
63
		$labelLookup = $this->getMock( LabelLookup::class );
64
65
		$labelLookup->expects( $this->any() )
66
			->method( 'getLabel' )
67
			->will( $this->returnCallback( function( EntityId $id ) use ( $languageCode ) {
68
				if ( $id->getSerialization() === 'Q42' && $languageCode === 'es' ) {
69
					return new Term( 'es', 'foo' );
70
				} else {
71
					throw new LabelDescriptionLookupException( $id, 'Label not found' );
72
				}
73
			} ) );
74
75
		return $labelLookup;
76
	}
77
78
}
79