Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663

This commit is contained in:
Greg Anderson 2015-10-08 11:40:12 -07:00
parent eb34d130a8
commit f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions

View file

@ -0,0 +1,59 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class DisableConstructorPatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
function its_priority_is_100()
{
$this->getPriority()->shouldReturn(100);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_supports_anything($node)
{
$this->supports($node)->shouldReturn(true);
}
/**
* @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);
}
/**
* @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);
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class HhvmExceptionPatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
function its_priority_is_minus_50()
{
$this->getPriority()->shouldReturn(-50);
}
/**
* @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);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Prophecy\Doubler\Generator\Node\MethodNode;
class KeywordPatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
function its_priority_is_49()
{
$this->getPriority()->shouldReturn(49);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method1
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method2
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method3
*/
function it_will_remove_echo_and_eval_methods($node, $method1, $method2, $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);
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Prophecy\Doubler\Generator\Node\MethodNode;
class MagicCallPatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_supports_anything($node)
{
$this->supports($node)->shouldReturn(true);
}
/**
* @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);
}
/**
* @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);
}
function it_has_50_priority()
{
$this->getPriority()->shouldReturn(50);
}
}
/**
* @method void undefinedMethod()
*/
class MagicalApi
{
/**
* @return void
*/
public function definedMethod()
{
}
}
/**
* @method void undefinedMethod()
* @method void definedMethod()
*/
class MagicalApiExtended extends MagicalApi
{
}

View file

@ -0,0 +1,83 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ProphecySubjectPatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
function it_has_priority_of_0()
{
$this->getPriority()->shouldReturn(0);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_supports_any_class($node)
{
$this->supports($node)->shouldReturn(true);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_forces_class_to_implement_ProphecySubjectInterface($node)
{
$node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface')->shouldBeCalled();
$node->addProperty('objectProphecy', 'private')->willReturn(null);
$node->getMethods()->willReturn(array());
$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);
$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);
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ReflectionClassNewInstancePatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
function its_priority_is_50()
{
$this->getPriority()->shouldReturn(50);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $reflectionClassNode
* @param \Prophecy\Doubler\Generator\Node\ClassNode $anotherClassNode
*/
function it_supports_ReflectionClass_only($reflectionClassNode, $anotherClassNode)
{
$reflectionClassNode->getParentClass()->willReturn('ReflectionClass');
$anotherClassNode->getParentClass()->willReturn('stdClass');
$this->supports($reflectionClassNode)->shouldReturn(true);
$this->supports($anotherClassNode)->shouldReturn(false);
}
/**
* @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);
}
}

View file

@ -0,0 +1,91 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class SplFileInfoPatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
function its_priority_is_50()
{
$this->getPriority()->shouldReturn(50);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_does_not_support_nodes_without_parent_class($node)
{
$node->getParentClass()->willReturn('stdClass');
$this->supports($node)->shouldReturn(false);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_supports_nodes_with_SplFileInfo_as_parent_class($node)
{
$node->getParentClass()->willReturn('SplFileInfo');
$this->supports($node)->shouldReturn(true);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_supports_nodes_with_derivative_of_SplFileInfo_as_parent_class($node)
{
$node->getParentClass()->willReturn('SplFileInfo');
$this->supports($node)->shouldReturn(true);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_adds_a_method_to_node_if_not_exists($node)
{
$node->hasMethod('__construct')->willReturn(false);
$node->addMethod(Argument::any())->shouldBeCalled();
$node->getParentClass()->shouldBeCalled();
$this->apply($node);
}
/**
* @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);
$node->getParentClass()->shouldBeCalled();
$method->useParentCode()->shouldBeCalled();
$this->apply($node);
}
/**
* @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);
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace spec\Prophecy\Doubler\ClassPatch;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class TraversablePatchSpec extends ObjectBehavior
{
function it_is_a_patch()
{
$this->shouldBeAnInstanceOf('Prophecy\Doubler\ClassPatch\ClassPatchInterface');
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_supports_class_that_implements_only_Traversable($node)
{
$node->getInterfaces()->willReturn(array('Traversable'));
$this->supports($node)->shouldReturn(true);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_does_not_support_class_that_implements_Iterator($node)
{
$node->getInterfaces()->willReturn(array('Traversable', 'Iterator'));
$this->supports($node)->shouldReturn(false);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_does_not_support_class_that_implements_IteratorAggregate($node)
{
$node->getInterfaces()->willReturn(array('Traversable', 'IteratorAggregate'));
$this->supports($node)->shouldReturn(false);
}
function it_has_100_priority()
{
$this->getPriority()->shouldReturn(100);
}
/**
* @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);
}
}

View file

@ -0,0 +1,122 @@
<?php
namespace spec\Prophecy\Doubler;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class DoublerSpec extends ObjectBehavior
{
/**
* @param \Prophecy\Doubler\Generator\ClassMirror $mirror
* @param \Prophecy\Doubler\Generator\ClassCreator $creator
* @param \Prophecy\Doubler\NameGenerator $namer
*/
function let($mirror, $creator, $namer)
{
$this->beConstructedWith($mirror, $creator, $namer);
}
function it_does_not_have_patches_by_default()
{
$this->getClassPatches()->shouldHaveCount(0);
}
/**
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $patch
*/
function its_registerClassPatch_adds_a_patch_to_the_doubler($patch)
{
$this->registerClassPatch($patch);
$this->getClassPatches()->shouldReturn(array($patch));
}
/**
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $alt1
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $alt2
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $alt3
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $alt4
*/
function its_getClassPatches_sorts_patches_by_priority($alt1, $alt2, $alt3, $alt4)
{
$alt1->getPriority()->willReturn(2);
$alt2->getPriority()->willReturn(50);
$alt3->getPriority()->willReturn(10);
$alt4->getPriority()->willReturn(0);
$this->registerClassPatch($alt1);
$this->registerClassPatch($alt2);
$this->registerClassPatch($alt3);
$this->registerClassPatch($alt4);
$this->getClassPatches()->shouldReturn(array($alt2, $alt3, $alt1, $alt4));
}
/**
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $alt1
* @param \Prophecy\Doubler\ClassPatch\ClassPatchInterface $alt2
* @param \ReflectionClass $class
* @param \ReflectionClass $interface1
* @param \ReflectionClass $interface2
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function its_double_mirrors_alterates_and_instantiates_provided_class(
$mirror, $creator, $namer, $alt1, $alt2, $class, $interface1, $interface2, $node
)
{
$mirror->reflect($class, array($interface1, $interface2))->willReturn($node);
$alt1->supports($node)->willReturn(true);
$alt2->supports($node)->willReturn(false);
$alt1->getPriority()->willReturn(1);
$alt2->getPriority()->willReturn(2);
$namer->name($class, array($interface1, $interface2))->willReturn('SplStack');
$class->getName()->willReturn('stdClass');
$interface1->getName()->willReturn('ArrayAccess');
$interface2->getName()->willReturn('Iterator');
$alt1->apply($node)->shouldBeCalled();
$alt2->apply($node)->shouldNotBeCalled();
$creator->create('SplStack', $node)->shouldBeCalled();
$this->registerClassPatch($alt1);
$this->registerClassPatch($alt2);
$this->double($class, array($interface1, $interface2))
->shouldReturnAnInstanceOf('SplStack');
}
/**
* @param \ReflectionClass $class
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_double_instantiates_a_class_with_constructor_argument($mirror, $class, $node, $namer)
{
$class->getName()->willReturn('ReflectionClass');
$mirror->reflect($class, array())->willReturn($node);
$namer->name($class, array())->willReturn('ReflectionClass');
$double = $this->double($class, array(), array('stdClass'));
$double->shouldBeAnInstanceOf('ReflectionClass');
$double->getName()->shouldReturn('stdClass');
}
/**
* @param \ReflectionClass $class
* @param \Prophecy\Doubler\Generator\Node\ClassNode $node
*/
function it_can_instantiate_class_with_final_constructor($mirror, $class, $node, $namer)
{
$class->getName()->willReturn('spec\Prophecy\Doubler\WithFinalConstructor');
$mirror->reflect($class, array())->willReturn($node);
$namer->name($class, array())->willReturn('spec\Prophecy\Doubler\WithFinalConstructor');
$double = $this->double($class, array());
$double->shouldBeAnInstanceOf('spec\Prophecy\Doubler\WithFinalConstructor');
}
}
class WithFinalConstructor
{
final public function __construct() {}
}

View file

@ -0,0 +1,191 @@
<?php
namespace spec\Prophecy\Doubler\Generator;
use PhpSpec\ObjectBehavior;
class ClassCodeGeneratorSpec extends ObjectBehavior
{
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $class
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method1
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method2
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method3
* @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument11
* @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument12
* @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument21
* @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument31
*/
function it_generates_proper_php_code_for_specific_ClassNode(
$class, $method1, $method2, $method3, $argument11, $argument12, $argument21, $argument31
)
{
$class->getParentClass()->willReturn('RuntimeException');
$class->getInterfaces()->willReturn(array(
'Prophecy\Doubler\Generator\MirroredInterface', 'ArrayAccess', 'ArrayIterator'
));
$class->getProperties()->willReturn(array('name' => 'public', 'email' => 'private'));
$class->getMethods()->willReturn(array($method1, $method2, $method3));
$method1->getName()->willReturn('getName');
$method1->getVisibility()->willReturn('public');
$method1->returnsReference()->willReturn(false);
$method1->isStatic()->willReturn(true);
$method1->getArguments()->willReturn(array($argument11, $argument12));
$method1->hasReturnType()->willReturn(true);
$method1->getReturnType()->willReturn('string');
$method1->getCode()->willReturn('return $this->name;');
$method2->getName()->willReturn('getEmail');
$method2->getVisibility()->willReturn('protected');
$method2->returnsReference()->willReturn(false);
$method2->isStatic()->willReturn(false);
$method2->getArguments()->willReturn(array($argument21));
$method2->hasReturnType()->willReturn(false);
$method2->getCode()->willReturn('return $this->email;');
$method3->getName()->willReturn('getRefValue');
$method3->getVisibility()->willReturn('public');
$method3->returnsReference()->willReturn(true);
$method3->isStatic()->willReturn(false);
$method3->getArguments()->willReturn(array($argument31));
$method3->hasReturnType()->willReturn(false);
$method3->getCode()->willReturn('return $this->refValue;');
$argument11->getName()->willReturn('fullname');
$argument11->getTypeHint()->willReturn('array');
$argument11->isOptional()->willReturn(true);
$argument11->getDefault()->willReturn(null);
$argument11->isPassedByReference()->willReturn(false);
$argument12->getName()->willReturn('class');
$argument12->getTypeHint()->willReturn('ReflectionClass');
$argument12->isOptional()->willReturn(false);
$argument12->isPassedByReference()->willReturn(false);
$argument21->getName()->willReturn('default');
$argument21->getTypeHint()->willReturn(null);
$argument21->isOptional()->willReturn(true);
$argument21->getDefault()->willReturn('ever.zet@gmail.com');
$argument21->isPassedByReference()->willReturn(false);
$argument31->getName()->willReturn('refValue');
$argument31->getTypeHint()->willReturn(null);
$argument31->isOptional()->willReturn(false);
$argument31->getDefault()->willReturn();
$argument31->isPassedByReference()->willReturn(false);
$code = $this->generate('CustomClass', $class);
$expected = <<<'PHP'
namespace {
class CustomClass extends \RuntimeException implements \Prophecy\Doubler\Generator\MirroredInterface, \ArrayAccess, \ArrayIterator {
public $name;
private $email;
public static function getName(array $fullname = NULL, \ReflectionClass $class): string {
return $this->name;
}
protected function getEmail( $default = 'ever.zet@gmail.com') {
return $this->email;
}
public function &getRefValue( $refValue) {
return $this->refValue;
}
}
}
PHP;
$expected = strtr($expected, array("\r\n" => "\n", "\r" => "\n"));
$code->shouldBe($expected);
}
/**
* @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->hasReturnType()->willReturn(false);
$method->returnsReference()->willReturn(false);
$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);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $class
*/
function it_generates_empty_class_for_empty_ClassNode($class)
{
$class->getParentClass()->willReturn('stdClass');
$class->getInterfaces()->willReturn(array('Prophecy\Doubler\Generator\MirroredInterface'));
$class->getProperties()->willReturn(array());
$class->getMethods()->willReturn(array());
$code = $this->generate('CustomClass', $class);
$expected =<<<'PHP'
namespace {
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 $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);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace spec\Prophecy\Doubler\Generator;
use PhpSpec\ObjectBehavior;
class ClassCreatorSpec extends ObjectBehavior
{
/**
* @param \Prophecy\Doubler\Generator\ClassCodeGenerator $generator
*/
function let($generator)
{
$this->beConstructedWith($generator);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $class
*/
function it_evaluates_code_generated_by_ClassCodeGenerator($generator, $class)
{
$generator->generate('stdClass', $class)->shouldBeCalled()->willReturn(
'return 42;'
);
$this->create('stdClass', $class)->shouldReturn(42);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ClassNode $class
*/
function it_throws_an_exception_if_class_does_not_exist_after_evaluation($generator, $class)
{
$generator->generate('CustomClass', $class)->shouldBeCalled()->willReturn(
'return 42;'
);
$class->getParentClass()->willReturn('stdClass');
$class->getInterfaces()->willReturn(array('Interface1', 'Interface2'));
$this->shouldThrow('Prophecy\Exception\Doubler\ClassCreatorException')
->duringCreate('CustomClass', $class);
}
}

View file

@ -0,0 +1,610 @@
<?php
namespace spec\Prophecy\Doubler\Generator;
use PhpSpec\ObjectBehavior;
use I\Simply;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
class ClassMirrorSpec extends ObjectBehavior
{
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method1
* @param ReflectionMethod $method2
* @param ReflectionMethod $method3
*/
function it_reflects_a_class_by_mirroring_all_its_public_methods(
$class, $method1, $method2, $method3
)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array(
$method1, $method2, $method3
));
$method1->getName()->willReturn('getName');
$method2->getName()->willReturn('isPublic');
$method3->getName()->willReturn('isAbstract');
$method1->isFinal()->willReturn(false);
$method2->isFinal()->willReturn(false);
$method3->isFinal()->willReturn(false);
$method1->isProtected()->willReturn(false);
$method2->isProtected()->willReturn(false);
$method3->isProtected()->willReturn(false);
$method1->isStatic()->willReturn(false);
$method2->isStatic()->willReturn(false);
$method3->isStatic()->willReturn(false);
$method1->returnsReference()->willReturn(false);
$method2->returnsReference()->willReturn(false);
$method3->returnsReference()->willReturn(false);
$method1->getParameters()->willReturn(array());
$method2->getParameters()->willReturn(array());
$method3->getParameters()->willReturn(array());
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method1->hasReturnType()->willReturn(false);
$method2->hasReturnType()->willReturn(false);
$method3->hasReturnType()->willReturn(false);
}
$classNode = $this->reflect($class, array());
$classNode->shouldBeAnInstanceOf('Prophecy\Doubler\Generator\Node\ClassNode');
$classNode->getParentClass()->shouldReturn('Custom\ClassName');
$methodNodes = $classNode->getMethods();
$methodNodes->shouldHaveCount(3);
$classNode->hasMethod('getName')->shouldReturn(true);
$classNode->hasMethod('isPublic')->shouldReturn(true);
$classNode->hasMethod('isAbstract')->shouldReturn(true);
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
* @param ReflectionParameter $parameter
*/
function it_changes_argument_names_if_they_are_varying($class, $method, $parameter)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method));
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$method->getParameters()->willReturn(array($parameter));
$method->getName()->willReturn('methodName');
$method->isFinal()->willReturn(false);
$method->isProtected()->willReturn(false);
$method->isStatic()->willReturn(false);
$method->returnsReference()->willReturn(false);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method->hasReturnType()->willReturn(false);
}
$parameter->getName()->willReturn('...');
$parameter->isDefaultValueAvailable()->willReturn(true);
$parameter->getDefaultValue()->willReturn(null);
$parameter->isPassedByReference()->willReturn(false);
$parameter->getClass()->willReturn($class);
$classNode = $this->reflect($class, array());
$methodNodes = $classNode->getMethods();
$argumentNodes = $methodNodes['methodName']->getArguments();
$argumentNode = $argumentNodes[0];
$argumentNode->getName()->shouldReturn('__dot_dot_dot__');
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
*/
function it_reflects_protected_abstract_methods($class, $method)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array($method));
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array());
$method->isProtected()->willReturn(true);
$method->isStatic()->willReturn(false);
$method->getParameters()->willReturn(array());
$method->getName()->willReturn('innerDetail');
$method->returnsReference()->willReturn(false);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method->hasReturnType()->willReturn(false);
}
$classNode = $this->reflect($class, array());
$classNode->shouldBeAnInstanceOf('Prophecy\Doubler\Generator\Node\ClassNode');
$classNode->getParentClass()->shouldReturn('Custom\ClassName');
$methodNodes = $classNode->getMethods();
$methodNodes->shouldHaveCount(1);
$methodNodes['innerDetail']->getVisibility()->shouldReturn('protected');
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
*/
function it_reflects_public_static_methods($class, $method)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array($method));
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array());
$method->isProtected()->willReturn(true);
$method->isStatic()->willReturn(true);
$method->getParameters()->willReturn(array());
$method->getName()->willReturn('innerDetail');
$method->returnsReference()->willReturn(false);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method->hasReturnType()->willReturn(false);
}
$classNode = $this->reflect($class, array());
$classNode->shouldBeAnInstanceOf('Prophecy\Doubler\Generator\Node\ClassNode');
$classNode->getParentClass()->shouldReturn('Custom\ClassName');
$methodNodes = $classNode->getMethods();
$methodNodes->shouldHaveCount(1);
$methodNodes['innerDetail']->getVisibility()->shouldReturn('protected');
$methodNodes['innerDetail']->isStatic()->shouldReturn(true);
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
* @param ReflectionParameter $param1
* @param ReflectionParameter $param2
* @param ReflectionClass $typeHint
* @param ReflectionParameter $param3
*/
function it_properly_reads_methods_arguments_with_types(
$class, $method, $param1, $param2, $typeHint, $param3
)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method));
$method->getName()->willReturn('methodWithArgs');
$method->isFinal()->willReturn(false);
$method->isProtected()->willReturn(true);
$method->isStatic()->willReturn(false);
$method->returnsReference()->willReturn(false);
$method->getParameters()->willReturn(array($param1, $param2, $param3));
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method->hasReturnType()->willReturn(false);
}
$param1->getName()->willReturn('arg_1');
$param1->isArray()->willReturn(true);
$param1->getClass()->willReturn(null);
$param1->isDefaultValueAvailable()->willReturn(true);
$param1->isPassedByReference()->willReturn(false);
$param1->allowsNull()->willReturn(false);
$param1->getDefaultValue()->willReturn(array());
$param2->getName()->willReturn('arg2');
$param2->isArray()->willReturn(false);
$param2->getClass()->willReturn($typeHint);
$param2->isDefaultValueAvailable()->willReturn(false);
$param2->isOptional()->willReturn(false);
$param2->isPassedByReference()->willReturn(false);
$param2->allowsNull()->willReturn(false);
$typeHint->getName()->willReturn('ArrayAccess');
$param3->getName()->willReturn('arg_3');
$param3->isArray()->willReturn(false);
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$param3->isCallable()->willReturn(true);
}
$param3->getClass()->willReturn(null);
$param3->isOptional()->willReturn(false);
$param3->isDefaultValueAvailable()->willReturn(false);
$param3->isPassedByReference()->willReturn(false);
$param3->allowsNull()->willReturn(true);
$classNode = $this->reflect($class, array());
$methodNodes = $classNode->getMethods();
$argNodes = $methodNodes['methodWithArgs']->getArguments();
$argNodes[0]->getName()->shouldReturn('arg_1');
$argNodes[0]->getTypeHint()->shouldReturn('array');
$argNodes[0]->isOptional()->shouldReturn(true);
$argNodes[0]->getDefault()->shouldReturn(array());
$argNodes[1]->getName()->shouldReturn('arg2');
$argNodes[1]->getTypeHint()->shouldReturn('ArrayAccess');
$argNodes[1]->isOptional()->shouldReturn(false);
$argNodes[2]->getName()->shouldReturn('arg_3');
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$argNodes[2]->getTypeHint()->shouldReturn('callable');
$argNodes[2]->isOptional()->shouldReturn(true);
$argNodes[2]->getDefault()->shouldReturn(null);
} else {
$argNodes[2]->isOptional()->shouldReturn(false);
}
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
* @param ReflectionParameter $param1
*/
function it_marks_required_args_without_types_as_not_optional(
$class, $method, $param1
)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method));
$method->getName()->willReturn('methodWithArgs');
$method->isFinal()->willReturn(false);
$method->isProtected()->willReturn(false);
$method->isStatic()->willReturn(false);
$method->returnsReference()->willReturn(false);
$method->getParameters()->willReturn(array($param1));
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method->hasReturnType()->willReturn(false);
}
$param1->getName()->willReturn('arg_1');
$param1->isArray()->willReturn(false);
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$param1->isCallable()->willReturn(false);
}
$param1->getClass()->willReturn(null);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$param1->hasType()->willReturn(false);
}
$param1->isDefaultValueAvailable()->willReturn(false);
$param1->isOptional()->willReturn(false);
$param1->isPassedByReference()->willReturn(false);
$param1->allowsNull()->willReturn(true);
if (defined('HHVM_VERSION')) {
$param1->getTypehintText()->willReturn(null);
}
$classNode = $this->reflect($class, array());
$methodNodes = $classNode->getMethods();
$argNodes = $methodNodes['methodWithArgs']->getArguments();
$argNodes[0]->isOptional()->shouldReturn(false);
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
* @param ReflectionParameter $param1
* @param ReflectionParameter $param2
* @param ReflectionClass $typeHint
*/
function it_marks_passed_by_reference_args_as_passed_by_reference(
$class, $method, $param1, $param2, $typeHint
)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method));
$method->getName()->willReturn('methodWithArgs');
$method->isFinal()->willReturn(false);
$method->isProtected()->willReturn(false);
$method->isStatic()->willReturn(false);
$method->returnsReference()->willReturn(false);
$method->getParameters()->willReturn(array($param1, $param2));
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method->hasReturnType()->willReturn(false);
}
$param1->getName()->willReturn('arg_1');
$param1->isArray()->willReturn(false);
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$param1->isCallable()->willReturn(false);
}
$param1->getClass()->willReturn(null);
$param1->isDefaultValueAvailable()->willReturn(false);
$param1->isOptional()->willReturn(true);
$param1->isPassedByReference()->willReturn(true);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$param1->hasType()->willReturn(false);
}
$param1->allowsNull()->willReturn(false);
if (defined('HHVM_VERSION')) {
$param1->getTypehintText()->willReturn(null);
}
$param2->getName()->willReturn('arg2');
$param2->isArray()->willReturn(false);
$param2->getClass()->willReturn($typeHint);
$param2->isDefaultValueAvailable()->willReturn(false);
$param2->isOptional()->willReturn(false);
$param2->isPassedByReference()->willReturn(false);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$param2->hasType()->willReturn(false);
}
$param2->allowsNull()->willReturn(false);
$typeHint->getName()->willReturn('ArrayAccess');
$classNode = $this->reflect($class, array());
$methodNodes = $classNode->getMethods();
$argNodes = $methodNodes['methodWithArgs']->getArguments();
$argNodes[0]->isPassedByReference()->shouldReturn(true);
$argNodes[1]->isPassedByReference()->shouldReturn(false);
}
/**
* @param ReflectionClass $class
*/
function it_throws_an_exception_if_class_is_final($class)
{
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(true);
$class->getName()->willReturn('Custom\ClassName');
$this->shouldThrow('Prophecy\Exception\Doubler\ClassMirrorException')
->duringReflect($class, array());
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
*/
function it_ignores_final_methods($class, $method)
{
$class->getName()->willReturn('Custom\ClassName');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method));
$method->isFinal()->willReturn(true);
$method->getName()->willReturn('finalImplementation');
$classNode = $this->reflect($class, array());
$classNode->getMethods()->shouldHaveCount(0);
}
/**
* @param ReflectionClass $interface
*/
function it_throws_an_exception_if_interface_provided_instead_of_class($interface)
{
$interface->isInterface()->willReturn(true);
$interface->getName()->willReturn('Custom\ClassName');
$this->shouldThrow('Prophecy\Exception\InvalidArgumentException')
->duringReflect($interface, array());
}
/**
* @param ReflectionClass $interface1
* @param ReflectionClass $interface2
* @param ReflectionMethod $method1
* @param ReflectionMethod $method2
* @param ReflectionMethod $method3
*/
function it_reflects_all_interfaces_methods(
$interface1, $interface2, $method1, $method2, $method3
)
{
$interface1->getName()->willReturn('MyInterface1');
$interface2->getName()->willReturn('MyInterface2');
$interface1->isInterface()->willReturn(true);
$interface2->isInterface()->willReturn(true);
$interface1->getMethods()->willReturn(array($method1));
$interface2->getMethods()->willReturn(array($method2, $method3));
$method1->getName()->willReturn('getName');
$method2->getName()->willReturn('isPublic');
$method3->getName()->willReturn('isAbstract');
$method1->isProtected()->willReturn(false);
$method2->isProtected()->willReturn(false);
$method3->isProtected()->willReturn(false);
$method1->returnsReference()->willReturn(false);
$method2->returnsReference()->willReturn(false);
$method3->returnsReference()->willReturn(false);
$method1->isStatic()->willReturn(false);
$method2->isStatic()->willReturn(false);
$method3->isStatic()->willReturn(false);
$method1->getParameters()->willReturn(array());
$method2->getParameters()->willReturn(array());
$method3->getParameters()->willReturn(array());
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method1->hasReturnType()->willReturn(false);
$method2->hasReturnType()->willReturn(false);
$method3->hasReturnType()->willReturn(false);
}
$classNode = $this->reflect(null, array($interface1, $interface2));
$classNode->shouldBeAnInstanceOf('Prophecy\Doubler\Generator\Node\ClassNode');
$classNode->getParentClass()->shouldReturn('stdClass');
$classNode->getInterfaces()->shouldReturn(array(
'Prophecy\Doubler\Generator\ReflectionInterface', 'MyInterface2', 'MyInterface1',
));
$methodNodes = $classNode->getMethods();
$methodNodes->shouldHaveCount(3);
$classNode->hasMethod('getName')->shouldReturn(true);
$classNode->hasMethod('isPublic')->shouldReturn(true);
$classNode->hasMethod('isAbstract')->shouldReturn(true);
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method1
* @param ReflectionMethod $method2
* @param ReflectionMethod $method3
*/
function it_ignores_virtually_private_methods($class, $method1, $method2, $method3)
{
$class->getName()->willReturn('SomeClass');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method1, $method2, $method3));
$method1->getName()->willReturn('_getName');
$method2->getName()->willReturn('__toString');
$method3->getName()->willReturn('isAbstract');
$method1->isFinal()->willReturn(false);
$method2->isFinal()->willReturn(false);
$method3->isFinal()->willReturn(false);
$method1->isProtected()->willReturn(false);
$method2->isProtected()->willReturn(false);
$method3->isProtected()->willReturn(false);
$method1->isStatic()->willReturn(false);
$method2->isStatic()->willReturn(false);
$method3->isStatic()->willReturn(false);
$method1->returnsReference()->willReturn(false);
$method2->returnsReference()->willReturn(false);
$method3->returnsReference()->willReturn(false);
$method1->getParameters()->willReturn(array());
$method2->getParameters()->willReturn(array());
$method3->getParameters()->willReturn(array());
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$method1->hasReturnType()->willReturn(false);
$method2->hasReturnType()->willReturn(false);
$method3->hasReturnType()->willReturn(false);
}
$classNode = $this->reflect($class, array());
$methodNodes = $classNode->getMethods();
$methodNodes->shouldHaveCount(2);
$classNode->hasMethod('isAbstract')->shouldReturn(true);
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
*/
function it_does_not_throw_exception_for_virtually_private_finals($class, $method)
{
$class->getName()->willReturn('SomeClass');
$class->isInterface()->willReturn(false);
$class->isFinal()->willReturn(false);
$class->getMethods(ReflectionMethod::IS_ABSTRACT)->willReturn(array());
$class->getMethods(ReflectionMethod::IS_PUBLIC)->willReturn(array($method));
$method->getName()->willReturn('__toString');
$method->isFinal()->willReturn(true);
$this->shouldNotThrow()->duringReflect($class, array());
}
/**
* @param ReflectionClass $class
*/
function it_throws_an_exception_if_class_provided_in_interfaces_list($class)
{
$class->getName()->willReturn('MyClass');
$class->isInterface()->willReturn(false);
$this->shouldThrow('InvalidArgumentException')
->duringReflect(null, array($class));
}
function it_throws_an_exception_if_not_reflection_provided_as_interface()
{
$this->shouldThrow('InvalidArgumentException')
->duringReflect(null, array(null));
}
function it_doesnt_fail_to_typehint_nonexistent_FQCN()
{
$classNode = $this->reflect(new ReflectionClass('spec\Prophecy\Doubler\Generator\OptionalDepsClass'), array());
$method = $classNode->getMethod('iHaveAStrangeTypeHintedArg');
$arguments = $method->getArguments();
$arguments[0]->getTypeHint()->shouldBe('I\Simply\Am\Nonexistent');
}
function it_doesnt_fail_to_typehint_nonexistent_RQCN()
{
$classNode = $this->reflect(new ReflectionClass('spec\Prophecy\Doubler\Generator\OptionalDepsClass'), array());
$method = $classNode->getMethod('iHaveAnEvenStrangerTypeHintedArg');
$arguments = $method->getArguments();
$arguments[0]->getTypeHint()->shouldBe('I\Simply\Am\Not');
}
function it_doesnt_use_scalar_typehints()
{
$classNode = $this->reflect(new ReflectionClass('ReflectionMethod'), array());
$method = $classNode->getMethod('export');
$arguments = $method->getArguments();
$arguments[0]->getTypeHint()->shouldReturn(null);
$arguments[1]->getTypeHint()->shouldReturn(null);
$arguments[2]->getTypeHint()->shouldReturn(null);
}
}
class OptionalDepsClass
{
public function iHaveAStrangeTypeHintedArg(\I\Simply\Am\Nonexistent $class)
{
}
public function iHaveAnEvenStrangerTypeHintedArg(Simply\Am\Not $class)
{
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace spec\Prophecy\Doubler\Generator\Node;
use PhpSpec\ObjectBehavior;
class ArgumentNodeSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('name');
}
function it_is_not_be_passed_by_reference_by_default()
{
$this->shouldNotBePassedByReference();
}
function it_is_passed_by_reference_if_marked()
{
$this->setAsPassedByReference();
$this->shouldBePassedByReference();
}
function it_has_name_with_which_it_was_been_constructed()
{
$this->getName()->shouldReturn('name');
}
function it_has_no_typehint_by_default()
{
$this->getTypeHint()->shouldReturn(null);
}
function its_typeHint_is_mutable()
{
$this->setTypeHint('array');
$this->getTypeHint()->shouldReturn('array');
}
function it_does_not_have_default_value_by_default()
{
$this->getDefault()->shouldReturn(null);
}
function it_is_not_optional_by_default()
{
$this->isOptional()->shouldReturn(false);
}
function its_default_is_mutable()
{
$this->setDefault(array());
$this->getDefault()->shouldReturn(array());
}
function it_is_marked_as_optional_when_default_is_set()
{
$this->setDefault(null);
$this->isOptional()->shouldReturn(true);
}
}

