From a83512d8da5d724eaa934827a66f5abf872a41e3 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Thu, 23 Mar 2023 07:49:11 +0100 Subject: [PATCH] Add TestArrayHelper.php --- tests/Unit/Helper/TestArrayHelper.php | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/Unit/Helper/TestArrayHelper.php diff --git a/tests/Unit/Helper/TestArrayHelper.php b/tests/Unit/Helper/TestArrayHelper.php new file mode 100644 index 00000000..a3784fa0 --- /dev/null +++ b/tests/Unit/Helper/TestArrayHelper.php @@ -0,0 +1,54 @@ +assertTrue(is_assoc(['test' => 'value'])); + } + + public function test_is_assoc_returns_false_on_indexed_array() + { + $this->assertFalse(is_assoc(['one', 'two', 'three'])); + } + + public function test_is_assoc_returns_true_on_mixed_array() + { + $this->assertTrue(is_assoc(['one', 'two', 'three' => 'value'])); + } + + public function test_array_find_returns_correct_element() + { + $arr = [ + [ + 'id' => 1 + ], + [ + 'id' => 2 + ], + [ + 'id' => 3 + ], + ]; + + $this->assertSame($arr[0], array_find($arr, fn($element) => $element['id'] === 1)); + } + + public function test_array_fields_returns_stripped_array() + { + $arr = [ + 'name' => 'John', + 'email' => 'john@example.org', + ]; + + $stripped = array_fields($arr, ['name']); + + $this->assertArrayHasKey('name', $stripped); + $this->assertArrayNotHasKey('email', $stripped); + } +}