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 Dec 20, 2020
1 parent 4b408f5 commit 3219575
Show file tree
Hide file tree
Showing 2 changed files with 87 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 @@ -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'");

0 comments on commit 3219575

Please sign in to comment.