View file

@ -0,0 +1,154 @@
<?php
namespace spec\Prophecy\Doubler\Generator\Node;
use PhpSpec\ObjectBehavior;
class ClassNodeSpec extends ObjectBehavior
{
function its_parentClass_is_a_stdClass_by_default()
{
$this->getParentClass()->shouldReturn('stdClass');
}
function its_parentClass_is_mutable()
{
$this->setParentClass('Exception');
$this->getParentClass()->shouldReturn('Exception');
}
function its_parentClass_is_set_to_stdClass_if_user_set_null()
{
$this->setParentClass(null);
$this->getParentClass()->shouldReturn('stdClass');
}
function it_does_not_implement_any_interface_by_default()
{
$this->getInterfaces()->shouldHaveCount(0);
}
function its_addInterface_adds_item_to_the_list_of_implemented_interfaces()
{
$this->addInterface('MyInterface');
$this->getInterfaces()->shouldHaveCount(1);
}
function its_hasInterface_returns_true_if_class_implements_interface()
{
$this->addInterface('MyInterface');
$this->hasInterface('MyInterface')->shouldReturn(true);
}
function its_hasInterface_returns_false_if_class_does_not_implements_interface()
{
$this->hasInterface('MyInterface')->shouldReturn(false);
}
function it_supports_implementation_of_multiple_interfaces()
{
$this->addInterface('MyInterface');
$this->addInterface('MySecondInterface');
$this->getInterfaces()->shouldHaveCount(2);
}
function it_ignores_same_interfaces_added_twice()
{
$this->addInterface('MyInterface');
$this->addInterface('MyInterface');
$this->getInterfaces()->shouldHaveCount(1);
$this->getInterfaces()->shouldReturn(array('MyInterface'));
}
function it_does_not_have_methods_by_default()
{
$this->getMethods()->shouldHaveCount(0);
}
/**
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method1
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method2
*/
function it_can_has_methods($method1, $method2)
{
$method1->getName()->willReturn('__construct');
$method2->getName()->willReturn('getName');
$this->addMethod($method1);
$this->addMethod($method2);
$this->getMethods()->shouldReturn(array(
'__construct' => $method1,
'getName' => $method2
));
}
/**
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method
*/
function its_hasMethod_returns_true_if_method_exists($method)
{
$method->getName()->willReturn('getName');
$this->addMethod($method);
$this->hasMethod('getName')->shouldReturn(true);
}
/**
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method
*/
function its_getMethod_returns_method_by_name($method)
{
$method->getName()->willReturn('getName');
$this->addMethod($method);
$this->getMethod('getName')->shouldReturn($method);
}
function its_hasMethod_returns_false_if_method_does_not_exists()
{
$this->hasMethod('getName')->shouldReturn(false);
}
/**
* @param \Prophecy\Doubler\Generator\Node\MethodNode $method
*/
function its_hasMethod_returns_false_if_method_has_been_removed($method)
{
$method->getName()->willReturn('getName');
$this->addMethod($method);
$this->removeMethod('getName');
$this->hasMethod('getName')->shouldReturn(false);
}
function it_does_not_have_properties_by_default()
{
$this->getProperties()->shouldHaveCount(0);
}
function it_is_able_to_have_properties()
{
$this->addProperty('title');
$this->addProperty('text', 'private');
$this->getProperties()->shouldReturn(array(
'title' => 'public',
'text' => 'private'
));
}
function its_addProperty_does_not_accept_unsupported_visibility()
{
$this->shouldThrow('InvalidArgumentException')->duringAddProperty('title', 'town');
}
function its_addProperty_lowercases_visibility_before_setting()
{
$this->addProperty('text', 'PRIVATE');
$this->getProperties()->shouldReturn(array('text' => 'private'));
}
}

