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

Bz2DumpReaderTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\Bz2DumpReader;
9
10
/**
11
 * @covers \Wikibase\JsonDumpReader\Reader\Bz2DumpReader
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class Bz2DumpReaderTest extends TestCase {
17
18
	public function setUp() {
19
		if ( !function_exists( 'bzopen' ) ) {
20
			self::markTestSkipped( 'bz2 is not installed' );
21
		}
22
	}
23
24
	private function assertFindsEntity( Bz2DumpReader $reader, $expectedId ) {
25
		$line = $reader->nextJsonLine();
26
		$this->assertJson( $line );
27
		$this->assertContains( $expectedId, $line );
28
	}
29
30
	public function testGivenInvalidPath_exceptionIsThrown() {
31
		$this->expectException( 'RuntimeException' );
32
		$reader = new Bz2DumpReader( __DIR__ . '/../../data/does-not-exist.json.bz2' );
33
		$reader->nextJsonLine();
34
	}
35
36
	public function testGivenFileWithNoEntities_nullIsReturned() {
37
		$reader = new Bz2DumpReader( ( new \JsonDumpData() )->getEmptyBz2DumpPath() );
38
39
		$this->assertNull( $reader->nextJsonLine() );
40
	}
41
42
	public function testGivenFileWithFiveEntities_fiveEntityAreFound() {
43
		$reader = new Bz2DumpReader( ( new \JsonDumpData() )->getFiveEntitiesBz2DumpPath() );
44
45
		$this->assertFindsEntity( $reader, 'Q1' );
46
		$this->assertFindsEntity( $reader, 'Q8' );
47
		$this->assertFindsEntity( $reader, 'P16' );
48
		$this->assertFindsEntity( $reader, 'P19' );
49
		$this->assertFindsEntity( $reader, 'P22' );
50
		$this->assertNull( $reader->nextJsonLine() );
51
	}
52
53
	public function testRewind() {
54
		$reader = new Bz2DumpReader( ( new \JsonDumpData() )->getFiveEntitiesBz2DumpPath() );
55
56
		$this->assertFindsEntity( $reader, 'Q1' );
57
		$this->assertFindsEntity( $reader, 'Q8' );
58
		$reader->rewind();
59
		$this->assertFindsEntity( $reader, 'Q1' );
60
	}
61
62
}