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

ExtractedDumpReaderTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 71
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A assertFindsAnotherJsonLine() 0 3 1
A assertFindsEntity() 0 5 1
A testGivenFileWithNoEntities_nullIsReturned() 0 5 1
A testGivenFileWithOneEntity_oneEntityIsFound() 0 6 1
A testGivenFileWithFiveEntities_fiveEntityAreFound() 0 10 1
A testRewind() 0 8 1
A testResumeFromPosition() 0 14 1
A testFindsAllEntitiesInBigFile() 0 9 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\JsonDumpReader\Tests\Unit\Reader;
6
7
use PHPUnit\Framework\TestCase;
8
use Wikibase\JsonDumpReader\Reader\ExtractedDumpReader;
9
10
/**
11
 * @covers \Wikibase\JsonDumpReader\Reader\ExtractedDumpReader
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class ExtractedDumpReaderTest extends TestCase {
17
18
	private function assertFindsAnotherJsonLine( ExtractedDumpReader $reader ) {
19
		$this->assertJson( $reader->nextJsonLine() );
20
	}
21
22
	private function assertFindsEntity( ExtractedDumpReader $reader, $expectedId ) {
23
		$line = $reader->nextJsonLine();
24
		$this->assertJson( $line );
25
		$this->assertContains( $expectedId, $line );
26
	}
27
28
	public function testGivenFileWithNoEntities_nullIsReturned() {
29
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getEmptyDumpPath() );
30
31
		$this->assertNull( $reader->nextJsonLine() );
32
	}
33
34
	public function testGivenFileWithOneEntity_oneEntityIsFound() {
35
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getOneItemDumpPath() );
36
37
		$this->assertFindsAnotherJsonLine( $reader );
38
		$this->assertNull( $reader->nextJsonLine() );
39
	}
40
41
	public function testGivenFileWithFiveEntities_fiveEntityAreFound() {
42
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getFiveEntitiesDumpPath() );
43
44
		$this->assertFindsAnotherJsonLine( $reader );
45
		$this->assertFindsAnotherJsonLine( $reader );
46
		$this->assertFindsAnotherJsonLine( $reader );
47
		$this->assertFindsAnotherJsonLine( $reader );
48
		$this->assertFindsAnotherJsonLine( $reader );
49
		$this->assertNull( $reader->nextJsonLine() );
50
	}
51
52
	public function testRewind() {
53
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getOneItemDumpPath() );
54
55
		$this->assertFindsAnotherJsonLine( $reader );
56
		$reader->rewind();
57
		$this->assertFindsAnotherJsonLine( $reader );
58
		$this->assertNull( $reader->nextJsonLine() );
59
	}
60
61
	public function testResumeFromPosition() {
62
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getFiveEntitiesDumpPath() );
63
64
		$this->assertFindsEntity( $reader, 'Q1' );
65
		$this->assertFindsEntity( $reader, 'Q8' );
66
67
		$position = $reader->getPosition();
68
		unset( $reader );
69
70
		$newReader = new ExtractedDumpReader( ( new \JsonDumpData() )->getFiveEntitiesDumpPath() );
71
		$newReader->seekToPosition( $position );
72
73
		$this->assertFindsEntity( $newReader, 'P16' );
74
	}
75
76
	public function testFindsAllEntitiesInBigFile() {
77
		$reader = new ExtractedDumpReader( ( new \JsonDumpData() )->getOneThousandEntitiesDumpPath() );
78
79
		foreach ( range( 0, 999 ) as $i ) {
80
			$this->assertFindsAnotherJsonLine( $reader );
81
		}
82
83
		$this->assertNull( $reader->nextJsonLine() );
84
	}
85
86
}