isAbstract()) { continue; } $instance = $ref->newInstance(); if (!$instance instanceof CodegenBaseTest) { continue; } echo sprintf("Start testing %s\n", $class); $methods = $ref->getMethods(); foreach ($methods as $method) { $name = $method->getName(); if (!Str::startsWith($name, 'test')) { continue; } $failure = self::runTest($instance, $method); if ($failure === null) { echo sprintf(" Test %s \e[0;32mpassed\e[0m\n", $name); } else { $fail_count++; echo sprintf(" Test %s \e[0;31mfailed\e[0m: %s\n", $name, $failure); } } } } if ($fail_count === 0) { echo "\e[0;32m** All the tests passed! **\e[0m\n"; return true; } else { echo sprintf("\e[0;31m** ERROR: %d test failures **\e[0m\n", $fail_count); return false; } } /** * Run a test method. * * @return a string containing the failure message, or null if the test * passed */ private static function runTest( CodegenBaseTest $instance, ReflectionMethod $method, ): ?string { $doc_block = $method->getDocComment(); $expected_exception = null; if (is_string($doc_block)) { foreach (explode("\n", $doc_block) as $line) { $find = strstr($line, '@expectedException'); if ($find) { $expected_exception = trim(substr($find, 19)); } } } try { $method->invoke($instance); if ($expected_exception) { return "Expected exception $expected_exception, nothing was thrown"; } } catch (Exception $ex) { if ($expected_exception === null) { return $ex->getMessage(); } else if (get_class($ex) !== $expected_exception) { return "Expected exception $expected_exception, ".get_class($ex). " was thrown instead"; } } return null; } }