|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Queryr\DumpReader; |
|
4
|
|
|
|
|
5
|
|
|
use Queryr\DumpReader\Page; |
|
6
|
|
|
use Queryr\DumpReader\DumpReader; |
|
7
|
|
|
use Queryr\DumpReader\XmlReader\DumpXmlReader; |
|
8
|
|
|
use Queryr\DumpReader\Revision; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers Queryr\DumpReader\XmlReader\DumpXmlReader |
|
12
|
|
|
* |
|
13
|
|
|
* @licence GNU GPL v2+ |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
class DumpXmlReaderTest extends \PHPUnit_Framework_TestCase { |
|
17
|
|
|
|
|
18
|
|
|
public function testGivenFileWithNoEntities_nullIsReturned() { |
|
19
|
|
|
|
|
20
|
|
|
$reader = $this->newReaderForFile( 'simple/empty.xml' ); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertNull( $reader->nextEntityPage() ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private function newReaderForFile( $fileName ) { |
|
26
|
|
|
return new DumpXmlReader( $this->getFilePath( $fileName ) ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function getFilePath( $fileName ) { |
|
30
|
|
|
return __DIR__ . '/../../../data/' . $fileName; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function assertFindsAnotherEntity( DumpReader $reader ) { |
|
34
|
|
|
$entityPage = $reader->nextEntityPage(); |
|
35
|
|
|
$this->assertIsEntityPage( $entityPage ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function assertIsEntityPage( $entityPage ) { |
|
39
|
|
|
/** |
|
40
|
|
|
* @var Page $entityPage |
|
41
|
|
|
*/ |
|
42
|
|
|
$this->assertInstanceOf( 'Queryr\DumpReader\Page', $entityPage ); |
|
43
|
|
|
|
|
44
|
|
|
$revision = $entityPage->getRevision(); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertTrue( $revision->hasEntityModel() ); |
|
47
|
|
|
$this->assertHasEntityJson( $revision ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function assertHasEntityJson( Revision $revision ) { |
|
51
|
|
|
$entityArray = json_decode( $revision->getText(), true ); |
|
52
|
|
|
$this->assertInternalType( 'array', $entityArray ); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertArrayHasKey( 'entity', $entityArray ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testGivenFileWithOneEntity_oneEntityIsFound() { |
|
58
|
|
|
$reader = $this->newReaderForFile( 'simple/one-item.xml' ); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertFindsAnotherEntity( $reader ); |
|
61
|
|
|
$this->assertNull( $reader->nextEntityPage() ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testRewind() { |
|
65
|
|
|
$reader = $this->newReaderForFile( 'simple/one-item.xml' ); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertFindsAnotherEntity( $reader ); |
|
68
|
|
|
$this->assertNull( $reader->nextEntityPage() ); |
|
69
|
|
|
|
|
70
|
|
|
$reader->rewind(); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertFindsAnotherEntity( $reader ); |
|
73
|
|
|
$this->assertNull( $reader->nextEntityPage() ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testGivenFileWithTwoEntities_twoEntitiesAreFound() { |
|
77
|
|
|
$reader = $this->newReaderForFile( 'simple/two-items.xml' ); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertFindsAnotherEntity( $reader ); |
|
80
|
|
|
$this->assertFindsAnotherEntity( $reader ); |
|
81
|
|
|
$this->assertNull( $reader->nextEntityPage() ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testGivenEntityAmongstNonEntities_itemIsFound() { |
|
85
|
|
|
$reader = $this->newReaderForFile( 'simple/item-amongst-wikitext.xml' ); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertFindsAnotherEntity( $reader ); |
|
88
|
|
|
$this->assertNull( $reader->nextEntityPage() ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testFetchedIteratorIterationWorks() { |
|
92
|
|
|
$reader = $this->newReaderForFile( 'simple/two-items.xml' )->getIterator(); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertCount( 2, iterator_to_array( $reader ) ); |
|
95
|
|
|
$this->assertContainsOnlyInstancesOf( 'Queryr\DumpReader\Page', $reader ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testCanUseAsTraversable() { |
|
99
|
|
|
$reader = $this->newReaderForFile( 'simple/two-items.xml' ); |
|
100
|
|
|
|
|
101
|
|
|
foreach ( $reader as $page ) { |
|
102
|
|
|
$this->assertIsEntityPage( $page ); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testGivenManyRevisions_allPropertiesAreFound() { |
|
107
|
|
|
$reader = $this->newReaderForFile( 'big/5341-revs-3-props.xml' ); |
|
108
|
|
|
|
|
109
|
|
|
$propertyCount = 0; |
|
110
|
|
|
|
|
111
|
|
|
while ( $page = $reader->nextEntityPage() ) { |
|
112
|
|
|
$this->assertIsEntityPage( $page ); |
|
113
|
|
|
|
|
114
|
|
|
if ( $this->isPropertyPage( $page ) ) { |
|
115
|
|
|
$propertyCount++; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals( 3, $propertyCount ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function isPropertyPage( Page $page ) { |
|
123
|
|
|
$array = json_decode( $page->getRevision()->getText(), true ); |
|
124
|
|
|
return $array['entity'][0] === 'property'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testGivenItemWithoutRevision_exceptionInThrown() { |
|
128
|
|
|
$reader = $this->newReaderForFile( 'invalid/item-without-revision.xml' ); |
|
129
|
|
|
|
|
130
|
|
|
$this->setExpectedException( 'Queryr\DumpReader\DumpReaderException' ); |
|
131
|
|
|
$reader->nextEntityPage(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testGivenFileWithOneEntity_correctPageObjectIsReturned() { |
|
135
|
|
|
$page = $this->newReaderForFile( 'simple/one-item.xml' )->nextEntityPage(); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertEquals( 'Q15831780', $page->getTitle() ); |
|
138
|
|
|
$this->assertEquals( '0', $page->getNamespace() ); |
|
139
|
|
|
$this->assertEquals( '17459977', $page->getId() ); |
|
140
|
|
|
|
|
141
|
|
|
$revision = $page->getRevision(); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertEquals( '112488012', $revision->getId() ); |
|
144
|
|
|
$this->assertEquals( '2014-02-27T11:40:12Z', $revision->getTimeStamp() ); |
|
145
|
|
|
$this->assertEquals( 'wikibase-item', $revision->getModel() ); |
|
146
|
|
|
$this->assertEquals( 'application/json', $revision->getFormat() ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testSeekToTitleSkipsNonMatchingPagesAndReturnsTheMatchingOne() { |
|
150
|
|
|
$reader = $this->newReaderForFile( 'simple/five-items.xml' ); |
|
151
|
|
|
|
|
152
|
|
|
$page = $reader->seekToTitle( 'Q15826086' ); |
|
153
|
|
|
$this->assertEquals( 'Q15826086', $page->getTitle() ); |
|
154
|
|
|
|
|
155
|
|
|
$page = $reader->nextEntityPage(); |
|
156
|
|
|
$this->assertEquals( 'Q15826087', $page->getTitle() ); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testWhenNoMatchingPages_seekToTitleReturnsNull() { |
|
160
|
|
|
$this->assertNull( $this->newReaderForFile( 'simple/five-items.xml' )->seekToTitle( 'Q1' ) ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |