Skip to content

Commit 4b29a6b

Browse files
Fix phpunit tests (#91)
* fix PHPUnit Tests * mini cleanup
1 parent f95de38 commit 4b29a6b

File tree

4 files changed

+72
-76
lines changed

4 files changed

+72
-76
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ composer.phar
88
composer.lock
99

1010
!.*
11-
.idea/
11+
.idea/
12+
.phpunit.result.cache

tests/phpunit/Unit/ApiSemanticFormsSelectRequestProcessorTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testMissingParametersThrowsException() {
4444

4545
$parameters = [];
4646

47-
$this->setExpectedException( 'InvalidArgumentException' );
47+
$this->expectException( 'InvalidArgumentException' );
4848
$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
4949
$parameters
5050
);
@@ -54,8 +54,7 @@ public function testJsonResultValuesFromRequestParameters() {
5454

5555
$parameters = [ 'query' => 'foo', 'sep' => ',' ];
5656

57-
$this->assertInternalType(
58-
'object',
57+
$this->assertIsObject(
5958
$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
6059
$parameters
6160
)
@@ -68,8 +67,7 @@ public function testJsonResultValuesFromRequestParameters_doProcessQueryFor(
6867
$parameters = [ 'approach' => 'smw', 'query' => 'foo, baa, gaah',
6968
'sep' => ',' ];
7069

71-
$this->assertInternalType(
72-
'object',
70+
$this->assertIsObject(
7371
$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
7472
$parameters
7573
)
@@ -80,8 +78,7 @@ public function testSetDebugFlag() {
8078
$this->ApiSFSRP->setDebugFlag( true );
8179
$parameters = [ 'query' => 'foo , function', 'sep' => ',' ];
8280

83-
$this->assertInternalType(
84-
'object',
81+
$this->assertIsObject(
8582
$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
8683
$parameters
8784
)
@@ -93,8 +90,7 @@ public function testSetDebugFlag_doProcessQueryFor() {
9390
$parameters = [ 'approach' => 'smw', 'query' => 'my Query,query2',
9491
'sep' => ',' ];
9592

96-
$this->assertInternalType(
97-
'object',
93+
$this->assertIsObject(
9894
$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
9995
$parameters
10096
)

tests/phpunit/Unit/SelectFieldTest.php

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace SFS\Tests;
1010

11-
use SFS\SelectField;
12-
11+
use MediaWiki\MediaWikiServices;
1312
use Parser;
13+
use SFS\SelectField;
14+
1415
use ParserOptions;
15-
use ParserOutput;
1616
use Title;
1717

1818
/**
@@ -21,25 +21,23 @@
2121
* @author FelixAba
2222
*/
2323
class SelectFieldTest extends \PHPUnit_Framework_TestCase {
24-
private $SelectField;
25-
private $parser;
26-
// Defined variables
24+
private $selectField;
25+
2726
private $other_args_query_parametrized = [ 'query' => '((Category:Building Complex))((Part Of Site::@@@@));?Display Title;format~list;sort~Display Title;sep~,;link~none;headers~hide;limit~500' ];
2827
private $expected_result_parametrized_setQuery = "[[Category:Building Complex]][[Part Of Site::@@@@]];?Display Title;format=list;sort=Display Title;sep=,;link=none;headers=hide;limit=500";
2928
private $other_args_query_unparametrized = [ 'query' => '((Category:Building Complex));?Display Title;format~list;sort~Display Title;sep~,;link~none;headers~hide;limit~500' ];
3029
private $other_args_function_parametrized = [ 'function' => '((Category:Building Complex))((Part Of Site::@@@@));?Display Title;format~list;sort~Display Title;sep~,;link~none;headers~hide;limit~500' ];
3130
private $expected_result_parametrized_seFunction = '{{#[[Category:Building Complex]][[Part Of Site::@@@@]];?Display Title;format=list;sort=Display Title;sep=,;link=none;headers=hide;limit=500}}';
3231
private $other_args_function_unparametrized = [ 'function' => 'ask:((Category:Building Complex));?Display Title;format~list;sort~Display Title;sep~@@;link~none;headers~hide;limit~500' ];
33-
private $expected_result_unparametrized_seFunction = "Building Complex:86543eab-4112-4616-be50-17dcdc24c346 (OFD.AEXH)@@Building Complex:5b9e26f8-6c57-48ff-a6b8-42a4e50fe472 (OFD.AEXH)@@Building Complex:93b076aa-cbe9-4371-8b61-c17c26f1872f (OFD.AMEXH)@@Building Complex:59577450-1582-4d6e-9621-3ac0531a728e (OFD.EEXH)@@Building Complex:1a9bed0b-67de-4e71-8528-f2b6a8907814 (RContiAve.Sport Complex)@@Building Complex:6a2242ea-7536-4a6d-85d2-f2ba4398ef44 (TB.BC)@@Building Complex:2db51fb1-10b6-4d4c-a152-f512914781ff (TB.BD)";
3432

3533
public function testCanConstruct() {
3634

37-
$this->assertInstanceOf( '\SFS\SelectField', $this->SelectField );
35+
$this->assertInstanceOf( '\SFS\SelectField', $this->selectField );
3836
}
3937

4038
public function testProcessParameters_Query() {
4139

42-
$this->SelectField->processParameters(
40+
$this->selectField->processParameters(
4341
"", $this->other_args_query_parametrized
4442
);
4543
$this->assertTrue(
@@ -49,7 +47,7 @@ public function testProcessParameters_Query() {
4947

5048
public function testProcessParameters_Function() {
5149

52-
$this->SelectField->processParameters(
50+
$this->selectField->processParameters(
5351
"", $this->other_args_function_parametrized
5452
);
5553
$this->assertArrayHasKey(
@@ -59,22 +57,22 @@ public function testProcessParameters_Function() {
5957

6058
public function testParametrized_setQuery() {
6159

62-
$this->SelectField->setQuery( $this->other_args_query_parametrized );
60+
$this->selectField->setQuery( $this->other_args_query_parametrized );
6361

6462
$this->assertEquals(
6563
$this->expected_result_parametrized_setQuery,
66-
$this->SelectField->getData()['selectquery']
64+
$this->selectField->getData()['selectquery']
6765
);
6866
/*
6967
* Optional Test.
7068
*/
7169
preg_match_all(
72-
"/[~(\(\()(\)\))]+/", $this->SelectField->getData()['selectquery'],
70+
"/[~(\(\()(\)\))]+/", $this->selectField->getData()['selectquery'],
7371
$was_remove
7472
);
7573

7674
preg_match_all(
77-
"/[=(\[\[)(\]\])]+/", $this->SelectField->getData()['selectquery'],
75+
"/[=(\[\[)(\]\])]+/", $this->selectField->getData()['selectquery'],
7876
$was_replaced
7977
);
8078

@@ -84,184 +82,185 @@ public function testParametrized_setQuery() {
8482

8583
public function testUnparametrized_setQuery() {
8684

87-
$this->SelectField->setQuery( $this->other_args_query_unparametrized );
85+
$this->selectField->setQuery( $this->other_args_query_unparametrized );
8886

89-
$this->assertTrue( $this->SelectField->getValues() !== null );
90-
$this->assertTrue( $this->SelectField->hasStaticValues() );
87+
$this->assertTrue( $this->selectField->getValues() !== null );
88+
$this->assertTrue( $this->selectField->hasStaticValues() );
9189
}
9290

9391
public function testParametrized_setFunction() {
9492

95-
$this->SelectField->setFunction(
93+
$this->selectField->setFunction(
9694
$this->other_args_function_parametrized
9795
);
9896
$this->assertTrue(
9997
strcmp(
10098
$this->expected_result_parametrized_seFunction,
101-
$this->SelectField->getData()['selectfunction']
99+
$this->selectField->getData()['selectfunction']
102100
) == 0
103101
);
104102
}
105103

106104
public function testUnparametrized_setFunction() {
107105

108-
$this->SelectField->setFunction(
106+
$this->selectField->setFunction(
109107
$this->other_args_function_unparametrized
110108
);
111109

112-
$this->assertTrue( $this->SelectField->hasStaticValues() );
110+
$this->assertTrue( $this->selectField->hasStaticValues() );
113111
}
114112

115113
public function testSetSelectIsMultiple_keyExistTrue() {
116114
$other_args = [ "part_of_multiple" => "bla bla bla" ];
117-
$this->SelectField->setSelectIsMultiple( $other_args );
118-
$this->assertTrue( $this->SelectField->getData()["selectismultiple"] );
115+
$this->selectField->setSelectIsMultiple( $other_args );
116+
$this->assertTrue( $this->selectField->getData()["selectismultiple"] );
119117
}
120118

121119
public function testSetSelectIsMultiple_keyExistFalse() {
122120

123121
$other_args = [ "Not_part_of_multiple" => "blas blas blas" ];
124-
$this->SelectField->setSelectIsMultiple( $other_args );
125-
$this->assertFalse( $this->SelectField->getData()["selectismultiple"] );
122+
$this->selectField->setSelectIsMultiple( $other_args );
123+
$this->assertFalse( $this->selectField->getData()["selectismultiple"] );
126124
}
127125

128126
public function testSetSelectTemplate_correctData() {
129127
$input_name = "{{#[[Category:Building Complex]][[Part Of Site::@@@@]]";
130128
$result = "{{#";
131-
$this->SelectField->setSelectTemplate( $input_name );
129+
$this->selectField->setSelectTemplate( $input_name );
132130

133131
$this->assertEquals(
134-
$this->SelectField->getData()['selecttemplate'], $result
132+
$this->selectField->getData()['selecttemplate'], $result
135133
);
136134
}
137135

138136
public function testSetSelectTemplate_wrongData() {
139137
$input_name = "Category:Building Complex";
140138
$result = "";
141-
$this->SelectField->setSelectTemplate( $input_name );
139+
$this->selectField->setSelectTemplate( $input_name );
142140

143141
$this->assertEquals(
144-
$this->SelectField->getData()['selecttemplate'], $result
142+
$this->selectField->getData()['selecttemplate'], $result
145143
);
146144
}
147145

148146
public function testSetSelectField_correctData() {
149147
$input_name = "{{#[[Category:Building Complex]][[Part Of Site::@@@@]]";
150148
$result = "Part Of Site::@@@@]";
151149

152-
$this->SelectField->setSelectField( $input_name );
150+
$this->selectField->setSelectField( $input_name );
153151

154152
$this->assertEquals(
155-
$this->SelectField->getData()['selectfield'], $result
153+
$this->selectField->getData()['selectfield'], $result
156154
);
157155
}
158156

159157
public function testSetSelectField_wrongData() {
160158
$input_name = "Category:Building Complex";
161159
$result = "";
162-
$this->SelectField->setSelectField( $input_name );
160+
$this->selectField->setSelectField( $input_name );
163161

164162
$this->assertNotEquals(
165-
$this->SelectField->getData()['selectfield'], $result
163+
$this->selectField->getData()['selectfield'], $result
166164
);
167165
}
168166

169167
public function testSetValueTemplate_containsMselectTemplate() {
170168
$input_name = "{{#[[Category:Building Complex]][[Part Of Site::@@@@]]";
171169
$other_args = [ "sametemplate" => "test values" ];
172170
$result = "{{#";
173-
$this->SelectField->setSelectTemplate( $input_name );
174-
$this->SelectField->setValueTemplate( $other_args );
171+
$this->selectField->setSelectTemplate( $input_name );
172+
$this->selectField->setValueTemplate( $other_args );
175173

176174
$this->assertEquals(
177-
$this->SelectField->getData()["valuetemplate"], $result
175+
$this->selectField->getData()["valuetemplate"], $result
178176
);
179177
}
180178

181179
public function testSetValueTemplate_containsOtherArgsTemplate() {
182180

183181
$other_args = [ "template" => "test values" ];
184182

185-
$this->SelectField->setValueTemplate( $other_args );
183+
$this->selectField->setValueTemplate( $other_args );
186184

187185
$this->assertEquals(
188-
$this->SelectField->getData()["valuetemplate"],
186+
$this->selectField->getData()["valuetemplate"],
189187
$other_args["template"]
190188
);
191189
}
192190

193191
public function testSetValueField() {
194192
$other_args = [ "field" => "test values Field" ];
195193

196-
$this->SelectField->setValueField( $other_args );
194+
$this->selectField->setValueField( $other_args );
197195

198196
$this->assertEquals(
199-
$this->SelectField->getData()["valuefield"], $other_args["field"]
197+
$this->selectField->getData()["valuefield"], $other_args["field"]
200198
);
201199
}
202200

203201
public function testSetSelectRemove_keyExistTrue() {
204202
$other_args = [ 'rmdiv' => "Test data" ];
205-
$this->SelectField->setSelectRemove( $other_args );
206-
$this->assertTrue( $this->SelectField->getData()["selectrm"] );
203+
$this->selectField->setSelectRemove( $other_args );
204+
$this->assertTrue( $this->selectField->getData()["selectrm"] );
207205
}
208206

209207
public function testSetSelectRemove_keyExistFalse() {
210208

211209
$other_args = [ "no_rmdiv" => "test data" ];
212-
$this->SelectField->setSelectRemove( $other_args );
213-
$this->assertFalse( $this->SelectField->getData()["selectrm"] );
210+
$this->selectField->setSelectRemove( $other_args );
211+
$this->assertFalse( $this->selectField->getData()["selectrm"] );
214212
}
215213

216214
public function testSetLabel_keyExistTrue() {
217215
$other_args = [ 'label' => "Test data" ];
218-
$this->SelectField->setLabel( $other_args );
219-
$this->assertTrue( $this->SelectField->getData()["label"] );
216+
$this->selectField->setLabel( $other_args );
217+
$this->assertTrue( $this->selectField->getData()["label"] );
220218
}
221219

222220
public function testSetLabel_keyExistFalse() {
223221

224222
$other_args = [ "no_label" => "test data" ];
225-
$this->SelectField->setLabel( $other_args );
226-
$this->assertArrayHasKey( "label", $this->SelectField->getData() );
227-
$this->assertFalse( $this->SelectField->getData()["label"] );
223+
$this->selectField->setLabel( $other_args );
224+
$this->assertArrayHasKey( "label", $this->selectField->getData() );
225+
$this->assertFalse( $this->selectField->getData()["label"] );
228226
}
229227

230228
public function testSetDelimiter_keyExistTrue() {
231229
$other_args = [ "delimiter" => ":" ];
232-
$this->SelectField->setDelimiter( $other_args );
230+
$this->selectField->setDelimiter( $other_args );
233231
$this->assertEquals(
234-
$this->SelectField->getDelimiter(), $other_args["delimiter"]
232+
$this->selectField->getDelimiter(), $other_args["delimiter"]
235233
);
236234
$this->assertEquals(
237-
$this->SelectField->getData()["sep"], $other_args["delimiter"]
235+
$this->selectField->getData()["sep"], $other_args["delimiter"]
238236
);
239237
}
240238

241239
public function testSetWgPageFormsListSeparator_keyExistTrue() {
242240

243-
$g_args = [ "Global_delimiter" => ";" ];
244-
$this->SelectField->setDelimiter( $g_args );
241+
$g_args = [ "delimiter" => ";" ];
242+
$this->selectField->setDelimiter( $g_args );
245243
$this->assertEquals(
246-
$this->SelectField->getDelimiter(), $g_args["Global_delimiter"]
244+
$this->selectField->getDelimiter(), $g_args["delimiter"]
247245
);
248246
$this->assertEquals(
249-
$this->SelectField->getData()["sep"], $g_args["Global_delimiter"]
247+
$this->selectField->getData()["sep"], $g_args["delimiter"]
250248
);
251249
}
252250

253251
protected function setUp(): void {
254252
parent::setUp();
255-
$this->parser = $GLOBALS['wgParser'];
256-
$this->parser->setTitle( Title::newFromText( 'NO TITLE' ) );
257-
$this->parser->mOptions = new ParserOptions();
258-
$this->parser->resetOutput();
259-
$this->parser->clearState();
260-
$this->SelectField = new SelectField( $this->parser );
253+
$parser = MediaWikiServices::getInstance()->getParser();
254+
$parser->setOutputType(Parser::OT_HTML);
255+
$parser->setTitle( Title::newFromText( 'NO TITLE' ) );
256+
$parser->mOptions = new ParserOptions();
257+
$parser->resetOutput();
258+
$parser->clearState();
259+
$this->selectField = new SelectField( $parser );
261260
}
262261

263262
protected function tearDown(): void {
264-
unset( $this->SelectField );
263+
unset( $this->selectField );
265264
parent::tearDown();
266265
}
267266

tests/phpunit/Unit/SemanticFormsSelectInputTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function testCanConstruct() {
5656

5757
public function testGetHTMLText() {
5858

59-
$this->assertInternalType(
60-
'string', $this->SFSInput->getHtmlText()
59+
$this->assertIsString(
60+
$this->SFSInput->getHtmlText()
6161
);
6262
}
6363

@@ -70,7 +70,7 @@ public function testGetName() {
7070

7171
public function testGetParameters() {
7272

73-
$this->assertInternalType( 'array', $this->SFSInput->getParameters() );
73+
$this->assertIsArray( $this->SFSInput->getParameters() );
7474
}
7575

7676

0 commit comments

Comments
 (0)