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
|
|
|
|