Completed
Push — master ( 27c003...7bb1fe )
by mw
16:46
created

ScribuntoLuaLibraryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Scribunto\Tests;
4
5
use SMW\Scribunto\ScribuntoLuaLibrary;
6
7
/**
8
 * @covers \SMW\Scribunto\ScribuntoLuaLibrary
9
 *
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author oetterer
14
 */
15
class ScribuntoLuaLibraryTest extends \Scribunto_LuaEngineTestBase {
16
17
	/**
18
	 * @var \SMW\Scribunto\ScribuntoLuaLibrary
19
	 */
20
	protected $scribuntoLuaLibrary;
21
22
	/**
23
	 * Lua test module
24
	 * @var string
25
	 */
26
	protected static $moduleName = self::class;
27
28
	/**
29
	 * ScribuntoLuaEngineTestBase::getTestModules
30
	 */
31
	public function getTestModules() {
32
		return parent::getTestModules() + array(
33
			self::$moduleName => __DIR__ . '/' . 'mw.smw.tests.lua',
34
		);
35
	}
36
37
	protected function setUp() {
38
		parent::setUp();
39
40
		/** @noinspection PhpParamsInspection */
41
		$this->scribuntoLuaLibrary = new ScribuntoLuaLibrary(
42
			$this->getEngine()
43
		);
44
	}
45
46
	public function testCanConstruct() {
47
		$this->assertInstanceOf(
48
			'\SMW\Scribunto\ScribuntoLuaLibrary',
49
			$this->scribuntoLuaLibrary
50
		);
51
	}
52
53
	/**
54
	 * Test, if all the necessary methods exists. Uses data provider {@see dataProviderFunctionTest}
55
	 * @dataProvider dataProviderFunctionTest
56
	 *
57
	 * @param string $method name of method to check
58
	 *
59
	 * @used \SMW\Scribunto\Tests\ScribuntoLuaLibraryTest::dataProviderFunctionTest
60
	 *
61
	 * @return void
62
	 */
63
	public function testMethodsExist( $method ) {
64
		$this->assertTrue(
65
			method_exists( $this->scribuntoLuaLibrary, $method ),
66
			'Class \SMW\Scribunto\ScribuntoLuaLibrary has method \'' . $method . '()\' missing!'
67
		);
68
	}
69
70
	/**
71
	 * Tests method getPropertyType
72
	 *
73
	 * @return void
74
	 */
75
	public function testGetPropertyType() {
76
		$this->assertEmpty(
77
			$this->scribuntoLuaLibrary->getPropertyType( '' )[0]
78
		);
79
		$this->assertEquals(
80
			'_dat',
81
			$this->scribuntoLuaLibrary->getPropertyType( 'Modification date' )[0]
82
		);
83
		$this->assertEquals(
84
			'_wpg',
85
			$this->scribuntoLuaLibrary->getPropertyType( 'Foo' )[0]
86
		);
87
	}
88
89
	/**
90
	 * Tests method getQueryResult
91
	 *
92
	 * @return void
93
	 */
94
	public function testGetQueryResult() {
95
		$this->assertArrayHasKey(
96
			'meta',
97
			$this->scribuntoLuaLibrary->getQueryResult()[0]
98
		);
99
		$this->assertArrayHasKey(
100
			'count',
101
			$this->scribuntoLuaLibrary->getQueryResult()[0]['meta']
102
		);
103
		$this->assertEquals(
104
			0,
105
			$this->scribuntoLuaLibrary->getQueryResult()[0]['meta']['count']
106
		);
107
		$this->assertArrayHasKey(
108
			'printrequests',
109
			$this->scribuntoLuaLibrary->getQueryResult( '[[Modification date::+]]|?Modification date|limit=0|mainlabel=-' )[0]
110
		);
111
		$this->assertArrayHasKey(
112
			0,
113
			$this->scribuntoLuaLibrary->getQueryResult( '[[Modification date::+]]|?Modification date|limit=0|mainlabel=-' )[0]['printrequests']
114
		);
115
		$this->assertArrayHasKey(
116
			'label',
117
			$this->scribuntoLuaLibrary->getQueryResult( '[[Modification date::+]]|?Modification date|limit=0|mainlabel=-' )[0]['printrequests'][0]
118
		);
119
		$this->assertEquals(
120
			'Modification date',
121
			$this->scribuntoLuaLibrary->getQueryResult( '[[Modification date::+]]|?Modification date|limit=0|mainlabel=-' )[0]['printrequests'][0]['label']
122
		);
123
		$this->assertEquals(
124
			'Modification date',
125
			$this->scribuntoLuaLibrary->getQueryResult( [ '[[Modification date::+]]', '?Modification date', 'limit' => 0, 'mainlabel=-' ] )[0]['printrequests'][0]['label']
126
		);
127
	}
128
129
	/**
130
	 * Tests method info
131
	 *
132
	 * @return void
133
	 */
134
	public function testInfo() {
135
		$this->assertEmpty(
136
			$this->scribuntoLuaLibrary->info( null )
137
		);
138
		$this->assertEmpty(
139
			$this->scribuntoLuaLibrary->info( '' )
140
		);
141
		$this->assertInternalType(
142
			'string',
143
			$this->scribuntoLuaLibrary->info( 'Test info text' )[0]
144
		);
145
		$this->assertStringStartsWith(
146
			'<span',
147
			$this->scribuntoLuaLibrary->info( 'Test info text' )[0]
148
		);
149
		$this->assertStringEndsWith(
150
			'</span>',
151
			$this->scribuntoLuaLibrary->info( 'Test info text' )[0]
152
		);
153
		$this->assertEquals(
154
			1,
155
			preg_match('~^<span class=.*<span class="[^"]*info">.*>Test info text<.*</span>$~', $this->scribuntoLuaLibrary->info( 'Test info text' )[0])
156
		);
157
		$this->assertEquals(
158
			1,
159
			preg_match('~^<span class=.*<span class="[^"]*warning">.*>Test info text<.*</span>$~', $this->scribuntoLuaLibrary->info( 'Test info text', 'warning' )[0])
160
		);
161
		$this->assertEquals(
162
			1,
163
			preg_match('~^<span class=.*<span class="[^"]*info">.*>Test info text<.*</span>$~', $this->scribuntoLuaLibrary->info( 'Test info text', 'invalid' )[0])
164
		);
165
	}
166
167
168
	/**
169
	 * Tests method set through assertions based upon
170
	 * dataProvider {@see \SMW\Scribunto\Tests\ScribuntoLuaLibraryTest::dataProviderSetTest}
171
	 *
172
	 * @dataProvider dataProviderSetTest
173
	 * @param array $arguments arguments passed to function
174
	 * @param mixed $expected expected return value
175
	 */
176
	public function testSet( $arguments, $expected) {
177
		$this->assertEquals(
178
			$expected,
179
			$this->scribuntoLuaLibrary->set($arguments)
180
		);
181
	}
182
183
	/**
184
	 * Tests method subobject through assertions based upon
185
	 * dataProvider {@see \SMW\Scribunto\Tests\ScribuntoLuaLibraryTest::dataProviderSubobjectTest}
186
	 *
187
	 * @dataProvider dataProviderSubobjectTest
188
	 * @param array $arguments arguments passed to function
189
	 * @param mixed $expected expected return value
190
	 */
191
	public function testSubobject( $arguments, $expected ) {
192
		$this->assertEquals(
193
			$expected,
194
			call_user_func_array( array( $this->scribuntoLuaLibrary, 'subobject' ), $arguments )
195
		);
196
	}
197
198
	/**
199
	 * Data provider for {@see testFunctions}
200
	 *
201
	 * @see testFunctions
202
	 *
203
	 * @return array
204
	 */
205
	public function dataProviderFunctionTest() {
206
207
		return [
208
			[ 'getPropertyType' ],
209
			[ 'getQueryResult' ],
210
			[ 'info' ],
211
			[ 'set' ],
212
			[ 'subobject' ]
213
		];
214
	}
215
216
	/**
217
	 * Data provider for {@see testSet}
218
	 *
219
	 * @see testSet
220
	 *
221
	 * @return array
222
	 */
223
	public function dataProviderSetTest() {
224
		$provider = array(
225
			[
226
				[],
227
				null
228
			],
229
			[
230
				[ 'has type=page' ],
231
				[ 1 => true ]
232
			],
233
			[
234
				[ 'has type=test' ],
235
				[ array( 1 => false, 'error' => wfMessage('smw_unknowntype')->inLanguage('en')->plain() ) ]
236
			],
237
			[
238
				[ '1215623e790d918773db943232068a93b21c9f1419cb85666c6558e87f5b7d47=test' ],
239
				[ 1 => true ]
240
			],
241
			[
242
				[ '1215623e790d918773db943232068a93b21c9f1419cb85666c6558e87f5b7d47=test', 'foo' => 'bar' ],
243
				[ 1 => true ]
244
			]
245
		);
246
247
		return $provider;
248
	}
249
250
	/**
251
	 * Data provider for {@see testSubobject}
252
	 *
253
	 * @see testSubobject
254
	 *
255
	 * @return array
256
	 */
257
	public function dataProviderSubobjectTest()
258
	{
259
		$provider = array(
260
			[
261
				[ [] ],
262
				null
263
			],
264
			[
265
				[ null ],
266
				null
267
			],
268
			[
269
				[ '' ],
270
				null
271
			],
272
			[
273
				[ [ 'has type=page', 'Allows value=test' ] ],
274
				[ 1 => true ]
275
			],
276
			[
277
				[ [ 'has type=test', 'Allows value=test' ] ],
278
				[ array( 1 => false, 'error' => wfMessage('smw_unknowntype')->inLanguage('en')->plain() ) ]
279
			],
280
			[
281
				[ [ 'has type=page', 'Allows value=test','1215623e790d918773db943232068a93b21c9f1419cb85666c6558e87f5b7d47=test' ] ],
282
				[ 1 => true ]
283
			],
284
			[
285
				[ [], '01234567890_testStringAsId' ],
286
				null
287
			],
288
			[
289
				[ null, '01234567890_testStringAsId' ],
290
				null
291
			],
292
			[
293
				[ '', '01234567890_testStringAsId' ],
294
				null
295
			],
296
			[
297
				[ [ 'has type=page', 'Allows value=test' ], '01234567890_testStringAsId' ],
298
				[ 1 => true ]
299
			],
300
			[
301
				[ [ 'has type=test', 'Allows value=test' ], '01234567890_testStringAsId' ],
302
				[ array( 1 => false, 'error' => wfMessage('smw_unknowntype')->inLanguage('en')->plain() ) ]
303
			],
304
			[
305
				[ [ 'has type=page', 'Allows value' => 'test','1215623e790d918773db943232068a93b21c9f1419cb85666c6558e87f5b7d47=test' ], '01234567890_testStringAsId' ],
306
				[ 1 => true ]
307
			],
308
		);
309
310
		return $provider;
311
	}
312
}