View file

@ -0,0 +1,138 @@
<?php
namespace spec\Prophecy\Doubler\Generator\Node;
use PhpSpec\ObjectBehavior;
class MethodNodeSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('getTitle');
}
function it_has_a_name()
{
$this->getName()->shouldReturn('getTitle');
}
function it_has_public_visibility_by_default()
{
$this->getVisibility()->shouldReturn('public');
}
function its_visibility_is_mutable()
{
$this->setVisibility('private');
$this->getVisibility()->shouldReturn('private');
}
function it_is_not_static_by_default()
{
$this->shouldNotBeStatic();
}
function it_does_not_return_a_reference_by_default()
{
$this->returnsReference()->shouldReturn(false);
}
function it_should_be_settable_as_returning_a_reference_through_setter()
{
$this->setReturnsReference();
$this->returnsReference()->shouldReturn(true);
}
function it_should_be_settable_as_static_through_setter()
{
$this->setStatic();
$this->shouldBeStatic();
}
function it_accepts_only_supported_visibilities()
{
$this->shouldThrow('InvalidArgumentException')->duringSetVisibility('stealth');
}
function it_lowercases_visibility_before_setting_it()
{
$this->setVisibility('Public');
$this->getVisibility()->shouldReturn('public');
}
/**
* @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);'
);
}
function its_code_is_mutable()
{
$this->setCode('echo "code";');
$this->getCode()->shouldReturn('echo "code";');
}
function its_reference_returning_methods_will_generate_exceptions()
{
$this->setCode('echo "code";');
$this->setReturnsReference();
$this->getCode()->shouldReturn("throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), 'getTitle');");
}
function its_setCode_provided_with_null_cleans_method_body()
{
$this->setCode(null);
$this->getCode()->shouldReturn('');
}
function it_is_constructable_with_code()
{
$this->beConstructedWith('getTitle', 'die();');
$this->getCode()->shouldReturn('die();');
}
function it_does_not_have_arguments_by_default()
{
$this->getArguments()->shouldHaveCount(0);
}
/**
* @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument1
* @param \Prophecy\Doubler\Generator\Node\ArgumentNode $argument2
*/
function it_supports_adding_arguments($argument1, $argument2)
{
$this->addArgument($argument1);
$this->addArgument($argument2);
$this->getArguments()->shouldReturn(array($argument1, $argument2));
}
function it_does_not_have_return_type_by_default()
{
$this->hasReturnType()->shouldReturn(false);
}
function it_setReturnType_sets_return_type()
{
$returnType = 'string';
$this->setReturnType($returnType);
$this->hasReturnType()->shouldReturn(true);
$this->getReturnType()->shouldReturn($returnType);
}
}

