testdoIterateOnArrayIterator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SMW\Notifications\Iterators\Tests;
4
5
use SMW\Notifications\Iterators\MappingIterator;
6
use ArrayIterator;
7
use SMW\Tests\PHPUnitCompat;
8
9
/**
10
 * @covers \SMW\Notifications\Iterators\MappingIterator
11
 * @group semantic-notifications
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class MappingIteratorTest extends \PHPUnit_Framework_TestCase {
19
20
	use PHPUnitCompat;
21
22
	public function testCanConstruct() {
23
24
		$this->assertInstanceOf(
25
			MappingIterator::class,
26
			new MappingIterator( array(), function() {} )
27
		);
28
	}
29
30
	public function testInvalidConstructorArgumentThrowsException() {
31
32
		$this->setExpectedException( 'RuntimeException' );
33
		$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...
34
	}
35
36
	public function testdoIterateOnArray() {
37
38
		$expected = array(
39
			1 , 42
40
		);
41
42
		$mappingIterator = new MappingIterator( $expected, function( $counter ) {
43
			return $counter;
44
		} );
45
46
		foreach ( $mappingIterator as $key => $value ) {
47
			$this->assertEquals(
48
				$expected[$key],
49
				$value
50
			);
51
		}
52
	}
53
54
	public function testdoIterateOnArrayIterator() {
55
56
		$expected = array(
57
			1001 , 42
58
		);
59
60
		$mappingIterator = new MappingIterator( new ArrayIterator( $expected ), function( $counter ) {
61
			return $counter;
62
		} );
63
64
		foreach ( $mappingIterator as $key => $value ) {
65
			$this->assertEquals(
66
				$expected[$key],
67
				$value
68
			);
69
		}
70
	}
71
72
}
73