Skip to content

Commit

Permalink
Added some methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed May 20, 2017
1 parent 4614ae5 commit dea740a
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 86 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<file>src/parsers.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
41 changes: 40 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ArrayIterator;
use Closure;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use Serializable;
use stdClass;
Expand Down Expand Up @@ -116,10 +117,22 @@ public function unserialize($serialized)

public function without(...$keys)
{
if (count($keys) === 1 && is_array($keys[0])) {
if (count($keys) < 1) {
throw new InvalidArgumentException("without called with no keys");
}

if (is_array($keys[0])) {
$keys = $keys[0];
}

if (is_object($keys[0]) && method_exists($keys[0], "toArray")) {
$keys = $keys[0]->toArray();
}

if (is_iterable($keys[0])) {
$keys = iterator_to_array($keys[0]);
}

$filtered = [];

foreach ($this->data as $key => $value) {
Expand Down Expand Up @@ -174,4 +187,30 @@ public function each(Closure $each)
$each($value, $key);
}
}

public function reduce(Closure $reduce, $accumulator = null)
{
foreach ($this->data as $key => $value) {
$accumulator = $reduce($value, $key, $accumulator);
}

return $accumulator;
}

public function merge(...$others)
{
$data = $this->data;

foreach ($others as $other) {
if (!is_iterable($other)) {
continue;
}

foreach ($other as $key => $value) {
$data[$key] = $value;
}
}

return new self($data);
}
}
180 changes: 180 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

namespace Pre\Collections;

use ArrayIterator;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Pre\Collections\Collection;

class CollectionTest extends TestCase
{
/**
* @test
* @covers Pre\Collections\Collection::map
*/
public function can_map_with_closure()
{
$collection = new Collection([
"one" => 1,
"two" => 2,
"three" => 3,
]);

$result = $collection->map(function($value, $key) {
return $value * 2;
});

$expected = [
"one" => 2,
"two" => 4,
"three" => 6,
];

$this->assertEquals($expected, $result->toArray());
}

/**
* @test
* @covers Pre\Collections\Collection::filter
*/
public function can_filter_with_closure()
{
$collection = new Collection([
"one" => 1,
"two" => 2,
"three" => 3,
]);

$result = $collection->filter(function($value, $key) {
return $value === 1 || $key === "three";
});

$expected = [
"one" => 1,
"three" => 3,
];

$this->assertEquals($expected, $result->toArray());
}

/**
* @test
* @covers Pre\Collections\Collection::each
*/
public function can_each_with_closure()
{
$collection = new Collection([
"one" => 1,
"two" => 2,
"three" => 3,
]);

$result = [];

$collection->each(function($value, $key) use (&$result) {
$result[] = "{$key} {$value}";
});

$expected = [
"one 1",
"two 2",
"three 3",
];

$this->assertEquals($expected, $result);
}

/**
* @test
* @covers Pre\Collections\Collection::reduce
*/
public function can_reduce_with_closure()
{
$collection = new Collection([
"one" => 1,
"two" => 2,
"three" => 3,
]);

$result = $collection->reduce(function($value, $key, $accumulator) {
return $accumulator + $value;
});

$expected = 6;

$this->assertEquals($expected, $result);
}

/**
* @test
* @covers Pre\Collections\Collection::merge
*/
public function can_merge_others()
{
$collection = new Collection([
"one" => 1,
"two" => 2,
"three" => 3,
]);

$result = $collection->merge([
"four" => 4,
], new Collection([
"five" => 5,
]), new ArrayIterator([
"six" => 6,
]), false /* not iterable */);

$expected = [
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4,
"five" => 5,
"six" => 6,
];

$this->assertEquals($expected, $result->toArray());
}

/**
* @test
* @covers Pre\Collections\Collection::without
*/
public function can_exclude_keys_with_without()
{
$collection = new Collection([
"one" => 1,
"two" => 2,
"three" => 3,
]);

$expected = [
"two" => 2,
"three" => 3,
];

$result = $collection->without("one");
$this->assertEquals($expected, $result->toArray());

$result = $collection->without(["one"]);
$this->assertEquals($expected, $result->toArray());

$result = $collection->without(new Collection(["one"]));
$this->assertEquals($expected, $result->toArray());

$result = $collection->without(new ArrayIterator(["one"]));
$this->assertEquals($expected, $result->toArray());
}

/**
* @test
* @covers Pre\Collections\Collection::without
*/
public function can_throw_with_no_without_keys()
{
$this->expectException(InvalidArgumentException::class);
(new Collection())->without();
}
}
85 changes: 0 additions & 85 deletions tests/IterationTest.php

This file was deleted.

0 comments on commit dea740a

Please sign in to comment.