Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasKey and hasNotKey assertions #427

Merged
merged 11 commits into from
Dec 20, 2020
32 changes: 32 additions & 0 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,38 @@ public static function notContains($needle, $actual, string $description = null)
}


/**
* Asserts that a haystack (string or array) has an expected key.
*/
public static function hasKey($key, $actual, string $description = null): void
{
self::$counter++;
if (is_array($actual)) {
if (!array_key_exists($key, $actual)) {
self::fail(self::describe('%1 should contain key %2', $description), $actual, $key);
}
} else {
self::fail(self::describe('%1 should be array', $description), $actual);
}
}


/**
* Asserts that a haystack (string or array) doesnt have an expected key.
*/
public static function hasNotKey($key, $actual, string $description = null): void
{
self::$counter++;
if (is_array($actual)) {
if (array_key_exists($key, $actual)) {
self::fail(self::describe('%1 should not contain key %2', $description), $actual, $key);
}
} else {
self::fail(self::describe('%1 should be array', $description), $actual);
}
}


/**
* Asserts that a value is true.
* @param mixed $actual
Expand Down
55 changes: 55 additions & 0 deletions tests/Framework/Assert.hasKey.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$array = [
1 => 1,
'one' => 'one',
];

$string= 'Lorem ipsum';

Assert::hasKey(1, $array);
Assert::hasKey('one', $array);

Assert::hasNotKey(2, $array);
Assert::hasNotKey('two', $array);

Assert::exception(function () use ($array) {
Assert::hasKey(2, $array);
}, Tester\AssertException::class, '%a% should contain key %a%');

Assert::exception(function () use ($array) {
Assert::hasKey('two', $array);
}, Tester\AssertException::class, '%a% should contain key %a%');

Assert::exception(function () use ($string) {
Assert::hasKey('two', $string);
}, Tester\AssertException::class, '%a% should be array');


Assert::exception(function () use ($array) {
Assert::hasNotKey(1, $array);
}, Tester\AssertException::class, '%a% should not contain key %a%');

Assert::exception(function () use ($array) {
Assert::hasNotKey('one', $array);
}, Tester\AssertException::class, '%a% should not contain key %a%');


Assert::exception(function () use ($string) {
Assert::hasNotKey('two', $string);
}, Tester\AssertException::class, '%a% should be array');

Assert::exception(function () use ($array) {
Assert::hasKey('two', $array, 'Custom description');
}, Tester\AssertException::class, "Custom description: [1 => 1, 'one' => 'one'] should contain key 'two'");

Assert::exception(function () use ($array) {
Assert::hasNotKey('one', $array, 'Custom description');
}, Tester\AssertException::class, "Custom description: [1 => 1, 'one' => 'one'] should not contain key 'one'");