|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Lambdish\Phunctional\Tests; |
| 6 | + |
| 7 | +use ArrayIterator; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use function Lambdish\Phunctional\get_each; |
| 10 | + |
| 11 | +final class GetEachTest extends TestCase { |
| 12 | + /** @test */ |
| 13 | + public function it_should_return_an_array_with_values_of_each_item_of_an_existent_key_indexed_by_integers(): void { |
| 14 | + $actual = [ |
| 15 | + [ |
| 16 | + 'key' => 1, |
| 17 | + 'other_key' => 3 |
| 18 | + ], |
| 19 | + [ |
| 20 | + 'key' => 2, |
| 21 | + 'other_key' => 4 |
| 22 | + ] |
| 23 | + ]; |
| 24 | + |
| 25 | + $this->assertSame([1, 2], get_each('key', $actual)); |
| 26 | + } |
| 27 | + |
| 28 | + /** @test */ |
| 29 | + public function it_should_return_an_array_with_values_of_each_item_of_an_existent_key(): void { |
| 30 | + $actual = [ |
| 31 | + 'one' => [ |
| 32 | + 'key' => 1, |
| 33 | + 'other_key' => 3 |
| 34 | + ], |
| 35 | + 'two' => [ |
| 36 | + 'key' => 2, |
| 37 | + 'other_key' => 4 |
| 38 | + ] |
| 39 | + ]; |
| 40 | + |
| 41 | + $this->assertSame([1, 2], get_each('key', $actual)); |
| 42 | + } |
| 43 | + |
| 44 | + /** @test */ |
| 45 | + public function it_should_return_an_array_with_values_of_each_item_of_an_existent_property_of_a_traversable(): void { |
| 46 | + $traversable = new ArrayIterator([ |
| 47 | + 'one' => [ |
| 48 | + 'key' => 1, |
| 49 | + 'other_key' => 3 |
| 50 | + ], |
| 51 | + 'two' => [ |
| 52 | + 'key' => 2, |
| 53 | + 'other_key' => 4 |
| 54 | + ] |
| 55 | + ]); |
| 56 | + |
| 57 | + $this->assertSame([1, 2], get_each('key', $traversable)); |
| 58 | + } |
| 59 | + |
| 60 | + /** @test */ |
| 61 | + public function it_should_return_an_empty_array_if_the_key_does_not_exist(): void { |
| 62 | + $actual = [ |
| 63 | + 'one' => [ |
| 64 | + 'key' => 1, |
| 65 | + 'other_key' => 3 |
| 66 | + ], |
| 67 | + 'two' => [ |
| 68 | + 'key' => 2, |
| 69 | + 'other_key' => 4 |
| 70 | + ] |
| 71 | + ]; |
| 72 | + |
| 73 | + $this->assertSame([], get_each('not_existing_key', $actual)); |
| 74 | + } |
| 75 | + |
| 76 | + /** @test */ |
| 77 | + public function it_should_return_an_empty_array_if_the_property_does_not_exist(): void { |
| 78 | + $traversable = new ArrayIterator([ |
| 79 | + 'one' => [ |
| 80 | + 'key' => 1, |
| 81 | + 'other_key' => 3 |
| 82 | + ], |
| 83 | + 'two' => [ |
| 84 | + 'key' => 2, |
| 85 | + 'other_key' => 4 |
| 86 | + ] |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->assertSame([], get_each('not_existing_key', $traversable)); |
| 90 | + } |
| 91 | + |
| 92 | + /** @test */ |
| 93 | + public function it_should_return_an_array_with_values_of_each_item_of_an_existent_false_value(): void { |
| 94 | + $actual = [ |
| 95 | + 'one' => [ |
| 96 | + 'key' => null, |
| 97 | + 'other_key' => true |
| 98 | + ], |
| 99 | + 'two' => [ |
| 100 | + 'key' => false, |
| 101 | + 'other_key' => true |
| 102 | + ] |
| 103 | + ]; |
| 104 | + |
| 105 | + $this->assertSame([false], get_each('key', $actual)); |
| 106 | + } |
| 107 | + |
| 108 | + /** @test */ |
| 109 | + public function it_should_return_an_array_with_values_of_each_item_of_an_existent_false_property_of_a_traversable(): void { |
| 110 | + $traversable = new ArrayIterator([ |
| 111 | + 'one' => [ |
| 112 | + 'key' => null, |
| 113 | + 'other_key' => true |
| 114 | + ], |
| 115 | + 'two' => [ |
| 116 | + 'key' => false, |
| 117 | + 'other_key' => true |
| 118 | + ] |
| 119 | + ]); |
| 120 | + |
| 121 | + $this->assertSame([false], get_each('key', $traversable)); |
| 122 | + } |
| 123 | +} |
0 commit comments