public function testWithReturnsPlinqWrapper() { $input = [1]; $expected = 'plinq\\plinqWrapper'; $actual = plinq::with($input); $this->assertInstanceOf($expected, $actual); }
public function testAnyReturnsFalseWithEmptyInputAndNoCondition() { $input = []; $expected = false; $actual = plinq::any($input); $this->assertEquals($expected, $actual); }
public function testAllReturnsTrueWhenInputIsEmpty() { $input = []; $expected = true; $actual = plinq::all($input, $this->condition); $this->assertEquals($expected, $actual); }
public function testTailReturnsEmptyArrayIfInputHasOneElement() { $input = [1]; $expected = []; $actual = plinq::tail($input); $this->assertEquals($expected, $actual); }
public function testCountTwoParamsReturnsNumberOfElementsMatchingTheExpression() { $input = [1, 2, 3]; $expected = 1; $actual = plinq::count($input, $this->evenNumberExpression); $this->assertEquals($expected, $actual); }
public function testReturnsKeyAndValueWhenFlagSet() { $input = ["a" => 1, "b" => 2, "c" => 3, "d" => 4]; $expected = ["d" => 4]; $actual = plinq::on($input)->first($this->condition, plinq::$MAINTAIN_KEY); $this->assertEquals($expected, $actual->toArray()); }
public function testResultIsNotPlinqWrapperWhenChainDoesntStartWithOn() { $input = [1, 2, 3, 4, 5, 6]; $expected = 'plinq\\plinqWrapper'; $actual = plinq::where($input, $this->evenNumberExpression); $this->assertNotInstanceOf($expected, $actual); }
public function testSelectReturnsArrayWhenChainDoesntStartWithOn() { $input = [1, 2, 3]; $expected = "array"; $doublingSelection = function ($key, $value) { return $value * 2; }; $actual = plinq::select($input, $doublingSelection); $this->assertInternalType($expected, $actual); }
public function testMaxChoosesNewNumberAsLargerIfComparatorReturnsTrue() { $input = [1, 2, "three"]; $expected = "three"; $callback = function ($currentMax, $compareTo) { return true; }; $actual = plinq::max($input, $callback); $this->assertEquals($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); }
public function testMinChoosesNewNumberAsSmallerIfComparatorReturnsTrue() { $input = [1, 2, "three"]; $expected = "three"; // The mock callback always returns true, thus the last element is "smallest" $callback = function ($currentMin, $compareTo) { return true; }; $actual = plinq::min($input, $callback); $this->assertEquals($expected, $actual); }
public function testFunctionAppliedToEachElement() { $input = [1, 2, 3, 4, 5]; $seed = 0; $accumulator = function ($acc, $key, $val) { $acc += $val; return $acc; }; $expected = 15; $actual = plinq::aggregate($input, $seed, $accumulator); $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";
/** * Alias of on * @param array $input * * @return plinqArrayWrapper */ public static function with(array $input) { return plinq::on($input); }