|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package Functional-php |
| 5 | + * @author Lars Strojny <lstrojny@php.net> |
| 6 | + * @copyright 2011-2021 Lars Strojny |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + * @link https://github.com/lstrojny/functional-php |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Functional\Tests; |
| 12 | + |
| 13 | +use ArrayIterator; |
| 14 | +use Functional\Exceptions\InvalidArgumentException; |
| 15 | + |
| 16 | +use function Functional\frequencies; |
| 17 | + |
| 18 | +class FrequenciesTest extends AbstractTestCase |
| 19 | +{ |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + $this->list = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']; |
| 24 | + $this->listIterator = new ArrayIterator($this->list); |
| 25 | + $this->hash = ['a' => 'one', 'b' => 'two', 'c' => 'one', 'd' => 'two', 'e' => 'one']; |
| 26 | + $this->hashIterator = new ArrayIterator($this->hash); |
| 27 | + } |
| 28 | + |
| 29 | + public function test(): void |
| 30 | + { |
| 31 | + $expected = ['apple' => 3, 'banana' => 2, 'cherry' => 1]; |
| 32 | + self::assertSame($expected, frequencies($this->list)); |
| 33 | + self::assertSame($expected, frequencies($this->listIterator)); |
| 34 | + |
| 35 | + $expectedHash = ['one' => 3, 'two' => 2]; |
| 36 | + self::assertSame($expectedHash, frequencies($this->hash)); |
| 37 | + self::assertSame($expectedHash, frequencies($this->hashIterator)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testWithCallback(): void |
| 41 | + { |
| 42 | + $fn = function ($v, $k, $collection) { |
| 43 | + InvalidArgumentException::assertCollection($collection, __FUNCTION__, 3); |
| 44 | + return \strlen($v); |
| 45 | + }; |
| 46 | + |
| 47 | + $expected = [5 => 3, 6 => 3]; |
| 48 | + self::assertSame($expected, frequencies($this->list, $fn)); |
| 49 | + self::assertSame($expected, frequencies($this->listIterator, $fn)); |
| 50 | + |
| 51 | + $expectedHash = [3 => 5]; |
| 52 | + self::assertSame($expectedHash, frequencies($this->hash, $fn)); |
| 53 | + self::assertSame($expectedHash, frequencies($this->hashIterator, $fn)); |
| 54 | + } |
| 55 | + |
| 56 | + public function testEmptyCollection(): void |
| 57 | + { |
| 58 | + self::assertSame([], frequencies([])); |
| 59 | + self::assertSame([], frequencies(new ArrayIterator([]))); |
| 60 | + } |
| 61 | + |
| 62 | + public function testNumericValues(): void |
| 63 | + { |
| 64 | + $list = [1, 2, 1, 3, 2, 1]; |
| 65 | + $expected = [1 => 3, 2 => 2, 3 => 1]; |
| 66 | + self::assertSame($expected, frequencies($list)); |
| 67 | + self::assertSame($expected, frequencies(new ArrayIterator($list))); |
| 68 | + } |
| 69 | + |
| 70 | + public function testExceptionIsThrownWhenCallbackReturnsInvalidKey(): void |
| 71 | + { |
| 72 | + $invalidTypes = [ |
| 73 | + 'resource' => \stream_context_create(), |
| 74 | + 'object' => new \stdClass(), |
| 75 | + 'array' => [] |
| 76 | + ]; |
| 77 | + |
| 78 | + foreach ($invalidTypes as $type => $value) { |
| 79 | + $fn = function () use ($value) { |
| 80 | + return $value; |
| 81 | + }; |
| 82 | + try { |
| 83 | + frequencies(['v1'], $fn); |
| 84 | + self::fail(\sprintf('Error expected for array key type "%s"', $type)); |
| 85 | + } catch (\Exception $e) { |
| 86 | + self::assertSame( |
| 87 | + \sprintf( |
| 88 | + 'Functional\frequencies(): callback returned invalid array key of type "%s". Expected NULL, string, integer, double or boolean', |
| 89 | + $type |
| 90 | + ), |
| 91 | + $e->getMessage() |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public function testPassNoCollection(): void |
| 98 | + { |
| 99 | + $this->expectArgumentError('Functional\frequencies() expects parameter 1 to be array or instance of Traversable'); |
| 100 | + frequencies('invalidCollection'); |
| 101 | + } |
| 102 | + |
| 103 | + public function testPassNonCallable(): void |
| 104 | + { |
| 105 | + $this->expectCallableArgumentError('Functional\frequencies', 2); |
| 106 | + frequencies($this->list, 'undefinedFunction'); |
| 107 | + } |
| 108 | +} |
0 commit comments