/** * @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(), false) && is_file($resource->getSpecFilename())) { require_once StreamWrapper::wrapPath($resource->getSpecFilename()); } if (!class_exists($resource->getSpecClassname(), false)) { 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->methodAnalyser->reflectionMethodIsEmpty($method)) { $example->markAsPending(); } $spec->addExample($example); } $suite->addSpecification($spec); } return $suite; }
/** * @test * @requires PHP 7.0 */ function it_loads_a_spec_with_no_typehints() { require StreamWrapper::wrapPath(__DIR__ . '/examples/ExampleSpec.php'); $reflection = new \ReflectionClass('integration\\PhpSpec\\Loader\\examples\\ExampleSpec'); $method = $reflection->getMethod('it_requires_a_stdclass'); $parameters = $method->getParameters(); $this->assertNull($parameters[0]->getClass()); }
/** * @param \ReflectionMethod $reflectionMethod * * @return string */ private function getCodeBody(\ReflectionMethod $reflectionMethod) { $endLine = $reflectionMethod->getEndLine(); $startLine = $reflectionMethod->getStartLine(); $reflectionClass = $this->getMethodOwner($reflectionMethod, $startLine, $endLine); $length = $endLine - $startLine; $lines = file(StreamWrapper::wrapPath($reflectionClass->getFileName())); $code = join(PHP_EOL, array_slice($lines, $startLine - 1, $length + 1)); return preg_replace('/.*function[^{]+{/s', '', $code); }