Completed
Push — master ( 356d31...2bbcd7 )
by mw
02:43
created

testdoIterateOnArrayIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
namespace SMW\Notifications\Iterator\Tests;
4
5
use SMW\Notifications\Iterator\MappingIterator;
6
use ArrayIterator;
7
8
/**
9
 * @covers \SMW\Notifications\Iterator\MappingIterator
10
 * @group semantic-notifications
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class MappingIteratorTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$this->assertInstanceOf(
22
			MappingIterator::class,
23
			new MappingIterator( array(), function() {} )
24
		);
25
	}
26
27
	public function testInvalidConstructorArgumentThrowsException() {
28
29
		$this->setExpectedException( 'RuntimeException' );
30
		$instance = new MappingIterator( 2, function() {} );
0 ignored issues
show
Documentation introduced by
2 is of type integer, but the function expects a object<Iterator>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$instance is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
	}
32
33
	public function testdoIterateOnArray() {
34
35
		$expected = array(
36
			1 , 42
37
		);
38
39
		$mappingIterator = new MappingIterator( $expected, function( $counter ) {
40
			return $counter;
41
		} );
42
43
		foreach ( $mappingIterator as $key => $value ) {
44
			$this->assertEquals(
45
				$expected[$key],
46
				$value
47
			);
48
		}
49
	}
50
51
	public function testdoIterateOnArrayIterator() {
52
53
		$expected = array(
54
			1001 , 42
55
		);
56
57
		$mappingIterator = new MappingIterator( new ArrayIterator( $expected ), function( $counter ) {
58
			return $counter;
59
		} );
60
61
		foreach ( $mappingIterator as $key => $value ) {
62
			$this->assertEquals(
63
				$expected[$key],
64
				$value
65
			);
66
		}
67
	}
68
69
}
70