Passed
Pull Request — master (#182)
by
unknown
02:22
created

EntityIdComposerTest::validUniquePartProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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