public function onRun(RunEvent $e) { /* @var $test \ZFTool\Diagnostics\Test\TestInterface */ $test = $e->getTarget(); try { ErrorHandler::start($this->getCatchErrorSeverity()); $result = $test->run(); ErrorHandler::stop(true); } catch (ErrorException $e) { return new Failure('PHP ' . static::getSeverityDescription($e->getSeverity()) . ': ' . $e->getMessage(), $e); } catch (\Exception $e) { ErrorHandler::stop(false); return new Failure('Uncaught ' . get_class($e) . ': ' . $e->getMessage(), $e); } // Check if we've received a Result object if (is_object($result)) { if (!$result instanceof ResultInterface) { return new Failure('Test returned unknown object ' . get_class($result), $result); } return $result; // Interpret boolean as a failure or success } elseif (is_bool($result)) { if ($result) { return new Success(); } else { return new Failure(); } // Convert scalars to a warning } elseif (is_scalar($result)) { return new Warning('Test returned unexpected ' . gettype($result), $result); // Otherwise interpret as failure } else { return new Failure('Test returned unknown result of type ' . gettype($result), $result); } }
/** * @throws Exception\RuntimeException * @return Collection The result of tests */ public function run() { $breakOnFailure = $this->getConfig()->getBreakOnFailure(); $em = $this->getEventManager(); $testRun = new RunEvent(); $results = new Collection(); $testRun->setResults($results); $testRun->setParam('tests', $this->tests); // trigger START event $em->trigger(RunEvent::EVENT_START, $testRun); // Iterate over all tests foreach ($this->tests as $test) { $testRun->setTarget($test); $testRun->clearLastResult(); // Skip testing if BEFORE_RUN returned false or has been stopped $beforeRun = $em->trigger(RunEvent::EVENT_BEFORE_RUN, $testRun); if ($beforeRun->stopped() || $beforeRun->contains(false)) { continue; } // Run the test! $result = $em->trigger(RunEvent::EVENT_RUN, $testRun, function ($r) { if ($r instanceof ResultInterface) { return true; } else { return false; } })->last(); // Interpret result if (!is_object($result) || !$result instanceof ResultInterface) { $what = is_object($result) ? 'object of class ' . get_class($result) : gettype($result); throw new RuntimeException('Test run resulted in ' . $what . ' Expected instance of ' . __NAMESPACE__ . '\\Result\\ResultInterface'); } // Save test result $results[$test] = $result; $testRun->setLastResult($result); // Stop testing if AFTER_RUN returned false or has been stopped $afterRun = $em->trigger(RunEvent::EVENT_AFTER_RUN, $testRun); if ($afterRun->stopped() || $afterRun->contains(false)) { $em->trigger(RunEvent::EVENT_STOP, $testRun); break; } // Stop testing on first failure if ($breakOnFailure && $result instanceof Failure) { $em->trigger(RunEvent::EVENT_STOP, $testRun); break; } } // trigger FINISH event $em->trigger(RunEvent::EVENT_FINISH, $testRun); return $results; }
public function onFinish(RunEvent $e) { /* @var $results \ZFTool\Diagnostics\Result\Collection */ $results = $e->getResults(); // Display information that the test has been aborted. if ($this->stopped) { $this->console->writeLine('Diagnostics aborted because of a failure.', Color::RED); } // Display a summary line if ($results->getFailureCount() == 0 && $results->getWarningCount() == 0 && $results->getUnknownCount() == 0) { $this->console->write(' OK ', Color::NORMAL, Color::GREEN); $this->console->write(' '); $this->console->writeLine(str_pad(' (' . $this->total . ' diagnostic tests)', $this->width - 8, ' ', STR_PAD_RIGHT), Color::NORMAL, Color::GREEN); } elseif ($results->getFailureCount() == 0) { $line = ' (' . $results->getWarningCount() . ' warnings, '; $line .= $results->getSuccessCount() . ' successful tests'; if ($results->getUnknownCount() > 0) { $line .= ', ' . $results->getUnknownCount() . ' unknown test results'; } $line .= ')'; $this->console->write(' WARN ', Color::NORMAL, Color::YELLOW); $this->console->write(' '); $this->console->writeLine(str_pad($line, $this->width - 8, ' ', STR_PAD_RIGHT), Color::NORMAL, Color::YELLOW); } else { $line = ' (' . $results->getFailureCount() . ' failures, '; $line .= $results->getWarningCount() . ' warnings, '; $line .= $results->getSuccessCount() . ' successful tests'; if ($results->getUnknownCount() > 0) { $line .= ', ' . $results->getUnknownCount() . ' unknown test results'; } $line .= ')'; $this->console->write(' FAIL ', Color::NORMAL, Color::RED); $this->console->write(' '); $this->console->writeLine(str_pad($line, $this->width - 8, ' ', STR_PAD_RIGHT), Color::NORMAL, Color::RED); } $this->console->writeLine(); }
public function testRunEventWrongTarget() { $e = new RunEvent(); $this->setExpectedException('ZFTool\\Diagnostics\\Exception\\InvalidArgumentException'); $e->setTarget(new \stdClass()); }
public function testStoppedNotice() { $e = new RunEvent(); $tests = array(); $test = null; $results = new Collection(); for ($x = 0; $x < 15; $x++) { $tests[] = $test = new AlwaysSuccessTest(); $results[$test] = new Success(); } $e->setParam('tests', $tests); $e->setResults($results); ob_start(); $this->em->trigger(RunEvent::EVENT_START, $e); ob_clean(); $this->em->trigger(RunEvent::EVENT_STOP, $e); $this->em->trigger(RunEvent::EVENT_FINISH, $e); $this->assertStringMatchesFormat('%ADiagnostics aborted%A', trim(ob_get_clean())); }
public function onFinish(RunEvent $e) { /* @var $results \ZFTool\Diagnostics\Result\Collection */ $results = $e->getResults(); $this->console->writeLine(); $this->console->writeLine(); // Display a summary line if ($results->getFailureCount() == 0 && $results->getWarningCount() == 0 && $results->getUnknownCount() == 0) { $this->console->write(' OK ', Color::NORMAL, Color::GREEN); $this->console->write(' '); $this->console->writeLine(str_pad(' (' . $this->total . ' diagnostic tests)', $this->width - 8, ' ', STR_PAD_RIGHT), Color::NORMAL, Color::GREEN); } elseif ($results->getFailureCount() == 0) { $line = ' (' . $results->getWarningCount() . ' warnings, '; $line .= $results->getSuccessCount() . ' successful tests'; if ($results->getUnknownCount() > 0) { $line .= ', ' . $results->getUnknownCount() . ' unknown test results'; } $line .= ')'; $this->console->write(' WARN ', Color::NORMAL, Color::YELLOW); $this->console->write(' '); $this->console->writeLine(str_pad($line, $this->width - 8, ' ', STR_PAD_RIGHT), Color::NORMAL, Color::YELLOW); } else { $line = ' (' . $results->getFailureCount() . ' failures, '; $line .= $results->getWarningCount() . ' warnings, '; $line .= $results->getSuccessCount() . ' successful tests'; if ($results->getUnknownCount() > 0) { $line .= ', ' . $results->getUnknownCount() . ' unknown test results'; } $line .= ')'; $this->console->write(' FAIL ', Color::NORMAL, Color::RED); $this->console->write(' '); $this->console->writeLine(str_pad($line, $this->width - 8, ' ', STR_PAD_RIGHT), Color::NORMAL, Color::RED); } $this->console->writeLine(); // Display a list of failures and warnings foreach ($results as $test) { /* @var $test \ZFTool\Diagnostics\Test\TestInterface */ /* @var $result \ZFTool\Diagnostics\Result\ResultInterface */ $result = $results[$test]; if ($result instanceof Failure) { $this->console->writeLine(' Failure: ' . $test->getLabel(), Color::RED); $message = $result->getMessage(); if ($message) { $this->console->writeLine(' => ' . $message, Color::RED); } $this->console->writeLine(); } elseif ($result instanceof Warning) { $this->console->writeLine(' Warning: ' . $test->getLabel(), Color::YELLOW); $message = $result->getMessage(); if ($message) { $this->console->writeLine(' => ' . $message, Color::YELLOW); } $this->console->writeLine(); } elseif (!$result instanceof Success) { $this->console->writeLine(' Unknown result ' . get_class($result) . ': ' . $test->getLabel(), Color::YELLOW); $message = $result->getMessage(); if ($message) { $this->console->writeLine(' => ' . $message, Color::YELLOW); } $this->console->writeLine(); } } // Display information that the test has been aborted. if ($this->stopped) { $this->console->write(' STOP ', Color::NORMAL, Color::RED); $this->console->write(' '); $this->console->writeLine('Diagnostics aborted because of a failure.', Color::RED); $this->console->writeLine(); } }
public function testCatchErrors() { \PHPUnit_Framework_Error_Warning::$enabled = false; \PHPUnit_Framework_Error_Notice::$enabled = false; $e = new RunEvent(); $this->listener->setCatchErrorSeverity(E_WARNING); $test = new TriggerWarningTest(); $e->setTarget($test); $result = $this->em->trigger(RunEvent::EVENT_RUN, $e)->last(); $this->assertInstanceOf('ZFTool\\Diagnostics\\Result\\Failure', $result); $this->assertInstanceOf('ErrorException', $result->getData()); $e = new RunEvent(); $this->listener->setCatchErrorSeverity(E_WARNING | E_USER_ERROR); $test = new TriggerUserErrorTest('bar', E_USER_ERROR); $e->setTarget($test); $result = $this->em->trigger(RunEvent::EVENT_RUN, $e)->last(); $this->assertInstanceOf('ZFTool\\Diagnostics\\Result\\Failure', $result); $this->assertStringStartsWith('PHP USER_ERROR: bar', $result->getMessage()); $this->assertInstanceOf('ErrorException', $result->getData()); }