ComposingIteratorTest::testBasicFunctionality()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 9.232
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace itertools;
4
5
use PHPUnit_Framework_TestCase;
6
7
8
class ComposingIteratorTest extends PHPUnit_Framework_TestCase
9
{
10
	/** @test */
11
	public function testBasicFunctionality()
12
	{
13
		$values = ComposingIterator::newInstance()
0 ignored issues
show
Documentation Bug introduced by
The method range does not exist on object<itertools\ComposingIterator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
14
			->range(0, 10)
15
			->filter(function($v) { return 10 != $v; })
16
			->groupBy(function($v) { return 5 < $v; })
17
			->chain()
18
			->slice(1)
19
			->map(function($v) { return (int) ($v / 2); })
20
			->unique()
21
			->takeWhile(function($v) { return 4 > $v; })
22
			->toArray(ComposingIterator::DONT_USE_KEYS);
23
		$this->assertEquals(range(0, 3), $values);
24
25
		$values = ComposingIterator::newInstance()
26
			->source(new RepeatIterator(1))
27
			->cacheCurrent()
28
			->chunk(20)
29
			->map(function($batch) { return array_slice($batch, 0, 10); })
30
			->slice(0, 10)
31
			->chain()
32
			->zipWith(new RepeatIterator(2))
33
			->zipWithAll(array(new RepeatIterator(3), new RepeatIterator(4)))
34
			->takeWhile(function($v) { return 4 == $v[2]; })
35
			->count();
36
		$this->assertEquals(100, $values);
37
38
		$count = ComposingIterator::newInstance()
39
			->source(array(1, 2, 3, 4))
40
			->skipFirst()
41
			->count();
42
		$this->assertEquals(3, $count);
43
44
		$values = ComposingIterator::newInstance()
45
			->source(array(
46
				'jos   , 5',
47
				'piet  , 7',
48
			))
49
			->fixedLengthFormattedStringFromTemplate('name  , age')
50
			->toArray();
51
		$this->assertEquals('jos   ', $values[0]['name']);
52
		$this->assertEquals('5', $values[0]['age']);
53
	}
54
55
	/**
56
	 * @test
57
	 * @expectedException BadMethodCallException
58
	 */
59
	public function testCallingUnknownMethods()
60
	{
61
		ComposingIterator::newInstance()->bliablabloe();
0 ignored issues
show
Documentation Bug introduced by
The method bliablabloe does not exist on object<itertools\ComposingIterator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
62
	}
63
}
64
65