Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

405 lines (329 sloc) 12.393 kB
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Code
*/
namespace ZendTest\Code\Generator;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Reflection\ClassReflection;
/**
* @category Zend
* @package Zend_Code_Generator
* @subpackage UnitTests
*
* @group Zend_Code_Generator
* @group Zend_Code_Generator_Php
*/
class ClassGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testConstruction()
{
$class = new ClassGenerator();
$this->isInstanceOf($class, 'Zend\Code\Generator\ClassGenerator');
}
public function testNameAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setName('TestClass');
$this->assertEquals($classGenerator->getName(), 'TestClass');
}
public function testClassDocBlockAccessors()
{
$this->markTestIncomplete();
}
public function testAbstractAccessors()
{
$classGenerator = new ClassGenerator();
$this->assertFalse($classGenerator->isAbstract());
$classGenerator->setAbstract(true);
$this->assertTrue($classGenerator->isAbstract());
}
public function testExtendedClassAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setExtendedClass('ExtendedClass');
$this->assertEquals($classGenerator->getExtendedClass(), 'ExtendedClass');
}
public function testImplementedInterfacesAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setImplementedInterfaces(array('Class1', 'Class2'));
$this->assertEquals($classGenerator->getImplementedInterfaces(), array('Class1', 'Class2'));
}
public function testPropertyAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperties(array(
'propOne',
new PropertyGenerator('propTwo')
));
$properties = $classGenerator->getProperties();
$this->assertEquals(count($properties), 2);
$this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', current($properties));
$property = $classGenerator->getProperty('propTwo');
$this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', $property);
$this->assertEquals($property->getName(), 'propTwo');
// add a new property
$classGenerator->addProperty('prop3');
$this->assertEquals(count($classGenerator->getProperties()), 3);
}
public function testSetPropertyAlreadyExistsThrowsException()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperty('prop3');
$this->setExpectedException(
'Zend\Code\Generator\Exception\InvalidArgumentException',
'A property by name prop3 already exists in this class'
);
$classGenerator->addProperty('prop3');
}
public function testSetPropertyNoArrayOrPropertyThrowsException()
{
$classGenerator = new ClassGenerator();
$this->setExpectedException(
'Zend\Code\Generator\Exception\InvalidArgumentException',
'Zend\Code\Generator\ClassGenerator::addProperty expects string for name'
);
$classGenerator->addProperty(true);
}
public function testMethodAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->addMethods(array(
'methodOne',
new MethodGenerator('methodTwo')
));
$methods = $classGenerator->getMethods();
$this->assertEquals(count($methods), 2);
$this->isInstanceOf(current($methods), '\Zend\Code\Generator\PhpMethod');
$method = $classGenerator->getMethod('methodOne');
$this->isInstanceOf($method, '\Zend\Code\Generator\PhpMethod');
$this->assertEquals($method->getName(), 'methodOne');
// add a new property
$classGenerator->addMethod('methodThree');
$this->assertEquals(count($classGenerator->getMethods()), 3);
}
public function testSetMethodNoMethodOrArrayThrowsException()
{
$classGenerator = new ClassGenerator();
$this->setExpectedException(
'Zend\Code\Generator\Exception\ExceptionInterface',
'Zend\Code\Generator\ClassGenerator::addMethod expects string for name'
);
$classGenerator->addMethod(true);
}
public function testSetMethodNameAlreadyExistsThrowsException()
{
$methodA = new MethodGenerator();
$methodA->setName("foo");
$methodB = new MethodGenerator();
$methodB->setName("foo");
$classGenerator = new ClassGenerator();
$classGenerator->addMethodFromGenerator($methodA);
$this->setExpectedException(
'Zend\Code\Generator\Exception\InvalidArgumentException',
'A method by name foo already exists in this class.'
);
$classGenerator->addMethodFromGenerator($methodB);
}
/**
* @group ZF-7361
*/
public function testHasMethod()
{
$classGenerator = new ClassGenerator();
$classGenerator->addMethod('methodOne');
$this->assertTrue($classGenerator->hasMethod('methodOne'));
}
public function testRemoveMethod()
{
$classGenerator = new ClassGenerator();
$classGenerator->addMethod('methodOne');
$this->assertTrue($classGenerator->hasMethod('methodOne'));
$classGenerator->removeMethod('methodOne');
$this->assertFalse($classGenerator->hasMethod('methodOne'));
}
/**
* @group ZF-7361
*/
public function testHasProperty()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperty('propertyOne');
$this->assertTrue($classGenerator->hasProperty('propertyOne'));
}
public function testToString()
{
$classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'flags' => ClassGenerator::FLAG_ABSTRACT,
'extendedClass' => 'ExtendedClassName',
'implementedInterfaces' => array('Iterator', 'Traversable'),
'properties' => array('foo',
array('name' => 'bar')
),
'methods' => array(
array('name' => 'baz')
),
));
$expectedOutput = <<<EOS
abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
{
public \$foo = null;
public \$bar = null;
public function baz()
{
}
}
EOS;
$output = $classGenerator->generate();
$this->assertEquals($expectedOutput, $output, $output);
}
/**
* @group ZF-7909
*/
public function testClassFromReflectionThatImplementsInterfaces()
{
$reflClass = new ClassReflection('ZendTest\Code\Generator\TestAsset\ClassWithInterface');
$classGenerator = ClassGenerator::fromReflection($reflClass);
$classGenerator->setSourceDirty(true);
$code = $classGenerator->generate();
$expectedClassDef = 'class ClassWithInterface'
. ' implements ZendTest\Code\Generator\TestAsset\OneInterface'
. ', ZendTest\Code\Generator\TestAsset\TwoInterface';
$this->assertContains($expectedClassDef, $code);
}
/**
* @group ZF-7909
*/
public function testClassFromReflectionDiscardParentImplementedInterfaces()
{
$reflClass = new ClassReflection('ZendTest\Code\Generator\TestAsset\NewClassWithInterface');
$classGenerator = ClassGenerator::fromReflection($reflClass);
$classGenerator->setSourceDirty(true);
$code = $classGenerator->generate();
$expectedClassDef = 'class NewClassWithInterface'
. ' extends ZendTest\Code\Generator\TestAsset\ClassWithInterface'
. ' implements ZendTest\Code\Generator\TestAsset\ThreeInterface';
$this->assertContains($expectedClassDef, $code);
}
/**
* @group ZF-9602
*/
public function testSetextendedclassShouldIgnoreEmptyClassnameOnGenerate()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass
->setName('MyClass')
->setExtendedClass('');
$expected = <<<CODE
class MyClass
{
}
CODE;
$this->assertEquals($expected, $classGeneratorClass->generate());
}
/**
* @group ZF-9602
*/
public function testSetextendedclassShouldNotIgnoreNonEmptyClassnameOnGenerate()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass
->setName('MyClass')
->setExtendedClass('ParentClass');
$expected = <<<CODE
class MyClass extends ParentClass
{
}
CODE;
$this->assertEquals($expected, $classGeneratorClass->generate());
}
/**
* @group namespace
*/
public function testCodeGenerationShouldTakeIntoAccountNamespacesFromReflection()
{
$reflClass = new ClassReflection('ZendTest\Code\Generator\TestAsset\ClassWithNamespace');
$classGenerator = ClassGenerator::fromReflection($reflClass);
$this->assertEquals('ZendTest\Code\Generator\TestAsset', $classGenerator->getNamespaceName());
$this->assertEquals('ClassWithNamespace', $classGenerator->getName());
$expected = <<<CODE
namespace ZendTest\Code\Generator\\TestAsset;
class ClassWithNamespace
{
}
CODE;
$received = $classGenerator->generate();
$this->assertEquals($expected, $received, $received);
}
/**
* @group namespace
*/
public function testSetNameShouldDetermineIfNamespaceSegmentIsPresent()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName('My\Namespaced\FunClass');
$this->assertEquals('My\Namespaced', $classGeneratorClass->getNamespaceName());
}
/**
* @group namespace
*/
public function testPassingANamespacedClassnameShouldGenerateANamespaceDeclaration()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName('My\Namespaced\FunClass');
$received = $classGeneratorClass->generate();
$this->assertContains('namespace My\Namespaced;', $received, $received);
}
/**
* @group namespace
*/
public function testPassingANamespacedClassnameShouldGenerateAClassnameWithoutItsNamespace()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName('My\Namespaced\FunClass');
$received = $classGeneratorClass->generate();
$this->assertContains('class FunClass', $received, $received);
}
/**
* @group ZF2-151
*/
public function testAddUses()
{
$classGenerator = new ClassGenerator();
$classGenerator->setName('My\Class');
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');
$generated = $classGenerator->generate();
$this->assertContains('use My\First\Use\Class;', $generated);
$this->assertContains('use My\Second\Use\Class as MyAlias;', $generated);
}
public function testCreateFromArrayWithDocBlockFromArray()
{
$classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'docblock' => array(
'shortdescription' => 'foo',
),
));
$docBlock = $classGenerator->getDocBlock();
$this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $docBlock);
}
public function testCreateFromArrayWithDocBlockInstance()
{
$classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'docblock' => new DocBlockGenerator('foo'),
));
$docBlock = $classGenerator->getDocBlock();
$this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $docBlock);
}
}
Jump to Line
Something went wrong with that request. Please try again.