Completed
Push — master ( 0a532a...959a2b )
by mw
122:08 queued 105:20
created

testQueryProcessorWithInvalidSourceSwitchesToDefault()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Integration\Query;
4
5
use SMW\StoreFactory;
6
use SMWQueryProcessor;
7
use SMW\Tests\TestEnvironment;
8
9
/**
10
 * @group semantic-mediawiki
11
 * @group medium
12
 *
13
 * @license GNU GPL v2+
14
 * @since   1.9.2
15
 *
16
 * @author mwjames
17
 */
18
class QuerySourceIntegrationTest extends \PHPUnit_Framework_TestCase {
19
20
	private $testEnvironment;
21
	private $store;
22
23
	protected function setUp() {
24
		parent::setUp();
25
26
		$this->testEnvironment = new TestEnvironment();
27
28
		$this->testEnvironment->addConfiguration(
29
			'smwgQuerySources',
30
			array(
31
				'foo'    => 'SMW\Tests\Utils\Mock\FakeQueryStore',
32
				'foobar' => 'SMW\Tests\Integration\Query\AnotherFakeQueryStoreWhichDoesNotImplentTheQueryEngineInterface',
33
				'bar'    => 'SMW\Tests\NonExistentQueryStore',
34
			)
35
		);
36
37
		$this->store = $this->getMockBuilder( '\SMW\Store' )
38
			->disableOriginalConstructor()
39
			->getMockForAbstractClass();
40
41
		$this->testEnvironment->registerObject( 'Store', $this->store );
42
	}
43
44
	protected function tearDown() {
45
		$this->testEnvironment->tearDown();
46
		parent::tearDown();
47
	}
48
49
	public function testQueryProcessorWithDefaultSource() {
50
51
		$queryResult = $this->getMockBuilder( 'SMWQueryResult' )
52
			->disableOriginalConstructor()
53
			->getMock();
54
55
		$queryResult->expects( $this->atLeastOnce() )
56
			->method( 'getErrors' )
57
			->will( $this->returnValue( array() ) );
58
59
		$this->store->expects( $this->atLeastOnce() )
60
			->method( 'getQueryResult' )
61
			->will( $this->returnValue( $queryResult ) );
62
63
		$rawParams = array(
64
			'[[Modification date::+]]',
65
			'?Modification date',
66
			'format=list',
67
			'source=default'
68
		);
69
70
		$this->assertInternalType(
71
			'string',
72
			$this->makeQueryResultFromRawParameters( $rawParams )
73
		);
74
	}
75
76
	public function testQueryProcessorWithValidSource() {
77
78
		$queryResult = $this->getMockBuilder( 'SMWQueryResult' )
79
			->disableOriginalConstructor()
80
			->getMock();
81
82
		$queryResult->expects( $this->any() )
83
			->method( 'getErrors' )
84
			->will( $this->returnValue( array() ) );
85
86
		$this->store->expects( $this->atLeastOnce() )
87
			->method( 'getQueryResult' )
88
			->will( $this->returnValue( $queryResult ) );
89
90
		$rawParams = array(
91
			'[[Modification date::+]]',
92
			'?Modification date',
93
			'format=list',
94
			'source=foo'
95
		);
96
97
		$this->assertInternalType(
98
			'string',
99
			$this->makeQueryResultFromRawParameters( $rawParams )
100
		);
101
	}
102
103
	public function testQueryProcessorWithInvalidSourceSwitchesToDefault() {
104
105
		$queryResult = $this->getMockBuilder( 'SMWQueryResult' )
106
			->disableOriginalConstructor()
107
			->getMock();
108
109
		$queryResult->expects( $this->atLeastOnce() )
110
			->method( 'getErrors' )
111
			->will( $this->returnValue( array() ) );
112
113
		$this->store->expects( $this->atLeastOnce() )
114
			->method( 'getQueryResult' )
115
			->will( $this->returnValue( $queryResult ) );
116
117
		$rawParams = array(
118
			'[[Modification date::+]]',
119
			'?Modification date',
120
			'format=list',
121
			'source=bar'
122
		);
123
124
		$this->assertInternalType(
125
			'string',
126
			$this->makeQueryResultFromRawParameters( $rawParams )
127
		);
128
	}
129
130
	public function testQuerySourceOnCount() {
131
132
		$queryResult = $this->getMockBuilder( 'SMWQueryResult' )
133
			->disableOriginalConstructor()
134
			->getMock();
135
136
		$queryResult->expects( $this->atLeastOnce() )
137
			->method( 'getCountValue' )
138
			->will( $this->returnValue( 42 ) );
139
140
		$this->store->expects( $this->atLeastOnce() )
141
			->method( 'getQueryResult' )
142
			->will( $this->returnValue( $queryResult ) );
143
144
		$rawParams = array(
145
			'[[Modification date::+]]',
146
			'?Modification date',
147
			'format=count',
148
			'source=foo'
149
		);
150
151
		$this->assertInternalType(
152
			'string',
153
			$this->makeQueryResultFromRawParameters( $rawParams )
154
		);
155
	}
156
157
	public function testQueryProcessorWithInvalidSource() {
158
159
		$rawParams = array(
160
			'[[Modification date::+]]',
161
			'?Modification date',
162
			'format=list',
163
			'source=foobar'
164
		);
165
166
		$this->setExpectedException( 'RuntimeException' );
167
		$this->makeQueryResultFromRawParameters( $rawParams );
168
	}
169
170
	protected function makeQueryResultFromRawParameters( $rawParams ) {
171
172
		list( $query, $params ) = SMWQueryProcessor::getQueryAndParamsFromFunctionParams(
173
			$rawParams,
174
			SMW_OUTPUT_WIKI,
175
			SMWQueryProcessor::INLINE_QUERY,
176
			false
177
		);
178
179
		return SMWQueryProcessor::getResultFromQuery(
180
			$query,
181
			$params,
182
			SMW_OUTPUT_WIKI,
183
			SMWQueryProcessor::INLINE_QUERY
184
		);
185
	}
186
187
}
188
189
class AnotherFakeQueryStoreWhichDoesNotImplentTheQueryEngineInterface {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
190
}
191