Esempio n. 1
0
 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);
 }
Esempio n. 2
0
 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));
 }
Esempio n. 3
0
 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.');
 }
Esempio n. 4
0
 public function testNotCallable()
 {
     try {
         Stream::of()->map(true);
     } catch (\InvalidArgumentException $ex) {
         return;
     }
     $this->fail('An expected exception has not been raised.');
 }
Esempio n. 5
0
<?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);
Esempio n. 6
0
 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);
 }
Esempio n. 7
0
 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.');
 }