View file

@ -0,0 +1,96 @@
<?php
namespace spec\Prophecy\Doubler;
use PhpSpec\ObjectBehavior;
class LazyDoubleSpec extends ObjectBehavior
{
/**
* @param \Prophecy\Doubler\Doubler $doubler
*/
function let($doubler)
{
$this->beConstructedWith($doubler);
}
/**
* @param \Prophecy\Prophecy\ProphecySubjectInterface $double
*/
function it_returns_anonymous_double_instance_by_default($doubler, $double)
{
$doubler->double(null, array())->willReturn($double);
$this->getInstance()->shouldReturn($double);
}
/**
* @param \Prophecy\Prophecy\ProphecySubjectInterface $double
* @param \ReflectionClass $class
*/
function it_returns_class_double_instance_if_set($doubler, $double, $class)
{
$doubler->double($class, array())->willReturn($double);
$this->setParentClass($class);
$this->getInstance()->shouldReturn($double);
}
/**
* @param \Prophecy\Prophecy\ProphecySubjectInterface $double1
* @param \Prophecy\Prophecy\ProphecySubjectInterface $double2
*/
function it_returns_same_double_instance_if_called_2_times(
$doubler, $double1, $double2
)
{
$doubler->double(null, array())->willReturn($double1);
$doubler->double(null, array())->willReturn($double2);
$this->getInstance()->shouldReturn($double2);
$this->getInstance()->shouldReturn($double2);
}
function its_setParentClass_throws_ClassNotFoundException_if_class_not_found()
{
$this->shouldThrow('Prophecy\Exception\Doubler\ClassNotFoundException')
->duringSetParentClass('SomeUnexistingClass');
}
/**
* @param \Prophecy\Prophecy\ProphecySubjectInterface $double
*/
function its_setParentClass_throws_exception_if_prophecy_is_already_created(
$doubler, $double
)
{
$doubler->double(null, array())->willReturn($double);
$this->getInstance();
$this->shouldThrow('Prophecy\Exception\Doubler\DoubleException')
->duringSetParentClass('stdClass');
}
function its_addInterface_throws_InterfaceNotFoundException_if_no_interface_found()
{
$this->shouldThrow('Prophecy\Exception\Doubler\InterfaceNotFoundException')
->duringAddInterface('SomeUnexistingInterface');
}
/**
* @param \Prophecy\Prophecy\ProphecySubjectInterface $double
*/
function its_addInterface_throws_exception_if_prophecy_is_already_created(
$doubler, $double
)
{
$doubler->double(null, array())->willReturn($double);
$this->getInstance();
$this->shouldThrow('Prophecy\Exception\Doubler\DoubleException')
->duringAddInterface('ArrayAccess');
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace spec\Prophecy\Doubler;
use PhpSpec\ObjectBehavior;
class NameGeneratorSpec extends ObjectBehavior
{
/**
* @param \ReflectionClass $class
*/
function its_name_generates_name_based_on_simple_class_reflection($class)
{
$class->getName()->willReturn('stdClass');
$this->name($class, array())->shouldStartWith('Double\stdClass\\');
}
/**
* @param \ReflectionClass $class
*/
function its_name_generates_name_based_on_namespaced_class_reflection($class)
{
$class->getName()->willReturn('Some\Custom\Class');
$this->name($class, array())->shouldStartWith('Double\Some\Custom\Class\P');
}
/**
* @param \ReflectionClass $interface1
* @param \ReflectionClass $interface2
*/
function its_name_generates_name_based_on_interface_shortnames($interface1, $interface2)
{
$interface1->getShortName()->willReturn('HandlerInterface');
$interface2->getShortName()->willReturn('LoaderInterface');
$this->name(null, array($interface1, $interface2))->shouldStartWith(
'Double\HandlerInterface\LoaderInterface\P'
);
}
function it_generates_proper_name_for_no_class_and_interfaces_list()
{
$this->name(null, array())->shouldStartWith('Double\stdClass\P');
}
/**
* @param \ReflectionClass $class
* @param \ReflectionClass $interface1
* @param \ReflectionClass $interface2
*/
function its_name_generates_name_based_only_on_class_if_its_available(
$class, $interface1, $interface2
)
{
$class->getName()->willReturn('Some\Custom\Class');
$interface1->getShortName()->willReturn('HandlerInterface');
$interface2->getShortName()->willReturn('LoaderInterface');
$this->name($class, array($interface1, $interface2))->shouldStartWith(
'Double\Some\Custom\Class\P'
);
}
public function getMatchers()
{
return array(
'startWith' => function ($subject, $string) {
return 0 === strpos($subject, $string);
},
);
}
}