public function elapsedTimeGreaterThanZeroUsingStartAndStop() { $fixture = new Timer(); $fixture->start(); usleep(100 * 1000); $fixture->stop(); $elapsed = $fixture->elapsedTime(); $this->assertTrue($elapsed > 0.0, 'Elapsed time should be greater than zero'); }
/** * Run a test case. * * @param unittest.TestCase test * @param unittest.TestResult result * @throws lang.MethodNotImplementedException */ protected function runInternal($test, $result) { $class = $test->getClass(); $method = $class->getMethod($test->name); $this->notifyListeners('testStarted', array($test)); // Check for @ignore if ($method->hasAnnotation('ignore')) { $this->notifyListeners('testNotRun', array($result->set($test, new TestNotRun($test, $method->getAnnotation('ignore'))))); return; } // Check for @expect $expected = NULL; if ($method->hasAnnotation('expect', 'class')) { $message = $method->getAnnotation('expect', 'withMessage'); if ('/' === $message[0]) { $pattern = $message; } else { $pattern = '/' . preg_quote($message, '/') . '/'; } $expected = array(XPClass::forName($method->getAnnotation('expect', 'class')), $pattern); } else { if ($method->hasAnnotation('expect')) { $expected = array(XPClass::forName($method->getAnnotation('expect')), NULL); } } // Check for @limit $eta = 0; if ($method->hasAnnotation('limit')) { $eta = $method->getAnnotation('limit', 'time'); } // Check for @values if ($method->hasAnnotation('values')) { $annotation = $method->getAnnotation('values'); $variation = TRUE; $values = $this->valuesFor($test, $annotation); } else { $variation = FALSE; $values = array(array()); } // Check for @actions, initialize setUp and tearDown call chains $actions = array_merge($this->actionsFor($class, 'unittest.TestAction'), $this->actionsFor($method, 'unittest.TestAction')); $setUp = function ($test) use($actions) { foreach ($actions as $action) { $action->beforeTest($test); } $test->setUp(); }; $tearDown = function ($test) use($actions) { $test->tearDown(); foreach ($actions as $action) { $action->afterTest($test); } }; $timer = new Timer(); foreach ($values as $args) { $t = $variation ? new TestVariation($test, $args) : $test; xp::gc(); $timer->start(); // Setup test try { $setUp($test); } catch (PrerequisitesNotMetError $e) { $timer->stop(); $this->notifyListeners('testSkipped', array($result->setSkipped($t, $e, $timer->elapsedTime()))); xp::gc(); continue; } catch (AssertionFailedError $e) { $timer->stop(); $this->notifyListeners('testFailed', array($result->setFailed($t, $e, $timer->elapsedTime()))); xp::gc(); continue; } catch (Throwable $x) { $timer->stop(); $this->notifyListeners('testFailed', array($result->set($t, new TestError($t, $x, $timer->elapsedTime())))); xp::gc(); continue; } // Run test try { $method->invoke($test, is_array($args) ? $args : array($args)); } catch (TargetInvocationException $x) { $timer->stop(); $tearDown($test); $e = $x->getCause(); // Was that an expected exception? if ($expected && $expected[0]->isInstance($e)) { if ($eta && $timer->elapsedTime() > $eta) { $this->notifyListeners('testFailed', array($result->setFailed($t, new AssertionFailedError('Timeout', sprintf('%.3f', $timer->elapsedTime()), sprintf('%.3f', $eta)), $timer->elapsedTime()))); } else { if ($expected[1] && !preg_match($expected[1], $e->getMessage())) { $this->notifyListeners('testFailed', array($result->setFailed($t, new AssertionFailedError('Expected ' . $e->getClassName() . '\'s message differs', $e->getMessage(), $expected[1]), $timer->elapsedTime()))); } else { if (sizeof(xp::$errors) > 0) { $this->notifyListeners('testWarning', array($result->set($t, new TestWarning($t, $this->formatErrors(xp::$errors), $timer->elapsedTime())))); } else { $this->notifyListeners('testSucceeded', array($result->setSucceeded($t, $timer->elapsedTime()))); } } } } else { if ($expected && !$expected[0]->isInstance($e)) { $this->notifyListeners('testFailed', array($result->setFailed($t, new AssertionFailedError('Expected exception not caught', $e->getClassName(), $expected[0]->getName()), $timer->elapsedTime()))); } else { if ($e instanceof AssertionFailedError) { $this->notifyListeners('testFailed', array($result->setFailed($t, $e, $timer->elapsedTime()))); } else { if ($e instanceof PrerequisitesNotMetError) { $this->notifyListeners('testSkipped', array($result->setSkipped($t, $e, $timer->elapsedTime()))); } else { $this->notifyListeners('testError', array($result->set($t, new TestError($t, $e, $timer->elapsedTime())))); } } } } xp::gc(); continue; } $timer->stop(); $tearDown($test); // Check expected exception if ($expected) { $this->notifyListeners('testFailed', array($result->setFailed($t, new AssertionFailedError('Expected exception not caught', NULL, $expected[0]->getName()), $timer->elapsedTime()))); } else { if (sizeof(xp::$errors) > 0) { $this->notifyListeners('testWarning', array($result->set($t, new TestWarning($t, $this->formatErrors(xp::$errors), $timer->elapsedTime())))); } else { if ($eta && $timer->elapsedTime() > $eta) { $this->notifyListeners('testFailed', array($result->setFailed($t, new AssertionFailedError('Timeout', sprintf('%.3f', $timer->elapsedTime()), sprintf('%.3f', $eta)), $timer->elapsedTime()))); } else { $this->notifyListeners('testSucceeded', array($result->setSucceeded($t, $timer->elapsedTime()))); } } } xp::gc(); } }
/** * Run a test case. * * @param unittest.TestCase test * @param unittest.TestResult result * @throws lang.MethodNotImplementedException */ protected function runInternal($test, $result) { $method = $test->getClass()->getMethod($test->name); $this->notifyListeners('testStarted', array($test)); // Check for @ignore if ($method->hasAnnotation('ignore')) { $this->notifyListeners('testNotRun', array($result->set($test, new TestNotRun($test, $method->getAnnotation('ignore'))))); return; } // Check for @expect $expected = NULL; if ($method->hasAnnotation('expect', 'class')) { $message = $method->getAnnotation('expect', 'withMessage'); if ('/' === $message[0]) { $pattern = $message; } else { $pattern = '/' . preg_quote($message, '/') . '/'; } $expected = array(XPClass::forName($method->getAnnotation('expect', 'class')), $pattern); } else { if ($method->hasAnnotation('expect')) { $expected = array(XPClass::forName($method->getAnnotation('expect')), NULL); } } // Check for @limit $eta = 0; if ($method->hasAnnotation('limit')) { $eta = $method->getAnnotation('limit', 'time'); } xp::gc(); $timer = new Timer(); $timer->start(); // Setup test try { $test->setUp(); } catch (PrerequisitesNotMetError $e) { $timer->stop(); $this->notifyListeners('testSkipped', array($result->setSkipped($test, $e, $timer->elapsedTime()))); xp::gc(); return; } catch (AssertionFailedError $e) { $timer->stop(); $this->notifyListeners('testFailed', array($result->setFailed($test, $e, $timer->elapsedTime()))); xp::gc(); return; } catch (Throwable $t) { $timer->stop(); $this->notifyListeners('testFailed', array($result->set($test, new TestError($test, $t, $timer->elapsedTime())))); xp::gc(); return; } // Run test try { $method->invoke($test, NULL); } catch (TargetInvocationException $t) { $timer->stop(); try { $test->tearDown(); $e = $t->getCause(); } catch (AssertionFailedError $afe) { $timer->stop(); $this->notifyListeners('testFailed', array($result->setFailed($test, $afe, $timer->elapsedTime()))); xp::gc(); return; } catch (Throwable $t1) { $timer->stop(); $this->notifyListeners('testFailed', array($result->set($test, new TestError($test, $t1, $timer->elapsedTime())))); xp::gc(); return; } catch (TargetInvocationException $tie) { $cause = $tie->getCause(); $timer->stop(); $this->notifyListeners('testFailed', array($result->set($test, new TestError($test, $cause, $timer->elapsedTime())))); xp::gc(); return; } catch (Exception $ex) { $timer->stop(); $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError($ex->getMessage() . PHP_EOL . $ex->getTraceAsString()), $timer->elapsedTime()))); xp::gc(); return; } $e = $t->getCause(); // Was that an expected exception? if ($expected && $expected[0]->isInstance($e)) { if ($eta && $timer->elapsedTime() > $eta) { $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError('Timeout', sprintf('%.3f', $timer->elapsedTime()), sprintf('%.3f', $eta)), $timer->elapsedTime()))); } else { if ($expected[1] && !preg_match($expected[1], $e->getMessage())) { $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError('Expected ' . $e->getClassName() . '\'s message differs', $e->getMessage(), $expected[1]), $timer->elapsedTime()))); } else { if (sizeof(xp::$errors) > 0) { $this->notifyListeners('testWarning', array($result->set($test, new TestWarning($test, $this->formatErrors(xp::$errors), $timer->elapsedTime())))); } else { $this->notifyListeners('testSucceeded', array($result->setSucceeded($test, $timer->elapsedTime()))); } } } } else { if ($expected && !$expected[0]->isInstance($e)) { $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError('Expected exception not caught', $e->getClassName(), $expected[0]->getName()), $timer->elapsedTime()))); } else { if ($e instanceof AssertionFailedError) { $this->notifyListeners('testFailed', array($result->setFailed($test, $e, $timer->elapsedTime()))); } else { if ($e instanceof PrerequisitesNotMetError) { $this->notifyListeners('testSkipped', array($result->setSkipped($test, $e, $timer->elapsedTime()))); } else { $this->notifyListeners('testError', array($result->set($test, new TestError($test, $e, $timer->elapsedTime())))); } } } } xp::gc(); return; } $timer->stop(); try { $test->tearDown(); } catch (AssertionFailedError $e) { $timer->stop(); $this->notifyListeners('testFailed', array($result->setFailed($test, $e, $timer->elapsedTime()))); xp::gc(); return; } catch (Throwable $t) { $timer->stop(); $this->notifyListeners('testFailed', array($result->set($test, new TestError($test, $t, $timer->elapsedTime())))); xp::gc(); return; } catch (TargetInvocationException $e) { $cause = $e->getCause(); $timer->stop(); $this->notifyListeners('testFailed', array($result->set($test, new TestError($test, $cause, $timer->elapsedTime())))); xp::gc(); return; } catch (Exception $e) { $timer->stop(); $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError($e->getMessage() . PHP_EOL . $e->getTraceAsString()), $timer->elapsedTime()))); xp::gc(); return; } // Check expected exception if ($expected) { $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError('Expected exception not caught', NULL, $expected[0]->getName()), $timer->elapsedTime()))); } else { if (sizeof(xp::$errors) > 0) { $this->notifyListeners('testWarning', array($result->set($test, new TestWarning($test, $this->formatErrors(xp::$errors), $timer->elapsedTime())))); } else { if ($eta && $timer->elapsedTime() > $eta) { $this->notifyListeners('testFailed', array($result->setFailed($test, new AssertionFailedError('Timeout', sprintf('%.3f', $timer->elapsedTime()), sprintf('%.3f', $eta)), $timer->elapsedTime()))); } else { $this->notifyListeners('testSucceeded', array($result->setSucceeded($test, $timer->elapsedTime()))); } } } xp::gc(); }