From ac2019b0d04de189499d572c761353cc8f2141d4 Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 6 Aug 2024 21:30:11 +0100 Subject: [PATCH] more test coverage --- src/Naija.php | 8 ++++---- src/State.php | 4 ++-- tests/Unit/NaijaTest.php | 42 ++++++++++++++++++++++++++++++++++++++++ tests/Unit/StateTest.php | 34 +++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/Naija.php b/src/Naija.php index 88ab5e3..d31baae 100644 --- a/src/Naija.php +++ b/src/Naija.php @@ -46,13 +46,13 @@ public static function states(): array /** * Filter states conditionally - * @param string $key + * @param string|null $key * @param string $operator * @param string|int $value * * @return array */ - public static function where(string $key, string $operator, string|int $value = null): array + public static function where(string|null $key = null, string $operator, string|int $value = null): array { if (func_num_args() === 2) { $value = $operator; @@ -117,12 +117,12 @@ protected static function filter($items, callable $callback = null): array * Get an item from an array or object using "dot" notation. * * @param mixed $target - * @param string|array $key + * @param string|array|null $key * @param mixed $default * * @return mixed */ - protected static function get($target, $key, $default = null) + protected static function get($target, $key = null, $default = null) { if (is_null($key)) { return $target; diff --git a/src/State.php b/src/State.php index d72a401..e6221a1 100644 --- a/src/State.php +++ b/src/State.php @@ -75,7 +75,7 @@ public function set(string $key, mixed $value) * * @return mixed */ - public function get(string $key, mixed $default = null) + public function get(string $key = null, mixed $default = null) { $array = $this->attributes; @@ -191,7 +191,7 @@ public function getNickName(): ?string } - + public function getLgas(): ?array { return $this->get('lga'); diff --git a/tests/Unit/NaijaTest.php b/tests/Unit/NaijaTest.php index 2b78429..505c893 100644 --- a/tests/Unit/NaijaTest.php +++ b/tests/Unit/NaijaTest.php @@ -58,3 +58,45 @@ expect(array_values($states)[0]['name'])->toBe("Adamawa"); }); +test("Filter states with less than operator", function () { + $states = Naija::where('name', "<", "Abia"); + + expect(count($states))->toEqual(0); +}); + +test("Filter states with greater than operator", function () { + $states = Naija::where('name', ">", "Zamfara"); + + expect(count($states))->toEqual(0); +}); + +test("Filter states with less than or equals to operator", function () { + $states = Naija::where('name', "<=", "Abia"); + + expect(count($states))->toEqual(1); +}); + +test("Filter states with greater than or equals operator", function () { + $states = Naija::where('name', ">=", "Zamfara"); + + expect(count($states))->toEqual(1); +}); + +test("Filter states with strict equality operator", function () { + $states = Naija::where('name', "===", "Zamfara"); + + expect(count($states))->toEqual(1); +}); + +test("Filter states with strict non-equality operator", function () { + $states = Naija::where('name', "!==", "Zamfara"); + + expect(count($states))->toEqual(36); +}); + + +test("Filter states with null key", function () { + $states = Naija::where(null,"Abia"); + + expect(count($states))->toEqual(0); +}); diff --git a/tests/Unit/StateTest.php b/tests/Unit/StateTest.php index ae4a666..f69c331 100644 --- a/tests/Unit/StateTest.php +++ b/tests/Unit/StateTest.php @@ -14,7 +14,7 @@ expect($state->getCapital())->toBe('Umuahia'); expect($state->getGeoPoliticalZone())->toBe('South East'); expect($state->getDateCreated())->toBe('27-08-1991'); - + $population = $state->getPopulation(); expect($population['male'])->toEqual(2006420); expect($population['female'])->toEqual(1927737); @@ -25,7 +25,39 @@ expect($state->getDemonym())->toBeEmpty(); expect($state->getNickName())->toBe("God's Own State"); + expect($state->getLanguages())->toBe([ + 'english' => 'English', + 'igbo' => 'Igbo' + ]); expect(count($state->getLanguages()))->toEqual(2); expect(count($state->getLgas()))->toEqual(14); expect(count($state->getAreas()))->toEqual(131); }); + +test('set an attribute', function () { + $state = new State([]); + + $state->set('name.common', 'Abia'); + + expect($state->getName())->toBe('Abia'); +}); + +test("set attributes", function () { + $data = json_decode(file_get_contents(__DIR__ . "/../../resources/data/abia.json"), true); + + $state = new State([]); + + $state->setAttributes($data); + + expect($state->get('name.common'))->toBe('Abia'); +}); + +test("get an attribute with empty key given", function () { + $data = json_decode(file_get_contents(__DIR__ . "/../../resources/data/abia.json"), true); + + $state = new State([]); + + $state->setAttributes($data); + + expect($state->get())->toBeArray(); +});