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_should_not_dispatch_an_event_if_the_file_already_existed($dispatcher, $filesystem, Resource $resource) { $path = '/foo'; $resource->getSrcFilename()->willReturn($path); $filesystem->pathExists($path)->willReturn(true); $this->generate($resource, array()); $dispatcher->dispatch('afterFileCreation', Argument::any())->shouldNotHaveBeenCalled(); }
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); }
/** * @param Resource $resource * @param array $data */ public function generate(Resource $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = $this->codeWriter->insertMethodFirstInClass($code, $content); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
function it_uses_the_resource_from_the_highest_priority_locator_when_duplicates_occur($locator1, $locator2, Resource $resource1, Resource $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)); }
/** * @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); }
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(); }
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 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); }
public function generate(Resource $resource, array $data = []) { $generateMethod = new GenerateMethod($resource->getSrcFilename(), $resource->getSrcClassName(), $data['name'], $data['arguments']); $this->commandBus->handle($generateMethod); }
/** * @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); }