/** * @param ClassGenProperty $property */ public function add_property(ClassGenProperty $property) { // add property to the right array if ($property->isStatic) { $this->staticPropertiesArray[] = $property; } else { if ($property->is_const()) { $this->constantPropertiesArray[] = $property; } else { switch ($property->get_visibility()) { case 'public': $this->publicPropertiesArray[] = $property; break; case 'private': $this->privatePropertiesArray[] = $property; break; case 'protected': $this->protectedPropertiesArray[] = $property; break; default: break; } } } }
public function test_add_function() { $generator = new ClassGenGenerator($this->class, '/tmp/testClass.php'); $property = new ClassGenProperty('p1'); $property->set_public(); $generator->add_property($property); $function = new \Scoop\ClassGen\ClassGenFunction('testFunction', '', '$one = 1;'); $generator->add_function($function); $function = new \Scoop\ClassGen\ClassGenFunction('testFunction2', '', '$two = 2;'); $generator->add_function($function); $expected = '<?php class test { public $p1 = NULL; public function testFunction () { $one = 1; } public function testFunction2 () { $two = 2; } } ?>'; $this->assertEquals($expected, $generator->get_file_contents()); }
public function test_get_header() { $property = new ClassGenProperty('name'); $expected = ClassGenGenerator::$indentation . 'public $name = NULL;' . PHP_EOL; $this->assertEquals($expected, $property->get_header(), "empty properties should be null"); $property->set_value('value'); $expected = ClassGenGenerator::$indentation . 'public $name = \'value\';' . PHP_EOL; $this->assertEquals($expected, $property->get_header(), "property values should be assigned"); $property->set_static(); $expected = ClassGenGenerator::$indentation . 'public static $name = \'value\';' . PHP_EOL; $this->assertEquals($expected, $property->get_header(), "property should be static"); $property->set_const(); $expected = ClassGenGenerator::$indentation . 'const NAME = \'value\';' . PHP_EOL; $this->assertEquals($expected, $property->get_header(), "constants should be uppercased"); $property = new ClassGenProperty('name'); $property->set_value(['key' => 'value']); $expected = ClassGenGenerator::$indentation . 'public $name = ' . PHP_EOL . ClassGenGenerator::$indentation . ClassGenGenerator::$indentation . 'array (' . PHP_EOL . ClassGenGenerator::$indentation . ClassGenGenerator::$indentation . " 'key' => 'value'," . PHP_EOL . ClassGenGenerator::$indentation . ClassGenGenerator::$indentation . ");" . PHP_EOL; $this->assertEquals($expected, $property->get_header(), "arrays should be formatted properly"); }