Passed
Push — master ( 079aca...05e9ba )
by Jeroen De
04:40
created

EntityDumpIteratorTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 118
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A newIteratorForFile() 0 7 1
A assertFindsEntities() 0 9 2
A testGivenFileWithNoEntities_noEntitiesAreReturned() 0 5 1
A testGivenFileWithOneEntity_oneEntityIsFound() 0 5 1
A testGivenFileWithFiveEntities_fiveEntityAreFound() 0 5 1
A testGivenFileWithInvalidEntity_noEntityIsFound() 0 4 1
A testGivenFileWithInvalidEntities_validEntitiesAreFound() 0 4 1
A testCanDoMultipleIterations() 0 6 1
A testInitialPosition() 0 26 1
A testGivenFileWithInvalidEntities_errorsAreReported() 0 18 2
A testGivenFileWithInvalidJsonLine_errorIsRecorded() 0 15 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\JsonDumpReader\Tests\Integration;
6
7
use Iterator;
8
use PHPUnit\Framework\TestCase;
9
use Wikibase\JsonDumpReader\Iterator\EntityDumpIterator;
10
use Wikibase\JsonDumpReader\JsonDumpFactory;
11
use Wikibase\JsonDumpReader\Reader\ExtractedDumpReader;
12
use Wikibase\JsonDumpReader\Tests\TestFactory;
13
14
/**
15
 * @covers \Wikibase\JsonDumpReader\Iterator\EntityDumpIterator
16
 * @covers \Wikibase\JsonDumpReader\JsonDumpFactory
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class EntityDumpIteratorTest extends TestCase {
22
23
	private function newIteratorForFile( $filePath, callable $onError = null ) {
24
		return ( new JsonDumpFactory() )->newEntityDumpIterator(
25
			new ExtractedDumpReader( $filePath ),
26
			TestFactory::newInstance()->newEntityDeserializer(),
27
			$onError
28
		);
29
	}
30
31
	private function assertFindsEntities( array $expectedIds, Iterator $dumpIterator, $message = '' ) {
32
		$actualIds = [];
33
34
		foreach ( $dumpIterator as $entity ) {
35
			$actualIds[] = $entity->getId()->getSerialization();
36
		}
37
38
		$this->assertEquals( $expectedIds, $actualIds, $message );
39
	}
40
41
	public function testGivenFileWithNoEntities_noEntitiesAreReturned() {
42
		$iterator = $this->newIteratorForFile( ( new \JsonDumpData() )->getEmptyDumpPath() );
43
44
		$this->assertFindsEntities( [], $iterator );
45
	}
46
47
	public function testGivenFileWithOneEntity_oneEntityIsFound() {
48
		$iterator = $this->newIteratorForFile( ( new \JsonDumpData() )->getOneItemDumpPath() );
49
50
		$this->assertFindsEntities( [ 'Q1' ], $iterator );
51
	}
52
53
	public function testGivenFileWithFiveEntities_fiveEntityAreFound() {
54
		$iterator = $this->newIteratorForFile( ( new \JsonDumpData() )->getFiveEntitiesDumpPath() );
55
56
		$this->assertFindsEntities( [ 'Q1', 'Q8', 'P16', 'P19', 'P22' ], $iterator );
57
	}
58
59
	public function testGivenFileWithInvalidEntity_noEntityIsFound() {
60
		$iterator = $this->newIteratorForFile( __DIR__ . '/../data/invalid-item.json' );
61
		$this->assertFindsEntities( [], $iterator );
62
	}
63
64
	public function testGivenFileWithInvalidEntities_validEntitiesAreFound() {
65
		$iterator = $this->newIteratorForFile( __DIR__ . '/../data/3valid-2invalid.json' );
66
		$this->assertFindsEntities( [ 'Q1', 'P16', 'P22' ], $iterator );
67
	}
68
69
	public function testCanDoMultipleIterations() {
70
		$iterator = $this->newIteratorForFile( ( new \JsonDumpData() )->getFiveEntitiesDumpPath() );
71
72
		$this->assertFindsEntities( [ 'Q1', 'Q8', 'P16', 'P19', 'P22' ], $iterator, 'first iteration' );
73
		$this->assertFindsEntities( [ 'Q1', 'Q8', 'P16', 'P19', 'P22' ], $iterator, 'second iteration' );
74
	}
75
76
	public function testInitialPosition() {
77
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getFiveEntitiesDumpPath() );
78
79
		$iterator = new EntityDumpIterator(
80
			( new JsonDumpFactory() )->newObjectDumpIterator( $reader ),
81
			TestFactory::newInstance()->newEntityDeserializer()
82
		);
83
84
		$iterator->rewind();
85
		$this->assertSame( 'Q1', $iterator->current()->getId()->getSerialization() );
86
87
		$iterator->next();
88
		$this->assertSame( 'Q8', $iterator->current()->getId()->getSerialization() );
89
90
		$newReader = new ExtractedDumpReader(
91
			( new \JsonDumpData() )->getFiveEntitiesDumpPath(),
92
			$reader->getPosition()
93
		);
94
95
		$newIterator = new EntityDumpIterator(
96
			( new JsonDumpFactory() )->newObjectDumpIterator( $newReader ),
97
			TestFactory::newInstance()->newEntityDeserializer()
98
		);
99
100
		$this->assertFindsEntities( [ 'P16', 'P19', 'P22' ], $newIterator );
101
	}
102
103
	public function testGivenFileWithInvalidEntities_errorsAreReported() {
104
		$errors = [];
105
106
		$iterator = $this->newIteratorForFile(
107
			__DIR__ . '/../data/3valid-2invalid.json',
108
			function( $errorMessage ) use ( &$errors ) {
109
				$errors[] = $errorMessage;
110
			}
111
		);
112
113
		$iterator->rewind();
114
		while ( $iterator->valid() ) {
115
			$iterator->next();
116
		}
117
118
		$this->assertContainsOnly( 'string', $errors );
119
		$this->assertCount( 2, $errors );
0 ignored issues
show
Documentation introduced by
$errors is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
	}
121
122
	public function testGivenFileWithInvalidJsonLine_errorIsRecorded() {
123
		$errors = [];
124
125
		$iterator = $this->newIteratorForFile(
126
			__DIR__ . '/../data/invalid-json.json',
127
			function( $errorMessage ) use ( &$errors ) {
128
				$errors[] = $errorMessage;
129
			}
130
		);
131
132
		iterator_to_array( $iterator );
133
134
		$this->assertContainsOnly( 'string', $errors );
135
		$this->assertCount( 1, $errors );
0 ignored issues
show
Documentation introduced by
$errors is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
	}
137
138
}