public function testFilterWorksAsExpected() { $filter = new SkipNulls(); $expected = [1, 2, 4]; $array = [1, 2, null, 4]; $actual = []; Each::shallow($filter)->begin($array, function ($bag) use(&$actual) { $actual[] = $bag->value(); }); $this->assertEquals($expected, $actual); }
public function testFilterWorksAsExpected() { $filter = new Regex('/[0-9]+/'); $expected = [5, 6]; $array = ['a', 5, 'b', 6, 'c']; $actual = []; Each::shallow($filter)->begin($array, function ($bag) use(&$actual) { $actual[] = $bag->value(); }); $this->assertEquals($expected, $actual); }
public function testFilterWorksAsExpected() { $filter = new Greater(5); $expected = [6]; $array = [1, 5, 4, 6, 5]; $actual = []; Each::shallow($filter)->begin($array, function ($bag) use(&$actual) { $actual[] = $bag->value(); }); $this->assertEquals($expected, $actual); }
public function testExtraDeepBeginMultipleCyclesAndFollowIndex() { $expected = 1; $cycles = 5; $cur_cycle = 0; $array = [1, 2, 3, 4 => [4, 5, 6 => [6, 7, 8, 9]]]; $index = 0; Each::deep()->begin($array, function ($bag) use(&$expected, &$cur_cycle, &$index, $array) { $actual = $bag->value(); $this->assertEquals($expected, $actual); $expected++; $actual = $bag->cycle(); $this->assertEquals($cur_cycle, $actual); $actual = $bag->index(); $this->assertEquals($index, $actual); $index++; // Here the number six refers to the last item in the // array [1, 2, 3, 4 => [4, 5, 6 => [6, 7, 8, 9]]] if it were flattened. if ($expected > 9) { $expected = 1; $cur_cycle++; } }, $cycles); }