Completed
Pull Request — master (#562)
by no
08:00 queued 04:18
created

StatementRankTest::testFindBestRank()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Statement;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Statement\StatementRank;
7
8
/**
9
 * @covers Wikibase\DataModel\Statement\StatementRank
10
 *
11
 * @group Wikibase
12
 * @group WikibaseDataModel
13
 * @group WikibaseStatement
14
 *
15
 * @license GNU GPL v2+
16
 * @author Thiemo Kreuz
17
 */
18
class StatementRankTest extends PHPUnit_Framework_TestCase {
19
20
	public function testConstants() {
21
		$this->assertSame( 0, StatementRank::DEPRECATED );
22
		$this->assertSame( 1, StatementRank::NORMAL );
23
		$this->assertSame( 2, StatementRank::PREFERRED );
24
	}
25
26
	public function testGetNames() {
27
		$this->assertSame( [
28
			'deprecated',
29
			'normal',
30
			'preferred',
31
		], StatementRank::getNames() );
32
	}
33
34
	public function testGetAllRanks() {
35
		$this->assertSame( [
36
			'deprecated' => 0,
37
			'normal' => 1,
38
			'preferred' => 2,
39
		], StatementRank::getAllRanks() );
40
	}
41
42
	/**
43
	 * @dataProvider notInIntegerRangeProvider
44
	 */
45
	public function testGivenInvalidRank_assertIsValidThrowsException( $rank ) {
46
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
		StatementRank::assertIsValid( $rank );
48
	}
49
50
	/**
51
	 * @dataProvider integerRangeProvider
52
	 */
53
	public function testGivenValidRank_assertIsValidSucceeds( $rank ) {
54
		StatementRank::assertIsValid( $rank );
55
		$this->assertTrue( true );
56
	}
57
58
	/**
59
	 * @dataProvider notInIntegerRangeProvider
60
	 */
61
	public function testGivenInvalidRank_isValidFails( $rank ) {
62
		$this->assertFalse( StatementRank::isValid( $rank ) );
63
	}
64
65
	/**
66
	 * @dataProvider integerRangeProvider
67
	 */
68
	public function testGivenInvalidRank_isValidSucceeds( $rank ) {
69
		$this->assertTrue( StatementRank::isValid( $rank ) );
70
	}
71
72
	/**
73
	 * @dataProvider notInIntegerRangeProvider
74
	 */
75
	public function testGivenInvalidRank_isFalseThrowsException( $rank ) {
76
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77
		StatementRank::isFalse( $rank );
78
	}
79
80
	/**
81
	 * @dataProvider isFalseProvider
82
	 */
83
	public function testIsFalse( $rank, $expected ) {
84
		$this->assertSame( $expected, StatementRank::isFalse( $rank ) );
85
	}
86
87
	public function isFalseProvider() {
88
		return [
89
			[ 0, true ],
90
			[ 1, false ],
91
			[ 2, false ],
92
		];
93
	}
94
95
	/**
96
	 * @dataProvider invalidComparisonPairProvider
97
	 */
98
	public function testGivenInvalidRank_isEqualThrowsException( $rank1, $rank2 ) {
99
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
		StatementRank::isEqual( $rank1, $rank2 );
101
	}
102
103
	/**
104
	 * @dataProvider isEqualProvider
105
	 */
106
	public function testIsEqual( $rank1, $rank2, $expected ) {
107
		$this->assertSame( $expected, StatementRank::isEqual( $rank1, $rank2 ) );
108
	}
109
110
	public function isEqualProvider() {
111
		return [
112
			[ null, null, true ],
113
			[ null, 0, false ],
114
			[ null, 1, false ],
115
			[ null, 2, false ],
116
			[ 0, null, false ],
117
			[ 0, 0, true ],
118
			[ 0, 1, false ],
119
			[ 0, 2, false ],
120
			[ 1, null, false ],
121
			[ 1, 0, false ],
122
			[ 1, 1, true ],
123
			[ 1, 2, false ],
124
			[ 2, null, false ],
125
			[ 2, 0, false ],
126
			[ 2, 1, false ],
127
			[ 2, 2, true ],
128
		];
129
	}
130
131
	/**
132
	 * @dataProvider invalidComparisonPairProvider
133
	 */
134
	public function testGivenInvalidRank_isLowerThrowsException( $rank1, $rank2 ) {
135
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
136
		StatementRank::isLower( $rank1, $rank2 );
137
	}
138
139
	/**
140
	 * @dataProvider isLowerProvider
141
	 */
142
	public function testIsLower( $rank1, $rank2, $expected ) {
143
		$this->assertSame( $expected, StatementRank::isLower( $rank1, $rank2 ) );
144
	}
145
146
	public function isLowerProvider() {
147
		return [
148
			[ null, null, false ],
149
			[ null, 0, true ],
150
			[ null, 1, true ],
151
			[ null, 2, true ],
152
			[ 0, null, false ],
153
			[ 0, 0, false ],
154
			[ 0, 1, true ],
155
			[ 0, 2, true ],
156
			[ 1, null, false ],
157
			[ 1, 0, false ],
158
			[ 1, 1, false ],
159
			[ 1, 2, true ],
160
			[ 2, null, false ],
161
			[ 2, 0, false ],
162
			[ 2, 1, false ],
163
			[ 2, 2, false ],
164
		];
165
	}
166
167
	/**
168
	 * @dataProvider invalidComparisonPairProvider
169
	 */
170
	public function testGivenInvalidRank_isHigherThrowsException( $rank1, $rank2 ) {
171
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
172
		StatementRank::isHigher( $rank1, $rank2 );
173
	}
174
175
	/**
176
	 * @dataProvider isHigherProvider
177
	 */
178
	public function testIsHigher( $rank1, $rank2, $expected ) {
179
		$this->assertSame( $expected, StatementRank::isHigher( $rank1, $rank2 ) );
180
	}
181
182
	public function isHigherProvider() {
183
		return [
184
			[ null, null, false ],
185
			[ null, 0, false ],
186
			[ null, 1, false ],
187
			[ null, 2, false ],
188
			[ 0, null, true ],
189
			[ 0, 0, false ],
190
			[ 0, 1, false ],
191
			[ 0, 2, false ],
192
			[ 1, null, true ],
193
			[ 1, 0, true ],
194
			[ 1, 1, false ],
195
			[ 1, 2, false ],
196
			[ 2, null, true ],
197
			[ 2, 0, true ],
198
			[ 2, 1, true ],
199
			[ 2, 2, false ],
200
		];
201
	}
202
203
	/**
204
	 * @dataProvider invalidComparisonPairProvider
205
	 */
206
	public function testGivenInvalidRank_compareThrowsException( $rank1, $rank2 ) {
207
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
208
		StatementRank::compare( $rank1, $rank2 );
209
	}
210
211
	/**
212
	 * @dataProvider compareProvider
213
	 */
214
	public function testCompare( $rank1, $rank2, $expected ) {
215
		$this->assertSame( $expected, StatementRank::compare( $rank1, $rank2 ) );
216
	}
217
218
	public function compareProvider() {
219
		return [
220
			[ null, null, 0 ],
221
			[ null, 0, -1 ],
222
			[ null, 1, -1 ],
223
			[ null, 2, -1 ],
224
			[ 0, null, 1 ],
225
			[ 0, 0, 0 ],
226
			[ 0, 1, -1 ],
227
			[ 0, 2, -1 ],
228
			[ 1, null, 1 ],
229
			[ 1, 0, 1 ],
230
			[ 1, 1, 0 ],
231
			[ 1, 2, -1 ],
232
			[ 2, null, 1 ],
233
			[ 2, 0, 1 ],
234
			[ 2, 1, 1 ],
235
			[ 2, 2, 0 ],
236
		];
237
	}
238
239
	/**
240
	 * @dataProvider neitherIntegerRangeNorNullProvider
241
	 */
242
	public function testGivenInvalidRank_findBestRankThrowsException( $rank ) {
243
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
244
		StatementRank::findBestRank( $rank );
245
	}
246
247
	/**
248
	 * @dataProvider invalidComparisonPairProvider
249
	 */
250
	public function testGivenInvalidArray_findBestRankThrowsException( $rank1, $rank2 ) {
251
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
252
		StatementRank::findBestRank( [ $rank1, $rank2 ] );
253
	}
254
255
	/**
256
	 * @dataProvider findBestRankProvider
257
	 */
258
	public function testFindBestRank( $ranks, $expected ) {
259
		$this->assertSame( $expected, StatementRank::findBestRank( $ranks ) );
260
	}
261
262
	public function findBestRankProvider() {
263
		return [
264
			[ null, null ],
265
			[ 0, 0 ],
266
			[ 1, 1 ],
267
			[ [], null ],
268
			[ [ null ], null ],
269
			[ [ 0 ], 0 ],
270
			[ [ 1 ], 1 ],
271
			[ [ null, 0 ], 0 ],
272
			[ [ 0, null ], 0 ],
273
			[ [ 0, 1 ], 1 ],
274
			[ [ null, 0, 1, 2 ], 2 ],
275
			[ [ 2, 1, 0, null ], 2 ],
276
		];
277
	}
278
279
	public function integerRangeProvider() {
280
		return [
281
			[ 0 ],
282
			[ 1 ],
283
			[ 2 ],
284
		];
285
	}
286
287
	public function neitherIntegerRangeNorNullProvider() {
288
		return [
289
			[ false ],
290
			[ true ],
291
			[ NAN ],
292
			[ INF ],
293
			[ '0' ],
294
			[ '1' ],
295
			[ 0.0 ],
296
			[ 1.0 ],
297
			[ -1 ],
298
			[ 3 ],
299
		];
300
	}
301
302
	public function notInIntegerRangeProvider() {
303
		$invalid = $this->neitherIntegerRangeNorNullProvider();
304
		$invalid[] = [ null ];
305
		return $invalid;
306
	}
307
308
	public function invalidComparisonPairProvider() {
309
		$invalid = $this->neitherIntegerRangeNorNullProvider();
310
311
		foreach ( $invalid as $args ) {
312
			yield [ 1, $args[0] ];
313
			yield [ $args[0], 1 ];
314
		}
315
	}
316
317
}
318