1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tester;
9:
10:
11: 12: 13: 14: 15:
16: class Environment
17: {
18:
19: const COLORS = 'NETTE_TESTER_COLORS';
20:
21:
22: const RUNNER = 'NETTE_TESTER_RUNNER';
23:
24:
25:
26: public static $debugMode = TRUE;
27:
28:
29: public static $useColors;
30:
31:
32: 33: 34: 35:
36: public static function setup()
37: {
38: self::$useColors = getenv(self::COLORS) !== FALSE
39: ? (bool) getenv(self::COLORS)
40: : (PHP_SAPI === 'cli' && ((function_exists('posix_isatty') && posix_isatty(STDOUT))
41: || getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== FALSE));
42:
43: class_exists('Tester\Runner\Job');
44: class_exists('Tester\Dumper');
45: class_exists('Tester\Assert');
46:
47: error_reporting(E_ALL | E_STRICT);
48: ini_set('display_errors', TRUE);
49: ini_set('html_errors', FALSE);
50: ini_set('log_errors', FALSE);
51:
52: set_exception_handler(array(__CLASS__, 'handleException'));
53: set_error_handler(function($severity, $message, $file, $line) {
54: if (in_array($severity, array(E_RECOVERABLE_ERROR, E_USER_ERROR)) || ($severity & error_reporting()) === $severity) {
55: Environment::handleException(new \ErrorException($message, 0, $severity, $file, $line));
56: }
57: return FALSE;
58: });
59:
60: register_shutdown_function(function() {
61: Assert::$onFailure = array(__CLASS__, 'handleException');
62:
63: $error = error_get_last();
64: if (in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE)) && ($error['type'] & error_reporting()) !== $error['type']) {
65: register_shutdown_function(function() use ($error) {
66: echo "\nFatal error: $error[message] in $error[file] on line $error[line]\n";
67: });
68: }
69: });
70:
71: ob_start(function($s) {
72: return Environment::$useColors ? $s : Dumper::removeColors($s);
73: }, PHP_VERSION_ID < 50400 ? 2 : 1);
74: }
75:
76:
77:
78: public static function handleException($e)
79: {
80: echo self::$debugMode ? Dumper::dumpException($e) : "\nError: {$e->getMessage()}\n";
81: exit($e instanceof AssertException ? Runner\Job::CODE_FAIL : Runner\Job::CODE_ERROR);
82: }
83:
84:
85: 86: 87: 88:
89: public static function skip($message = '')
90: {
91: echo "\nSkipped:\n$message\n";
92: die(Runner\Job::CODE_SKIP);
93: }
94:
95:
96: 97: 98: 99:
100: public static function lock($name = '', $path = '')
101: {
102: static $lock;
103: flock($lock = fopen($path . '/lock-' . md5($name), 'w'), LOCK_EX);
104: }
105:
106: }
107: