Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
emmadonjo committed Aug 6, 2024
1 parent d1b79a7 commit ac2019b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/Naija.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -191,7 +191,7 @@ public function getNickName(): ?string

}


public function getLgas(): ?array
{
return $this->get('lga');
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/NaijaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
34 changes: 33 additions & 1 deletion tests/Unit/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});

0 comments on commit ac2019b

Please sign in to comment.