EntityIdListNormalizerTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testGetNormalizedReturnsIterator() 0 3 1
A assertInputResultsInNormalization() 0 6 1
A testEmptyListIsNormalizedToEmptyList() 0 3 1
A testItemIdsGetParsed() 0 6 1
A testMixedIdsGetParsed() 0 6 1
A testItemRangeGetsExpanded() 0 12 1
A testMultipleRangesGetExpanded() 0 14 1
A testGivenNonEntityId_exceptionIsThrown() 0 4 1
A testGivenMixedRange_exceptionIsThrown() 0 4 1
A testRangeWithInvalidId_exceptionIsThrown() 0 4 1
A testRangeInWrongDirection_exceptionIsThrown() 0 4 1
1
<?php
2
3
namespace Tests\Queryr\Replicator;
4
5
use PHPUnit\Framework\TestCase;
6
use Queryr\Replicator\EntityIdListNormalizer;
7
use Wikibase\DataModel\Entity\BasicEntityIdParser;
8
use Wikibase\DataModel\Entity\ItemId;
9
use Wikibase\DataModel\Entity\PropertyId;
10
11
/**
12
 * @covers \Queryr\Replicator\EntityIdListNormalizer
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class EntityIdListNormalizerTest extends TestCase {
18
19
	/**
20
	 * @var EntityIdListNormalizer
21
	 */
22
	private $normalizer;
23
24
	public function setUp() {
25
		$this->normalizer = new EntityIdListNormalizer( new BasicEntityIdParser() );
26
	}
27
28
	public function testGetNormalizedReturnsIterator() {
29
		$this->assertInstanceOf( 'Iterator', $this->normalizer->getNormalized( [] ) );
30
	}
31
32
	private function assertInputResultsInNormalization( array $input, array $normalization ) {
33
		$this->assertEquals(
34
			$normalization,
35
			iterator_to_array( $this->normalizer->getNormalized( $input ) )
36
		);
37
	}
38
39
	public function testEmptyListIsNormalizedToEmptyList() {
40
		$this->assertInputResultsInNormalization( [], [] );
41
	}
42
43
	public function testItemIdsGetParsed() {
44
		$this->assertInputResultsInNormalization(
45
			[ 'Q321', 'Q123' ],
46
			[ new ItemId( 'Q321' ), new ItemId( 'Q123' ) ]
47
		);
48
	}
49
50
	public function testMixedIdsGetParsed() {
51
		$this->assertInputResultsInNormalization(
52
			[ 'P321', 'Q123' ],
53
			[ new PropertyId( 'P321' ), new ItemId( 'Q123' ) ]
54
		);
55
	}
56
57
	public function testItemRangeGetsExpanded() {
58
		$this->assertInputResultsInNormalization(
59
			[ 'P321', 'Q123-Q125', 'P42' ],
60
			[
61
				new PropertyId( 'P321' ),
62
				new ItemId( 'Q123' ),
63
				new ItemId( 'Q124' ),
64
				new ItemId( 'Q125' ),
65
				new PropertyId( 'P42' )
66
			]
67
		);
68
	}
69
70
	public function testMultipleRangesGetExpanded() {
71
		$this->assertInputResultsInNormalization(
72
			[ 'Q1', 'Q100-Q102', 'P400-P401', 'Q42' ],
73
			[
74
				new ItemId( 'Q1' ),
75
				new ItemId( 'Q100' ),
76
				new ItemId( 'Q101' ),
77
				new ItemId( 'Q102' ),
78
				new PropertyId( 'P400' ),
79
				new PropertyId( 'P401' ),
80
				new ItemId( 'Q42' ),
81
			]
82
		);
83
	}
84
85
	public function testGivenNonEntityId_exceptionIsThrown() {
86
		$this->expectException( 'InvalidArgumentException' );
87
		iterator_to_array( $this->normalizer->getNormalized( [ 'kittens' ] ) );
88
	}
89
90
	public function testGivenMixedRange_exceptionIsThrown() {
91
		$this->expectException( 'InvalidArgumentException' );
92
		iterator_to_array( $this->normalizer->getNormalized( [ 'Q1-P3' ] ) );
93
	}
94
95
	public function testRangeWithInvalidId_exceptionIsThrown() {
96
		$this->expectException( 'InvalidArgumentException' );
97
		iterator_to_array( $this->normalizer->getNormalized( [ 'Q123-kittens' ] ) );
98
	}
99
100
	public function testRangeInWrongDirection_exceptionIsThrown() {
101
		$this->expectException( 'InvalidArgumentException' );
102
		iterator_to_array( $this->normalizer->getNormalized( [ 'Q102-Q100' ] ) );
103
	}
104
105
}
106
107