/** * @covers PHPUnit_Framework_TestFailure::toString */ public function testToString() { $test = new self(__FUNCTION__); $exception = new PHPUnit_Framework_Exception('message'); $failure = new PHPUnit_Framework_TestFailure($test, $exception); $this->assertEquals(__METHOD__ . ': message', $failure->toString()); }
protected function printDefect(\PHPUnit_Framework_TestFailure $defect, $count) { $failedTest = $defect->failedTest(); if (!$failedTest instanceof \Codeception\TestCase) { return parent::printDefect($defect, $count); } $this->dispatcher->dispatch('fail.print', new \Codeception\Event\Fail($defect->failedTest(), $defect->thrownException())); }
protected function printDefectTrace(\PHPUnit_Framework_TestFailure $defect) { $e = $defect->thrownException(); if (!$e instanceof \atk4\core\PHPUnit_AgileExceptionWrapper) { return parent::printDefectTrace($defect); } $this->write((string) $e); $p = $e->getPrevious(); if ($p instanceof \atk4\core\Exception or $p instanceof \atk4\dsql\Exception) { $this->write($p->getColorfulText()); } }
/** * @param \PHPUnit_Framework_TestFailure $defect */ protected function printDefectTrace(\PHPUnit_Framework_TestFailure $defect) { $this->write($defect->getExceptionAsString()); $this->writeNewLine(); $stackTrace = \PHPUnit_Util_Filter::getFilteredStacktrace($defect->thrownException(), false); foreach ($stackTrace as $i => $frame) { if (!isset($frame['file'])) { continue; } $this->write(sprintf("#%d %s(%s)", $i + 1, $frame['file'], isset($frame['line']) ? $frame['line'] : '?')); $this->writeNewLine(); } }
protected function printDefectTrace(\PHPUnit_Framework_TestFailure $defect) { $this->write($this->formatExceptionMsg($defect->getExceptionAsString())); $trace = \PHPUnit_Util_Filter::getFilteredStacktrace($defect->thrownException()); if (!empty($trace)) { $this->write("\n" . $trace); } $exception = $defect->thrownException()->getPrevious(); while ($exception) { $this->write("\nCaused by\n" . \PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" . \PHPUnit_Util_Filter::getFilteredStacktrace($e)); $exception = $exception->getPrevious(); } }
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) { $class = get_class($test); $message = $this->escape(PHPUnit_Framework_TestFailure::exceptionToString($e)); $trace = $this->escape($e->getTraceAsString()); echo "##teamcity[testFailed type='failure' name='{$class}.{$test->getName()}' message='{$message}'" . " details='{$trace}']\n"; }
/** * @return string */ public function __toString() { $string = PHPUnit_Framework_TestFailure::exceptionToString($this); if ($trace = PHPUnit_Util_Filter::getFilteredStacktrace($this)) { $string .= "\n" . $trace; } return $string; }
private function _askForRetry(PHPUnit_Framework_Test $test, Exception $e, $time) { if ($e instanceof PHPUnit_Framework_IncompleteTest) { return false; } if ($e instanceof PHPUnit_Framework_SkippedTest) { return false; } if (file_exists("/www/testtimes")) { $app = Kwf_Registry::get('config')->application->id; file_put_contents("/www/testtimes/failure_{$app}/" . get_class($test), time()); } if (!$this->_retryOnError) { return false; } $error = new PHPUnit_Framework_TestFailure($test, $e); if ($test instanceof PHPUnit_Framework_SelfDescribing) { echo $test->toString(); } else { echo get_class($test); } echo "\n"; echo $error->getExceptionAsString() . PHPUnit_Util_Filter::getFilteredStacktrace($error->thrownException(), FALSE); echo "\nTest failed. Try again? [Y/n]"; if (isset($_SERVER['USER']) && $_SERVER['USER'] == 'niko') { $msg = Kwf_Registry::get('config')->application->name . ' Test failed. Try again?'; $msg = str_replace(" ", "\\ ", utf8_decode($msg)); system("ssh niko \"export DISPLAY=:0 && /usr/bin/kdialog --passivepopup {$msg} 2\""); } $stdin = fopen('php://stdin', 'r'); $input = strtolower(trim(fgets($stdin, 2))); fclose($stdin); if ($input == 'j' || $input == 'y' || $input == '') { $this->run($test); return true; } return false; }
/** * A failure occurred. * * @param PHPUnit_Framework_Test $test * @param PHPUnit_Framework_AssertionFailedError $e * @param float $time */ public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) { $this->writeNotOk($test, 'Failure'); $message = explode("\n", PHPUnit_Framework_TestFailure::exceptionToString($e)); $diagnostic = array('message' => $message[0], 'severity' => 'fail'); if ($e instanceof PHPUnit_Framework_ExpectationFailedException) { $cf = $e->getComparisonFailure(); if ($cf !== null) { $diagnostic['data'] = array('got' => $cf->getActual(), 'expected' => $cf->getExpected()); } } $yaml = new Symfony\Component\Yaml\Dumper(); $this->write(sprintf(" ---\n%s ...\n", $yaml->dump($diagnostic, 2, 2))); }
/** * @Then /^It should (fail|pass) with:$/ */ public function assertFailOrPassWith($success, PyStringNode $data) { if ('fail' === $success) { assertNotEquals(0, $this->return); } else { assertEquals(0, $this->return); } $realData = preg_replace('/\\# \\/.*BehatBundle\\//', '# ', (string) $this->output); try { assertEquals((string) $data, $realData); } catch (\Exception $e) { $diff = \PHPUnit_Framework_TestFailure::exceptionToString($e); throw new \Exception($diff, $e->getCode(), $e); } }
public function testInvalidMediaType() { $mediaType = 'application/pdf'; self::assertFalse($this->constraint->evaluate($mediaType, '', true)); try { $this->constraint->evaluate($mediaType); self::fail('Expected ExpectationFailedException to be thrown'); } catch (ExpectationFailedException $e) { self::assertEquals(<<<EOF Failed asserting that 'application/pdf' is an allowed media type (application/json, application/xml, text/xml, text/html). EOF , TestFailure::exceptionToString($e)); } }
/** * @return string */ public function __toString() { $string = \PHPUnit_Framework_TestFailure::exceptionToString($this); $e = $this->getWrappedException(); $string .= "\n Request: " . $this->requestToString($e) . "\n" . " HTTP status Code: " . $e->getHttpStatusCode() . "\n" . " Is transient: " . ($e->isTransient() ? 'true' : 'false') . "\n" . " Code: " . $e->getCode() . "\n" . " Error Subcode: " . $e->getErrorSubcode() . "\n" . " Error User Title: " . $e->getErrorUserTitle() . "\n" . " Error User Message: " . $e->getErrorUserMessage() . "\n"; if ($e->getErrorBlameFieldSpecs()) { $string .= " Error Blame Fields: " . $this->blameFieldSpecsToString() . "\n"; } if ($trace = \PHPUnit_Util_Filter::getFilteredStacktrace($this)) { $string .= "\n" . $trace; } if ($this->previous) { $string .= "\nCaused by\n" . $this->previous; } return $string; }
public function testInvalidHeaderType() { $headers = ['Content-Type' => 'application/json']; self::assertFalse($this->constraint->evaluate($headers, '', true)); try { $this->constraint->evaluate($headers); self::fail('Expected ExpectationFailedException to be thrown'); } catch (ExpectationFailedException $e) { self::assertEquals(<<<EOF Failed asserting that {"Content-Type":"application\\/json"} is valid. [etag] The property etag is required EOF , TestFailure::exceptionToString($e)); } }
/** * Print test * * @param array $test * @param null $name * @return string */ public function printTest(array $test, $name = NULL) { $testName = $name ? $name : $test['testName']; $roundPrecision = $test['time'] < 10 ? 2 : 0; $result = ''; $result .= '<div class="test ' . $this->getStatusName($test['status']) . '">'; $result .= '<div class="duration">' . round($test['time'], $roundPrecision) . 's</div>'; $result .= '<h2>' . $this->shorten($testName) . '</h2>'; $result .= '<div class="description">'; if (!empty($test['description'])) { $result .= '<p>' . nl2br($test['description']) . '</p>'; } $result .= '</div><!-- description -->'; if (isset($test['loggedTestSteps'])) { $result .= $this->renderTestSteps($test['loggedTestSteps']); } if (is_array($test['info'])) { $result .= '<ul class="info">'; foreach ($test['info'] as $info) { $result .= '<li>' . $info . '</li>'; } $result .= '</ul>'; } $result .= '<div class="content">'; if ($test['exception'] instanceof Exception) { $e = $test['exception']; /* @var $e Exception */ $result .= '<div class="exception">'; $result .= '<i>' . nl2br($this->escape(PHPUnit_Util_Filter::getFilteredStacktrace($e))) . '</i>' . "<br />\n"; $result .= '<pre>' . $this->escape(PHPUnit_Framework_TestFailure::exceptionToString($e)) . '</pre>'; $result .= '</div><!-- exception -->'; } if (isset($test['screenshots'])) { $result .= '<div class="screenshots">'; $result .= $this->printScreenshots($test['screenshots']); $result .= '</div><!-- screenshots -->'; } $result .= '</div><!-- content -->'; $result .= '</div><!-- test -->'; return $result; }
public function testInvalidSchema() { $response = <<<JSON [ { "id": 123456789 } ] JSON; $response = json_decode($response); self::assertFalse($this->constraint->evaluate($response, '', true)); try { $this->constraint->evaluate($response); self::fail('Expected ExpectationFailedException to be thrown'); } catch (ExpectationFailedException $e) { self::assertEquals(<<<EOF Failed asserting that [{"id":123456789}] is valid. [name] The property name is required EOF , TestFailure::exceptionToString($e)); } }
/** * Gets the thrown exception from a PHPUnit_Framework_TestFailure. * * @param PHPUnit_Framework_TestFailure $error * @since Method available since Release 3.6.0 * @see https://github.com/sebastianbergmann/phpunit/issues/74 */ protected function getException(PHPUnit_Framework_TestFailure $error) { $exception = $error->thrownException(); if ($exception instanceof __PHP_Incomplete_Class) { $exceptionArray = array(); foreach ((array) $exception as $key => $value) { $key = substr($key, strrpos($key, "") + 1); $exceptionArray[$key] = $value; } $exception = new PHPUnit_Framework_SyntheticError(sprintf('%s: %s', $exceptionArray['_PHP_Incomplete_Class_Name'], $exceptionArray['message']), $exceptionArray['code'], $exceptionArray['file'], $exceptionArray['line'], $exceptionArray['trace']); } return $exception; }
/** * @covers PHPUnit_Framework_Constraint_Exception * @covers PHPUnit_Framework_TestFailure::exceptionToString */ public function testConstraintException() { $constraint = new PHPUnit_Framework_Constraint_Exception('FoobarException'); $exception = new DummyException('Test'); $stackTrace = $exception->getTraceAsString(); try { $constraint->evaluate($exception); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals(<<<EOF Failed asserting that exception of type "DummyException" matches expected exception "FoobarException". Message was: "Test" at {$stackTrace}. EOF , PHPUnit_Framework_TestFailure::exceptionToString($e)); return; } $this->fail(); }
protected function printScenarioFail(ScenarioDriven $failedTest, $fail) { $failToString = \PHPUnit_Framework_TestFailure::exceptionToString($fail); $failedStep = ""; foreach ($failedTest->getScenario()->getSteps() as $step) { if ($step->hasFailed()) { $failedStep = (string) $step; break; } } $this->printException($fail, $failedStep); $this->printScenarioTrace($failedTest, $failToString); if ($this->output->getVerbosity() == OutputInterface::VERBOSITY_DEBUG) { $this->printExceptionTrace($fail); return; } if (!$fail instanceof \PHPUnit_Framework_AssertionFailedError) { $this->printExceptionTrace($fail); return; } }
/** * @covers PHPUnit_Framework_Constraint_SameSize * @covers PHPUnit_Framework_Constraint_Not * @covers PHPUnit_Framework_Assert::logicalNot * @covers PHPUnit_Framework_TestFailure::exceptionToString */ public function testConstraintNotSameSizeFailing() { $constraint = PHPUnit_Framework_Assert::logicalNot(new PHPUnit_Framework_Constraint_SameSize(array(1, 2))); try { $constraint->evaluate(array(3, 4)); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals(<<<EOF Failed asserting that actual size 2 does not match expected size 2. EOF , PHPUnit_Framework_TestFailure::exceptionToString($e)); return; } $this->fail(); }
/** * @param \PHPUnit_Framework_Test $test * @param \Exception $e * @param float $time * @param string $failureOrError */ protected function writeFailureOrError(\PHPUnit_Framework_Test $test, \Exception $e, $time, $failureOrError) { $testIsArtificial = false; if (!$this->testStarted) { $this->startTest($test); $testIsArtificial = true; } if ($test instanceof \PHPUnit_Framework_SelfDescribing) { $message = $test->toString() . "\n\n"; } else { $message = ''; } $message .= \PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n"; if ($test instanceof \PHPUnit_Framework_Warning) { $testClass = new \ReflectionClass($this->currentTestClassName); $file = $testClass->getFileName(); $line = 1; } elseif ($test instanceof \PHPUnit_Framework_WarningTestCase && preg_match('/^No tests found in class "([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)"\\.$/', $test->getMessage(), $matches)) { $testClass = new \ReflectionClass($this->currentTestClassName); $file = $testClass->getFileName(); $line = 1; } else { list($file, $line) = FailureTrace::findFileAndLineOfFailureOrError($this->testTargetRepository->getRequiredSuperTypes(), $e, new \ReflectionClass($test)); } $trace = \PHPUnit_Util_Filter::getFilteredStacktrace($e, true); $this->junitXMLWriter->{'write' . $failureOrError}($message . $trace, get_class($e), $file, $line, $message); if ($testIsArtificial) { $this->endTest($test, 0); } }
/** * @param PHPUnit_Framework_TestFailure $defect */ protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { $this->write($defect->getExceptionAsString() . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($defect->thrownException())); $e = $defect->thrownException()->getPrevious(); while ($e) { $this->write("\nCaused by\n" . PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e)); $e = $e->getPrevious(); } }
/** * @param PHPUnit_Framework_TestFailure $defect */ protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { $e = $defect->thrownException(); $this->write((string) $e); while ($e = $e->getPrevious()) { $this->write("\nCaused by\n" . $e); } }
/** * @param PHPUnit_Framework_TestFailure $defect */ protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { $this->write($defect->getExceptionAsString() . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($defect->thrownException(), FALSE)); }
/** * Checks whether last command output contains provided string. * * @Then the output should contain: * * @param Behat\Gherkin\Node\PyStringNode $text PyString text instance */ public function theOutputShouldContain(PyStringNode $text) { $text = strtr($text, array('\'\'\'' => '"""', '%PATH%' => realpath(getcwd()))); // windows path fix if ('/' !== DIRECTORY_SEPARATOR) { $text = preg_replace_callback('/ features\\/[^\\n ]+/', function ($matches) { return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); }, (string) $text); $text = preg_replace_callback('/\\<span class\\="path"\\>features\\/[^\\<]+/', function ($matches) { return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); }, (string) $text); $text = preg_replace_callback('/\\+[fd] [^ ]+/', function ($matches) { return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); }, (string) $text); } try { assertContains((string) $text, $this->output); } catch (Exception $e) { $diff = PHPUnit_Framework_TestFailure::exceptionToString($e); throw new Exception($diff, $e->getCode(), $e); } }
/** * Skipped test. * * @param PHPUnit_Framework_Test $test * @param Exception $e * @param float $time */ public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) { $message = PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE); $this->storeResult(PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED, $time, $message); $this->currentTestSuccess = FALSE; }
/** * Method which generalizes addError() and addFailure() * * @param PHPUnit_Framework_Test $test * @param Exception $e * @param float $time * @param string $type */ private function doAddFault(PHPUnit_Framework_Test $test, Exception $e, $time, $type) { if ($this->currentTestCase === null) { return; } if ($test instanceof PHPUnit_Framework_SelfDescribing) { $buffer = $test->toString() . "\n"; } else { $buffer = ''; } $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e); $fault = $this->document->createElement($type, PHPUnit_Util_XML::prepareString($buffer)); $fault->setAttribute('type', get_class($e)); $this->currentTestCase->appendChild($fault); }
protected function printScenarioFail(ScenarioDriven $failedTest, $fail) { $feature = $failedTest->getFeature(); $failToString = \PHPUnit_Framework_TestFailure::exceptionToString($fail); $failMessage = $this->message($failedTest->getSignature())->style('bold')->append(' (')->append(codecept_relative_path($failedTest->getFileName()))->append(')'); if ($fail instanceof \PHPUnit_Framework_SkippedTest || $fail instanceof \PHPUnit_Framework_IncompleteTest) { $this->printSkippedTest($feature, $failedTest->getFileName(), $failToString); return; } if ($feature) { $failMessage->prepend("Failed to {$feature} in "); } $failMessage->writeln(); $failedStep = ""; foreach ($failedTest->getScenario()->getSteps() as $step) { if ($step->hasFailed()) { $failedStep = (string) $step; break; } } $this->printException($fail, $failedStep); $this->printScenarioTrace($failedTest, $failToString); if ($this->output->getVerbosity() == OutputInterface::VERBOSITY_DEBUG) { $this->printExceptionTrace($fail); return; } if (!$fail instanceof \PHPUnit_Framework_AssertionFailedError) { $this->printExceptionTrace($fail); return; } }
/** * A failure occurred. * * @param PHPUnit_Framework_Test $test * @param PHPUnit_Framework_AssertionFailedError $e * @param float $time */ public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) { if ($this->currentTestCase !== null) { if (!$test instanceof PHPUnit_Framework_Warning) { if ($test instanceof PHPUnit_Framework_SelfDescribing) { $buffer = $test->toString() . "\n"; } else { $buffer = ''; } $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" . PHPUnit_Util_Filter::getFilteredStacktrace($e); $failure = $this->document->createElement('failure', PHPUnit_Util_XML::prepareString($buffer)); $failure->setAttribute('type', get_class($e)); $this->currentTestCase->appendChild($failure); $this->testSuiteFailures[$this->testSuiteLevel]++; } } }
/** * @throws PHPUnit_Framework_Exception * @throws PHPUnit_Framework_ExpectationFailedException */ public function verify() { if ($this->invocationMatcher === null) { throw new PHPUnit_Framework_Exception('No invocation matcher is set'); } if ($this->methodNameMatcher === null) { throw new PHPUnit_Framework_Exception('No method matcher is set'); } try { $this->invocationMatcher->verify(); if ($this->parametersMatcher === null) { $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters(); } $invocationIsAny = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount'; $invocationIsNever = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_InvokedCount' && $this->invocationMatcher->isNever(); if (!$invocationIsAny && !$invocationIsNever) { $this->parametersMatcher->verify(); } } catch (PHPUnit_Framework_ExpectationFailedException $e) { throw new PHPUnit_Framework_ExpectationFailedException(sprintf("Expectation failed for %s when %s.\n%s", $this->methodNameMatcher->toString(), $this->invocationMatcher->toString(), PHPUnit_Framework_TestFailure::exceptionToString($e))); } }
/** * @param PHPUnit_Framework_TestFailure $defect * @access protected */ protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { $e = $defect->thrownException(); if ($e instanceof PHPUnit_Framework_SelfDescribing) { $buffer = $e->toString(); if (!empty($buffer)) { $buffer .= "\n"; } if ($e instanceof PHPUnit_Framework_ExpectationFailedException) { $comparisonFailure = $e->getComparisonFailure(); if ($comparisonFailure !== NULL) { if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Scalar) { $buffer .= sprintf("Failed asserting that %s matches expected value %s.\n", PHPUnit_Util_Type::toString($comparisonFailure->getActual()), PHPUnit_Util_Type::toString($comparisonFailure->getExpected())); } else { if (PHPUnit_Framework_ComparisonFailure::hasDiff() && ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array || $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object || $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String)) { $buffer .= sprintf("Failed asserting that two %ss are equal.\n%s\n", strtolower(substr(get_class($comparisonFailure), 36)), $comparisonFailure->toString()); } } if ($this->verbose && !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array && !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object && !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) { $buffer .= $comparisonFailure->toString() . "\n"; } } else { $buffer .= $e->getDescription() . "\n"; } } } else { if ($e instanceof PHPUnit_Framework_Error) { $buffer = $e->getMessage() . "\n"; } else { $buffer = get_class($e) . ': ' . $e->getMessage() . "\n"; } } $this->write($buffer . PHPUnit_Util_Filter::getFilteredStacktrace($defect->thrownException(), FALSE)); }