Passed
Push — master ( 26fb24...a0d140 )
by no
02:17
created

testGivenValidFragment_buildSucceeds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\EntityId;
4
5
use InvalidArgumentException;
6
use PHPUnit_Framework_TestCase;
7
use UnexpectedValueException;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Services\EntityId\EntityIdComposer;
11
12
/**
13
 * @covers Wikibase\DataModel\Services\EntityId\EntityIdComposer
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Thiemo Mättig
17
 */
18
class EntityIdComposerTest extends PHPUnit_Framework_TestCase {
19
20
	private function getComposer() {
21
		return new EntityIdComposer( [
22
			'numeric-item' => function( $repositoryName, $uniquePart ) {
23
				return new ItemId( 'Q' . $uniquePart );
24
			},
25
			'custom-item' => function( $repositoryName, $uniquePart ) {
26
				return new ItemId( 'Q100' . $uniquePart );
27
			},
28
		] );
29
	}
30
31
	public function invalidConstructorArgumentProvider() {
32
		$callable = function() {
33
		};
34
35
		return [
36
			[ [ 0 => $callable ] ],
37
			[ [ '' => $callable ] ],
38
			[ [ 'string' => null ] ],
39
			[ [ 'string' => 'not a callable' ] ],
40
		];
41
	}
42
43
	/**
44
	 * @dataProvider invalidConstructorArgumentProvider
45
	 */
46
	public function testGivenInvalidComposer_constructorFails( $composers ) {
47
		$this->setExpectedException( InvalidArgumentException::class );
48
		new EntityIdComposer( $composers );
49
	}
50
51
	public function testGivenInvalidCallback_buildFails() {
52
		$composer = new EntityIdComposer( [
53
			'item' => function() {
54
				return null;
55
			},
56
		] );
57
		$this->setExpectedException( UnexpectedValueException::class );
58
		$composer->composeEntityId( '', 'item', 1 );
59
	}
60
61
	public function validUniquePartProvider() {
62
		return [
63
			'int' => [ 'numeric-item', 3, new ItemId( 'Q3' ) ],
64
			'float' => [ 'numeric-item', 4.0, new ItemId( 'Q4' ) ],
65
			'string' => [ 'numeric-item', '5', new ItemId( 'Q5' ) ],
66
67
			'custom' => [ 'custom-item', 6, new ItemId( 'Q1006' ) ],
68
		];
69
	}
70
71
	/**
72
	 * @dataProvider validUniquePartProvider
73
	 */
74
	public function testGivenValidFragment_buildSucceeds( $entityType, $uniquePart, EntityId $expected ) {
75
		$id = $this->getComposer()->composeEntityId( '', $entityType, $uniquePart );
76
		$this->assertEquals( $expected, $id );
77
	}
78
79
	public function invalidUniquePartProvider() {
80
		return [
81
			[ null, 1 ],
82
			[ 'unknown', 2 ],
83
			[ 'numeric-item', null ],
84
			[ 'numeric-item', new ItemId( 'Q4' ) ],
85
		];
86
	}
87
88
	/**
89
	 * @dataProvider invalidUniquePartProvider
90
	 */
91
	public function testGivenInvalidFragment_buildFails( $entityType, $uniquePart ) {
92
		$composer = $this->getComposer();
93
		$this->setExpectedException( InvalidArgumentException::class );
94
		$composer->composeEntityId( '', $entityType, $uniquePart );
95
	}
96
97
}
98