/** * Generate the body part of this method. * * @return string The body or an empty string if no body is set. */ private function generateBody() { if (!$this->body) { return ''; } $lineFeed = self::getConfig()->getNewline(); $body = $lineFeed; $body .= rtrim($this->body->generate(), $lineFeed); $body .= $lineFeed; $body = $this->indent($body); return $body; }
/** * Test if can generate raw code. */ public function testGenerateRawCode() { $file = Bootstrap::$resourceDir . '/manitou/generators/php/rawcode.txt'; $expected = $this->getFileContent($file); $code = new PHPRawCode(); $code->setCode('<?php'); $code->addLine(); $code->openScope('class Test extends AbstractTest {'); $code->addLine(); $code->addLine('private $rows = 10;'); $code->addLine(); $code->openScope('public function __construct() {'); $code->addLine(); $code->openScope('for ($i = 0; $i < $this->rows; $i++) {'); $code->addLine('echo $i;'); $code->closeScope('}'); $code->addLine(); $code->openScope('if (true) {'); $code->addLine(); $code->openScope('} else if (false) {', true); $code->addLine(); $code->closeScope('} else {', true); $code->addLine(); $code->closeScope('}'); $code->closeScope('}'); $code->closeScope('}'); $code->addLine(); $this->assertEquals($expected, $code->generate()); }
/** * Test if can generate a class method with body. */ public function testGenerateClassMethodWithBody() { $file = Bootstrap::$resourceDir . '/manitou/generators/php/method_class_body.txt'; $expected = $this->getFileContent($file); $body = new PHPRawCode(); $body->openScope('for ($i = 0; $i < 10; $i++) {'); $body->addLine('echo $i;'); $body->closeScope('}'); $body->addLine(); $body->addLine('return true;'); $method = new PHPMethod('test'); $method->setBody($body); $this->assertEquals($expected, $method->generate()); }