|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (C) 2019 by Jesus Franco Martinez <tezcatl@fedoraproject.org> |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +namespace Functional\Tests; |
| 24 | + |
| 25 | +use Functional\Functional; |
| 26 | + |
| 27 | +use function Functional\create_assoc; |
| 28 | +use function Apantle\HashMapper\hashMapper; |
| 29 | + |
| 30 | +class CreateAssocTest extends AbstractTestCase |
| 31 | +{ |
| 32 | + public function testBasicCreateAssoc() |
| 33 | + { |
| 34 | + $input = new \DateTimeImmutable(); |
| 35 | + |
| 36 | + $expected = [ |
| 37 | + 'targetA' => $input, |
| 38 | + 'targetB' => $input |
| 39 | + ]; |
| 40 | + |
| 41 | + $actual = create_assoc([ |
| 42 | + 'targetA' => Functional::id, |
| 43 | + 'targetB' => Functional::id, |
| 44 | + ])($input); |
| 45 | + |
| 46 | + $this->assertEquals($expected, $actual); |
| 47 | + $this->assertEquals($input->format('U'), $actual['targetA']->format('U')); |
| 48 | + $this->assertEquals($input->format('U'), $actual['targetB']->format('U')); |
| 49 | + } |
| 50 | + |
| 51 | + public function testExpectsTameme() |
| 52 | + { |
| 53 | + $input = [ 1, 2, 3 ]; |
| 54 | + |
| 55 | + $expected = [ |
| 56 | + 'targetA' => [ 4 => 1, 5 => 2, 6 => 3 ], |
| 57 | + 'targetB' => [ 5 => 2 ] |
| 58 | + ]; |
| 59 | + |
| 60 | + $tameme = new class extends \ArrayObject {}; |
| 61 | + |
| 62 | + $actual = create_assoc([ |
| 63 | + 'targetA' => function ($member, $input = null, $tameme) { |
| 64 | + $build = \array_reduce($member, function ($accum, $num) { |
| 65 | + $accum[$num + 3] = $num; |
| 66 | + return $accum; |
| 67 | + }, []); |
| 68 | + |
| 69 | + $tameme['prev'] = $build; |
| 70 | + return $build; |
| 71 | + }, |
| 72 | + 'targetB' => function ($member, $input, $tameme) { |
| 73 | + $prev = $tameme['prev']; |
| 74 | + $build = []; |
| 75 | + foreach($prev as $key => $val) { |
| 76 | + if($key % 2 !== 0) { |
| 77 | + $build[$key] = $val; |
| 78 | + } |
| 79 | + } |
| 80 | + return $build; |
| 81 | + } |
| 82 | + ], $tameme)($input); |
| 83 | + |
| 84 | + $this->assertEquals($expected, $actual); |
| 85 | + } |
| 86 | + |
| 87 | + public function testReceivesOptionalArgument() |
| 88 | + { |
| 89 | + $input = [ |
| 90 | + 'vendor' => 'tzkmx', |
| 91 | + 'utility' => 'unfold' |
| 92 | + ]; |
| 93 | + |
| 94 | + $expected = [ |
| 95 | + 'vendorName' => 'tzkmx', |
| 96 | + 'vendorLen' => 5, |
| 97 | + 'serialized' => 'a:2:{s:6:"vendor";s:5:"tzkmx";s:7:"utility";s:6:"unfold";}', |
| 98 | + 'utility' => 'unfold', |
| 99 | + 'utilLen' => 6, |
| 100 | + 'package' => [ 'tzkmx/unfold' => $input ] |
| 101 | + ]; |
| 102 | + |
| 103 | + $tameme = new class extends \ArrayObject {}; |
| 104 | + |
| 105 | + $actual = hashMapper([ |
| 106 | + 'vendor' => [ '...', create_assoc([ |
| 107 | + 'vendorName' => 'strval', |
| 108 | + 'vendorLen' => 'strlen', |
| 109 | + 'serialized' => function ($member, $hash, $tameme) { |
| 110 | + $tameme['name'] = $member; |
| 111 | + return \serialize($hash); |
| 112 | + } |
| 113 | + ], $tameme) |
| 114 | + ], |
| 115 | + 'utility' => [ '...', create_assoc([ |
| 116 | + 'utility' => 'strval', |
| 117 | + 'utilLen' => 'strlen', |
| 118 | + 'package' => function ($member, $hash, $tameme) { |
| 119 | + $name = $tameme['name']; |
| 120 | + return [ "$name/$member" => $hash ]; |
| 121 | + } |
| 122 | + ], $tameme) |
| 123 | + ] |
| 124 | + ])($input); |
| 125 | + |
| 126 | + $this->assertEquals($expected, $actual); |
| 127 | + } |
| 128 | +} |
0 commit comments