/**
     * Apply Prophecy functionality to class node.
     *
     * @param ClassNode $node
     */
    public function apply(ClassNode $node)
    {
        $node->addInterface('Prophecy\\Prophecy\\ProphecySubjectInterface');
        $node->addProperty('objectProphecy', 'private');
        foreach ($node->getMethods() as $name => $method) {
            if ('__construct' === strtolower($name)) {
                continue;
            }
            $method->setCode('return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());');
        }
        $prophecySetter = new MethodNode('setProphecy');
        $prophecyArgument = new ArgumentNode('prophecy');
        $prophecyArgument->setTypeHint('Prophecy\\Prophecy\\ProphecyInterface');
        $prophecySetter->addArgument($prophecyArgument);
        $prophecySetter->setCode('$this->objectProphecy = $prophecy;');
        $prophecyGetter = new MethodNode('getProphecy');
        $prophecyGetter->setCode('return $this->objectProphecy;');
        if ($node->hasMethod('__call')) {
            $__call = $node->getMethod('__call');
        } else {
            $__call = new MethodNode('__call');
            $__call->addArgument(new ArgumentNode('name'));
            $__call->addArgument(new ArgumentNode('arguments'));
            $node->addMethod($__call);
        }
        $__call->setCode(<<<PHP
throw new \\Prophecy\\Exception\\Doubler\\MethodNotFoundException(
    sprintf('Method `%s::%s()` not found.', get_class(\$this), func_get_arg(0)),
    \$this->getProphecy(), func_get_arg(0)
);
PHP
);
        $node->addMethod($prophecySetter);
        $node->addMethod($prophecyGetter);
    }
Esempio n. 2
0
 /**
  * Remove methods that clash with php keywords
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $methodNames = array_keys($node->getMethods());
     $methodsToRemove = array_intersect($methodNames, $this->getKeywords());
     foreach ($methodsToRemove as $methodName) {
         $node->removeMethod($methodName);
     }
 }
Esempio n. 3
0
 function it_will_remove_echo_and_eval_methods(ClassNode $node, MethodNode $method1, MethodNode $method2, MethodNode $method3)
 {
     $node->removeMethod('eval')->shouldBeCalled();
     $node->removeMethod('echo')->shouldBeCalled();
     $method1->getName()->willReturn('echo');
     $method2->getName()->willReturn('eval');
     $method3->getName()->willReturn('notKeyword');
     $node->getMethods()->willReturn(array('echo' => $method1, 'eval' => $method2, 'notKeyword' => $method3));
     $this->apply($node);
 }
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode  $node
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $constructor
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method1
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method2
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method3
  */
 function it_forces_all_class_methods_except_constructor_to_proxy_calls_into_prophecy_makeCall($node, $constructor, $method1, $method2, $method3)
 {
     $node->addInterface('Prophecy\\Prophecy\\ProphecySubjectInterface')->willReturn(NULL);
     $node->addProperty('objectProphecy', 'private')->willReturn(NULL);
     $node->hasMethod(Argument::any())->willReturn(FALSE);
     $node->addMethod(Argument::type('Prophecy\\Doubler\\Generator\\Node\\MethodNode'))->willReturn(NULL);
     $node->addMethod(Argument::type('Prophecy\\Doubler\\Generator\\Node\\MethodNode'))->willReturn(NULL);
     $constructor->getName()->willReturn('__construct');
     $method1->getName()->willReturn('method1');
     $method2->getName()->willReturn('method2');
     $method3->getName()->willReturn('method3');
     $node->getMethods()->willReturn(array('method1' => $method1, 'method2' => $method2, 'method3' => $method3));
     $constructor->setCode(Argument::any())->shouldNotBeCalled();
     $method1->setCode('return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());')->shouldBeCalled();
     $method2->setCode('return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());')->shouldBeCalled();
     $method3->setCode('return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());')->shouldBeCalled();
     $this->apply($node);
 }
    /**
     * @param \Prophecy\Doubler\Generator\Node\ClassNode $class
     */
    function it_wraps_class_in_namespace_if_it_is_namespaced($class)
    {
        $class->getParentClass()->willReturn('stdClass');
        $class->getInterfaces()->willReturn(array('Prophecy\\Doubler\\Generator\\MirroredInterface'));
        $class->getProperties()->willReturn(array());
        $class->getMethods()->willReturn(array());
        $code = $this->generate('My\\Awesome\\CustomClass', $class);
        $expected = <<<'PHP'
namespace My\Awesome {
class CustomClass extends \stdClass implements \Prophecy\Doubler\Generator\MirroredInterface {


}
}
PHP;
        $expected = strtr($expected, array("\r\n" => "\n", "\r" => "\n"));
        $code->shouldBe($expected);
    }