Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Add support to dot notation in toHaveKey expectation #15

Closed
wants to merge 10 commits into from
12 changes: 10 additions & 2 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use BadMethodCallException;
use Pest\Expectations\Concerns\Extendable;
use Pest\Expectations\Helpers\Arr;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Exporter\Exporter;

/**
Expand Down Expand Up @@ -522,10 +524,16 @@ public function toHaveKey($key, $value = null): Expectation
$array = (array) $this->value;
}

Assert::assertArrayHasKey($key, $array);
try {
Assert::assertTrue(Arr::has($array, $key));

/* @phpstan-ignore-next-line */
} catch (ExpectationFailedException $exception) {
throw new ExpectationFailedException("Failed asserting that an array has the key '$key'", $exception->getComparisonFailure());
}

if (func_num_args() > 1) {
Assert::assertEquals($value, $array[$key]);
Assert::assertEquals($value, Arr::get($array, $key));
}

return $this;
Expand Down
68 changes: 68 additions & 0 deletions src/Helpers/Arr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Pest\Expectations\Helpers;

/**
* Credits: most of this class methods and implementations
* belongs to the Arr helper of laravel/framework project
* (https://github.com/laravel/framework).
*
* @internal
*/
final class Arr
fabio-ivona marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @param array<mixed> $array
* @param string|int $key
*/
public static function has(array $array, $key): bool
{
$key = (string) $key;

if (array_key_exists($key, $array)) {
return true;
}

foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return false;
}
}

return true;
}

/**
* @param array<mixed> $array
* @param string|int $key
* @param null $default
*
* @return array|mixed|null
*/
public static function get(array $array, $key, $default = null)
{
$key = (string) $key;

if (array_key_exists($key, $array)) {
return $array[$key];
}

if (strpos($key, '.') === false) {
return $array[$key] ?? $default;
}

foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return $default;
}
}

return $array;
}
}
95 changes: 86 additions & 9 deletions tests/Expect/toHaveKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,91 @@

use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKey('c');
});
dataset('test array', [
'test array' => [
'array' => [
'a' => 1,
'b',
'c' => 'world',
'd' => [
'e' => 'hello',
],
'key.with.dots' => false,
],
],
]);

test('failures', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKey('hello');
})->throws(ExpectationFailedException::class);
test('pass', function ($array, $key) {
expect($array)->toHaveKey($key);
})->with('test array')->with([
'plain key' => 'c',
'nested key' => 'd.e',
'plain key with dots' => 'key.with.dots',
]);

test('not failures', function () {
expect(['a' => 1, 'hello' => 'world', 'c'])->not->toHaveKey('hello');
})->throws(ExpectationFailedException::class);
test('pass with value', function ($array, $key, $value) {
expect($array)->toHaveKey($key, $value);
})->with('test array')->with([
'plain key' => [
'key' => 'c',
'value' => 'world',
],
'nested key' => [
'key' => 'd.e',
'value' => 'hello',
],
'plain key with dots' => [
'key' => 'key.with.dots',
'value' => false,
],
]);

test('failures', function ($array, $key) {
expect($array)->toHaveKey($key);
})->with('test array')->with([
'plain key' => 'foo',
'nested key' => 'd.bar',
'plain key with dots' => 'missing.key.with.dots',
])->throws(ExpectationFailedException::class);

test('fails with wrong value', function ($array, $key, $value) {
expect($array)->toHaveKey($key, $value);
})->with('test array')->with([
'plain key' => [
'key' => 'c',
'value' => 'bar',
],
'nested key' => [
'key' => 'd.e',
'value' => 'foo',
],
'plain key with dots' => [
'key' => 'key.with.dots',
'value' => true,
],
])->throws(ExpectationFailedException::class);

test('not failures', function ($array, $key) {
expect($array)->not->toHaveKey($key);
})->with('test array')->with([
'plain key' => 'c',
'nested key' => 'd.e',
'plain key with dots' => 'key.with.dots',
])->throws(ExpectationFailedException::class);

test('not failures with correct value', function ($array, $key, $value) {
expect($array)->not->toHaveKey($key, $value);
})->with('test array')->with([
'plain key' => [
'key' => 'c',
'value' => 'world',
],
'nested key' => [
'key' => 'd.e',
'value' => 'hello',
],
'plain key with dots' => [
'key' => 'key.with.dots',
'value' => false,
],
])->throws(ExpectationFailedException::class);
6 changes: 3 additions & 3 deletions tests/Expect/toHaveKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKeys(['a', 'c']);
expect(['a' => 1, 'b', 'c' => 'world', 'foo' => ['bar' => 'baz']])->toHaveKeys(['a', 'c', 'foo.bar']);
});

test('failures', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKeys(['a', 'd']);
expect(['a' => 1, 'b', 'c' => 'world', 'foo' => ['bar' => 'baz']])->toHaveKeys(['a', 'd', 'foo.bar', 'hello.world']);
})->throws(ExpectationFailedException::class);

test('not failures', function () {
expect(['a' => 1, 'hello' => 'world', 'c'])->not->toHaveKeys(['hello', 'c']);
expect(['a' => 1, 'b', 'c' => 'world', 'foo' => ['bar' => 'baz']])->not->toHaveKeys(['foo.bar', 'c', 'z']);
})->throws(ExpectationFailedException::class);