PHP assertions library
-
Checking output of functions that return
false
ornull
on error// good (may silently fail depending on the error handler) $handle = fopen('/my/file', 'rb'); $data = fread($handle, 100); fclose($handle);
// better (verbose) $handle = fopen('/my/file', 'rb'); if ($handle === false) throw new Exception('fopen() failed'); $data = fread($handle, 100); if ($data === false) throw new Exception('fread() failed'); if (fclose($handle) === false) throw new Exception('fclose() failed');
// best (will fail regardless of the error handler) use IVT\Assert; Assert::resource($handle = fopen('/my/file', 'rb')); Assert::string($data = fread($handle, 100)); Assert::true(fclose($handle));
-
Tests
function humanize_bytes($n) { $i = (int)log(max(abs($n), 1), 1000); $p = 'KMGTPEZY'; if ($i == 0) return "$n B"; else return number_format($n / pow(1000, $i), 1) . " {$p[$i - 1]}B"; } function humanize_bytes_test() { Assert::equal(humanize_bytes(1), '1 B'); Assert::equal(humanize_bytes(-1402), '-1.4 KB'); // ... }
-
Checking invariants
Assert::int($object->getId()); // Object must have ID
-
etc