Completed
Push — master ( dd8aad...6526e1 )
by mw
04:06
created

testHttpRequestToReturnWithError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 9.6716
cc 1
eloc 38
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SEQL\ByAskApiHttpRequest\Tests;
4
5
use SEQL\ByAskApiHttpRequest\QueryResultFetcher;
6
use SEQL\QueryResultFactory;
7
use SMW\DIWikiPage;
8
use SMW\DIProperty;
9
10
/**
11
 * @covers \SEQL\ByAskApiHttpRequest\QueryResultFetcher
12
 * @group semantic-external-query-lookup
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class QueryResultFetcherTest extends \PHPUnit_Framework_TestCase {
20
21
	private $store;
22
	private $httpRequest;
23
	private $httpRequestFactory;
24
	private $jsonResponseParser;
25
26
	protected function setUp() {
27
28
		$this->store = $this->getMockBuilder( '\SMW\Store' )
29
			->disableOriginalConstructor()
30
			->getMockForAbstractClass();
31
32
		$this->httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\HttpRequest' )
33
			->disableOriginalConstructor()
34
			->getMock();
35
36
		$this->httpRequestFactory = $this->getMockBuilder( '\Onoi\HttpRequest\HttpRequestFactory' )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$this->httpRequestFactory->expects( $this->any() )
41
			->method( 'newCachedCurlRequest' )
42
			->will( $this->returnValue( $this->httpRequest ) );
43
44
		$this->jsonResponseParser = $this->getMockBuilder( '\SEQL\ByAskApiHttpRequest\JsonResponseParser' )
45
			->disableOriginalConstructor()
46
			->getMock();
47
	}
48
49
	public function testCanConstruct() {
50
51
		$queryResultFactory = $this->getMockBuilder( '\SEQL\QueryResultFactory' )
52
			->disableOriginalConstructor()
53
			->getMock();
54
55
		$this->assertInstanceOf(
56
			'\SEQL\ByAskApiHttpRequest\QueryResultFetcher',
57
			new QueryResultFetcher( $this->httpRequestFactory, $queryResultFactory, $this->jsonResponseParser )
58
		);
59
	}
60
61
	public function testFetchEmptyQueryResult() {
62
63
		$queryResultFactory = new QueryResultFactory( $this->store );
64
65
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
66
			->disableOriginalConstructor()
67
			->getMock();
68
69
		$printRequest->expects( $this->any() )
70
			->method( 'getSerialisation' )
71
			->will( $this->returnValue( '?ABC' ) );
72
73
		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
74
			->disableOriginalConstructor()
75
			->getMock();
76
77
		$description->expects( $this->any() )
78
			->method( 'getPrintrequests' )
79
			->will( $this->returnValue( array( $printRequest ) ) );
80
81
		$query = $this->getMockBuilder( '\SMWQuery' )
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$query->expects( $this->any() )
86
			->method( 'getSortKeys' )
87
			->will( $this->returnValue( array() ) );
88
89
		$query->expects( $this->any() )
90
			->method( 'getExtraPrintouts' )
91
			->will( $this->returnValue( array( $printRequest ) ) );
92
93
		$query->expects( $this->any() )
94
			->method( 'getDescription' )
95
			->will( $this->returnValue( $description ) );
96
97
		$instance = new QueryResultFetcher(
98
			$this->httpRequestFactory,
99
			$queryResultFactory,
100
			$this->jsonResponseParser
101
		);
102
103
		$this->assertInstanceOf(
104
			'\SMWQueryResult',
105
			$instance->fetchQueryResult( $query )
106
		);
107
	}
108
109
	public function testResetOfPrintRequest() {
110
111
		$queryResultFactory = new QueryResultFactory( $this->store );
112
113
		$dataValue = $this->getMockBuilder( '\SMWPropertyValue' )
114
			->disableOriginalConstructor()
115
			->getMock();
116
117
		$dataValue->expects( $this->once() )
118
			->method( 'getDataItem' )
119
			->will( $this->returnValue( new DIProperty( 'Foo' ) ) );
120
121
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
122
			->disableOriginalConstructor()
123
			->getMock();
124
125
		$printRequest->expects( $this->any() )
126
			->method( 'getSerialisation' )
127
			->will( $this->returnValue( '?ABC' ) );
128
129
		$printRequest->expects( $this->atLeastOnce() )
130
			->method( 'getData' )
131
			->will( $this->returnValue( $dataValue ) );
132
133
		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
134
			->disableOriginalConstructor()
135
			->getMock();
136
137
		$description->expects( $this->any() )
138
			->method( 'getPrintrequests' )
139
			->will( $this->returnValue( array( $printRequest ) ) );
140
141
		$query = $this->getMockBuilder( '\SMWQuery' )
142
			->disableOriginalConstructor()
143
			->getMock();
144
145
		$query->expects( $this->any() )
146
			->method( 'getSortKeys' )
147
			->will( $this->returnValue( array() ) );
148
149
		$query->expects( $this->any() )
150
			->method( 'getExtraPrintouts' )
151
			->will( $this->returnValue( array( $printRequest ) ) );
152
153
		$query->expects( $this->any() )
154
			->method( 'getDescription' )
155
			->will( $this->returnValue( $description ) );
156
157
		$instance = new QueryResultFetcher(
158
			$this->httpRequestFactory,
159
			$queryResultFactory,
160
			$this->jsonResponseParser
161
		);
162
163
		$this->assertInstanceOf(
164
			'\SMWQueryResult',
165
			$instance->fetchQueryResult( $query )
166
		);
167
	}
168
169
	public function testFetchQueryResultWithResponseCache() {
170
171
		$queryResultFactory = new QueryResultFactory( $this->store );
172
173
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
174
			->disableOriginalConstructor()
175
			->getMock();
176
177
		$printRequest->expects( $this->any() )
178
			->method( 'getSerialisation' )
179
			->will( $this->returnValue( '?ABC' ) );
180
181
		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
182
			->disableOriginalConstructor()
183
			->getMock();
184
185
		$description->expects( $this->any() )
186
			->method( 'getPrintrequests' )
187
			->will( $this->returnValue( array( $printRequest ) ) );
188
189
		$query = $this->getMockBuilder( '\SMWQuery' )
190
			->disableOriginalConstructor()
191
			->getMock();
192
193
		$query->expects( $this->any() )
194
			->method( 'getSortKeys' )
195
			->will( $this->returnValue( array() ) );
196
197
		$query->expects( $this->any() )
198
			->method( 'getExtraPrintouts' )
199
			->will( $this->returnValue( array( $printRequest ) ) );
200
201
		$query->expects( $this->any() )
202
			->method( 'getDescription' )
203
			->will( $this->returnValue( $description ) );
204
205
		$instance = new QueryResultFetcher(
206
			$this->httpRequestFactory,
207
			$queryResultFactory,
208
			$this->jsonResponseParser
209
		);
210
211
		$instance->setHttpResponseCacheLifetime( 42 );
212
		$instance->setHttpResponseCachePrefix( 'Foo' );
213
214
		// PHUNIT 4.1
215
	//	$this->httpRequest->expects( $this->any() )
0 ignored issues
show
Unused Code Comprehensibility introduced by mwjames
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
216
	//		->method( 'setOption' )
0 ignored issues
show
Unused Code Comprehensibility introduced by mwjames
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
217
	//		->withConsecutive(
218
	//			array( $this->anything(), $this->equalTo( 42 ) ),
0 ignored issues
show
Unused Code Comprehensibility introduced by mwjames
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
219
	//			array( $this->anything(), $this->equalTo( 'Foo:' ) ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by mwjames
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
220
221
		$this->httpRequest->expects( $this->at( 0 ) )
222
			->method( 'setOption' )
223
			->with( $this->anything(), $this->equalTo( 42 ) );
224
225
		$this->httpRequest->expects( $this->at( 1 ) )
226
			->method( 'setOption' )
227
			->with( $this->anything(), $this->equalTo( 'Foo:' ) );
228
229
		$this->assertInstanceOf(
230
			'\SMWQueryResult',
231
			$instance->fetchQueryResult( $query )
232
		);
233
	}
234
235
	public function testHttpRequestToReturnWithError() {
236
237
		$queryResultFactory = new QueryResultFactory( $this->store );
238
239
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
240
			->disableOriginalConstructor()
241
			->getMock();
242
243
		$printRequest->expects( $this->any() )
244
			->method( 'getSerialisation' )
245
			->will( $this->returnValue( '?ABC' ) );
246
247
		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
248
			->disableOriginalConstructor()
249
			->getMock();
250
251
		$description->expects( $this->any() )
252
			->method( 'getPrintrequests' )
253
			->will( $this->returnValue( array( $printRequest ) ) );
254
255
		$query = $this->getMockBuilder( '\SMWQuery' )
256
			->disableOriginalConstructor()
257
			->getMock();
258
259
		$query->expects( $this->any() )
260
			->method( 'getSortKeys' )
261
			->will( $this->returnValue( array() ) );
262
263
		$query->expects( $this->any() )
264
			->method( 'getExtraPrintouts' )
265
			->will( $this->returnValue( array( $printRequest ) ) );
266
267
		$query->expects( $this->any() )
268
			->method( 'getDescription' )
269
			->will( $this->returnValue( $description ) );
270
271
		$instance = new QueryResultFetcher(
272
			$this->httpRequestFactory,
273
			$queryResultFactory,
274
			$this->jsonResponseParser
275
		);
276
277
		$instance->setHttpRequestEndpoint( 'http://example.org/api.php' );
278
		$instance->setRepositoryTargetUrl( 'http://example.org/$1' );
279
280
		$this->httpRequest->expects( $this->any() )
281
			->method( 'execute' )
282
			->will( $this->returnValue( json_encode( array( 'error' => array( 'info' => 'error' ) ) ) ) );
283
284
		$this->assertInstanceOf(
285
			'\SMWQueryResult',
286
			$instance->fetchQueryResult( $query )
287
		);
288
	}
289
290
	public function testHttpRequestToReturnWithValidJson() {
291
292
		$queryResultFactory = new QueryResultFactory( $this->store );
293
294
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
295
			->disableOriginalConstructor()
296
			->getMock();
297
298
		$printRequest->expects( $this->any() )
299
			->method( 'getSerialisation' )
300
			->will( $this->returnValue( '?ABC' ) );
301
302
		$description = $this->getMockBuilder( '\SMW\Query\Language\Description' )
303
			->disableOriginalConstructor()
304
			->getMock();
305
306
		$description->expects( $this->any() )
307
			->method( 'getPrintrequests' )
308
			->will( $this->returnValue( array( $printRequest ) ) );
309
310
		$query = $this->getMockBuilder( '\SMWQuery' )
311
			->disableOriginalConstructor()
312
			->getMock();
313
314
		$query->expects( $this->any() )
315
			->method( 'getSortKeys' )
316
			->will( $this->returnValue( array() ) );
317
318
		$query->expects( $this->any() )
319
			->method( 'getExtraPrintouts' )
320
			->will( $this->returnValue( array( $printRequest ) ) );
321
322
		$query->expects( $this->any() )
323
			->method( 'getDescription' )
324
			->will( $this->returnValue( $description ) );
325
326
		$this->jsonResponseParser->expects( $this->any() )
327
			->method( 'getResultSubjectList' )
328
			->will( $this->returnValue( array() ) );
329
330
		$instance = new QueryResultFetcher(
331
			$this->httpRequestFactory,
332
			$queryResultFactory,
333
			$this->jsonResponseParser
334
		);
335
336
		$instance->setHttpRequestEndpoint( 'http://example.org/api.php' );
337
		$instance->setRepositoryTargetUrl( 'http://example.org/$1' );
338
339
		$expected = array(
340
			'query-continue-offset' => 3,
341
			'query' => array()
342
		);
343
344
		$this->httpRequest->expects( $this->once() )
345
			->method( 'execute' )
346
			->will( $this->returnValue( json_encode( $expected ) ) );
347
348
		$this->assertInstanceOf(
349
			'\SMWQueryResult',
350
			$instance->fetchQueryResult( $query )
351
		);
352
	}
353
354
}
355