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,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);
}
}