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
|
|
|
} |