| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- namespace Michel\UniTester;
- use Exception;
- use Michel\UniTester\Exception\AssertionFailureException;
- /**
- * Formats a value for display in assertion messages.
- *
- * @param mixed $value
- * @return string
- */
- function _dump($value): string
- {
- if (is_null($value)) {
- return 'null';
- }
- if (is_bool($value)) {
- return $value ? 'true' : 'false';
- }
- if (is_string($value)) {
- return "'{$value}'";
- }
- if (is_array($value)) {
- // Use json_encode for compact array representation
- return json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- }
- if (is_object($value)) {
- return get_class($value) . ' ' . json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- }
- if (is_resource($value)) {
- return get_resource_type($value);
- }
- return (string)$value;
- }
- /**
- * Formats a value with its type for display in assertion messages.
- *
- * @param mixed $value
- * @return string
- */
- function _dump_with_type($value): string
- {
- $type = gettype($value);
- $dump = _dump($value);
- return "{$dump} ({$type})";
- }
- function assert_strict_equals($expected, $actual, string $message = ''): void
- {
- if ($expected !== $actual) {
- $expectedStr = _dump_with_type($expected);
- $actualStr = _dump_with_type($actual);
- throw new AssertionFailureException($message ?: "Expected strictly {$expectedStr}, got {$actualStr}");
- }
- }
- function assert_equals($expected, $actual, string $message = ''): void
- {
- if ($expected != $actual) {
- $expectedStr = _dump($expected);
- $actualStr = _dump($actual);
- throw new AssertionFailureException($message ?: "Expected {$expectedStr}, got {$actualStr}");
- }
- }
- function assert_not_strict_equals($expected, $actual, string $message = ''): void
- {
- if ($expected === $actual) {
- $expectedStr = _dump_with_type($expected);
- throw new AssertionFailureException($message ?: "Expected value to not be strictly equal to {$expectedStr}");
- }
- }
- function assert_not_equals($expected, $actual, string $message = ''): void
- {
- if ($expected == $actual) {
- $expectedStr = _dump($expected);
- throw new AssertionFailureException($message ?: "Expected value to not be equal to {$expectedStr}");
- }
- }
- function assert_similar($expected, $actual, string $message = ''): void
- {
- if ($expected != $actual) {
- $expectedStr = _dump($expected);
- $actualStr = _dump($actual);
- throw new AssertionFailureException($message ?: "Expected similar to {$expectedStr}, got {$actualStr}");
- }
- }
- function assert_true($condition, string $message = ''): void
- {
- if ($condition !== true) {
- $actualStr = _dump_with_type($condition);
- throw new AssertionFailureException($message ?: "Expected true, got {$actualStr}");
- }
- }
- function assert_false($condition, string $message = ''): void
- {
- if ($condition !== false) {
- $actualStr = _dump_with_type($condition);
- throw new AssertionFailureException($message ?: "Expected false, got {$actualStr}");
- }
- }
- function assert_null($value, string $message = ''): void
- {
- if (!is_null($value)) {
- $actualStr = _dump_with_type($value);
- throw new AssertionFailureException($message ?: "Expected null, got {$actualStr}");
- }
- }
- function assert_not_null($value, string $message = ''): void
- {
- if (is_null($value)) {
- throw new AssertionFailureException($message ?: "Expected not null, got null");
- }
- }
- function assert_empty($value, string $message = ''): void
- {
- if (!empty($value)) {
- $actualStr = _dump_with_type($value);
- throw new AssertionFailureException($message ?: "Expected empty, got {$actualStr}");
- }
- }
- function assert_not_empty($value, string $message = ''): void
- {
- if (empty($value)) {
- $actualStr = _dump_with_type($value);
- throw new AssertionFailureException($message ?: "Expected not empty, got {$actualStr}");
- }
- }
- function assert_instanceof(string $expected, $actual, string $message = ''): void
- {
- if (!is_object($actual)) {
- $actualStr = _dump_with_type($actual);
- throw new AssertionFailureException($message ?: "Expected instance of {$expected}, got {$actualStr}");
- }
- if (!is_a($actual, $expected)) {
- $actualClass = get_class($actual);
- throw new AssertionFailureException($message ?: "Expected instance of {$expected}, got {$actualClass}");
- }
- }
- function assert_string_length($string, int $length, string $message = ''): void
- {
- if (!is_string($string)) {
- $actualStr = _dump_with_type($string);
- throw new AssertionFailureException($message ?: "Expected string, got {$actualStr}");
- }
- if (strlen($string) !== $length) {
- $actualLength = strlen($string);
- throw new AssertionFailureException($message ?: "Expected string length of {$length}, got {$actualLength}");
- }
- }
- function assert_string_contains($haystack, $needle, string $message = ''): void
- {
- if (!is_string($haystack)) {
- $actualStr = _dump_with_type($haystack);
- throw new AssertionFailureException($message ?: "Expected haystack to be string, got {$actualStr}");
- }
- if (!is_string($needle)) {
- $needleStr = _dump_with_type($needle);
- throw new AssertionFailureException($message ?: "Expected needle to be string, got {$needleStr}");
- }
- if (strpos($haystack, $needle) === false) {
- throw new AssertionFailureException($message ?: "Expected '{$haystack}' to contain '{$needle}'");
- }
- }
- function assert_string_starts_with($haystack, $needle, string $message = ''): void
- {
- if (!is_string($haystack)) {
- $actualStr = _dump_with_type($haystack);
- throw new AssertionFailureException($message ?: "Expected haystack to be string, got {$actualStr}");
- }
- if (!is_string($needle)) {
- $needleStr = _dump_with_type($needle);
- throw new AssertionFailureException($message ?: "Expected needle to be string, got {$needleStr}");
- }
- if (strpos($haystack, $needle) !== 0) {
- throw new AssertionFailureException($message ?: "Expected '{$haystack}' to start with '{$needle}'");
- }
- }
- function assert_string_ends_with($haystack, $needle, string $message = ''): void
- {
- if (!is_string($haystack)) {
- $actualStr = _dump_with_type($haystack);
- throw new AssertionFailureException($message ?: "Expected haystack to be string, got {$actualStr}");
- }
- if (!is_string($needle)) {
- $needleStr = _dump_with_type($needle);
- throw new AssertionFailureException($message ?: "Expected needle to be string, got {$needleStr}");
- }
- if (substr($haystack, -strlen($needle)) !== $needle) {
- throw new AssertionFailureException($message ?: "Expected '{$haystack}' to end with '{$needle}'");
- }
- }
- function assert_positive_int($value, string $message = ''): void
- {
- if (!is_int($value) || $value <= 0) {
- $actualStr = _dump_with_type($value);
- throw new AssertionFailureException($message ?: "Expected positive integer, got {$actualStr}");
- }
- }
- function assert_negative_int($value, string $message = ''): void
- {
- if (!is_int($value) || $value >= 0) {
- $actualStr = _dump_with_type($value);
- throw new AssertionFailureException($message ?: "Expected negative integer, got {$actualStr}");
- }
- }
- function assert_count(int $expectedCount, $haystack, string $message = ''): void
- {
- if (!is_array($haystack) && !$haystack instanceof \Countable) {
- $actualStr = _dump_with_type($haystack);
- throw new AssertionFailureException($message ?: "Expected array or Countable, got {$actualStr}");
- }
- $actualCount = count($haystack);
- if ($actualCount !== $expectedCount) {
- throw new AssertionFailureException($message ?: "Expected count {$expectedCount}, got {$actualCount}");
- }
- }
- function assert_array_has_key($key, $array, string $message = ''): void
- {
- if (!is_array($array) && !($array instanceof \ArrayAccess)) {
- $actualStr = _dump_with_type($array);
- throw new AssertionFailureException($message ?: "Expected array or ArrayAccess, got {$actualStr}");
- }
- if (is_array($array)) {
- if (!array_key_exists($key, $array)) {
- $keyStr = _dump($key);
- throw new AssertionFailureException($message ?: "Expected array to have key {$keyStr}");
- }
- } else {
- if (!$array->offsetExists($key)) {
- $keyStr = _dump($key);
- throw new AssertionFailureException($message ?: "Expected ArrayAccess to have key {$keyStr}");
- }
- }
- }
- function fail(string $message = ''): void
- {
- throw new AssertionFailureException($message ?: "Test failed");
- }
- function assert_execution_time_less_than(float $maxMs, callable $callback, string $message = ''): void
- {
- $start = microtime(true);
- $callback();
- $end = microtime(true);
- $executionTimeMs = ($end - $start) * 1000;
- if ($executionTimeMs > $maxMs) {
- throw new AssertionFailureException($message ?: "Expected execution time to be less than {$maxMs}ms, but it took {$executionTimeMs}ms");
- }
- }
- function assert_memory_usage_less_than(int $maxBytes, callable $callback, string $message = ''): void
- {
- $startMemory = memory_get_usage();
- $callback();
- $endMemory = memory_get_usage();
- $memoryUsage = $endMemory - $startMemory;
- if ($memoryUsage > $maxBytes) {
- throw new AssertionFailureException($message ?: "Expected memory usage to be less than {$maxBytes} bytes, but it used {$memoryUsage} bytes");
- }
- }
- function assert_file_exists(string $path, string $message = ''): void
- {
- if (!file_exists($path)) {
- throw new AssertionFailureException($message ?: "Expected file '{$path}' to exist");
- }
- }
- function assert_file_not_exists(string $path, string $message = ''): void
- {
- if (file_exists($path)) {
- throw new AssertionFailureException($message ?: "Expected file '{$path}' to not exist");
- }
- }
- function assert_directory_exists(string $path, string $message = ''): void
- {
- if (!is_dir($path)) {
- throw new AssertionFailureException($message ?: "Expected directory '{$path}' to exist");
- }
- }
- function assert_is_readable(string $path, string $message = ''): void
- {
- if (!is_readable($path)) {
- throw new AssertionFailureException($message ?: "Expected path '{$path}' to be readable");
- }
- }
- function assert_is_writable(string $path, string $message = ''): void
- {
- if (!is_writable($path)) {
- throw new AssertionFailureException($message ?: "Expected path '{$path}' to be writable");
- }
- }
|