Skip to content

Commit 6e05472

Browse files
KminekMatejdg
authored andcommitted
Asssert: add hasKey and hasNotKey assertions (#427)
1 parent 4b408f5 commit 6e05472

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/Framework/Assert.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,38 @@ public static function notContains($needle, $actual, string $description = null)
141141
}
142142

143143

144+
/**
145+
* Asserts that a haystack has an expected key.
146+
* @param string|int $key
147+
*/
148+
public static function hasKey($key, array $actual, string $description = null): void
149+
{
150+
self::$counter++;
151+
if (!is_int($key) && !is_string($key)) {
152+
self::fail(self::describe('Key %1 should be string or integer'), $key);
153+
154+
} elseif (!array_key_exists($key, $actual)) {
155+
self::fail(self::describe('%1 should contain key %2', $description), $actual, $key);
156+
}
157+
}
158+
159+
160+
/**
161+
* Asserts that a haystack doesn't have an expected key.
162+
* @param string|int $key
163+
*/
164+
public static function hasNotKey($key, array $actual, string $description = null): void
165+
{
166+
self::$counter++;
167+
if (!is_int($key) && !is_string($key)) {
168+
self::fail(self::describe('Key %1 should be string or integer'), $key);
169+
170+
} elseif (array_key_exists($key, $actual)) {
171+
self::fail(self::describe('%1 should not contain key %2', $description), $actual, $key);
172+
}
173+
}
174+
175+
144176
/**
145177
* Asserts that a value is true.
146178
* @param mixed $actual

tests/Framework/Assert.hasKey.phpt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Assert;
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
$array = [
11+
1 => 1,
12+
'one' => 'one',
13+
];
14+
15+
$string= 'Lorem ipsum';
16+
17+
Assert::hasKey(1, $array);
18+
Assert::hasKey('1', $array);
19+
Assert::hasKey('one', $array);
20+
21+
Assert::hasNotKey(2, $array);
22+
Assert::hasNotKey('two', $array);
23+
24+
foreach ([[], true, false, null, new stdClass, 1.0] as $key) {
25+
Assert::exception(function () use ($key, $array) {
26+
Assert::hasKey($key, $array);
27+
}, Tester\AssertException::class, 'Key %a% should be string or integer');
28+
29+
Assert::exception(function () use ($key, $array) {
30+
Assert::hasNotKey($key, $array);
31+
}, Tester\AssertException::class, 'Key %a% should be string or integer');
32+
}
33+
34+
Assert::exception(function () use ($array) {
35+
Assert::hasKey(2, $array);
36+
}, Tester\AssertException::class, '%a% should contain key %a%');
37+
38+
Assert::exception(function () use ($array) {
39+
Assert::hasKey('two', $array);
40+
}, Tester\AssertException::class, '%a% should contain key %a%');
41+
42+
43+
Assert::exception(function () use ($array) {
44+
Assert::hasNotKey(1, $array);
45+
}, Tester\AssertException::class, '%a% should not contain key %a%');
46+
47+
Assert::exception(function () use ($array) {
48+
Assert::hasNotKey('one', $array);
49+
}, Tester\AssertException::class, '%a% should not contain key %a%');
50+
51+
52+
Assert::exception(function () use ($array) {
53+
Assert::hasKey('two', $array, 'Custom description');
54+
}, Tester\AssertException::class, "Custom description: [1 => 1, 'one' => 'one'] should contain key 'two'");
55+
56+
Assert::exception(function () use ($array) {
57+
Assert::hasNotKey('one', $array, 'Custom description');
58+
}, Tester\AssertException::class, "Custom description: [1 => 1, 'one' => 'one'] should not contain key 'one'");

0 commit comments

Comments
 (0)