/**
  *
  * @param \Prophecy\Doubler\Generator\Node\ClassNode $class        	
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method        	
  * @param \Prophecy\Doubler\Generator\Node\ArgumentNode $arg1        	
  * @param \Prophecy\Doubler\Generator\Node\ArgumentNode $arg2        	
  */
 function it_makes_all_newInstance_arguments_optional($class, $method, $arg1, $arg2)
 {
     $class->getMethod('newInstance')->willReturn($method);
     $method->getArguments()->willReturn(array($arg1));
     $arg1->setDefault(null)->shouldBeCalled();
     $this->apply($class);
 }
예제 #2
0
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode $node
  */
 function it_discovers_api_using_phpdoc_from_extended_parent_interfaces($node)
 {
     $node->getParentClass()->willReturn('spec\\Prophecy\\Doubler\\ClassPatch\\MagicalApiImplementedExtended');
     $node->getInterfaces()->willReturn(array());
     $node->addMethod(new MethodNode('implementedMethod'))->shouldBeCalled();
     $this->apply($node);
 }
 /**
  *
  * @param \Prophecy\Doubler\Generator\Node\ClassNode $node        	
  */
 function it_ignores_existing_methods($node)
 {
     $node->getParentClass()->willReturn('spec\\Prophecy\\Doubler\\ClassPatch\\MagicalApiExtended');
     $node->addMethod(new MethodNode('undefinedMethod'))->shouldBeCalled();
     $node->addMethod(new MethodNode('definedMethod'))->shouldNotBeCalled();
     $this->apply($node);
 }
예제 #4
0
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode  $node
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method
  */
 function it_updates_existing_method_if_found($node, $method)
 {
     $node->hasMethod('__construct')->willReturn(true);
     $node->getMethod('__construct')->willReturn($method);
     $method->setCode(Argument::any())->shouldBeCalled();
     $this->apply($node);
 }
예제 #5
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);
     }
 }
 /**
  * Removes special exception static methods from the doubled methods.
  *
  * @param ClassNode $node
  *
  * @return void
  */
 public function apply(ClassNode $node)
 {
     if ($node->hasMethod('setTraceOptions')) {
         $node->getMethod('setTraceOptions')->useParentCode();
     }
     if ($node->hasMethod('getTraceOptions')) {
         $node->getMethod('getTraceOptions')->useParentCode();
     }
 }
예제 #7
0
 function it_should_supply_a_file_for_a_spl_file_object(ClassNode $node, MethodNode $method)
 {
     $node->hasMethod('__construct')->willReturn(true);
     $node->getMethod('__construct')->willReturn($method);
     $node->getParentClass()->willReturn('SplFileObject');
     $method->setCode(Argument::that(function ($value) {
         return strpos($value, '.php') !== false;
     }))->shouldBeCalled();
     $this->apply($node);
 }
예제 #8
0
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode $node
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method
  */
 function it_should_not_supply_a_file_for_a_directory_iterator($node, $method)
 {
     $node->hasMethod('__construct')->willReturn(true);
     $node->getMethod('__construct')->willReturn($method);
     $node->getParentClass()->willReturn('DirectoryIterator');
     $method->setCode(Argument::that(function ($value) {
         return strpos($value, '.php') === false;
     }))->shouldBeCalled();
     $this->apply($node);
 }
예제 #9
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);
 }
예제 #10
0
 /**
  * Updated constructor code to call parent one with dummy file argument.
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     if ($node->hasMethod('__construct')) {
         $constructor = $node->getMethod('__construct');
     } else {
         $constructor = new MethodNode('__construct');
         $node->addMethod($constructor);
     }
     $constructor->setCode('return parent::__construct("' . __FILE__ . '");');
 }
    /**
     * 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);
    }
예제 #12
0
 /**
  * Discover Magical API
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $parentClass = $node->getParentClass();
     $reflectionClass = new \ReflectionClass($parentClass);
     $phpdoc = new DocBlock($reflectionClass->getDocComment());
     $tagList = $phpdoc->getTagsByName('method');
     foreach ($tagList as $tag) {
         $methodNode = new MethodNode($tag->getMethodName());
         $methodNode->setStatic($tag->isStatic());
         $node->addMethod($methodNode);
     }
 }
예제 #13
0
 /**
  * Discover Magical API
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $parentClass = $node->getParentClass();
     $reflectionClass = new \ReflectionClass($parentClass);
     $tagList = $this->tagRetriever->getTagList($reflectionClass);
     foreach ($tagList as $tag) {
         $methodName = $tag->getMethodName();
         if (empty($methodName)) {
             continue;
         }
         if (!$reflectionClass->hasMethod($methodName)) {
             $methodNode = new MethodNode($methodName);
             $methodNode->setStatic($tag->isStatic());
             $node->addMethod($methodNode);
         }
     }
 }
    /**
     * Makes all class constructor arguments optional.
     *
     * @param ClassNode $node
     */
    public function apply(ClassNode $node)
    {
        if (!$node->hasMethod('__construct')) {
            $node->addMethod(new MethodNode('__construct', ''));
            return;
        }
        $constructor = $node->getMethod('__construct');
        foreach ($constructor->getArguments() as $argument) {
            $argument->setDefault(null);
        }
        $constructor->setCode(<<<PHP
if (0 < func_num_args()) {
    call_user_func_array(array('parent', '__construct'), func_get_args());
}
PHP
);
    }
