public function test_get_header() { $class = new ClassGenClass('testClass'); $expected = "<?php\n\nclass testClass {\n"; $this->assertEquals($expected, $class->get_header()); $class->set_namespace('testNamespace'); $expected = "<?php\n\nnamespace testNamespace;\n\nclass testClass {\n"; $this->assertEquals($expected, $class->get_header(), "namespace should be in the class header"); $expected = "<?php\n\nnamespace testNamespace;\n\nclass testClass extends testBaseClass {\n"; $class->set_extends('testBaseClass'); $this->assertEquals($expected, $class->get_header(), "class should extend another class"); $expected = "<?php\n\nnamespace testNamespace;\n\nclass testClass extends testBaseClass implements Interface1, Interface2, Interface3 {\n"; $class->set_implements(['Interface1', 'Interface2', 'Interface3']); $this->assertEquals($expected, $class->get_header(), "class should implement interfaces"); $expected = "<?php\n\nnamespace testNamespace;\n\n/**\n * Class testClass\n */\nclass testClass extends testBaseClass implements Interface1, Interface2, Interface3 {\n"; $class->set_phpDoc('/** * Class testClass */'); $this->assertEquals($expected, $class->get_header(), "class should have a phpdoc block"); $expected = "<?php\n\nnamespace testNamespace;\n\nuse someOtherTestClass;\nuse someTestClass;\n\n/**\n * Class testClass\n */\nclass testClass extends testBaseClass implements Interface1, Interface2, Interface3 {\n"; $class->set_use(['someTestClass', 'someOtherTestClass']); $this->assertEquals($expected, $class->get_header(), "class should use classes in alphabetical order"); $expected = "<?php\n\nnamespace testNamespace;\n\nuse someOtherTestClass;\nuse someTestClass;\n\n/**\n * Class testClass\n */\nabstract class testClass extends testBaseClass implements Interface1, Interface2, Interface3 {\n"; $class->set_abstract(); $this->assertEquals($expected, $class->get_header(), "class should be abstract"); $expected = "<?php\n\nnamespace testNamespace;\n\nuse someOtherTestClass;\nuse someTestClass;\n\n/**\n * Class testClass\n */\nfinal class testClass extends testBaseClass implements Interface1, Interface2, Interface3 {\n"; $class->set_abstract(false); $class->set_final(); $this->assertEquals($expected, $class->get_header(), "class should be final"); $this->expectException(\Exception::class); $class->set_abstract(); $class->get_header(); }
/** * @return string * @throws \Exception */ public function get_file_contents() : string { $classBody = ''; /* START PROPERTY GENERATION */ /** * @var $constantProperty ClassGenProperty */ // generate constants foreach ($this->constantPropertiesArray as $constantProperty) { $classBody .= $constantProperty->get(); } if (!empty($this->constantPropertiesArray)) { $classBody .= PHP_EOL; } /** * @var $staticProperty ClassGenProperty */ // generate static properties foreach ($this->staticPropertiesArray as $staticProperty) { $classBody .= $staticProperty->get(); } if (!empty($this->staticPropertiesArray)) { $classBody .= PHP_EOL; } /** * @var $publicProperty ClassGenProperty */ // generate public properties foreach ($this->publicPropertiesArray as $publicProperty) { $classBody .= $publicProperty->get(); } if (!empty($this->publicPropertiesArray)) { $classBody .= PHP_EOL; } /** * @var $protectedProperty ClassGenProperty */ // generate protected properties foreach ($this->protectedPropertiesArray as $protectedProperty) { $classBody .= $protectedProperty->get(); } if (!empty($this->protectedPropertiesArray)) { $classBody .= PHP_EOL; } /** * @var $privateProperty ClassGenProperty */ // generate private properties foreach ($this->privatePropertiesArray as $privateProperty) { $classBody .= $privateProperty->get(); } if (!empty($this->privatePropertiesArray)) { $classBody .= PHP_EOL; } $classBody = PHP_EOL . $classBody; foreach ($this->functionsArray as $function) { $functionBody = $function->get(); $functionBody = self::$indentation . preg_replace('/\\n/', PHP_EOL . self::$indentation, $functionBody); $classBody .= $functionBody . PHP_EOL . PHP_EOL; } return $this->class->get_header() . $classBody . $this->class->get_footer(); }