1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
<?php
namespace Peridot\Core;
use Error;
use ErrorException;
use Exception;
use Throwable;
class Test extends AbstractTest
{
public function __construct($description, callable $definition = null, $focused = false)
{
if ($definition === null) {
$this->pending = true;
$definition = function () {
};
}
parent::__construct($description, $definition, $focused);
}
public function isFocused()
{
return $this->focused;
}
public function run(TestResult $result)
{
$result->startTest($this);
if ($this->getPending()) {
$result->pendTest($this);
return;
}
$this->executeTest($result);
$result->endTest($this);
}
protected function executeTest(TestResult $result)
{
$action = ['passTest', $this];
$handler = $this->handleErrors($result, $action);
try {
$this->runSetup();
call_user_func_array($this->getDefinition(), $this->getDefinitionArguments());
} catch (Throwable $e) {
$this->failIfPassing($action, $e);
} catch (Exception $e) {
$this->failIfPassing($action, $e);
}
$this->runTearDown($result, $action);
$this->restoreErrorHandler($handler);
}
protected function runSetup()
{
$this->forEachNodeTopDown(function (TestInterface $node) {
$setups = $node->getSetupFunctions();
foreach ($setups as $setup) {
$setup();
}
});
}
protected function runTearDown(TestResult $result, array $action)
{
$this->forEachNodeBottomUp(function (TestInterface $test) use ($result, &$action) {
$tearDowns = $test->getTearDownFunctions();
foreach ($tearDowns as $tearDown) {
try {
$tearDown();
} catch (Throwable $e) {
$this->failIfPassing($action, $e);
} catch (Exception $e) {
$this->failIfPassing($action, $e);
}
}
});
call_user_func_array([$result, $action[0]], array_slice($action, 1));
}
protected function handleErrors(TestResult $result, array &$action)
{
$handler = null;
$handler = set_error_handler(function ($severity, $message, $path, $line) use ($result, &$action, &$handler) {
$arguments = func_get_args();
$isHandled = $handler && false !== call_user_func_array($handler, $arguments);
if (!$isHandled) {
$result->getEventEmitter()->emit('error', $arguments);
$errorReporting = error_reporting();
$shouldHandle = $severity === ($severity & $errorReporting);
if ($shouldHandle) {
$this->failIfPassing($action, new ErrorException($message, 0, $severity, $path, $line));
}
}
});
return $handler;
}
protected function restoreErrorHandler($handler)
{
if ($handler) {
set_error_handler($handler);
} else {
set_error_handler(function () { return false; });
}
}
protected function failIfPassing(array &$action, $error)
{
if ('passTest' === $action[0]) {
$action = ['failTest', $this, $error];
}
}
}