Skip to content

Commit b3feb2b

Browse files
committed
test: each
1 parent 81cfdfe commit b3feb2b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/CollectionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Ahc\Underscore\Tests;
4+
5+
use Ahc\Underscore\Underscore as _;
6+
7+
class CollectionTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function test_each()
10+
{
11+
_::_([1, 2, 3])->each(function ($num, $i) {
12+
$this->assertSame($num, $i + 1, 'each iterators provide value and iteration count');
13+
});
14+
15+
$answers = [];
16+
$count = 0;
17+
_::_([1, 2, 3])->each(function($num) use (&$answers, &$count) {
18+
$answers[] = $num * 5;
19+
$count++;
20+
});
21+
22+
$this->assertSame($answers, [5, 10, 15], 'callback applied on each member');
23+
$this->assertSame($count, 3, 'callback applied exactly 3 times');
24+
25+
$answers = [];
26+
_::_(['one' => 1, 'two' => 2, 'three' => 3])->each(function($num, $index) use (&$answers) {
27+
$answers[] = $index;
28+
});
29+
30+
$this->assertSame($answers, ['one', 'two', 'three'], 'callback applied on each member of assoc array');
31+
}
32+
}

0 commit comments

Comments
 (0)