예제 #1
0
 public function testSelectRejectSortBy()
 {
     // from js
     $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
     $numbers = __($numbers)->chain()->select(function ($n) {
         return $n % 2 === 0;
     })->reject(function ($n) {
         return $n % 4 === 0;
     })->sortBy(function ($n) {
         return -$n;
     })->value();
     $this->assertEquals(array(10, 6, 2), $numbers, 'filtered and reversed the numbers in OO-style call');
     $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
     $numbers = __::chain($numbers)->select(function ($n) {
         return $n % 2 === 0;
     })->reject(function ($n) {
         return $n % 4 === 0;
     })->sortBy(function ($n) {
         return -$n;
     })->value();
     $this->assertEquals(array(10, 6, 2), $numbers, 'filtered and reversed the numbers in static call');
 }
예제 #2
0
 public function testTap()
 {
     // from js
     $intercepted = null;
     $interceptor = function ($obj) use(&$intercepted) {
         $intercepted = $obj;
     };
     $returned = __::tap(1, $interceptor);
     $this->assertEquals(1, $intercepted, 'passed tapped object to interceptor');
     $this->assertEquals(1, $returned, 'returns tapped object');
     $returned = __(array(1, 2, 3))->chain()->map(function ($n) {
         return $n * 2;
     })->max()->tap($interceptor)->value();
     $this->assertTrue($returned === 6 && $intercepted === 6, 'can use tapped objects in a chain');
     $returned = __::chain(array(1, 2, 3))->map(function ($n) {
         return $n * 2;
     })->max()->tap($interceptor)->value();
     $this->assertTrue($returned === 6 && $intercepted === 6, 'can use tapped objects in a chain with static call');
     // docs
     $interceptor = function ($obj) {
         return $obj * 2;
     };
     $result = __(array(1, 2, 3))->chain()->max()->tap($interceptor)->value();
     $this->assertEquals(3, $result);
 }