function it_does_not_generate_interface_when_prompt_is_answered_with_no(IO $io, ExampleEvent $exampleEvent, SuiteEvent $suiteEvent, GeneratorManager $generator) { $io->askConfirmation('Would you like me to generate an interface `Example\\ExampleClass` for you?')->willReturn(false); $this->afterExample($exampleEvent); $this->afterSuite($suiteEvent); $generator->generate(Argument::cetera())->shouldNotHaveBeenCalled(); $suiteEvent->markAsWorthRerunning()->shouldNotHaveBeenCalled(); }
public function beforeSuite() { if ($bootstrap = $this->io->getBootstrapPath()) { if (!is_file($bootstrap)) { throw new \RuntimeException(sprintf("Bootstrap file '%s' does not exist", $bootstrap)); } require $bootstrap; } }
/** * * @param ExampleEvent $event * * @throws \PhpSpec\Exception\Example\StopOnFailureException */ public function afterExample(ExampleEvent $event) { if (!$this->io->isStopOnFailureEnabled()) { return; } if ($event->getResult() === ExampleEvent::FAILED || $event->getResult() === ExampleEvent::BROKEN) { throw new StopOnFailureException('Example failed', 0, null, $event->getResult()); } }
function it_prompts_and_warns_when_one_method_name_is_correct_but_other_reserved($exampleEvent, SuiteEvent $suiteEvent, IO $io, NameCheckerInterface $nameChecker) { $this->callAfterExample($exampleEvent, $nameChecker, 'throw', false); $this->callAfterExample($exampleEvent, $nameChecker, 'foo'); $io->writeBrokenCodeBlock("I cannot generate the method 'throw' for you because it is a reserved keyword", 2)->shouldBeCalled(); $io->askConfirmation('Do you want me to create `stdClass::foo()` for you?')->shouldBeCalled(); $suiteEvent->markAsNotWorthRerunning()->shouldBeCalled(); $this->afterSuite($suiteEvent); }
function it_outputs_progress_as_33_when_3_of_3_examples_have_run_and_one_passed(ExampleEvent $event, IO $io, StatisticsCollector $stats) { $stats->getEventsCount()->willReturn(3); $stats->getCountsHash()->willReturn(array('passed' => 1, 'pending' => 0, 'skipped' => 0, 'failed' => 2, 'broken' => 0)); $stats->getTotalSpecs()->willReturn(3); $stats->getTotalSpecsCount()->willReturn(3); $this->afterExample($event); $expected = '/ skipped: 0% / pending: 0% / passed: 33% / failed: 66% / broken: 0% / 3 examples'; $io->writeTemp($expected)->shouldHaveBeenCalled(); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $methodName = $data['name']; $arguments = $data['arguments']; $content = $this->getContent($resource, $methodName, $arguments); $code = $this->appendMethodToCode($this->filesystem->getFileContents($filepath), $content); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $methodName), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $methodName = $data['name']; $arguments = $data['arguments']; $content = $this->getContent($resource, $methodName, $arguments); $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been created.</info>", $resource->getSrcClassname(), $methodName), 2); }
function it_should_provide_extra_output_in_verbose_mode(\PHP_CodeCoverage $coverage, \PHP_CodeCoverage_Report_HTML $html, SuiteEvent $event, IO $io) { $reports = array('html' => $html); $this->beConstructedWith($coverage, $reports); $this->setOptions(array('format' => 'html', 'output' => array('html' => 'coverage'))); $io->isVerbose()->willReturn(true); $this->setIO($io); $io->writeln('')->shouldBeCalled(); $io->writeln('Generating code coverage report in html format ...')->shouldBeCalled(); $this->afterSuite($event); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = $this->codeWriter->insertMethodFirstInClass($code, $content); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
public function displayFatal(CurrentExampleTracker $currentExample, $error) { if (null !== $error && $currentExample->getCurrentExample() || is_null($currentExample->getCurrentExample()) && defined('HHVM_VERSION')) { ini_set('display_errors', "stderr"); $failedOpen = $this->io->isDecorated() ? '<failed>' : ''; $failedClosed = $this->io->isDecorated() ? '</failed>' : ''; $failedCross = $this->io->isDecorated() ? '✘' : ''; $this->io->writeln("{$failedOpen}{$failedCross} Fatal error happened while executing the following {$failedClosed}"); $this->io->writeln("{$failedOpen} {$currentExample->getCurrentExample()} {$failedClosed}"); $this->io->writeln("{$failedOpen} {$error['message']} in {$error['file']} on line {$error['line']} {$failedClosed}"); } }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->buildArgumentString($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('interface-method-signature', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $this->insertMethodSignature($filepath, $content); $this->io->writeln(sprintf("<info>Method signature <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $destination = $this->getSavePath($resource); if (file_exists($destination) && !$this->io->askConfirmation(sprintf('File "%s" already exists. Overwrite?', basename($destination)), false)) { return; } $directory = dirname($destination); if (!file_exists($directory)) { $this->createDir($directory); } $code = $this->generateCodeForResource($resource, $data); $this->filesystem->putFileContents($destination, $code); $this->io->writeln($this->getPromptMessage($resource, $resource->getSrcFilename())); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = count($arguments) ? '$argument' . implode(', $argument', range(1, count($arguments))) : ''; $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $this->filesystem->putFileContents($filepath, $this->getUpdatedCode($name, $content, $code)); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param ResourceInterface $resource * @param array $data * * @return mixed */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->argumentBuilder->buildFrom($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been created.</info>", $resource->getSrcClassname(), $name), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $method = $data['method']; $expected = $data['expected']; $code = $this->filesystem->getFileContents($resource->getSrcFilename()); $values = array('%constant%' => var_export($expected, true)); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $pattern = '/' . '(function\\s+' . preg_quote($method, '/') . '\\s*\\([^\\)]*\\))\\s+{[^}]*?}/'; $replacement = '$1' . $content; $modifiedCode = preg_replace($pattern, $replacement, $code); $this->filesystem->putFileContents($resource->getSrcFilename(), $modifiedCode); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been modified.</info>", $resource->getSrcClassname(), $method), 2); }
/** * @param SuiteEvent $event */ public function afterSuite(SuiteEvent $event) { if (!$this->io->isCodeGenerationEnabled()) { return; } foreach ($this->exceptions as $exception) { $resource = $this->resources->createResource($exception->getCollaboratorName()); if ($this->resourceIsInSpecNamespace($exception, $resource)) { continue; } if ($this->io->askConfirmation(sprintf('Would you like me to generate an interface `%s` for you?', $exception->getCollaboratorName()))) { $this->generator->generate($resource, 'interface'); $event->markAsWorthRerunning(); } } }
public function afterSuite(SuiteEvent $event) { if (!$this->io->isCodeGenerationEnabled()) { return; } if (!$this->io->isFakingEnabled()) { return; } foreach ($this->nullMethods as $methodString => $failedCall) { $failedCall['expected'] = array_unique($failedCall['expected']); if (count($failedCall['expected']) > 1) { continue; } $expected = current($failedCall['expected']); $class = $failedCall['class']; $message = sprintf('Do you want me to make `%s()` always return %s for you?', $methodString, var_export($expected, true)); try { $resource = $this->resources->createResource($class); } catch (\RuntimeException $exception) { continue; } if ($this->io->askConfirmation($message)) { $this->generator->generate($resource, 'returnConstant', array('method' => $failedCall['method'], 'expected' => $expected)); $event->markAsWorthRerunning(); } } }
private function writeErrorMessage() { foreach ($this->wrongMethodNames as $methodName) { $message = sprintf("I cannot generate the method '%s' for you because it is a reserved keyword", $methodName); $this->io->writeBrokenCodeBlock($message, 2); } }
/** * @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(); }
/** * @param SuiteEvent $event */ public function afterSuite(SuiteEvent $event) { foreach ($this->interfaces as $interface => $methods) { try { $resource = $this->resources->createResource($interface); } catch (ResourceCreationException $e) { continue; } foreach ($methods as $method => $arguments) { if ($this->io->askConfirmation(sprintf(self::PROMPT, $interface, $method))) { $this->generator->generate($resource, 'method-signature', array('name' => $method, 'arguments' => $this->getRealArguments($arguments))); $event->markAsWorthRerunning(); } } } }
function it_outputs_a_suite_summary(SuiteEvent $event, IO $io, StatisticsCollector $stats) { $stats->getEventsCount()->willReturn(1); $stats->getFailedEvents()->willReturn(array()); $stats->getBrokenEvents()->willReturn(array()); $stats->getPendingEvents()->willReturn(array()); $stats->getSkippedEvents()->willReturn(array()); $stats->getTotalSpecs()->willReturn(15); $event->getTime()->willReturn(12.345); $stats->getCountsHash()->willReturn(array('passed' => 1, 'pending' => 0, 'skipped' => 0, 'failed' => 2, 'broken' => 0)); $this->afterSuite($event); $io->writeln('15 specs')->shouldHaveBeenCalled(); $io->writeln("\n12345ms")->shouldHaveBeenCalled(); $io->write('1 example ')->shouldHaveBeenCalled(); $expected = '(<passed>1 passed</passed>, <failed>2 failed</failed>)'; $io->write($expected)->shouldHaveBeenCalled(); }
function it_marks_the_suite_as_being_worth_rerunning_when_generation_happens(IO $io, ExampleEvent $event, SuiteEvent $suiteEvent, MethodNotFoundException $exception) { $io->askConfirmation(Argument::any())->willReturn(true); $exception->getClassname()->willReturn('spec\\PhpSpec\\Listener\\DoubleOfInterface'); $exception->getMethodName()->willReturn('aMethod'); $this->afterExample($event); $this->afterSuite($suiteEvent); $suiteEvent->markAsWorthRerunning()->shouldHaveBeenCalled(); }
function let(IO $io) { $io->isStopOnFailureEnabled()->willReturn(false); $this->beConstructedWith($io); }
/** * @param ResourceInterface $resource * @param string $filepath */ private function generateFileAndRenderTemplate(ResourceInterface $resource, $filepath) { $content = $this->renderTemplate($resource, $filepath); $this->filesystem->putFileContents($filepath, $content); $this->io->writeln($this->getGeneratedMessage($resource, $filepath)); }
public function reRunSuite() { if ($this->io->isRerunEnabled()) { $this->decoratedRerunner->reRunSuite(); } }
function it_invokes_method_body_generation_when_prompt_is_answered_yes(MethodCallEvent $methodCallEvent, ExampleEvent $exampleEvent, IO $io, GeneratorManager $generatorManager, ResourceManager $resourceManager, ResourceInterface $resource, SuiteEvent $event) { $io->askConfirmation(Argument::any())->willReturn(true); $resourceManager->createResource(Argument::any())->willReturn($resource); $methodCallEvent->getSubject()->willReturn(new \StdClass()); $methodCallEvent->getMethod()->willReturn('myMethod'); $this->afterMethodCall($methodCallEvent); $this->afterExample($exampleEvent); $this->afterSuite($event); $generatorManager->generate($resource, 'returnConstant', array('method' => 'myMethod', 'expected' => 100))->shouldHaveBeenCalled(); }
/** * @param IO $io * @param array $progress * @param int $total */ private function updateProgressBar(IO $io, array $progress, $total) { if ($io->isDecorated()) { $progressBar = implode('', $progress); $pad = $this->getIO()->getBlockWidth() - strlen(strip_tags($progressBar)); $io->writeTemp($progressBar . str_repeat(' ', $pad + 1) . $total); } else { $io->writeTemp('/' . implode('/', $progress) . '/ ' . $total . ' examples'); } }
/** * @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_outputs_undefined_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(999); $spec->getTitle()->willReturn('spec1'); $this->beforeSpecification($specEvent); $this->afterExample($exampleEvent); $expected = "not ok 1 - spec1: foobar\n ---\n message: 'The example result type was unknown to formatter'\n severity: fail\n ..."; $io->writeln($expected)->shouldHaveBeenCalled(); }