StopwatchIteratorTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testBasicFunctionality() 0 22 3
A testAutoPrint() 0 9 2
A testAutoPrintWithStringFile() 0 6 2
A testAutoPrintWithInvalidStringFile() 0 6 2
A testAutoPrintWithInvalidFile() 0 6 2
A testAutoPrintWithInvalidPrintInterval() 0 6 2
A testFileCsvIteratorWithInvalidArguments() 0 4 1
1
<?php
2
3
namespace itertools;
4
5
use DateInterval;
6
use PHPUnit_Framework_TestCase;
7
8
9
class StopwatchIteratorTest extends PHPUnit_Framework_TestCase
10
{
11
	/** @test */
12
	public function testBasicFunctionality()
13
	{
14
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10));
15
		$this->assertEquals(0, $stopwatchIterator->getElapsedTime());
16
		$this->assertTrue(is_nan($stopwatchIterator->getSpeed()));
17
18
		$count = 0;
19
		foreach($stopwatchIterator as $element) {
20
			usleep(10 * 1000);
21
			$this->assertEquals($count, $element);
22
			$this->assertEquals($count, $stopwatchIterator->getIterationCount());
23
			$count += 1;
24
			if($element != 0) {
25
				$this->assertGreaterThan(10, $stopwatchIterator->getSpeed());
26
			}
27
		}
28
		$this->assertLessThan(microtime(true), $stopwatchIterator->getStartTime());
29
30
		$elapsedTime = $stopwatchIterator->getElapsedTime();
31
		$this->assertEquals($elapsedTime, $stopwatchIterator->getElapsedTime(), 'Time counter should stop after finish');
32
		$this->assertNotNull($stopwatchIterator->getStopTime());
33
	}
34
35
	/** @test */
36
	public function testAutoPrint()
37
	{
38
		$fp = fopen('php://memory', 'r+');
39
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10), array('autoPrint' => true, 'printTo' => $fp, 'printInterval' => DateInterval::createFromDateString('0 seconds')));
40
		foreach($stopwatchIterator as $element) {
41
		}
42
		rewind($fp);
43
		$this->assertEquals(11, substr_count(stream_get_contents($fp), "\n"));
44
	}
45
46
	/** @test */
47
	public function testAutoPrintWithStringFile()
48
	{
49
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10), array('autoPrint' => true, 'printTo' => 'php://memory', 'printInterval' => DateInterval::createFromDateString('0 seconds')));
50
		foreach($stopwatchIterator as $element) {
51
		}
52
	}
53
54
	/**
55
	 * @test
56
	 * @expectedException InvalidArgumentException
57
	 */
58
	public function testAutoPrintWithInvalidStringFile()
59
	{
60
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10), array('autoPrint' => true, 'printTo' => '', 'printInterval' => DateInterval::createFromDateString('0 seconds')));
61
		foreach($stopwatchIterator as $element) {
62
		}
63
	}
64
65
	/**
66
	 * @test
67
	 * @expectedException InvalidArgumentException
68
	 */
69
	public function testAutoPrintWithInvalidFile()
70
	{
71
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10), array('autoPrint' => true, 'printTo' => 1234, 'printInterval' => DateInterval::createFromDateString('0 seconds')));
72
		foreach($stopwatchIterator as $element) {
73
		}
74
	}
75
76
	/**
77
	 * @test
78
	 * @expectedException InvalidArgumentException
79
	 */
80
	public function testAutoPrintWithInvalidPrintInterval()
81
	{
82
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10), array('printInterval' => 123));
83
		foreach($stopwatchIterator as $element) {
84
		}
85
	}
86
87
	/**
88
	 * @test
89
	 * @expectedException InvalidArgumentException
90
	 */
91
	public function testFileCsvIteratorWithInvalidArguments()
92
	{
93
		$stopwatchIterator = new StopwatchIterator(new RangeIterator(0, 10), array('a' => 'b'));
0 ignored issues
show
Unused Code introduced by
$stopwatchIterator 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...
94
	}
95
}
96
97