public function testMap() { $array = ['a' => 1, 'b' => 2, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 4, 'g' => 5, 'h' => 6]; $stream = new Stream($array); $res = $stream->distinct()->collect(new MapCollector()); $this->assertEquals(['a' => 1, 'b' => 2, 'd' => 3, 'e' => 4, 'g' => 5, 'h' => 6], $res); }
public function testAnyReturningData() { $array = [1, 2, 3, 4, 5, 6]; $stream = new Stream($array); $res = $stream->findAny(); $this->assertFalse($res->isEmpty()); $this->assertTrue(in_array($res->get(), $array, true)); }
public function testException() { try { $array = [2, 4, -2, -9]; $stream = new Stream($array); $res = $stream->sort('reverseComparator'); } catch (\InvalidArgumentException $ex) { return; } $this->fail('An expected exception has not been raised.'); }
public function testNotCallable() { try { Stream::of()->map(true); } catch (\InvalidArgumentException $ex) { return; } $this->fail('An expected exception has not been raised.'); }
<?php require_once '../../src/autoload.php'; use phpstream\Stream; $stream = new Stream(['0', 'aa', '1', 'bb', '2', 'cc', '3', 'dd', '4']); $res = $stream->map(function ($value) { return intval($value); })->filter(function ($value) { return $value > 0; })->filter(function ($value) { return $value % 2 == 0; })->map(function ($value) { return $value + 1; })->reduceWithDefault(function ($acc, $v) { return $acc + $v; }, 0); // Returns 8 ! var_dump($res);
public function testToMap() { $res = Stream::of(['a' => 1, 'z' => 2, 'e' => 3, 'r' => 4, 't' => 5, 'y' => 6])->toMap(); $this->assertEquals(['a' => 1, 'z' => 2, 'e' => 3, 'r' => 4, 't' => 5, 'y' => 6], $res); }
public function testExceptionWithDefault() { try { $array = []; $stream = new Stream($array); $stream->reduceWithDefault('MinFunction', phpstream\util\Optional::of(-25)); } catch (\InvalidArgumentException $ex) { return; } $this->fail('An expected exception has not been raised.'); }