|
| 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