123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace fphammerle\helpers\tests;
- use fphammerle\helpers\ArrayHelper;
- class ArrayHelperTest extends \PHPUnit_Framework_TestCase
- {
- public function testFlattenEmpty()
- {
- $this->assertEquals(
- [],
- ArrayHelper::flatten([])
- );
- }
- public function testFlattenFlat()
- {
- $this->assertEquals(
- [1, 2, 3],
- ArrayHelper::flatten([1, 2, 3])
- );
- }
- public function testFlatten2D()
- {
- $this->assertEquals(
- [1, 2, 3, 4, 5],
- ArrayHelper::flatten([1, [2, 3], [4], 5])
- );
- }
- public function testFlatten2DEmpty()
- {
- $this->assertEquals(
- [1, 2, 3, 4, 5],
- ArrayHelper::flatten([[], 1, [2, 3], [], [4], 5])
- );
- }
- public function testFlatten3D()
- {
- $this->assertEquals(
- [1, 2, 3, 4, 5, 6],
- ArrayHelper::flatten([1, [[2, 3], 4], [[5], [6]]])
- );
- }
- public function testFlatten3DEmpty()
- {
- $this->assertEquals(
- [1, 2, 3, 4, 5, 6],
- ArrayHelper::flatten([1, [[2, [], 3], 4, []], [[5], [6]], []])
- );
- }
- public function testFlattenAssociativeLetterFlat()
- {
- $this->assertEquals(
- [1, 2, 3, 4],
- ArrayHelper::flatten(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])
- );
- }
- public function testFlattenAssociativeDigit()
- {
- $this->assertEquals(
- [1, 2, 3, 4],
- ArrayHelper::flatten([1 => 1, 2 => 2, 3 => 3, 4 => 4])
- );
- }
- public function testFlattenAssociativeDigitReverse()
- {
- $this->assertEquals(
- [1, 2, 3, 4],
- ArrayHelper::flatten([3 => 1, 2 => 2, 1 => 3, 0 => 4])
- );
- }
- public function testFlattenMixed2D()
- {
- $this->assertEquals(
- [1, 2, 3, 4, 5, 6, 7, 8],
- ArrayHelper::flatten([
- 'a' => 1,
- 'b' => [2, 3],
- 'c' => ['#' => 4, '@' => 5],
- 'd' => [6, '$' => 7],
- 8,
- ])
- );
- }
- public function mapProvider()
- {
- return [
- [
- null,
- function($v) { return 1; },
- 1,
- ],
- [
- 'string',
- function($v) { return strrev($v); },
- 'gnirts',
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($v) { return $v; },
- ['a' => 1, 'b' => 2, 'c' => 3],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($v) { return $v < 2 ? null : $v; },
- ['a' => null, 'b' => 2, 'c' => 3],
- ],
- [
- ['a' => 1, 'b' => null, 'c' => 3],
- function($v) { return $v; },
- ['a' => 1, 'b' => null, 'c' => 3],
- ],
- [
- ['a' => 1, 'b' => 2, 'b' => 3],
- function($v) { return $v * $v; },
- ['a' => 1, 'b' => 4, 'b' => 9],
- ],
- ];
- }
- /**
- * @dataProvider mapProvider
- */
- public function testMap($source, $callback, $expected)
- {
- $this->assertSame($expected, ArrayHelper::map($source, $callback));
- }
- public function multimapProvider()
- {
- return [
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return [$k => $v]; },
- ['a' => 1, 'b' => 2, 'c' => 3],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return [$v => $k]; },
- [1 => 'a', 2 => 'b', 3 => 'c'],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return [strtoupper($k) => $v * $v]; },
- ['A' => 1, 'B' => 4, 'C' => 9],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return [$k => null]; },
- ['a' => null, 'b' => null, 'c' => null],
- ],
- [
- ['a' => 1, 'b' => 2, 'b' => 3],
- function($k, $v) { return [$k => $v]; },
- ['a' => 1, 'b' => 3],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return ($v % 2) ? [$k => $v * $v] : null; },
- ['a' => 1, 'c' => 9],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return ($v % 2) ? [$k => $v * $v] : []; },
- ['a' => 1, 'c' => 9],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return [$k => $v, $k.'^2' => $v * $v]; },
- ['a' => 1, 'a^2' => 1, 'b' => 2, 'b^2' => 4, 'c' => 3, 'c^2' => 9],
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return [$k => [[$v]], strtoupper($k) => []]; },
- ['a' => [[1]], 'A' => [], 'b' => [[2]], 'B' => [], 'c' => [[3]], 'C' => []],
- ],
- ];
- }
- /**
- * @dataProvider multimapProvider
- */
- public function testMultimap($source, $callback, $expected)
- {
- $this->assertSame($expected, ArrayHelper::multimap($source, $callback));
- }
- public function multimapUnexpectedValueProvider()
- {
- return [
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return $v; },
- ],
- [
- ['a' => 1, 'b' => 2, 'c' => 3],
- function($k, $v) { return 'string'; },
- ],
- ];
- }
- /**
- * @dataProvider multimapUnexpectedValueProvider
- * @expectedException \UnexpectedValueException
- */
- public function testMultimapUnexpectedValue($source, $callback)
- {
- ArrayHelper::multimap($source, $callback);
- }
- }
|