예제 #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;
 }
예제 #2
0
 function it_dispatches_SpecificationEvent_before_and_after_examples_run(EventDispatcherInterface $dispatcher, SpecificationNode $specification)
 {
     $specification->getExamples()->willReturn(array());
     $dispatcher->dispatch('beforeSpecification', Argument::type('PhpSpec\\Event\\SpecificationEvent'))->shouldBeCalled();
     $dispatcher->dispatch('afterSpecification', Argument::type('PhpSpec\\Event\\SpecificationEvent'))->shouldBeCalled();
     $this->run($specification);
 }
 function it_should_creates_result_event(ExampleEvent $exampleEvent, SpecificationNode $specificationNode, CodeCoverageSession $coverageSession)
 {
     $exampleEvent->getResult()->shouldBeCalled()->willReturn(ExampleEvent::PASSED);
     $specificationNode->getTitle()->shouldBeCalled()->willReturn('SomeSpesification');
     $coverageSession->stop()->shouldBeCalled();
     $this->afterExample($exampleEvent);
     $this->getResults()->shouldHaveCount(1);
 }
 function it_orders_an_array_of_specification_nodes(SpecificationNode $a, SpecificationNode $b, SpecificationNode $c)
 {
     $a->getTitle()->willReturn('foo1');
     $b->getTitle()->willReturn('foo2');
     $c->getTitle()->willReturn('foo3');
     $nodes = [$a, $b, $c];
     $expected = [$c, $b, $a];
     $this->filter($nodes)->shouldReturn($expected);
 }
 function it_filters_specification_nodes_to_include_only_given_specs(SpecificationNode $a, SpecificationNode $b, SpecificationNode $c)
 {
     $a->getTitle()->willReturn('foo1');
     $b->getTitle()->willReturn('foo2');
     $c->getTitle()->willReturn('foo3');
     $nodes = [$a, $b, $c];
     $expected = [$b];
     $this->setSpecs(['foo2']);
     $this->filter($nodes)->shouldReturn($expected);
 }
 /**
  * @param  SpecificationNode $specification
  * @return int|mixed
  */
 public function run(SpecificationNode $specification)
 {
     $startTime = microtime(true);
     $this->dispatcher->dispatch('beforeSpecification', new Event\SpecificationEvent($specification));
     $result = Event\ExampleEvent::PASSED;
     foreach ($specification->getExamples() as $example) {
         $result = max($result, $this->exampleRunner->run($example));
     }
     $this->dispatcher->dispatch('afterSpecification', new Event\SpecificationEvent($specification, microtime(true) - $startTime, $result));
     return $result;
 }
 function it_prepares_the_subject(ExampleNode $example, ObjectBehavior $context, MatcherManager $matchers, CollaboratorManager $collaborators, SpecificationNode $specification, ResourceInterface $resource, VarienWrapper $wrapper, Subject $subject, $factory)
 {
     $factory->create(Argument::cetera())->willReturn($wrapper);
     $wrapper->wrap(null)->willReturn($subject);
     $subject->beAnInstanceOf('\\stdObject');
     $subject = $subject->getWrappedObject();
     $resource->getSrcClassname()->willReturn('\\stdObject');
     $specification->getResource()->willReturn($resource);
     $example->getSpecification()->willReturn($specification);
     $context->setSpecificationSubject($subject)->shouldBeCalled();
     $this->prepare($example, $context, $matchers, $collaborators);
 }
예제 #8
0
 private function addResult($result, SpecificationNode $spec, $title = null)
 {
     $map = array(ResultEvent::SUCCEED => 'Succeed: %title%', ResultEvent::FAILED => 'Failed: %title%', ResultEvent::BROKEN => 'Broken: %title%', ResultEvent::ERROR => 'Error: %title%');
     $r = $spec->getClassReflection();
     $arguments = array('file' => $r->getFileName());
     $key = md5($r->getFileName() . $title);
     $format = $map[$result];
     $title = $title == null ? $spec->getTitle() : $spec->getTitle() . '::' . $title;
     $message = strtr($format, array('%title%' => '<highlight>' . $title . '</highlight>'));
     $this->results[$key] = ResultEvent::create($result, $message, $arguments);
 }
예제 #9
0
 /**
  * @return \PhpSpec\Loader\Suite
  */
 public function getSuite()
 {
     return $this->specification->getSuite();
 }
 private function specificationEvent(SpecificationNode $node)
 {
     return new SpecificationEvent($node->getWrappedObject());
 }
예제 #11
0
 private function addErrorThrowingExampleToSuite(Resource $resource, Suite $suite, \Error $error)
 {
     $reflection = new ReflectionClass(ErrorSpecification::class);
     $spec = new Node\SpecificationNode($resource->getSrcClassname(), $reflection, $resource);
     $errorFunction = new \ReflectionFunction(function () use($error) {
         throw $error;
     });
     $example = new Node\ExampleNode('Loading specification', $errorFunction);
     $spec->addExample($example);
     $suite->addSpecification($spec);
 }