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')));
    }
Ejemplo n.º 2
0
 /**
  * @param string $code
  * @param string $method
  * @return string
  */
 private function appendMethodToCode($code, $method)
 {
     try {
         return $this->codeWriter->insertAfterMethod($code, '__construct', $method);
     } catch (NamedMethodNotFoundException $e) {
         return $this->codeWriter->insertMethodFirstInClass($code, $method);
     }
 }