/**
 * A function for test of code generator.
 *
 * @param string $name class name
 */
function define_class_for_code_genration_test()
{
    $className = 'ExampleForCodeGenerationTest';
    $class = new Stagehand_PHP_Class($className);
    $publicProperty = new Stagehand_PHP_Class_Property('a');
    $protectedProperty = new Stagehand_PHP_Class_Property('b', 100);
    $privateProperty = new Stagehand_PHP_Class_Property('c', array(1, 3, 5));
    $staticProperty = new Stagehand_PHP_Class_Property('d', 'static');
    $protectedProperty->defineProtected();
    $privateProperty->definePrivate();
    $staticProperty->defineStatic();
    $argumentA = new Stagehand_PHP_Class_Method_Argument('a');
    $argumentB = new Stagehand_PHP_Class_Method_Argument('b');
    $argumentC = new Stagehand_PHP_Class_Method_Argument('c');
    $argumentD = new Stagehand_PHP_Class_Method_Argument('d');
    $argumentE = new Stagehand_PHP_Class_Method_Argument('e');
    $argumentF = new Stagehand_PHP_Class_Method_Argument('f');
    $argumentB->setTypeHinting('array');
    $argumentC->setRequirement(false);
    $argumentD->setRequirement(false);
    $argumentD->setValue('d');
    $argumentE->setRequirement(false);
    $argumentE->setValue(array(2, 4, 6));
    $argumentF->setReference();
    $publicMethod = new Stagehand_PHP_Class_Method('foo');
    $protectedMethod = new Stagehand_PHP_Class_Method('bar');
    $privateMethod = new Stagehand_PHP_Class_Method('baz');
    $staticMethod = new Stagehand_PHP_Class_Method('qux');
    $abstractMethod = new Stagehand_PHP_Class_Method('quux');
    $abstractStaticMethod = new Stagehand_PHP_Class_Method('corge');
    $finalMethod = new Stagehand_PHP_Class_Method('grault');
    $finalStaticMethod = new Stagehand_PHP_Class_Method('garply');
    $referenceMethod = new Stagehand_PHP_Class_Method('waldo');
    $protectedMethod->defineProtected();
    $protectedMethod->addArgument($argumentA);
    $protectedMethod->addArgument($argumentB);
    $protectedMethod->setCode('return true;');
    $privateMethod->definePrivate();
    $privateMethod->addArgument($argumentC);
    $privateMethod->addArgument($argumentD);
    $privateMethod->setCode('$c += 1;
return $d;');
    $staticMethod->defineStatic();
    $staticMethod->addArgument($argumentE);
    $abstractMethod->defineAbstract();
    $abstractStaticMethod->defineAbstract();
    $abstractStaticMethod->defineStatic();
    $finalMethod->defineFinal();
    $finalMethod->addArgument($argumentF);
    $finalStaticMethod->defineFinal();
    $finalStaticMethod->defineStatic();
    $referenceMethod->setReference();
    $constant1 = new Stagehand_PHP_Class_Constant('A');
    $constant2 = new Stagehand_PHP_Class_Constant('B', 10);
    $constant3 = new Stagehand_PHP_Class_Constant('C', 'text constant');
    $class->addProperty($publicProperty);
    $class->addProperty($protectedProperty);
    $class->addProperty($privateProperty);
    $class->addProperty($staticProperty);
    $class->addMethod($publicMethod);
    $class->addMethod($protectedMethod);
    $class->addMethod($privateMethod);
    $class->addMethod($staticMethod);
    $class->addMethod($abstractMethod);
    $class->addMethod($abstractStaticMethod);
    $class->addMethod($finalMethod);
    $class->addMethod($finalStaticMethod);
    $class->addMethod($referenceMethod);
    $class->addConstant($constant1);
    $class->addConstant($constant2);
    $class->addConstant($constant3);
    return $class;
}
Esempio n. 2
0
 /**
  * @test
  */
 public function setPropertyAndUse()
 {
     $className = 'ExampleForProperty';
     $class = new Stagehand_PHP_Class($className);
     $propertyA = new Stagehand_PHP_Class_Property('a', 100);
     $propertyB = new Stagehand_PHP_Class_Property('b');
     $propertyC = new Stagehand_PHP_Class_Property('c');
     $propertyD = new Stagehand_PHP_Class_Property('d', array(1, 3, 5));
     $propertyE = new Stagehand_PHP_Class_Property('e', 200);
     $propertyB->defineProtected();
     $propertyC->definePrivate();
     $propertyE->defineStatic();
     $class->addProperty($propertyA);
     $class->addProperty($propertyB);
     $class->addProperty($propertyC);
     $class->addProperty($propertyD);
     $class->addProperty($propertyE);
     $class->load();
     $instance = new $className();
     $this->assertObjectHasAttribute('a', $instance);
     $this->assertObjectHasAttribute('b', $instance);
     $this->assertObjectHasAttribute('c', $instance);
     $this->assertObjectHasAttribute('d', $instance);
     $this->assertObjectHasAttribute('e', $instance);
     $refClass = new ReflectionClass($className);
     $a = $refClass->getProperty('a');
     $b = $refClass->getProperty('b');
     $c = $refClass->getProperty('c');
     $this->assertTrue($a->isPublic());
     $this->assertTrue($b->isProtected());
     $this->assertTrue($c->isPrivate());
     $this->assertEquals($instance->a, 100);
     $this->assertEquals($instance->d, array(1, 3, 5));
     $this->assertEquals(ExampleForProperty::$e, 200);
 }
 /**
  * @test
  */
 public function renderProperty()
 {
     $name = 'example';
     $property = new Stagehand_PHP_Class_Property($name);
     $this->assertEquals($property->render(), 'public $example;');
     $property->defineProtected();
     $this->assertEquals($property->render(), 'protected $example;');
     $property->definePrivate();
     $property->setValue(10);
     $this->assertEquals($property->render(), 'private $example = 10;');
     $property->defineStatic();
     $this->assertEquals($property->render(), 'private static $example = 10;');
     $name = 'example';
     $property = new Stagehand_PHP_Class_Property($name);
     $property->setValue(true);
     $this->assertEquals($property->render(), 'public $example = true;');
     $property->setValue('true');
     $this->assertEquals($property->render(), "public \$example = 'true';");
     $property->setValue(false);
     $this->assertEquals($property->render(), 'public $example = false;');
     $property->setValue('false');
     $this->assertEquals($property->render(), "public \$example = 'false';");
 }