LuaAskResultProcessorTest::testCanConstruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SMW\Scribunto\Tests;
4
5
use \SMW\Scribunto\LuaAskResultProcessor;
6
use \SMWQueryResult;
7
use \SMW\Query\PrintRequest;
8
use \SMWResultArray;
9
use \SMWNumberValue;
10
11
/**
12
 * @covers \SMW\Scribunto\LuaAskResultProcessor
13
 * @group semantic-scribunto
14
 *
15
 * @license GNU GPL v2+
16
 * @since 1.0
17
 *
18
 * @author Tobias Oetterer
19
 */
20
class LuaAskResultProcessorTest extends \PHPUnit_Framework_TestCase {
21
22
	/**
23
	 * Holds a mock of a query result for this test
24
	 *
25
	 * @var \SMWQueryResult
26
	 */
27
	private $queryResult;
28
29
	/**
30
	 * Set-up method prepares a mock {@see \SMWQueryResult}
31
	 */
32
	protected function setUp() {
33
34
		parent::setUp();
35
36
		$this->queryResult = $this->getMockBuilder( SMWQueryResult::class )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$this->queryResult->expects( $this->any() )
41
			->method( 'getNext' )
42
			->will( $this->onConsecutiveCalls( [ $this->constructResultArray() ], false ) );
43
44
		$this->queryResult->expects( $this->any() )
45
			->method( 'getCount' )
46
			->will( $this->returnValue( 1 ) );
47
48
	}
49
50
	/**
51
	 * Test, if the constructor works
52
	 *
53
	 * @see \SMW\Scribunto\LuaAskResultProcessor::__construct
54
	 *
55
	 * @return void
56
	 */
57
	public function testCanConstruct() {
58
59
		$this->assertInstanceOf(
60
			'\SMW\Scribunto\LuaAskResultProcessor',
61
			new LuaAskResultProcessor(
62
				$this->queryResult
63
			)
64
		);
65
	}
66
67
	/**
68
	 * Tests the conversion of a {@see \SMWQueryResult} in a lua table
69
	 *
70
	 * @see \SMW\Scribunto\LuaAskResultProcessor::getProcessedResult
71
	 *
72
	 * @return void
73
	 */
74
	public function testGetProcessedResult() {
75
76
		$instance = new LuaAskResultProcessor( $this->queryResult );
77
78
		$result = $instance->getProcessedResult();
79
80
		$this->assertInternalType(
81
			'array',
82
			$result
83
		);
84
85
		$this->assertEquals(
86
			1,
87
			count( $result )
88
		);
89
	}
90
91
	/**
92
	 * Tests the data extraction from a result row
93
	 *
94
	 * @see \SMW\Scribunto\LuaAskResultProcessor::getDataFromQueryResultRow
95
	 *
96
	 * @return void
97
	 */
98
	public function testGetDataFromQueryResultRow() {
99
100
		$instance = new LuaAskResultProcessor( $this->queryResult );
101
102
		$resultRow = [ $this->constructResultArray() ];
103
104
		$result = $instance->getDataFromQueryResultRow( $resultRow );
105
106
		$this->assertInternalType( 'array', $result );
107
108
		$this->assertEquals( 1, count( $result ) );
109
	}
110
111
	/**
112
	 * Tests the retrieval of a key (string label or numeric index) from
113
	 * a print request
114
	 *
115
	 * @see \SMW\Scribunto\LuaAskResultProcessor::getKeyFromPrintRequest
116
	 *
117
	 * @return void
118
	 */
119
	public function testGetKeyFromPrintRequest() {
120
121
		$instance = new LuaAskResultProcessor( $this->queryResult );
122
123
		$printRequest = $this->constructPrintRequest();
124
125
		$printRequest->expects( $this->any() )
126
			->method( 'getLabel' )
127
			->will( $this->returnValue( 'label' ) );
128
129
		$printRequest->expects( $this->any() )
130
			->method( 'getText' )
131
			->will( $this->returnValue( 'label' ) );
132
133
		$printRequest2 = $this->constructPrintRequest();
134
135
		$printRequest2->expects( $this->any() )
136
			->method( 'getLabel' )
137
			->will( $this->returnValue( '' ) );
138
139
		/** @noinspection PhpParamsInspection */
140
		$this->assertInternalType(
141
			'string',
142
			$instance->getKeyFromPrintRequest( $printRequest )
143
		);
144
145
		/** @noinspection PhpParamsInspection */
146
		$this->assertEquals(
147
			'label',
148
			$instance->getKeyFromPrintRequest( $printRequest )
149
		);
150
151
		/** @noinspection PhpParamsInspection */
152
		$this->assertInternalType(
153
			'integer',
154
			$instance->getKeyFromPrintRequest( $printRequest2 )
155
		);
156
157
		/** @noinspection PhpParamsInspection */
158
		$this->assertGreaterThan(
159
			0,
160
			$instance->getKeyFromPrintRequest( $printRequest2 )
161
		);
162
	}
163
164
	/**
165
	 * Tests the extraction of data from a SMWResultArray
166
	 *
167
	 * @see \SMW\Scribunto\LuaAskResultProcessor::getDataFromResultArray
168
	 *
169
	 * @return void
170
	 */
171
	public function testGetDataFromResultArray() {
172
173
		$instance = new LuaAskResultProcessor( $this->queryResult );
174
175
		$resultArray = $this->constructResultArray();
176
177
		/** @noinspection PhpParamsInspection */
178
		$this->assertInternalType(
179
			'array',
180
			$instance->getDataFromResultArray( $resultArray )
181
		);
182
	}
183
184
	/**
185
	 * Tests data value extraction. Uses data provider {@see dataProvidergetValueFromDataValueTest}
186
	 * @dataProvider dataProvidergetValueFromDataValueTest
187
	 *
188
	 * @param string $class name of data value class
189
	 * @param string $type data value type
190
	 * @param string $expects return value type
191
	 *
192
	 * @see \SMW\Scribunto\LuaAskResultProcessor::getValueFromDataValue
193
	 *
194
	 * @return void
195
	 */
196
	public function testGetValueFromDataValue( $class, $type, $expects ) {
197
198
		$instance = new LuaAskResultProcessor( $this->queryResult );
199
200
		$dataValue = $this->getMockBuilder( '\\' . $class )
201
			->setConstructorArgs( [ $type ] )
202
			->getMock();
203
204
		$dataValue->expects( $this->any() )
205
			->method( 'getTypeID' )
206
			->will( $this->returnValue( $type ) );
207
208
209
		/** @noinspection PhpParamsInspection */
210
		$this->assertInternalType(
211
			$expects,
212
			$instance->getValueFromDataValue( $dataValue )
213
		);
214
	}
215
216
	/**
217
	 * Tests the conversion of a list of result values into a value, usable in lua.
218
	 * Uses data provider {@see dataProviderExtractLuaDataFromDVData}
219
	 * @dataProvider dataProviderExtractLuaDataFromDVData
220
	 *
221
	 * @param mixed $expects expected return value
222
	 * @param array $input input for method
223
	 *
224
	 * @see \SMW\Scribunto\LuaAskResultProcessor::extractLuaDataFromDVData
225
	 *
226
	 * @return void
227
	 */
228
	public function testExtractLuaDataFromDVData( $expects, $input ) {
229
230
		$instance = new LuaAskResultProcessor( $this->queryResult );
231
232
		$this->assertEquals(
233
			$expects,
234
			$instance->extractLuaDataFromDVData( $input )
235
		);
236
	}
237
238
	/**
239
	 * Tests the generation of a numeric index key
240
	 *
241
	 * @see \SMW\Scribunto\LuaAskResultProcessor::getNumericIndex
242
	 *
243
	 * @return void
244
	 */
245
	public function testGetNumericIndex() {
246
247
		$instance = new LuaAskResultProcessor( $this->queryResult );
248
249
		$this->assertInternalType(
250
			'integer',
251
			$instance->getNumericIndex()
252
		);
253
254
		$this->assertGreaterThan(
255
			1,
256
			$instance->getNumericIndex()
257
		);
258
	}
259
260
	/**
261
	 * Data provider for {@see testgetValueFromDataValue}
262
	 *
263
	 * @see testgetValueFromDataValue
264
	 *
265
	 * @return array
266
	 */
267
	public function dataProvidergetValueFromDataValueTest() {
268
269
		return [
270
			[ 'SMWNumberValue', '_num', 'integer' ],
271
			[ 'SMWWikiPageValue', '_wpg', 'null' ],
272
			[ 'SMWStringValue', '_boo', 'boolean' ],
273
			[ 'SMWTimeValue', '_dat', 'null' ],
274
		];
275
	}
276
277
	/**
278
	 * Data provider for {@see testExtractLuaDataFromDVData}
279
	 *
280
	 * @see testExtractLuaDataFromDVData
281
	 *
282
	 * @return array
283
	 */
284
	public function dataProviderExtractLuaDataFromDVData() {
285
		return [
286
			[ null, [] ],
287
			[ 42, [ 42 ] ],
288
			[ [ 'foo', 'bar' ], [ 'foo', 'bar' ] ]
289
		];
290
	}
291
292
	/**
293
	 * Constructs a mock {@see \SMWResultArray}
294
	 *
295
	 * @return \PHPUnit_Framework_MockObject_MockObject
296
	 */
297
	private function constructResultArray() {
298
299
		$resultArray = $this->getMockBuilder( SMWResultArray::class )
300
			->disableOriginalConstructor()
301
			->getMock();
302
303
		$resultArray->expects( $this->any() )
304
			->method( 'getPrintRequest' )
305
			->will( $this->returnValue(
306
				$this->constructPrintRequest()
307
			) );
308
309
		$resultArray->expects( $this->any() )
310
			->method( 'getNextDataValue' )
311
			->will( $this->onConsecutiveCalls(
312
				$this->constructSMWNumberValue(),
313
				false
314
			) );
315
316
		return $resultArray;
317
	}
318
319
	/**
320
	 * Constructs a mock {@see \SMW\Query\PrintRequest}
321
	 *
322
	 * @return \PHPUnit_Framework_MockObject_MockObject
323
	 */
324
	private function constructPrintRequest() {
325
326
		$printRequest = $this->getMockBuilder( PrintRequest::class )
327
			->disableOriginalConstructor()
328
			->getMock();
329
330
		return $printRequest;
331
	}
332
333
334
	/**
335
	 * Constructs a mock {@see \SMWNumberValue}
336
	 *
337
	 * @return \PHPUnit_Framework_MockObject_MockObject
338
	 */
339
	private function constructSMWNumberValue() {
340
341
		$printRequest = $this->getMockBuilder( SMWNumberValue::class )
342
			->setConstructorArgs( [ '_num' ] )
343
			->getMock();
344
345
		return $printRequest;
346
	}
347
}
348