function it_does_not_prompt_to_generate_when_there_was_an_exception_of_the_wrong_type(IO $io, ExampleEvent $exampleEvent, SuiteEvent $suiteEvent, \InvalidArgumentException $otherException) { $exampleEvent->getException()->willReturn($otherException); $this->afterExample($exampleEvent); $this->afterSuite($suiteEvent); $io->askConfirmation(Argument::any())->shouldNotHaveBeenCalled(); }
function it_does_not_prompt_when_wrong_exception_is_thrown(IO $io, ExampleEvent $event, SuiteEvent $suiteEvent) { $event->getException()->willReturn(new RuntimeException()); $this->afterExample($event); $this->afterSuite($suiteEvent); $io->askConfirmation(Argument::any())->shouldNotHaveBeenCalled(); }
public function afterExample(ExampleEvent $exampleEvent) { $exception = $exampleEvent->getException(); if (!$exception instanceof NotEqualException) { return; } if ($exception->getActual() !== null) { return; } if (is_object($exception->getExpected()) || is_array($exception->getExpected()) || is_resource($exception->getExpected())) { return; } if (!$this->lastMethodCallEvent) { return; } $class = get_class($this->lastMethodCallEvent->getSubject()); $method = $this->lastMethodCallEvent->getMethod(); if (!$this->methodAnalyser->methodIsEmpty($class, $method)) { return; } $key = $class . '::' . $method; if (!array_key_exists($key, $this->nullMethods)) { $this->nullMethods[$key] = array('class' => $this->methodAnalyser->getMethodOwnerName($class, $method), 'method' => $method, 'expected' => array()); } $this->nullMethods[$key]['expected'][] = $exception->getExpected(); }
/** * @param ExampleEvent $event */ protected function printException(ExampleEvent $event) { if (null === ($exception = $event->getException())) { return; } $title = str_replace('\\', DIRECTORY_SEPARATOR, $event->getSpecification()->getTitle()); $title = str_pad($title, 50, ' ', STR_PAD_RIGHT); $message = $this->getPresenter()->presentException($exception, $this->io->isVerbose()); if ($exception instanceof PendingException) { $this->io->writeln(sprintf('<pending-bg>%s</pending-bg>', $title)); $this->io->writeln(sprintf('<lineno>%4d</lineno> <pending>- %s</pending>', $event->getExample()->getFunctionReflection()->getStartLine(), $event->getExample()->getTitle())); $this->io->writeln(sprintf('<pending>%s</pending>', lcfirst($message)), 6); $this->io->writeln(); } elseif ($exception instanceof SkippingException) { if ($this->io->isVerbose()) { $this->io->writeln(sprintf('<skipped-bg>%s</skipped-bg>', $title)); $this->io->writeln(sprintf('<lineno>%4d</lineno> <skipped>? %s</skipped>', $event->getExample()->getFunctionReflection()->getStartLine(), $event->getExample()->getTitle())); $this->io->writeln(sprintf('<skipped>%s</skipped>', lcfirst($message)), 6); $this->io->writeln(); } } elseif (ExampleEvent::FAILED === $event->getResult()) { $this->io->writeln(sprintf('<failed-bg>%s</failed-bg>', $title)); $this->io->writeln(sprintf('<lineno>%4d</lineno> <failed>✘ %s</failed>', $event->getExample()->getFunctionReflection()->getStartLine(), $event->getExample()->getTitle())); $this->io->writeln(sprintf('<failed>%s</failed>', lcfirst($message)), 6); $this->io->writeln(); } else { $this->io->writeln(sprintf('<broken-bg>%s</broken-bg>', $title)); $this->io->writeln(sprintf('<lineno>%4d</lineno> <broken>! %s</broken>', $event->getExample()->getFunctionReflection()->getStartLine(), $event->getExample()->getTitle())); $this->io->writeln(sprintf('<broken>%s</broken>', lcfirst($message)), 6); $this->io->writeln(); } }
function it_identifies_a_resource_model_type(ExampleEvent $exampleEvent, ClassNotFoundException $exception, SuiteEvent $suiteEvent, $configGenerator) { $exampleEvent->getException()->willReturn($exception); $exception->getClassname()->willReturn('Vendor_Module_Model_Resource_Foo'); $this->getClassNameAfterExample($exampleEvent); $this->createXmlAfterSuite($suiteEvent); $configGenerator->generateElement('resource_model', 'Vendor_Module')->shouldHavebeenCalled(); }
function it_writes_a_fail_message_for_a_failing_example(Template $template, ExampleEvent $event, Presenter $presenter) { $event->getTitle()->willReturn(self::EVENT_TITLE); $event->getMessage()->willReturn(self::EVENT_MESSAGE); $event->getBacktrace()->willReturn(self::$BACKTRACE); $event->getException()->willReturn(new \Exception()); $template->render(Template::DIR . '/Template/ReportFailed.html', array('title' => self::EVENT_TITLE, 'message' => self::EVENT_MESSAGE, 'backtrace' => self::BACKTRACE, 'code' => self::CODE, 'index' => 1, 'specification' => 1))->shouldBeCalled(); $presenter->presentException(Argument::cetera())->willReturn(self::CODE); $this->write(1); }
/** * @param ExampleEvent $event */ public function afterExample(ExampleEvent $event) { if (null === ($exception = $event->getException())) { return; } if (!$exception instanceof PhpSpecClassException && !$exception instanceof ProphecyClassException) { return; } $this->classes[$exception->getClassname()] = true; }
public function afterExample(ExampleEvent $event) { if (null === ($exception = $event->getException())) { return; } if (!$exception instanceof MethodNotFoundException) { return; } $this->methods[get_class($exception->getSubject()) . '::' . $exception->getMethodName()] = $exception->getArguments(); }
/** * @param ExampleEvent $event * @param string $type */ protected function printSpecificException(ExampleEvent $event, $type) { $title = str_replace('\\', DIRECTORY_SEPARATOR, $event->getSpecification()->getTitle()); $message = $this->getPresenter()->presentException($event->getException(), $this->io->isVerbose()); foreach (explode("\n", wordwrap($title, $this->io->getBlockWidth(), "\n", true)) as $line) { $this->io->writeln(sprintf('<%s-bg>%s</%s-bg>', $type, str_pad($line, $this->io->getBlockWidth()), $type)); } $this->io->writeln(sprintf('<lineno>%4d</lineno> <%s>- %s</%s>', $event->getExample()->getFunctionReflection()->getStartLine(), $type, $event->getExample()->getTitle(), $type)); $this->io->writeln(sprintf('<%s>%s</%s>', $type, lcfirst($message), $type), 6); $this->io->writeln(); }
function it_stores_a_testcase_node_after_failed_example_run(ExampleEvent $event, SpecificationNode $specification, \ReflectionClass $refClass) { $event->getResult()->willReturn(ExampleEvent::FAILED); $event->getTitle()->willReturn('example title'); $event->getTime()->willReturn(1337); $event->getException()->willReturn(new ExceptionStub('Something went wrong', 'Exception trace')); $event->getSpecification()->willReturn($specification); $specification->getClassReflection()->willReturn($refClass); $refClass->getName()->willReturn('Acme\\Foo\\Bar'); $this->afterExample($event); $this->getTestCaseNodes()->shouldReturn(array('<testcase name="example title" time="1337" classname="Acme\\Foo\\Bar" status="failed">' . "\n" . '<failure type="spec\\PhpSpec\\Formatter\\ExceptionStub" message="Something went wrong" />' . "\n" . '<system-err>' . "\n" . '<![CDATA[' . "\n" . 'Exception trace' . "\n" . ']]>' . "\n" . '</system-err>' . "\n" . '</testcase>')); }
public function afterExample(ExampleEvent $event) { if (null === ($exception = $event->getException())) { return; } if (!$exception instanceof MethodNotFoundException) { return; } $classname = get_class($exception->getSubject()); $methodName = $exception->getMethodName(); $this->methods[$classname . '::' . $methodName] = $exception->getArguments(); $this->checkIfMethodNameAllowed($methodName); }
/** * @param ExampleEvent $event */ public function afterExample(ExampleEvent $event) { $io = $this->getIO(); $output = ob_get_clean(); if ($output) { $io->writeln($this->teamCityMessage("testStdOut", array("name" => $event->getTitle(), "out" => "Test Output\n>>>>>>>>>>>\n{$output}\n<<<<<<<<<<<\n"))); } switch ($event->getResult()) { case ExampleEvent::PASSED: break; case ExampleEvent::PENDING: $io->writeln($this->teamCityMessage('testIgnored', array('name' => $event->getTitle(), 'details' => $event->getMessage()))); break; case ExampleEvent::FAILED: $io->writeln($this->teamCityMessage('testFailed', array('name' => $event->getTitle(), 'message' => "Failed Test\n\n" . $event->getMessage(), 'details' => $event->getException()->getTraceAsString()))); break; case ExampleEvent::BROKEN: $io->writeln($this->teamCityMessage('testFailed', array('name' => $event->getTitle(), 'message' => "Broken Test\n\n" . $event->getMessage(), 'details' => $event->getException()->getTraceAsString()))); break; } $io->writeln($this->teamCityMessage('testFinished', array('name' => $event->getTitle()))); }
function it_stores_a_testcase_node_after_skipped_example_run(ExampleEvent $event, SpecificationNode $specification, \ReflectionClass $refClass) { $event->getResult()->willReturn(ExampleEvent::SKIPPED); $event->getTitle()->willReturn('example title'); $event->getTime()->willReturn(1337); $event->getException()->willReturn(new SkippingException('zog zog')); $event->getSpecification()->willReturn($specification); $specification->getClassReflection()->willReturn($refClass); $refClass->getName()->willReturn('Acme\\Foo\\Bar'); $this->afterExample($event); // skipped tag is escaped because a skipped tag is also registered in the console formatter $this->getTestCaseNodes()->shouldReturn(array('<testcase name="example title" time="1337.000000" classname="Acme\\Foo\\Bar" status="skipped">' . "\n" . '\\<skipped><![CDATA[ skipped: zog zog ]]>\\</skipped>' . "\n" . '</testcase>')); }
public function getClassNameAfterExample(ExampleEvent $event) { if (null === ($exception = $event->getException())) { return; } if (!$exception instanceof PhpSpecClassException && !$exception instanceof ProphecyClassException) { return; } $className = $exception->getClassname(); if (strlen($className)) { $parts = explode('_', $className); if (!isset($parts[0]) || !isset($parts[1])) { return; } $this->classNames[$className] = $parts[0] . '_' . $parts[1]; } }
function it_outputs_exceptions_for_failed_examples(SuiteEvent $event, ExampleEvent $pendingEvent, ConsoleIO $io, StatisticsCollector $stats, SpecificationNode $specification, ExampleNode $example) { $example->getLineNumber()->willReturn(37); $example->getTitle()->willReturn('it tests something'); $pendingEvent->getException()->willReturn(new PendingException()); $pendingEvent->getSpecification()->willReturn($specification); $pendingEvent->getExample()->willReturn($example); $stats->getEventsCount()->willReturn(1); $stats->getFailedEvents()->willReturn(array()); $stats->getBrokenEvents()->willReturn(array()); $stats->getPendingEvents()->willReturn(array($pendingEvent)); $stats->getSkippedEvents()->willReturn(array()); $stats->getTotalSpecs()->willReturn(1); $stats->getCountsHash()->willReturn(array('passed' => 0, 'pending' => 1, 'skipped' => 0, 'failed' => 0, 'broken' => 0)); $this->afterSuite($event); $expected = '<lineno> 37</lineno> <pending>- it tests something</pending>'; $io->writeln($expected)->shouldHaveBeenCalled(); }
/** * * @param int $index */ public function write($index) { $code = $this->presenter->presentException($this->event->getException(), true); $this->template->render(Template::DIR . '/Template/ReportFailed.html', array('title' => htmlentities(strip_tags($this->event->getTitle())), 'message' => htmlentities(strip_tags($this->event->getMessage())), 'backtrace' => $this->formatBacktrace(), 'code' => $code, 'index' => self::$failingExamplesCount++, 'specification' => $index)); }
function it_warns_if_a_method_name_is_wrong(ExampleEvent $event, SuiteEvent $suiteEvent, ConsoleIO $io, NameChecker $nameChecker) { $exception = new MethodNotFoundException('Error', new DoubleOfInterface(), 'throw'); $event->getException()->willReturn($exception); $nameChecker->isNameValid('throw')->willReturn(false); $io->writeBrokenCodeBlock("I cannot generate the method 'throw' for you because it is a reserved keyword", 2)->shouldBeCalled(); $io->askConfirmation(Argument::any())->shouldNotBeCalled(); $this->afterExample($event); $this->afterSuite($suiteEvent); }
/** * {@inheritdoc} */ public function afterExample(ExampleEvent $event) { $testCaseNode = sprintf('<testcase name="%s" time="%s" classname="%s" status="%s"', $event->getTitle(), $event->getTime(), $event->getSpecification()->getClassReflection()->getName(), $this->jUnitStatuses[$event->getResult()]); $this->exampleStatusCounts[$event->getResult()]++; if (in_array($event->getResult(), array(ExampleEvent::BROKEN, ExampleEvent::FAILED))) { $exception = $event->getException(); $testCaseNode .= sprintf('>' . "\n" . '<%s type="%s" message="%s" />' . "\n" . '<system-err>' . "\n" . '<![CDATA[' . "\n" . '%s' . "\n" . ']]>' . "\n" . '</system-err>' . "\n" . '</testcase>', $this->resultTags[$event->getResult()], get_class($exception), htmlspecialchars($exception->getMessage()), $exception->getTraceAsString()); } else { $testCaseNode .= ' />'; } $this->testCaseNodes[] = $testCaseNode; }
protected function printException(ExampleEvent $event, $depth = null) { $io = $this->getIO(); if (null === ($exception = $event->getException())) { return; } $depth = $depth ?: 8; $message = $this->getPresenter()->presentException($exception, $io->isVerbose()); if (ExampleEvent::FAILED === $event->getResult()) { $io->writeln(sprintf('<failed>%s</failed>', lcfirst($message)), $depth); } elseif (ExampleEvent::PENDING === $event->getResult()) { $io->writeln(sprintf('<pending>%s</pending>', lcfirst($message)), $depth); } elseif (ExampleEvent::SKIPPED === $event->getResult()) { $io->writeln(sprintf('<skipped>%s</skipped>', lcfirst($message)), $depth); } else { $io->writeln(sprintf('<broken>%s</broken>', lcfirst($message)), $depth); } }
/** * Format message as two-space indented YAML when needed outside of a * SKIP or projects directive. * * @param ExampleEvent $event * @param int $result * @return string */ private function getResultData(ExampleEvent $event, $result = null) { if (null === $result) { return $this->stripNewlines($event->getException()->getMessage()); } switch ($result) { case ExampleEvent::PENDING: $message = sprintf(self::TODO, $this->stripNewlines($event->getException()->getMessage())) . $this->indent(sprintf(self::SEVERITY, Yaml::dump('todo'))); break; case self::UNDEFINED_RESULT: $message = 'The example result type was unknown to formatter'; $message = $this->indent(sprintf(self::MESSAGE . self::SEVERITY, Yaml::dump($message), Yaml::dump('fail'))); break; default: $message = $this->indent(sprintf(self::MESSAGE . self::SEVERITY, Yaml::dump($event->getException()->getMessage()), Yaml::dump('fail'))); } return $message; }
function it_does_not_prompt_when_multiple_contradictory_examples_are_found(MethodCallEvent $methodCallEvent, ExampleEvent $exampleEvent, NotEqualException $notEqualException, IO $io, ExampleEvent $exampleEvent2, NotEqualException $notEqualException2, SuiteEvent $event) { $exampleEvent->getException()->willReturn($notEqualException); $exampleEvent2->getException()->willReturn($notEqualException2); $notEqualException->getActual()->willReturn(null); $notEqualException2->getActual()->willReturn(null); $notEqualException->getExpected()->willReturn('foo'); $notEqualException2->getExpected()->willReturn('bar'); $this->afterMethodCall($methodCallEvent); $this->afterExample($exampleEvent); $this->afterMethodCall($methodCallEvent); $this->afterExample($exampleEvent2); $this->afterSuite($event); $io->askConfirmation(Argument::any())->shouldNotHaveBeenCalled(); }
function it_outputs_broken_progress_on_afterexample_event(SpecificationEvent $specEvent, ExampleEvent $exampleEvent, ExampleNode $example, SpecificationNode $spec, IO $io, StatisticsCollector $stats) { $specEvent->getSpecification()->willReturn($spec); $exampleEvent->getExample()->willReturn($example); $example->getTitle()->willReturn('foobar'); $exampleEvent->getResult()->willReturn(ExampleEvent::BROKEN); $exampleEvent->getException()->willReturn(new \Exception("Something broke's.\nIt hurts.")); $spec->getTitle()->willReturn('spec1'); $this->beforeSpecification($specEvent); $this->afterExample($exampleEvent); $expected = "not ok 1 - spec1: foobar\n ---\n message: \"Something broke's.\\nIt hurts.\"\n severity: fail\n ..."; $io->writeln($expected)->shouldHaveBeenCalled(); }
/** * @param ExampleEvent $event */ public function afterExample(ExampleEvent $event) { if (($exception = $event->getException()) && $exception instanceof CollaboratorNotFoundException) { $this->exceptions[$exception->getCollaboratorName()] = $exception; } }
/** * @param ExampleEvent $event * @return bool|\Exception */ private function getMethodNotFoundException(ExampleEvent $event) { if ($this->io->isCodeGenerationEnabled() && ($exception = $event->getException()) && $exception instanceof MethodNotFoundException) { return $exception; } }