예제 #15
0
 /**
  * Forces class to implement Iterator interface.
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $node->addInterface('Iterator');
     $node->addMethod(new MethodNode('current'));
     $node->addMethod(new MethodNode('key'));
     $node->addMethod(new MethodNode('next'));
     $node->addMethod(new MethodNode('rewind'));
     $node->addMethod(new MethodNode('valid'));
 }
 /**
  * Discover Magical API
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $parentClass = $node->getParentClass();
     $reflectionClass = new \ReflectionClass($parentClass);
     $phpdoc = new DocBlock($reflectionClass->getDocComment());
     $tagList = $phpdoc->getTagsByName('method');
     $interfaces = $reflectionClass->getInterfaces();
     foreach ($interfaces as $interface) {
         $phpdoc = new DocBlock($interface);
         $tagList = array_merge($tagList, $phpdoc->getTagsByName('method'));
     }
     foreach ($tagList as $tag) {
         $methodName = $tag->getMethodName();
         if (!$reflectionClass->hasMethod($methodName)) {
             $methodNode = new MethodNode($tag->getMethodName());
             $methodNode->setStatic($tag->isStatic());
             $node->addMethod($methodNode);
         }
     }
 }
예제 #17
0
 /**
  * Discover Magical API
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $types = array_filter($node->getInterfaces(), function ($interface) {
         return 0 !== strpos($interface, 'Prophecy\\');
     });
     $types[] = $node->getParentClass();
     foreach ($types as $type) {
         $reflectionClass = new \ReflectionClass($type);
         $tagList = $this->tagRetriever->getTagList($reflectionClass);
         foreach ($tagList as $tag) {
             $methodName = $tag->getMethodName();
             if (empty($methodName)) {
                 continue;
             }
             if (!$reflectionClass->hasMethod($methodName)) {
                 $methodNode = new MethodNode($methodName);
                 $methodNode->setStatic($tag->isStatic());
                 $node->addMethod($methodNode);
             }
         }
     }
 }
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode  $node
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $method
  * @param \Prophecy\Doubler\Generator\Node\MethodNode $getterMethod
  */
 function it_uses_parent_code_for_setTraceOptions($node, $method, $getterMethod)
 {
     $node->hasMethod('setTraceOptions')->willReturn(TRUE);
     $node->getMethod('setTraceOptions')->willReturn($method);
     $node->hasMethod('getTraceOptions')->willReturn(TRUE);
     $node->getMethod('getTraceOptions')->willReturn($getterMethod);
     $method->useParentCode()->shouldBeCalled();
     $getterMethod->useParentCode()->shouldBeCalled();
     $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_creates_new_constructor_if_object_has_none($class)
 {
     $class->hasMethod('__construct')->willReturn(false);
     $class->addMethod(Argument::type('Prophecy\\Doubler\\Generator\\Node\\MethodNode'))->shouldBeCalled();
     $this->apply($class);
 }
 /**
  * Updates newInstance's first argument to make it optional
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     foreach ($node->getMethod('newInstance')->getArguments() as $argument) {
         $argument->setDefault(null);
     }
 }
예제 #22
0
 private function reflectInterfaceToNode(ReflectionClass $interface, Node\ClassNode $node)
 {
     $node->addInterface($interface->getName());
     foreach ($interface->getMethods() as $method) {
         $this->reflectMethodToNode($method, $node);
     }
 }
예제 #23
0
 /**
  * @param ClassNode $node
  *
  * @return boolean
  */
 private function nodeIsDirectoryIterator(ClassNode $node)
 {
     $parent = $node->getParentClass();
     return 'DirectoryIterator' === $parent || is_subclass_of($parent, 'DirectoryIterator');
 }
예제 #24
0
 private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode)
 {
     $node = new Node\MethodNode($method->getName());
     if (true === $method->isProtected()) {
         $node->setVisibility('protected');
     }
     if (true === $method->isStatic()) {
         $node->setStatic();
     }
     if (true === $method->returnsReference()) {
         $node->setReturnsReference();
     }
     if (version_compare(PHP_VERSION, '7.0', '>=') && true === $method->hasReturnType()) {
         $node->setReturnType((string) $method->getReturnType());
     }
     if (is_array($params = $method->getParameters()) && count($params)) {
         foreach ($params as $param) {
             $this->reflectArgumentToNode($param, $node);
         }
     }
     $classNode->addMethod($node);
 }
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode $node
  */
 function it_forces_node_to_implement_IteratorAggregate($node)
 {
     $node->addInterface('Iterator')->shouldBeCalled();
     $node->addMethod(Argument::type('Prophecy\\Doubler\\Generator\\Node\\MethodNode'))->willReturn(null);
     $this->apply($node);
 }
예제 #26
0
 private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode)
 {
     $node = new Node\MethodNode($method->getName());
     if (true === $method->isProtected()) {
         $node->setVisibility('protected');
     }
     if (true === $method->isStatic()) {
         $node->setStatic();
     }
     if (is_array($params = $method->getParameters()) && count($params)) {
         foreach ($params as $param) {
             $this->reflectArgumentToNode($param, $node);
         }
     }
     $classNode->addMethod($node);
 }
예제 #27
0
 /**
  * @param ClassNode $node
  * @return boolean
  */
 private function nodeIsSplFileObject(ClassNode $node)
 {
     $parent = $node->getParentClass();
     return 'SplFileObject' === $parent || is_subclass_of($parent, 'SplFileObject');
 }
    /**
     * @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);
    }
 /**
  * @param \Prophecy\Doubler\Generator\Node\ClassNode $node
  */
 function it_discovers_api_using_phpdoc($node)
 {
     $node->getParentClass()->willReturn('spec\\Prophecy\\Doubler\\ClassPatch\\MagicalApi');
     $node->addMethod(new MethodNode('undefinedMethod'))->shouldBeCalled();
     $this->apply($node);
 }