/** * @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; }
/** * @param Suite $suite * * @return integer */ public function run(Suite $suite) { $this->dispatcher->dispatch('beforeSuite', new SuiteEvent($suite)); $result = 0; $startTime = microtime(true); foreach ($suite->getSpecifications() as $specification) { try { $result = max($result, $this->specRunner->run($specification)); } catch (StopOnFailureException $e) { break; } } $endTime = microtime(true); $this->dispatcher->dispatch('afterSuite', new SuiteEvent($suite, $endTime - $startTime, $result)); return $result; }
function let(EventDispatcher $dispatcher, SpecificationRunner $specRunner, Suite $suite, SpecificationNode $spec1, SpecificationNode $spec2) { $this->beConstructedWith($dispatcher, $specRunner); $suite->getSpecifications()->willReturn(array($spec1, $spec2)); }
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); }