Example #1
0
 /**
  * @param string       $locator
  * @param integer|null $line
  *
  * @return Suite
  */
 public function load($locator, $line = null)
 {
     $suite = new Suite();
     foreach ($this->manager->locateResources($locator) as $resource) {
         if (!class_exists($resource->getSpecClassname()) && is_file($resource->getSpecFilename())) {
             require_once $resource->getSpecFilename();
         }
         if (!class_exists($resource->getSpecClassname())) {
             continue;
         }
         $reflection = new ReflectionClass($resource->getSpecClassname());
         if ($reflection->isAbstract()) {
             continue;
         }
         if (!$reflection->implementsInterface('PhpSpec\\SpecificationInterface')) {
             continue;
         }
         $spec = new Node\SpecificationNode($resource->getSrcClassname(), $reflection, $resource);
         foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
             if (!preg_match('/^(it|its)[^a-zA-Z]/', $method->getName())) {
                 continue;
             }
             if (null !== $line && !$this->lineIsInsideMethod($line, $method)) {
                 continue;
             }
             $example = new Node\ExampleNode(str_replace('_', ' ', $method->getName()), $method);
             if ($this->methodIsEmpty($method)) {
                 $example->markAsPending();
             }
             $spec->addExample($example);
         }
         $suite->addSpecification($spec);
     }
     return $suite;
 }
 function it_does_not_prompt_if_it_cannot_generate_the_resource(ConsoleIO $io, ResourceManager $resources, ExampleEvent $event, SuiteEvent $suiteEvent, MethodNotFoundException $exception)
 {
     $resources->createResource(Argument::any())->willThrow(new ResourceCreationException());
     $exception->getClassname()->willReturn('spec\\PhpSpec\\Listener\\DoubleOfInterface');
     $exception->getMethodName()->willReturn('aMethod');
     $this->afterExample($event);
     $this->afterSuite($suiteEvent);
     $io->askConfirmation(Argument::any())->shouldNotHaveBeenCalled();
 }
 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();
         }
     }
 }
 /**
  * @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();
         }
     }
 }
 /**
  * @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 (in_array($method, $this->wrongMethodNames)) {
                 continue;
             }
             if ($this->io->askConfirmation(sprintf(self::PROMPT, $interface, $method))) {
                 $this->generator->generate($resource, 'method-signature', array('name' => $method, 'arguments' => $this->getRealArguments($arguments)));
                 $event->markAsWorthRerunning();
             }
         }
     }
     if ($this->wrongMethodNames) {
         $this->writeErrorMessage();
         $event->markAsNotWorthRerunning();
     }
 }
 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();
 }