function it_generates_class_method_from_resource($io, $tpl, $fs, Resource $resource, CodeWriter $codeWriter) { $codeWithoutMethod = <<<CODE <?php namespace Acme; class App { } CODE; $codeWithMethod = <<<CODE <?php namespace Acme; class App { METHOD } CODE; $values = array('%name%' => 'setName', '%arguments%' => '$argument1'); $resource->getSrcFilename()->willReturn('/project/src/Acme/App.php'); $resource->getSrcClassname()->willReturn('Acme\\App'); $tpl->render('method', $values)->willReturn(null); $tpl->renderString(Argument::type('string'), $values)->willReturn('METHOD'); $codeWriter->insertMethodLastInClass($codeWithoutMethod, 'METHOD')->willReturn($codeWithMethod); $fs->getFileContents('/project/src/Acme/App.php')->willReturn($codeWithoutMethod); $fs->putFileContents('/project/src/Acme/App.php', $codeWithMethod)->shouldBeCalled(); $this->generate($resource, array('name' => 'setName', 'arguments' => array('everzet'))); }
function it_generates_static_constructor_method_from_resource($io, $tpl, $fs, Resource $resource, CodeWriter $codeWriter) { $codeWithoutMethod = <<<CODE <?php namespace Acme; class App { } CODE; $codeWithMethod = <<<CODE <?php namespace Acme; class App { METHOD } CODE; $values = array('%methodName%' => 'setName', '%arguments%' => '$argument1', '%returnVar%' => '$app', '%className%' => 'App', '%constructorArguments%' => ''); $resource->getSrcFilename()->willReturn('/project/src/Acme/App.php'); $resource->getSrcClassname()->willReturn('Acme\\App'); $resource->getName()->willReturn('App'); $tpl->render('named_constructor_create_object', $values)->willReturn(null); $tpl->renderString(Argument::type('string'), $values)->willReturn('METHOD'); $codeWriter->insertAfterMethod($codeWithoutMethod, '__construct', 'METHOD')->willReturn($codeWithMethod); $fs->getFileContents('/project/src/Acme/App.php')->willReturn($codeWithoutMethod); $fs->putFileContents('/project/src/Acme/App.php', $codeWithMethod)->shouldBeCalled(); $this->generate($resource, array('name' => 'setName', 'arguments' => array('jmurphy'))); }
function it_calls_the_command_bus(CommandBus $commandBus, Resource $resource) { $resource->getSrcFilename()->willReturn(self::FILE_NAME); $resource->getSrcNamespace()->willReturn(self::NAME_SPACE); $resource->getSrcClassname()->willReturn(self::CLASS_NAME); $data = ['name' => self::METHOD_NAME, 'arguments' => []]; $command = Argument::type(GenerateConstructor::class); $commandBus->handle($command)->shouldbeCalled(); $this->generate($resource, $data); }
function it_asks_confirmation_if_spec_already_exists($io, $tpl, $fs, Resource $resource) { $resource->getSpecName()->willReturn('AppSpec'); $resource->getSpecFilename()->willReturn('/project/spec/Acme/AppSpec.php'); $resource->getSpecNamespace()->willReturn('spec\\Acme'); $resource->getSrcClassname()->willReturn('Acme\\App'); $fs->pathExists('/project/spec/Acme/AppSpec.php')->willReturn(true); $io->askConfirmation(Argument::type('string'), false)->willReturn(false); $fs->putFileContents(Argument::cetera())->shouldNotBeCalled(); $this->generate($resource); }
/** * @param Resource $resource * @param array $data */ public function generate(Resource $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->buildArgumentString($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('interface-method-signature', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $this->insertMethodSignature($filepath, $content); $this->io->writeln(sprintf("<info>Method signature <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param Resource $resource * @param array $data */ public function generate(Resource $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = count($arguments) ? '$argument' . implode(', $argument', range(1, count($arguments))) : ''; $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $this->filesystem->putFileContents($filepath, $this->getUpdatedCode($name, $content, $code)); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param Resource $resource * @param array $data */ public function generate(Resource $resource, array $data) { $method = $data['method']; $expected = $data['expected']; $code = $this->filesystem->getFileContents($resource->getSrcFilename()); $values = array('%constant%' => var_export($expected, true)); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $pattern = '/' . '(function\\s+' . preg_quote($method, '/') . '\\s*\\([^\\)]*\\))\\s+{[^}]*?}/'; $replacement = '$1' . $content; $modifiedCode = preg_replace($pattern, $replacement, $code); $this->filesystem->putFileContents($resource->getSrcFilename(), $modifiedCode); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been modified.</info>\n", $resource->getSrcClassname(), $method), 2); }
/** * @param Resource $resource * @param array $data * * @return mixed */ public function generate(Resource $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->argumentBuilder->buildFrom($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been created.</info>", $resource->getSrcClassname(), $name), 2); }
/** * @param Resource $resource * @param string $filepath * * @return string */ protected function getGeneratedMessage(Resource $resource, $filepath) { return sprintf("<info>Class <value>%s</value> created in <value>%s</value>.</info>\n", $resource->getSrcClassname(), $filepath); }
function it_records_that_class_was_created_in_executioncontext(Resource $resource, ExecutionContext $executionContext) { $resource->getName()->willReturn('App'); $resource->getSrcFilename()->willReturn('/project/src/Acme/App.php'); $resource->getSrcNamespace()->willReturn('Acme'); $resource->getSrcClassname()->willReturn('Acme\\App'); $this->generate($resource); $executionContext->addGeneratedType('Acme\\App')->shouldHaveBeenCalled(); }
/** * @param Resource $resource * @param string $methodName * @param array $arguments * @return string */ private function getContent(Resource $resource, $methodName, $arguments) { $className = $resource->getName(); $class = $resource->getSrcClassname(); $template = new CreateObjectTemplate($this->templates, $methodName, $arguments, $className); if (method_exists($class, '__construct')) { $template = new ExistingConstructorTemplate($this->templates, $methodName, $arguments, $className, $class); } return $template->getContent(); }
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); }