1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Queryr\Replicator\EntitySource\Api; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Queryr\Replicator\EntitySource\Api\GetEntitiesInterpreter; |
7
|
|
|
use Queryr\Replicator\Model\EntityPage; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Queryr\Replicator\EntitySource\Api\GetEntitiesInterpreter |
11
|
|
|
* |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class GetEntitiesInterpreterTest extends TestCase { |
16
|
|
|
|
17
|
|
|
public function testGivenInvalidJson_emptyArrayIsReturned() { |
18
|
|
|
$interpreter = new GetEntitiesInterpreter(); |
19
|
|
|
|
20
|
|
|
$entityPages = $interpreter->getEntityPagesFromResult( '~=[,,_,,]:3' ); |
21
|
|
|
|
22
|
|
|
$this->assertSame( [], $entityPages ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGivenMissingIdResponse_emptyArrayIsReturned() { |
26
|
|
|
$response = <<<EOT |
27
|
|
|
{ |
28
|
|
|
"entities": { |
29
|
|
|
"-1": { |
30
|
|
|
"id": "Q99999999", |
31
|
|
|
"missing": "" |
32
|
|
|
}, |
33
|
|
|
"-2": { |
34
|
|
|
"id": "Q999999991", |
35
|
|
|
"missing": "" |
36
|
|
|
} |
37
|
|
|
}, |
38
|
|
|
"success": 1 |
39
|
|
|
} |
40
|
|
|
EOT; |
41
|
|
|
|
42
|
|
|
$interpreter = new GetEntitiesInterpreter(); |
43
|
|
|
|
44
|
|
|
$entityPages = $interpreter->getEntityPagesFromResult( $response ); |
45
|
|
|
|
46
|
|
|
$this->assertSame( [], $entityPages ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGivenErrorResponse_emptyArrayIsReturned() { |
50
|
|
|
$response = <<<EOT |
51
|
|
|
{ |
52
|
|
|
"servedby": "mw1130", |
53
|
|
|
"error": { |
54
|
|
|
"code": "no-such-entity", |
55
|
|
|
"info": "Invalid id: Q0" |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
EOT; |
59
|
|
|
|
60
|
|
|
$interpreter = new GetEntitiesInterpreter(); |
61
|
|
|
|
62
|
|
|
$entityPages = $interpreter->getEntityPagesFromResult( $response ); |
63
|
|
|
|
64
|
|
|
$this->assertSame( [], $entityPages ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGivenQ1Response_arrayWithQ1IsReturned() { |
68
|
|
|
$response = file_get_contents( __DIR__ . '/wbgetentities-Q1.json' ); |
69
|
|
|
|
70
|
|
|
$interpreter = new GetEntitiesInterpreter(); |
71
|
|
|
|
72
|
|
|
$entityPages = $interpreter->getEntityPagesFromResult( $response ); |
73
|
|
|
|
74
|
|
|
$this->assertCount( 1, $entityPages ); |
75
|
|
|
$this->assertIsQ1Page( $entityPages[0] ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function assertIsQ1Page( EntityPage $entityPage ) { |
79
|
|
|
$this->assertSame( 0, $entityPage->getNamespaceId() ); |
80
|
|
|
$this->assertSame( '2014-06-16T06:28:09Z', $entityPage->getRevisionTime() ); |
81
|
|
|
$this->assertSame( 138106750, $entityPage->getRevisionId() ); |
82
|
|
|
$this->assertSame( 'Q1', $entityPage->getTitle() ); |
83
|
|
|
$this->assertContains( 'universe', $entityPage->getEntityJson() ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGivenMultipleEntityResponse_arrayWithAllEntitiesIsReturned() { |
87
|
|
|
$response = file_get_contents( __DIR__ . '/wbgetentities-Q1Q2Q3.json' ); |
88
|
|
|
|
89
|
|
|
$interpreter = new GetEntitiesInterpreter(); |
90
|
|
|
|
91
|
|
|
$entityPages = $interpreter->getEntityPagesFromResult( $response ); |
92
|
|
|
|
93
|
|
|
$this->assertCount( 3, $entityPages ); |
94
|
|
|
$this->assertIsQ1Page( $entityPages[0] ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |