Ejemplo n.º 1
0
    function it_generates_class_method_from_resource($io, $tpl, $fs, ResourceInterface $resource)
    {
        $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');
        $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')));
    }
 /**
  * @param ResourceInterface $resource
  * @param string            $methodName
  * @param array             $arguments
  * @return string
  */
 private function getContent(ResourceInterface $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();
 }
 function it_asks_confirmation_if_spec_already_exists($io, $tpl, $fs, ResourceInterface $resource)
 {
     $resource->getSpecName()->willReturn('App');
     $resource->getSpecFilename()->willReturn('/project/spec/Acme/App.php');
     $resource->getSpecNamespace()->willReturn('spec\\Acme');
     $resource->getSrcClassname()->willReturn('Acme\\App');
     $fs->pathExists('/project/spec/Acme/App.php')->willReturn(true);
     $io->askConfirmation(Argument::type('string'), false)->willReturn(false);
     $fs->putFileContents(Argument::cetera())->shouldNotBeCalled();
     $this->generate($resource);
 }
 public function generate(ResourceInterface $resource, array $data = array())
 {
     $filepath = $resource->getSpecFilename();
     if ($this->filesystem->pathExists($filepath)) {
         $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath));
         if (!$this->io->askConfirmation($message, false)) {
             return;
         }
         $this->io->writeln();
     }
     $path = dirname($filepath);
     if (!$this->filesystem->isDirectory($path)) {
         $this->filesystem->makeDirectory($path);
     }
     $values = array('%filepath%' => $filepath, '%name%' => $resource->getSpecName(), '%namespace%' => $resource->getSpecNamespace(), '%subject%' => $resource->getSrcClassname());
     if (!($content = $this->templates->render('controller_specification', $values))) {
         $content = $this->templates->renderString(file_get_contents(__DIR__ . '/templates/controller_spec.template'), $values);
     }
     $this->filesystem->putFileContents($filepath, $content);
     $this->io->writeln(sprintf("<info>ControllerSpecification for <value>%s</value> created in <value>'%s'</value>.</info>\n", $resource->getSrcClassname(), $filepath));
 }
 function it_prepares_the_subject(ExampleNode $example, ObjectBehavior $context, MatcherManager $matchers, CollaboratorManager $collaborators, SpecificationNode $specification, ResourceInterface $resource, VarienWrapper $wrapper, Subject $subject, $factory)
 {
     $factory->create(Argument::cetera())->willReturn($wrapper);
     $wrapper->wrap(null)->willReturn($subject);
     $subject->beAnInstanceOf('\\stdObject');
     $subject = $subject->getWrappedObject();
     $resource->getSrcClassname()->willReturn('\\stdObject');
     $specification->getResource()->willReturn($resource);
     $example->getSpecification()->willReturn($specification);
     $context->setSpecificationSubject($subject)->shouldBeCalled();
     $this->prepare($example, $context, $matchers, $collaborators);
 }
 /**
  * @param ResourceInterface $resource
  * @param array $data
  */
 public function generate(ResourceInterface $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);
 }
Ejemplo n.º 7
0
 /**
  * @param ResourceInterface $resource
  * @param array             $data
  */
 public function generate(ResourceInterface $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 ResourceInterface $resource
     * @param string            $filepath
     *
     * @return string
     */
    protected function renderTemplate(ResourceInterface $resource, $filepath)
    {
        $controller = <<<CONTROLLER
    function let(\\Fuel\\Core\\Request \$request, \\Fuel\\Core\\Response \$response){
        \$this->beConstructedWith(\$request, \$response);
    }

CONTROLLER;
        $values = array('%filepath%' => $filepath, '%name%' => $resource->getSpecName(), '%namespace%' => $resource->getSpecNamespace(), '%subject%' => $resource->getSrcClassname(), "%constructor%" => preg_match("!^Controller_!i", $resource->getSpecName()) ? $controller : "");
        if (!($content = $this->getTemplateRenderer()->render('specification', $values))) {
            $content = $this->getTemplateRenderer()->renderString($this->getTemplate(), $values);
        }
        return $content;
    }
 /**
  * @param ResourceInterface $resource
  * @param array $data
  */
 public function generate(ResourceInterface $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("\n<info>Method <value>%s::%s()</value> has been modified.</info>", $resource->getSrcClassname(), $method), 2);
 }
 /**
  * @param ResourceInterface $resource
  * @param array $data
  *
  * @return mixed
  */
 public function generate(ResourceInterface $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);
 }
Ejemplo n.º 11
0
 public function generate(ResourceInterface $resource, array $data = array())
 {
     $filepath = $resource->getSrcFilename();
     if ($this->filesystem->pathExists($filepath)) {
         $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath));
         if (!$this->io->askConfirmation($message, false)) {
             return;
         }
         $this->io->writeln();
     }
     $path = dirname($filepath);
     if (!$this->filesystem->isDirectory($path)) {
         $this->filesystem->makeDirectory($path);
     }
     $values = array('%filepath%' => $filepath, '%name%' => $resource->getName(), '%extends%' => 'Mage_Core_Helper_Abstract', '%namespace%' => $resource->getSrcNamespace(), '%namespace_block%' => '' !== $resource->getSrcNamespace() ? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace()) : '');
     if (!($content = $this->templates->render('mage_helper', $values))) {
         $content = $this->templates->renderString(file_get_contents(__DIR__ . '/templates/generic_class.template'), $values);
     }
     $this->filesystem->putFileContents($filepath, $content);
     $this->io->writeln(sprintf("<info>Magento helper <value>%s</value> created in <value>'%s'</value>.</info>\n", $resource->getSrcClassname(), $filepath));
 }
Ejemplo n.º 12
0
 /**
  * @param ResourceInterface $resource
  * @param string            $filepath
  *
  * @return string
  */
 protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
 {
     return sprintf("<info>Specification for <value>%s</value> created in <value>%s</value>.</info>\n", $resource->getSrcClassname(), $filepath);
 }
Ejemplo n.º 13
0
 function it_records_that_class_was_created_in_executioncontext(ResourceInterface $resource, ExecutionContextInterface $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 ResourceInterface $resource
  * @param string            $filepath
  *
  * @return string
  */
 protected function renderTemplate(ResourceInterface $resource, $filepath)
 {
     $values = array('%filepath%' => $filepath, '%name%' => $resource->getSpecName(), '%namespace%' => $resource->getSpecNamespace(), '%subject%' => $resource->getSrcClassname(), '%command%' => $this->commandName ? $this->getCommandExample() : '', '%commandImport%' => $this->commandName ? "\nuse {$this->commandName};" : '');
     $content = $this->getTemplateRenderer()->renderString($this->getTemplate(), $values);
     return $content;
 }
Ejemplo n.º 15
0
 /**
  * @param ResourceInterface $resource
  * @param string            $filepath
  *
  * @return string
  */
 protected function getPromptMessage(ResourceInterface $resource, $filepath)
 {
     return sprintf("<info>Handler <value>%s</value> created in <value>%s</value>.</info>\n", $resource->getSrcClassname(), $filepath);
 }
Ejemplo n.º 16
0
 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);
 }