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

testGivenInvalidFragment_buildFails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
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
		return [
37
			[ [ 0 => $callable ] ],
38
			[ [ '' => $callable ] ],
39
			[ [ 'string' => null ] ],
40
			[ [ 'string' => 'not a callable' ] ],
41
		];
42
	}
43
44
	/**
45
	 * @dataProvider invalidConstructorArgumentProvider
46
	 */
47
	public function testGivenInvalidComposer_constructorFails( $composers ) {
48
		$this->setExpectedException( InvalidArgumentException::class );
49
		new EntityIdComposer( $composers );
50
	}
51
52
	public function testGivenInvalidCallback_buildFails() {
53
		$composer = new EntityIdComposer( [
54
			'item' => function() {
55
				return null;
56
			},
57
		] );
58
		$this->setExpectedException( UnexpectedValueException::class );
59
		$composer->composeEntityId( '', 'item', 1 );
60
	}
61
62
	public function validUniquePartProvider() {
63
		return [
64
			'int' => [ 'numeric-item', 3, new ItemId( 'Q3' ) ],
65
			'float' => [ 'numeric-item', 4.0, new ItemId( 'Q4' ) ],
66
			'string' => [ 'numeric-item', '5', new ItemId( 'Q5' ) ],
67
68
			'custom' => [ 'custom-item', 6, new ItemId( 'Q1006' ) ],
69
		];
70
	}
71
72
	/**
73
	 * @dataProvider validUniquePartProvider
74
	 */
75
	public function testGivenValidFragment_buildSucceeds( $entityType, $uniquePart, EntityId $expected ) {
76
		$id = $this->getComposer()->composeEntityId( '', $entityType, $uniquePart );
77
		$this->assertEquals( $expected, $id );
78
	}
79
80
	public function invalidUniquePartProvider() {
81
		return [
82
			[ null, 1 ],
83
			[ 'unknown', 2 ],
84
			[ 'numeric-item', null ],
85
			[ 'numeric-item', new ItemId( 'Q4' ) ],
86
		];
87
	}
88
89
	/**
90
	 * @dataProvider invalidUniquePartProvider
91
	 */
92
	public function testGivenInvalidFragment_buildFails( $entityType, $uniquePart ) {
93
		$composer = $this->getComposer();
94
		$this->setExpectedException( InvalidArgumentException::class );
95
		$composer->composeEntityId( '', $entityType, $uniquePart );
96
	}
97
98
}
99