Skip to content

Commit

Permalink
Asssert: add hasKey and hasNotKey assertions (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
KminekMatej authored and dg committed Mar 1, 2021
1 parent 8de16b1 commit 6591365
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,38 @@ public static function notContains($needle, $actual, string $description = null)
}


/**
* Asserts that a haystack has an expected key.
* @param string|int $key
*/
public static function hasKey($key, array $actual, string $description = null): void
{
self::$counter++;
if (!is_int($key) && !is_string($key)) {
self::fail(self::describe('Key %1 should be string or integer'), $key);

} elseif (!array_key_exists($key, $actual)) {
self::fail(self::describe('%1 should contain key %2', $description), $actual, $key);
}
}


/**
* Asserts that a haystack doesn't have an expected key.
* @param string|int $key
*/
public static function hasNotKey($key, array $actual, string $description = null): void
{
self::$counter++;
if (!is_int($key) && !is_string($key)) {
self::fail(self::describe('Key %1 should be string or integer'), $key);

} elseif (array_key_exists($key, $actual)) {
self::fail(self::describe('%1 should not contain key %2', $description), $actual, $key);
}
}


/**
* Asserts that a value is true.
* @param mixed $actual
Expand Down
58 changes: 58 additions & 0 deletions tests/Framework/Assert.hasKey.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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('1', $array);
Assert::hasKey('one', $array);

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

foreach ([[], true, false, null, new stdClass, 1.0] as $key) {
Assert::exception(function () use ($key, $array) {
Assert::hasKey($key, $array);
}, Tester\AssertException::class, 'Key %a% should be string or integer');

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

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 ($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 ($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'");

0 comments on commit 6591365

Please sign in to comment.