protected function generateCodeForResource(ResourceInterface $resource, array $data) { /** @var Object $structure */ $structure = Object::make($resource->getSpecClassname()); $structure->extend(Object::make('PhpSpec\\ObjectBehavior')); $methodBody = <<<BODY \$this->shouldHaveType('{$resource->getSrcClassname()}'); BODY; $structure->addMethod(Method::make('it_is_initializable')->setBody($methodBody)); foreach ($data['params'] as $param) { $specExample = sprintf('it_should_retrieve_%s_getter_value', $param); $specMethodBody = ""; $specMethodBody .= " throw new PendingException('pending implementation');" . "\n"; $specMethodBody .= " \$expectation = 'put value here';" . "\n"; $specMethodBody .= " \$this->{$param}()->shouldBeLike(\$expectation);"; $specExampleMethod = Method::make($specExample)->setBody($specMethodBody); $structure->addMethod($specExampleMethod); } $file = File::make($resource->getSpecFilename()); $file->addFullyQualifiedName(new FullyQualifiedName('PhpSpec\\ObjectBehavior')); $file->addFullyQualifiedName(new FullyQualifiedName('Prophecy\\Argument')); $file->addFullyQualifiedName(new FullyQualifiedName('PhpSpec\\Exception\\Example\\PendingException')); $file->setStructure($structure); $prettyPrinter = Build::prettyPrinter(); return $prettyPrinter->generateCode($file); }
protected function generateCodeForResource(ResourceInterface $resource, array $data) { /** @var Object $structure */ $structure = Object::make($resource->getSpecClassname()); $handledClass = $data['handles']; $pieces = explode("\\", $handledClass); $handledClassShortName = end($pieces); $arguments = '$' . implode(', $', $data['params']); $structure->extend(Object::make('PhpSpec\\ObjectBehavior')); $methodBody = <<<BODY \$this->shouldHaveType('{$resource->getSrcClassname()}'); BODY; $structure->addMethod(Method::make('it_is_initializable')->setBody($methodBody)); $methodBody = <<<BODY throw new PendingException('Pending implementation'); \$command = new {$handledClassShortName}({$arguments}); \$this->handle(\$command); BODY; $structure->addMethod(Method::make('it_should_handle')->setBody($methodBody)); $file = File::make($resource->getSpecFilename()); $file->addFullyQualifiedName(new FullyQualifiedName('PhpSpec\\ObjectBehavior')); $file->addFullyQualifiedName(new FullyQualifiedName('Prophecy\\Argument')); $file->addFullyQualifiedName(new FullyQualifiedName('PhpSpec\\Exception\\Example\\PendingException')); $file->addFullyQualifiedName(new FullyQualifiedName($handledClass)); $file->setStructure($structure); $prettyPrinter = Build::prettyPrinter(); return $prettyPrinter->generateCode($file); }
function it_uses_the_resource_from_the_highest_priority_locator_when_duplicates_occur($locator1, $locator2, ResourceInterface $resource1, ResourceInterface $resource2) { $locator1->getPriority()->willReturn(2); $locator2->getPriority()->willReturn(1); $this->registerLocator($locator1); $this->registerLocator($locator2); $resource1->getSpecClassname()->willReturn('Some\\Spec'); $resource2->getSpecClassname()->willReturn('Some\\Spec'); $locator1->getAllResources()->willReturn(array($resource1)); $locator2->getAllResources()->willReturn(array($resource2)); $this->locateResources('')->shouldReturn(array($resource1)); }
private function importResource(Suite $suite, ResourceInterface $resource, $line = null) { if (!class_exists($resource->getSpecClassname()) && is_file($resource->getSpecFilename())) { require_once $resource->getSpecFilename(); } if (!class_exists($resource->getSpecClassname())) { return; } $reflection = new ReflectionClass($resource->getSpecClassname()); if ($reflection->isAbstract()) { return; } if (!$reflection->implementsInterface('PhpSpec\\SpecificationInterface')) { return; } $spec = new 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 ExampleNode(str_replace('_', ' ', $method->getName()), $method); if ($this->methodIsEmpty($method)) { $example->markAsPending(); } $spec->addExample($example); } $suite->addSpecification($spec); }