diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index eeaf9aa6..f9e63346 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -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 diff --git a/tests/Framework/Assert.hasKey.phpt b/tests/Framework/Assert.hasKey.phpt new file mode 100644 index 00000000..360b63cd --- /dev/null +++ b/tests/Framework/Assert.hasKey.phpt @@ -0,0 +1,58 @@ + 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'");