Completed
Push — master ( eee289...fdf890 )
by mw
19:14
created

testGetValueFromDataValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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