diff --git a/src/Accessor/FindAccessor.php b/src/Accessor/FindAccessor.php index 268f45c..820fce6 100644 --- a/src/Accessor/FindAccessor.php +++ b/src/Accessor/FindAccessor.php @@ -14,7 +14,7 @@ class FindAccessor extends Accessor * * @param Collection $collection * @param callable $iterator - * @return Collection + * @return bool */ public function __invoke($collection, $iterator) { diff --git a/src/Accessor/ValueAccessor.php b/src/Accessor/ValueAccessor.php index 75cd51c..bb06c95 100644 --- a/src/Accessor/ValueAccessor.php +++ b/src/Accessor/ValueAccessor.php @@ -5,6 +5,9 @@ use Underscore\Accessor; use Underscore\Collection; +/** + * @deprecated Use toArray() instead. Will be removed in later releases + */ class ValueAccessor extends Accessor { /** diff --git a/src/Collection.php b/src/Collection.php index f09ec8c..e059164 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -29,6 +29,7 @@ public function getIteratorReversed() /** * @return mixed + * @deprecated Use toArray() instead. Will be removed in later releases */ public function value() { diff --git a/tests/Accessor/AllAccessorTest.php b/tests/Accessor/AllAccessorTest.php new file mode 100644 index 0000000..d46b9cb --- /dev/null +++ b/tests/Accessor/AllAccessorTest.php @@ -0,0 +1,40 @@ +getDummy1()), $func], + ]; + + $ret[] = [ + false, + [new Collection($this->getDummy2()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/AnyAccessorTest.php b/tests/Accessor/AnyAccessorTest.php new file mode 100644 index 0000000..c610f8f --- /dev/null +++ b/tests/Accessor/AnyAccessorTest.php @@ -0,0 +1,40 @@ +getDummy1()), $func], + ]; + + $ret[] = [ + true, + [new Collection($this->getDummy2()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/BaseAccessorTest.php b/tests/Accessor/BaseAccessorTest.php new file mode 100644 index 0000000..53dfa8a --- /dev/null +++ b/tests/Accessor/BaseAccessorTest.php @@ -0,0 +1,91 @@ +name = 'dummy'; + $dummy->foo = 'bar'; + $dummy->baz = 'qux'; + + return $dummy; + } + + /** + * @return object + */ + protected static function getDummy2() + { + $dummy = self::getDummy1(); + $dummy->false = false; + $dummy->null = null; + $dummy->zero = 0; + + return $dummy; + } + + protected static function getDummy3() + { + $dummy = [ + 'Angela' => [ + 'position' => 'dean', + 'sex' => 'female', + ], + 'Bob' => [ + 'position' => 'janitor', + 'sex' => 'male', + ], + 'Mark' => [ + 'position' => 'teacher', + 'sex' => 'male', + 'tenured' => true, + ], + 'Wendy' => [ + 'position' => 'teacher', + 'sex' => 'female', + 'tenured' => 1, + ], + ]; + + return $dummy; + } + + + /** + * @return object + */ + protected static function getDummy4() + { + $dummy = self::getDummy2(); + $dummy->false = false; + $dummy->null = null; + $dummy->zero = 0; + $dummy->one = 1; + + return $dummy; + } + /** + * @return Accessor|callable + */ + abstract protected function getInstance(); + + /** + * @return mixed[] + */ + abstract protected function getTestInvokeData(); + + /** + * @param mixed $expected + * @param mixed $args + * @dataProvider getTestInvokeData + */ + public function testInvoke($expected, $args) + { + $this->assertSame($expected, call_user_func_array($this->getInstance(), $args)); + } +} diff --git a/tests/Accessor/ContainsAccessorTest.php b/tests/Accessor/ContainsAccessorTest.php new file mode 100644 index 0000000..0d801fc --- /dev/null +++ b/tests/Accessor/ContainsAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1()), 'bar'], + ]; + + $ret[] = [ + false, + [new Collection($this->getDummy1()), 'baz'], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/FindAccessorTest.php b/tests/Accessor/FindAccessorTest.php new file mode 100644 index 0000000..136a82b --- /dev/null +++ b/tests/Accessor/FindAccessorTest.php @@ -0,0 +1,43 @@ +getDummy1()), $func('foo')], + ]; + + $ret[] = [ + true, + [new Collection($this->getDummy1()), $func('bar')], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/HasAccessorTest.php b/tests/Accessor/HasAccessorTest.php new file mode 100644 index 0000000..5d3874a --- /dev/null +++ b/tests/Accessor/HasAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1()), 'foo'], + ]; + + $ret[] = [ + false, + [new Collection($this->getDummy1()), 'bar'], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/MaxAccessorTest.php b/tests/Accessor/MaxAccessorTest.php new file mode 100644 index 0000000..e7bb8a2 --- /dev/null +++ b/tests/Accessor/MaxAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/ReduceRightAccessorTest.php b/tests/Accessor/ReduceRightAccessorTest.php new file mode 100644 index 0000000..f831fe3 --- /dev/null +++ b/tests/Accessor/ReduceRightAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/SizeAccessorTest.php b/tests/Accessor/SizeAccessorTest.php new file mode 100644 index 0000000..ae2c520 --- /dev/null +++ b/tests/Accessor/SizeAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1())], + ]; + + $ret[] = [ + 6, + [new Collection($this->getDummy2())], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/ToArrayAccessorTest.php b/tests/Accessor/ToArrayAccessorTest.php new file mode 100644 index 0000000..372a704 --- /dev/null +++ b/tests/Accessor/ToArrayAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1(), + [new Collection($this->getDummy1())], + ]; + + $ret[] = [ + (array) $this->getDummy2(), + [new Collection($this->getDummy2())], + ]; + + return $ret; + } +} diff --git a/tests/Accessor/ValueAccessorTest.php b/tests/Accessor/ValueAccessorTest.php new file mode 100644 index 0000000..9383afb --- /dev/null +++ b/tests/Accessor/ValueAccessorTest.php @@ -0,0 +1,37 @@ +getDummy1(), + [new Collection($this->getDummy1())], + ]; + + $ret[] = [ + (array) $this->getDummy2(), + [new Collection($this->getDummy2())], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/BaseMutatorTest.php b/tests/Mutator/BaseMutatorTest.php new file mode 100644 index 0000000..1edc023 --- /dev/null +++ b/tests/Mutator/BaseMutatorTest.php @@ -0,0 +1,91 @@ +name = 'dummy'; + $dummy->foo = 'bar'; + $dummy->baz = 'qux'; + + return $dummy; + } + + /** + * @return object + */ + protected static function getDummy2() + { + $dummy = self::getDummy1(); + $dummy->false = false; + $dummy->null = null; + $dummy->zero = 0; + + return $dummy; + } + + protected static function getDummy3() + { + $dummy = [ + 'Angela' => [ + 'position' => 'dean', + 'sex' => 'female', + ], + 'Bob' => [ + 'position' => 'janitor', + 'sex' => 'male', + ], + 'Mark' => [ + 'position' => 'teacher', + 'sex' => 'male', + 'tenured' => true, + ], + 'Wendy' => [ + 'position' => 'teacher', + 'sex' => 'female', + 'tenured' => 1, + ], + ]; + + return $dummy; + } + + + /** + * @return object + */ + protected static function getDummy4() + { + $dummy = self::getDummy2(); + $dummy->false = false; + $dummy->null = null; + $dummy->zero = 0; + $dummy->one = 1; + + return $dummy; + } + /** + * @return Mutator|callable + */ + abstract protected function getInstance(); + + /** + * @return mixed[] + */ + abstract public function getTestInvokeData(); + + /** + * @param mixed $expected + * @param mixed $args + * @dataProvider getTestInvokeData + */ + public function testInvoke($expected, $args) + { + $this->assertSame($expected, call_user_func_array($this->getInstance(), $args)->toArray()); + } +} diff --git a/tests/Mutator/FilterMutatorTest.php b/tests/Mutator/FilterMutatorTest.php new file mode 100644 index 0000000..f70c065 --- /dev/null +++ b/tests/Mutator/FilterMutatorTest.php @@ -0,0 +1,36 @@ + 'dummy'], + [new Collection($this->getDummy1()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/GroupByMutatorTest.php b/tests/Mutator/GroupByMutatorTest.php new file mode 100644 index 0000000..aa61a92 --- /dev/null +++ b/tests/Mutator/GroupByMutatorTest.php @@ -0,0 +1,35 @@ + ['dummy'], + 3 => ['bar', 'qux'], + ], + [new Collection($this->getDummy1()), 'strlen'], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/HeadMutatorTest.php b/tests/Mutator/HeadMutatorTest.php new file mode 100644 index 0000000..3de26d0 --- /dev/null +++ b/tests/Mutator/HeadMutatorTest.php @@ -0,0 +1,32 @@ + 'dummy', 'foo' => 'bar'], + [new Collection($this->getDummy1()), 2], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/InitialMutatorTest.php b/tests/Mutator/InitialMutatorTest.php new file mode 100644 index 0000000..9dcbfea --- /dev/null +++ b/tests/Mutator/InitialMutatorTest.php @@ -0,0 +1,32 @@ + 'dummy'], + [new Collection($this->getDummy1()), 2], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/KeysMutatorTest.php b/tests/Mutator/KeysMutatorTest.php new file mode 100644 index 0000000..d78b95d --- /dev/null +++ b/tests/Mutator/KeysMutatorTest.php @@ -0,0 +1,32 @@ +getDummy1())], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/LastMutatorTest.php b/tests/Mutator/LastMutatorTest.php new file mode 100644 index 0000000..35b09ed --- /dev/null +++ b/tests/Mutator/LastMutatorTest.php @@ -0,0 +1,32 @@ + 'bar', 'baz' => 'qux'], + [new Collection($this->getDummy1()), 2], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/MapMutatorTest.php b/tests/Mutator/MapMutatorTest.php new file mode 100644 index 0000000..2195025 --- /dev/null +++ b/tests/Mutator/MapMutatorTest.php @@ -0,0 +1,36 @@ + 'name:dummy', 'foo' => 'foo:bar', 'baz' => 'baz:qux'], + [new Collection($this->getDummy1()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/MergeMutatorTest.php b/tests/Mutator/MergeMutatorTest.php new file mode 100644 index 0000000..18b3f30 --- /dev/null +++ b/tests/Mutator/MergeMutatorTest.php @@ -0,0 +1,39 @@ + 'dummy', + 'foo' => 'bar', + 'baz' => 'qux', + 'false' => false, + 'null' => null, + 'zero' => 0, + ], + [new Collection($this->getDummy1()), new Collection($this->getDummy2())], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/PickMutatorTest.php b/tests/Mutator/PickMutatorTest.php new file mode 100644 index 0000000..9722c1d --- /dev/null +++ b/tests/Mutator/PickMutatorTest.php @@ -0,0 +1,38 @@ +getDummy1(), $this->getDummy1()]), 'foo'], + ]; + + $ret[] = [ + ['foo'], + [new Collection([new Dummy()]), 'getFoo'], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/RejectMutatorTest.php b/tests/Mutator/RejectMutatorTest.php new file mode 100644 index 0000000..23c90b7 --- /dev/null +++ b/tests/Mutator/RejectMutatorTest.php @@ -0,0 +1,36 @@ + 'bar', 'baz' => 'qux'], + [new Collection($this->getDummy1()), $func], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/SortByMutatorTest.php b/tests/Mutator/SortByMutatorTest.php new file mode 100644 index 0000000..45d6468 --- /dev/null +++ b/tests/Mutator/SortByMutatorTest.php @@ -0,0 +1,32 @@ +getDummy1()), 'strlen'], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/TailMutatorTest.php b/tests/Mutator/TailMutatorTest.php new file mode 100644 index 0000000..d468c73 --- /dev/null +++ b/tests/Mutator/TailMutatorTest.php @@ -0,0 +1,32 @@ + 'bar', 'baz' => 'qux'], + [new Collection($this->getDummy1()), 1], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/ValuesMutatorTest.php b/tests/Mutator/ValuesMutatorTest.php new file mode 100644 index 0000000..865f108 --- /dev/null +++ b/tests/Mutator/ValuesMutatorTest.php @@ -0,0 +1,32 @@ +getDummy1())], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/WithoutMutatorTest.php b/tests/Mutator/WithoutMutatorTest.php new file mode 100644 index 0000000..265df36 --- /dev/null +++ b/tests/Mutator/WithoutMutatorTest.php @@ -0,0 +1,32 @@ + 'bar', 'baz' => 'qux'], + [new Collection($this->getDummy1()), ['dummy']], + ]; + + return $ret; + } +} diff --git a/tests/Mutator/ZipMutatorTest.php b/tests/Mutator/ZipMutatorTest.php new file mode 100644 index 0000000..5e75ef5 --- /dev/null +++ b/tests/Mutator/ZipMutatorTest.php @@ -0,0 +1,44 @@ + 'dummy', + 1 => 'bar', + '42' => 'qux', + ], + [new Collection($this->getDummy1()), ['a', 1, '42']], + ]; + + return $ret; + } + + /** + * @expectedException \LogicException + */ + public function testThrowIfWrongDimmensions() + { + $this->getInstance()->__invoke(new Collection($this->getDummy1()), ['a']); + } +} diff --git a/tests/UnderscoreTest.php b/tests/UnderscoreTest.php index ec8a775..951eb02 100644 --- a/tests/UnderscoreTest.php +++ b/tests/UnderscoreTest.php @@ -106,12 +106,6 @@ public function testRange($start, $stop, $step, $expected, $exception = null) ); } - public function testValue() - { - $value = Underscore::from($this->getDummy())->value(); - $this->assertEquals((array)$this->getDummy(), $value); - } - public function testInvoke() { $buffer = ''; @@ -124,198 +118,6 @@ function ($value, $key) use (&$buffer) { $this->assertSame('name:dummy|foo:bar|baz:qux|', $buffer); } - public function testMap() - { - $value = Underscore::from($this->getDummy()) - ->map( - function ($value, $key) use (&$buffer) { - return sprintf('%s:%s', $key, $value); - } - )->toArray(); - - $this->assertSame( - ['name' => 'name:dummy', 'foo' => 'foo:bar', 'baz' => 'baz:qux'], - $value - ); - } - - public function testReduce() - { - $value = Underscore::from($this->getDummy()) - ->reduce( - function ($accu, $value) { - $accu .= $value . ' '; - - return $accu; - }, - '' - ); - - $this->assertSame('dummy bar qux ', $value); - } - - public function testReduceRight() - { - $value = Underscore::from($this->getDummy()) - ->reduceRight( - function ($accumulator, $value) { - $accumulator .= $value . ' '; - - return $accumulator; - }, - '' - ); - - $this->assertSame('qux bar dummy ', $value); - } - - public function testPick() - { - $value = Underscore::from([$this->getDummy(), $this->getDummy(), $this->getDummy()]) - ->pick('foo') - ->toArray(); - - $this->assertSame(['bar', 'bar', 'bar'], $value); - } - - public function testPickGetter() - { - $value = Underscore::from([new Dummy()]) - ->pick('getFoo') - ->toArray(); - - $this->assertSame(['foo'], $value); - } - - public function testContains() - { - $this->assertTrue(Underscore::from($this->getDummy())->contains('bar')); - $this->assertFalse(Underscore::from($this->getDummy())->contains('baz')); - } - - public function testFind() - { - $iterator = function ($needle) { - return function ($value) use ($needle) { - return $value === $needle; - }; - }; - $this->assertTrue(Underscore::from($this->getDummy())->find($iterator('bar'))); - $this->assertFalse(Underscore::from($this->getDummy())->find($iterator('foo'))); - } - - public function testFilter() - { - $value = Underscore::from($this->getDummy()) - ->filter( - function ($value) { - return 3 < strlen($value); - } - ) - ->toArray(); - - $this->assertSame(['name' => 'dummy'], $value); - } - - public function testReject() - { - $value = Underscore::from($this->getDummy()) - ->reject( - function ($value) { - return 3 < strlen($value); - } - ) - ->toArray(); - - $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $value); - } - - public function testAny() - { - $value = Underscore::from($this->getDummy()) - ->any( - function ($value) { - return 3 < strlen($value); - } - ); - - $this->assertSame(true, $value); - - $value = Underscore::from($this->getDummy()) - ->any( - function ($value) { - return strlen($value) < 2; - } - ); - - $this->assertSame(false, $value); - } - - public function testAll() - { - $value = Underscore::from($this->getDummy()) - ->all( - function ($value) { - return 3 <= strlen($value); - } - ); - - $this->assertSame(true, $value); - - $value = Underscore::from($this->getDummy()) - ->all( - function ($value) { - return 3 < strlen($value); - } - ); - - $this->assertSame(false, $value); - } - - public function testSize() - { - $value = Underscore::from($this->getDummy()) - ->size(); - - $this->assertSame(3, $value); - } - - public function testHead() - { - $value = Underscore::from($this->getDummy()) - ->head(2) - ->value(); - - $this->assertSame(['name' => 'dummy', 'foo' => 'bar'], $value); - } - - public function testTail() - { - $value = Underscore::from($this->getDummy()) - ->tail(1) - ->value(); - - $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $value); - } - - public function testInitial() - { - $value = Underscore::from($this->getDummy()) - ->initial(2) - ->value(); - - $this->assertSame(['name' => 'dummy'], $value); - } - - public function testLast() - { - $value = Underscore::from($this->getDummy()) - ->last(2) - ->value(); - - $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $value); - } - public function testCompact() { $value = Underscore::from($this->getDummy2()) @@ -325,78 +127,6 @@ public function testCompact() $this->assertSame(['name' => 'dummy', 'foo' => 'bar', 'baz' => 'qux'], $value); } - public function testWithout() - { - $value = Underscore::from($this->getDummy()) - ->without(['dummy']) - ->toArray(); - - $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $value); - } - - public function testMerge() - { - $value = Underscore::from($this->getDummy()) - ->merge(new Collection($this->getDummy2())) - ->toArray(); - - $this->assertSame( - [ - 'name' => 'dummy', - 'foo' => 'bar', - 'baz' => 'qux', - 'false' => false, - 'null' => null, - 'zero' => 0, - ], - $value - ); - } - - public function testValues() - { - $value = Underscore::from($this->getDummy()) - ->values() - ->toArray(); - - $this->assertSame( - [ - 'dummy', - 'bar', - 'qux', - ], - $value - ); - } - - public function testKeys() - { - $value = Underscore::from($this->getDummy()) - ->keys() - ->toArray(); - - $this->assertSame( - [ - 'name', - 'foo', - 'baz', - ], - $value - ); - } - - public function testHas() - { - $collection = Underscore::from($this->getDummy()); - - $this->assertTrue($collection->has('name')); - $this->assertTrue($collection->has('foo')); - $this->assertTrue($collection->has('baz')); - - $this->assertFalse($collection->has('nope')); - $this->assertFalse($collection->has('missing')); - } - public function testClone() { $original = $this->getDummy(); @@ -411,57 +141,6 @@ public function testClone() ); } - public function testZip() - { - $value = Underscore::from($this->getDummy()) - ->zip(['a', 1, '42']) - ->toArray(); - - $this->assertSame( - [ - 'a' => 'dummy', - 1 => 'bar', - '42' => 'qux', - ], - $value - ); - - $this->setExpectedException('\LogicException'); - Underscore::from($this->getDummy()) - ->zip(['a']); - } - - public function testGroupBy() - { - $value = Underscore::from($this->getDummy()) - ->groupBy('strlen') - ->toArray(); - - $this->assertSame( - [ - 5 => ['dummy'], - 3 => ['bar', 'qux'], - ], - $value - ); - } - - public function testSortBy() - { - $value = Underscore::from($this->getDummy()) - ->sortBy('strlen') - ->toArray(); - - $this->assertSame( - [ - 'bar', - 'qux', - 'dummy', - ], - $value - ); - } - /** * @return array */