|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Redaxo\YForm\Test; |
| 6 | + |
| 7 | +use Countable; |
| 8 | +use Redaxo\YForm\Test\Exception\AssertionFailedException; |
| 9 | +use Throwable; |
| 10 | + |
| 11 | +/** |
| 12 | + * In-house assertion library used by AbstractTestSuite. |
| 13 | + * |
| 14 | + * Keeps PHPUnit out of the runtime path so the test commands can run on any |
| 15 | + * REDAXO install without a dev dependency. |
| 16 | + * |
| 17 | + * @package redaxo\yform |
| 18 | + * @internal |
| 19 | + */ |
| 20 | +final class Assert |
| 21 | +{ |
| 22 | + public static function same(mixed $expected, mixed $actual, string $msg = ''): void |
| 23 | + { |
| 24 | + if ($expected !== $actual) { |
| 25 | + throw new AssertionFailedException( |
| 26 | + ($msg ?: 'Failed asserting two values are identical.') |
| 27 | + . "\n expected: " . self::dump($expected) |
| 28 | + . "\n actual: " . self::dump($actual), |
| 29 | + ); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static function notSame(mixed $unexpected, mixed $actual, string $msg = ''): void |
| 34 | + { |
| 35 | + if ($unexpected === $actual) { |
| 36 | + throw new AssertionFailedException( |
| 37 | + ($msg ?: 'Failed asserting two values are not identical.') |
| 38 | + . "\n value: " . self::dump($actual), |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static function equals(mixed $expected, mixed $actual, string $msg = ''): void |
| 44 | + { |
| 45 | + if ($expected != $actual) { |
| 46 | + throw new AssertionFailedException( |
| 47 | + ($msg ?: 'Failed asserting two values are equal.') |
| 48 | + . "\n expected: " . self::dump($expected) |
| 49 | + . "\n actual: " . self::dump($actual), |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static function true(bool $cond, string $msg = ''): void |
| 55 | + { |
| 56 | + if (!$cond) { |
| 57 | + throw new AssertionFailedException($msg ?: 'Failed asserting that condition is true.'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static function false(bool $cond, string $msg = ''): void |
| 62 | + { |
| 63 | + if ($cond) { |
| 64 | + throw new AssertionFailedException($msg ?: 'Failed asserting that condition is false.'); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static function null(mixed $value, string $msg = ''): void |
| 69 | + { |
| 70 | + if (null !== $value) { |
| 71 | + throw new AssertionFailedException( |
| 72 | + ($msg ?: 'Failed asserting null.') . "\n actual: " . self::dump($value), |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static function notNull(mixed $value, string $msg = ''): void |
| 78 | + { |
| 79 | + if (null === $value) { |
| 80 | + throw new AssertionFailedException($msg ?: 'Failed asserting not-null.'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static function count(int $expected, Countable|array $actual, string $msg = ''): void |
| 85 | + { |
| 86 | + $c = is_array($actual) ? count($actual) : $actual->count(); |
| 87 | + if ($c !== $expected) { |
| 88 | + throw new AssertionFailedException( |
| 89 | + ($msg ?: 'Failed asserting count.') . " expected={$expected} actual={$c}", |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param class-string $class |
| 96 | + */ |
| 97 | + public static function instanceOf(string $class, mixed $actual, string $msg = ''): void |
| 98 | + { |
| 99 | + if (!($actual instanceof $class)) { |
| 100 | + $got = is_object($actual) ? $actual::class : gettype($actual); |
| 101 | + throw new AssertionFailedException( |
| 102 | + ($msg ?: 'Failed asserting instance.') . " expected={$class} got={$got}", |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param class-string<Throwable> $exceptionClass |
| 109 | + */ |
| 110 | + public static function throws(string $exceptionClass, callable $fn, string $msg = ''): void |
| 111 | + { |
| 112 | + try { |
| 113 | + $fn(); |
| 114 | + } catch (Throwable $e) { |
| 115 | + if (!($e instanceof $exceptionClass)) { |
| 116 | + throw new AssertionFailedException( |
| 117 | + ($msg ?: 'Wrong exception type.') |
| 118 | + . " expected={$exceptionClass} got=" . $e::class |
| 119 | + . "\n message: " . $e->getMessage(), |
| 120 | + ); |
| 121 | + } |
| 122 | + return; |
| 123 | + } |
| 124 | + throw new AssertionFailedException( |
| 125 | + ($msg ?: 'No exception was thrown.') . " expected={$exceptionClass}", |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + public static function stringContains(string $needle, string $haystack, string $msg = ''): void |
| 130 | + { |
| 131 | + if (!str_contains($haystack, $needle)) { |
| 132 | + throw new AssertionFailedException( |
| 133 | + ($msg ?: 'Failed asserting string contains.') |
| 134 | + . "\n needle: " . self::dump($needle) |
| 135 | + . "\n haystack: " . self::dump($haystack), |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public static function arrayHasKey(int|string $key, array $array, string $msg = ''): void |
| 141 | + { |
| 142 | + if (!array_key_exists($key, $array)) { |
| 143 | + throw new AssertionFailedException( |
| 144 | + ($msg ?: 'Failed asserting array has key.') . " key=" . self::dump($key), |
| 145 | + ); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static function arrayNotHasKey(int|string $key, array $array, string $msg = ''): void |
| 150 | + { |
| 151 | + if (array_key_exists($key, $array)) { |
| 152 | + throw new AssertionFailedException( |
| 153 | + ($msg ?: 'Failed asserting array does not have key.') . " key=" . self::dump($key), |
| 154 | + ); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Truncated, single-line representation for error messages. |
| 160 | + */ |
| 161 | + private static function dump(mixed $value): string |
| 162 | + { |
| 163 | + if (is_string($value)) { |
| 164 | + $v = mb_strlen($value) > 80 ? mb_substr($value, 0, 77) . '...' : $value; |
| 165 | + return '"' . $v . '"'; |
| 166 | + } |
| 167 | + if (is_array($value)) { |
| 168 | + $json = (string) json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 169 | + $json = mb_strlen($json) > 120 ? mb_substr($json, 0, 117) . '...' : $json; |
| 170 | + return '[' . count($value) . '] ' . $json; |
| 171 | + } |
| 172 | + if (is_object($value)) { |
| 173 | + return $value::class . '#' . spl_object_id($value); |
| 174 | + } |
| 175 | + if (is_bool($value)) { |
| 176 | + return $value ? 'true' : 'false'; |
| 177 | + } |
| 178 | + if (null === $value) { |
| 179 | + return 'null'; |
| 180 | + } |
| 181 | + return var_export($value, true); |
| 182 | + } |
| 183 | +} |
0 commit comments