|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SCQ\Tests\Api; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\Tests\Utils\MwApiFactory; |
|
6
|
|
|
use SCQ\Api\CompoundQuery; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers \SCQ\Api\CompoundQuery |
|
10
|
|
|
* @group semantic-compound-queries |
|
11
|
|
|
* |
|
12
|
|
|
* @license GNU GPL v2+ |
|
13
|
|
|
* @since 1.0 |
|
14
|
|
|
* |
|
15
|
|
|
* @author Peter Grassberger < [email protected] > |
|
16
|
|
|
*/ |
|
17
|
|
|
class CompoundQueryTest extends \PHPUnit_Framework_TestCase { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var MwApiFactory |
|
21
|
|
|
*/ |
|
22
|
|
|
private $apiFactory; |
|
23
|
|
|
|
|
24
|
|
|
protected function setUp() { |
|
25
|
|
|
parent::setUp(); |
|
26
|
|
|
$this->apiFactory = new MwApiFactory(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testCanConstruct() { |
|
30
|
|
|
|
|
31
|
|
|
$instance = new CompoundQuery( |
|
32
|
|
|
$this->apiFactory->newApiMain( [ 'query' => 'Foo' ] ), |
|
33
|
|
|
'compoundquery' |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertInstanceOf( |
|
37
|
|
|
CompoundQuery::class, |
|
38
|
|
|
$instance |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @dataProvider sampleQueryProvider |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testExecute( array $query, array $expected ) { |
|
46
|
|
|
|
|
47
|
|
|
$results = $this->apiFactory->doApiRequest( [ |
|
48
|
|
|
'action' => 'compoundquery', |
|
49
|
|
|
'query' => implode( '|', $query ) |
|
50
|
|
|
] ); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertInternalType( |
|
53
|
|
|
'array', |
|
54
|
|
|
$results |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
// If their is no printrequests array we expect an error array |
|
58
|
|
|
if ( isset( $results['query']['printrequests'] ) ) { |
|
59
|
|
|
$this->assertEquals( $expected, $results['query']['printrequests'] ); |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->assertArrayHasKey( 'error', $results ); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function sampleQueryProvider() { |
|
66
|
|
|
|
|
67
|
|
|
$provider['Standard query'] = [ |
|
|
|
|
|
|
68
|
|
|
[ |
|
69
|
|
|
'[[Modification date::+]];?Modification date;limit=10', |
|
70
|
|
|
], |
|
71
|
|
|
[ |
|
72
|
|
|
[ |
|
73
|
|
|
'label'=> '', |
|
74
|
|
|
'typeid' => '_wpg', |
|
75
|
|
|
'mode' => 2, |
|
76
|
|
|
'format' => false, |
|
77
|
|
|
'key' => '', |
|
78
|
|
|
'redi' => '' |
|
79
|
|
|
], |
|
80
|
|
|
[ |
|
81
|
|
|
'label'=> 'Modification date', |
|
82
|
|
|
'typeid' => '_dat', |
|
83
|
|
|
'mode' => 1, |
|
84
|
|
|
'format' => '', |
|
85
|
|
|
'key' => '_MDAT', |
|
86
|
|
|
'redi' => '' |
|
87
|
|
|
] |
|
88
|
|
|
] |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
$provider['Compound query'] = [ |
|
92
|
|
|
[ |
|
93
|
|
|
'[[Modification date::+]];?Modification date;limit=10', |
|
94
|
|
|
'[[Modification date::+]];?Modification date' |
|
95
|
|
|
], |
|
96
|
|
|
[ |
|
97
|
|
|
[ |
|
98
|
|
|
'label'=> '', |
|
99
|
|
|
'typeid' => '_wpg', |
|
100
|
|
|
'mode' => 2, |
|
101
|
|
|
'format' => false, |
|
102
|
|
|
'key' => '', |
|
103
|
|
|
'redi' => '' |
|
104
|
|
|
], |
|
105
|
|
|
[ |
|
106
|
|
|
'label'=> 'Modification date', |
|
107
|
|
|
'typeid' => '_dat', |
|
108
|
|
|
'mode' => 1, |
|
109
|
|
|
'format' => '', |
|
110
|
|
|
'key' => '_MDAT', |
|
111
|
|
|
'redi' => '' |
|
112
|
|
|
] |
|
113
|
|
|
] |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
return $provider; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.