public function testWithReturnsPlinqWrapper() { $input = [1]; $expected = 'plinq\\plinqWrapper'; $actual = plinq::with($input); $this->assertInstanceOf($expected, $actual); }
public function testReturnsDistinctValuesFromExpressionIfProvided() { $input = ["a", "A", "b", "B"]; $expected = ["a", "b"]; $expression = function ($k, $v) { return strtolower($v); }; $actual = plinq::with($input)->distinct($expression)->toArray(); $this->assertEquals($expected, $actual); }
return strtolower($v->genre) == "fantasy"; }); print "There are {$numFantasyBooks} fantasy books \r\n"; print "\r\n"; // Calculate the price of all romance books print "==== The price of all romance books ==== \r\n"; $sumAggregate = function ($acc, $k, $v) { return $acc + floatval($v->price); }; $priceOfRomanceBooks = plinq::with($books)->where(function ($v) { return strtolower($v->genre) == "romance"; })->aggregate(0.0, $sumAggregate); print "The price of all romance books is {$priceOfRomanceBooks} \r\n"; print "\r\n"; // Calculate the price of all genres of book print "==== The price of each genre book ==== \r\n"; $allPrices = plinq::with($books)->distinct(function ($k, $v) { return strtolower($v->genre); })->aggregate(array(), function ($acc, $k, $v) use($books, $sumAggregate) { if (!array_key_exists($v, $acc)) { // Find the sum of the prices in this genre $acc[$v] = plinq::on($books)->where(function ($b) use($v) { return strtolower($b->genre) == $v; })->aggregate(0.0, $sumAggregate); } return $acc; })->toArray(); foreach ($allPrices as $g => $p) { print "The price of all {$g} books is {$p} \r\n"; } print "\r\n";