/** * * @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); }
/** * 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); }
/** * @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument1 * @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument2 */ function its_useParentCode_causes_method_to_call_parent($argument1, $argument2) { $argument1->getName()->willReturn('objectName'); $argument2->getName()->willReturn('default'); $this->addArgument($argument1); $this->addArgument($argument2); $this->useParentCode(); $this->getCode()->shouldReturn('return parent::getTitle($objectName, $default);'); }
/** * @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_constructor_arguments_optional($class, $method, $arg1, $arg2) { $class->hasMethod('__construct')->willReturn(true); $class->getMethod('__construct')->willReturn($method); $method->getArguments()->willReturn(array($arg1, $arg2)); $arg1->setDefault(null)->shouldBeCalled(); $arg2->setDefault(null)->shouldBeCalled(); $method->setCode(Argument::type('string'))->shouldBeCalled(); $this->apply($class); }
function its_useParentCode_causes_method_to_call_parent(ArgumentNode $argument1, ArgumentNode $argument2) { $argument1->getName()->willReturn('objectName'); $argument2->getName()->willReturn('default'); $argument1->isVariadic()->willReturn(false); $argument2->isVariadic()->willReturn(true); $this->addArgument($argument1); $this->addArgument($argument2); $this->useParentCode(); $this->getCode()->shouldReturn('return parent::getTitle($objectName, ...$default);'); }
/** * @param \Prophecy\Doubler\Generator\Node\ClassNode $class * @param \Prophecy\Doubler\Generator\Node\MethodNode $method * @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument */ function it_overrides_properly_methods_with_args_passed_by_reference($class, $method, $argument) { $class->getParentClass()->willReturn('RuntimeException'); $class->getInterfaces()->willReturn(array('Prophecy\\Doubler\\Generator\\MirroredInterface')); $class->getProperties()->willReturn(array()); $class->getMethods()->willReturn(array($method)); $method->getName()->willReturn('getName'); $method->getVisibility()->willReturn('public'); $method->isStatic()->willReturn(false); $method->getArguments()->willReturn(array($argument)); $method->getCode()->willReturn('return $this->name;'); $argument->getName()->willReturn('fullname'); $argument->getTypeHint()->willReturn('array'); $argument->isOptional()->willReturn(true); $argument->getDefault()->willReturn(null); $argument->isPassedByReference()->willReturn(true); $code = $this->generate('CustomClass', $class); $expected = <<<'PHP' namespace { class CustomClass extends \RuntimeException implements \Prophecy\Doubler\Generator\MirroredInterface { public function getName(array &$fullname = NULL) { return $this->name; } } } PHP; $expected = strtr($expected, array("\r\n" => "\n", "\r" => "\n")); $code->shouldBe($expected); }
private function reflectArgumentToNode(ReflectionParameter $parameter, Node\MethodNode $methodNode) { $name = $parameter->getName() == '...' ? '__dot_dot_dot__' : $parameter->getName(); $node = new Node\ArgumentNode($name); $node->setTypeHint($this->getTypeHint($parameter)); if (true === $parameter->isDefaultValueAvailable()) { $node->setDefault($parameter->getDefaultValue()); } elseif (true === $parameter->isOptional() || true === $parameter->allowsNull()) { $node->setDefault(null); } if (true === $parameter->isPassedByReference()) { $node->setAsPassedByReference(); } $methodNode->addArgument($node); }
private function reflectArgumentToNode(ReflectionParameter $parameter, Node\MethodNode $methodNode) { $name = $parameter->getName() == '...' ? '__dot_dot_dot__' : $parameter->getName(); $node = new Node\ArgumentNode($name); $node->setTypeHint($this->getTypeHint($parameter)); if ($this->isVariadic($parameter)) { $node->setAsVariadic(); } if ($this->hasDefaultValue($parameter)) { $node->setDefault($this->getDefaultValue($parameter)); } if ($parameter->isPassedByReference()) { $node->setAsPassedByReference(); } $methodNode->addArgument($node); }
private function generateArgument(ArgumentNode $arg) { $argument = '$' . $arg->getName(); if ($arg->isVariadic()) { $argument = '...' . $argument; } return $argument; }