GzDumpReaderTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGivenInvalidGzPath_exceptionIsThrown() 0 5 1
A assertFindsEntity() 0 5 1
A testGivenGzFileWithNoEntities_nullIsReturned() 0 4 1
A testGivenGzFileWithFiveEntities_fiveEntityAreFound() 0 10 1
A testRewind() 0 8 1
A testResumeFromPosition() 0 14 1
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\GzDumpReader;
9
10
/**
11
 * @covers \Wikibase\JsonDumpReader\Reader\GzDumpReader
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class GzDumpReaderTest extends TestCase {
17
18
	public function testGivenInvalidGzPath_exceptionIsThrown() {
19
		$this->expectException( 'RuntimeException' );
20
		$reader = new GzDumpReader( __DIR__ . '/../../data/does-not-exist.json.gz' );
21
		$reader->nextJsonLine();
22
	}
23
24
	private function assertFindsEntity( GzDumpReader $reader, $expectedId ) {
25
		$line = $reader->nextJsonLine();
26
		$this->assertJson( $line );
27
		$this->assertContains( $expectedId, $line );
28
	}
29
30
	public function testGivenGzFileWithNoEntities_nullIsReturned() {
31
		$reader = new GzDumpReader( ( new \JsonDumpData() )->getEmptyGzDumpPath() );
32
		$this->assertNull( $reader->nextJsonLine() );
33
	}
34
35
	public function testGivenGzFileWithFiveEntities_fiveEntityAreFound() {
36
		$reader = new GzDumpReader( ( new \JsonDumpData() )->getFiveEntitiesGzDumpPath() );
37
38
		$this->assertFindsEntity( $reader, 'Q1' );
39
		$this->assertFindsEntity( $reader, 'Q8' );
40
		$this->assertFindsEntity( $reader, 'P16' );
41
		$this->assertFindsEntity( $reader, 'P19' );
42
		$this->assertFindsEntity( $reader, 'P22' );
43
		$this->assertNull( $reader->nextJsonLine() );
44
	}
45
46
	public function testRewind() {
47
		$reader = new GzDumpReader( ( new \JsonDumpData() )->getFiveEntitiesGzDumpPath() );
48
49
		$this->assertFindsEntity( $reader, 'Q1' );
50
		$this->assertFindsEntity( $reader, 'Q8' );
51
		$reader->rewind();
52
		$this->assertFindsEntity( $reader, 'Q1' );
53
	}
54
55
	public function testResumeFromPosition() {
56
		$reader = new GzDumpReader( ( new \JsonDumpData() )->getFiveEntitiesGzDumpPath() );
57
58
		$this->assertFindsEntity( $reader, 'Q1' );
59
		$this->assertFindsEntity( $reader, 'Q8' );
60
61
		$position = $reader->getPosition();
62
		unset( $reader );
63
64
		$newReader = new GzDumpReader( ( new \JsonDumpData() )->getFiveEntitiesGzDumpPath() );
65
		$newReader->seekToPosition( $position );
66
67
		$this->assertFindsEntity( $newReader, 'P16' );
68
	}
69
70
}