public function interact($template, $tplSource, $expected, $actual, $expectedFile, $help, $exceptionText) { while (true) { echo "Action (g/c/c!/r/d/do/de/ds/dc/dta/dtt/dd/dx/ge/ee/es/ir/v/q/?): "; $reply = strtolower(trim(fgets(STDIN))); if ($reply == "q") { exit(0); } elseif ($reply == "v") { echo "\n"; echo str_pad("Compile path: ", 17) . $template->configuration->compilePath . "\n"; echo str_pad("Template path: ", 17) . $template->configuration->templatePath . "\n"; echo str_pad("Source path: ", 17) . $tplSource . " \n"; echo str_pad("Expected output: ", 17) . $expectedFile . " \n"; echo "\n"; echo str_pad("Context: ", 17) . get_class($template->configuration->context); echo "\n"; continue; } elseif ($reply == "r") { $this->retryTest = true; return; } elseif ($reply == "g") { file_put_contents($expectedFile, $actual); return; } elseif ($reply == "do" || $reply == "de" || $reply == "dx" || $reply == "ds" || $reply == "dc" || $reply == "dta" || $reply == "dtt" || $reply == "dd" || $reply == "d") { $displayText = false; if ($reply == "ds" || $reply == "d") { if ($reply == "d") { $displayText .= "------Source template------\n"; } $displayText .= file_get_contents($tplSource); } if ($reply == "do" || $reply == "d") { if ($reply == "d") { $displayText .= "------Generated output------\n"; } $displayText .= $actual; } if ($reply == "de" || $reply == "d") { if ($reply == "d") { if (file_exists($expectedFile)) { $displayText .= "------Expected output------\n"; } else { $displayText .= "------Expected output (file missing)------\n"; } } $displayText .= $expected; } if ($reply == "dx" || $reply == "d") { if ($reply == "d") { $displayText .= "------Expection------\n"; } $displayText .= $exceptionText; } if ($reply == "dd" || $reply == "d") { if ($reply == "d") { $displayText .= "------Diff of output------\n"; } if (PHP_OS == 'Linux') { $expectedTmp = $this->getTempDir() . "/expected.tmp"; $actualTmp = $this->getTempDir() . "/actual.tmp"; file_put_contents($expectedTmp, $expected); file_put_contents($actualTmp, $actual); $displayText .= shell_exec("diff -U3 '{$expectedTmp}' '{$actualTmp}'"); unlink($expectedTmp); unlink($actualTmp); } else { } } if ($reply == "dc" || $reply == "d") { if (file_exists($template->compiledTemplatePath)) { if ($reply == "d") { $displayText .= "------Compiled PHP code------\n"; } $code = file_get_contents($template->compiledTemplatePath); $code = str_replace("<" . "?php", "", $code); $displayText .= str_replace("?" . ">", "", $code); } else { if ($reply == "d") { $displayText .= "------Compiled PHP code not found------\n"; } else { echo "The compiled file <" . $template->compiledTemplatePath . "> was not found\n"; echo "This usually means the template file contained syntax errors\n"; continue; } } } if ($reply == "dtt" || $reply == "d") { // NOTE: This currently fails due to missing implementations in the Tst class. if ($template->tstTree instanceof ezcTemplateTstNode) { if ($reply == "d") { $displayText .= "------TST------\n"; } $displayText .= ezcTemplateTstTreeOutput::output($template->tstTree); $displayText .= "\n"; } else { if ($reply == "d") { $displayText .= "------TST tree not available------\n"; } else { echo "The TST tree is not available\n"; continue; } } } if ($reply == "dta" || $reply == "d") { if ($template->astTree instanceof ezcTemplateAstNode) { if ($reply == "d") { $displayText .= "------AST------\n"; } $displayText .= ezcTemplateAstTreeOutput::output($template->astTree); } else { if ($reply == "d") { $displayText .= "------AST tree not available------\n"; } else { echo "The AST tree is not available\n"; continue; } } } if (PHP_OS == 'Linux') { // Pipe the text to less $l = popen("less", "w"); if ($l) { fwrite($l, $displayText); pclose($l); continue; } } echo $displayText, "\n"; continue; } elseif ($reply == 'c') { throw new PHPUnit_Framework_ExpectationFailedException($help); } elseif ($reply == 'c!') { self::$skipMissingTests = true; throw new PHPUnit_Framework_ExpectationFailedException($help); } elseif ($reply == "ir") { if (file_exists($tplSource . ".tmp")) { echo "Cannot rename the file to: {$tplSource}.tmp because the file already exists\n"; continue; } echo "renaming: " . $tplSource . " to: " . $tplSource . ".tmp\n"; rename($tplSource, $tplSource . ".tmp"); throw new PHPUnit_Framework_ExpectationFailedException($help); } elseif ($reply == "ge" || $reply == "ee") { $editor = isset($_ENV["EDITOR"]) && $_ENV["EDITOR"] != "" ? $_ENV["EDITOR"] : "vi"; if ($reply == "ge") { file_put_contents($expectedFile, $actual); } passthru($editor . " " . escapeshellcmd($expectedFile)); continue; } elseif ($reply == "es") { $editor = isset($_ENV["EDITOR"]) && $_ENV["EDITOR"] != "" ? $_ENV["EDITOR"] : "vi"; passthru($editor . " " . escapeshellcmd($tplSource)); continue; } elseif ($reply == '?') { echo "The actions are:\n", "g - Generate output file (Implies success of test)\n", "c - Continue, Skip the current test (Implies failure of test)\n", "c! - Continue, Skip all test with a missing out file.\n", "r - Retry the test\n", "do - Display the generated output\n", "de - Display the expected output\n", "dx - Display the exception\n", "ds - Display source template\n", "dc - Display generated/compiled PHP code\n", "d - Display all\n", "dta - Display the AST tree\n", "dtt - Display the TST tree\n", "dd - Display difference between generated output and expected output\n", "v - Display verbose template information\n", "q - Quit\n", "ir - Input Rename. Rename the input file so that it won't be available in the next run.\n", "ge - Generate and edit the expected output file.\n"; "ee - Edit the expected output file.\n"; "es - Edit the source file.\n"; continue; } else { continue; } return; // No more testing to be done now since the file is generated } }
/** * Test parsing the "if" block. */ public function testParsingIfBlock() { self::assertThat($this->templatePath . "if_test.tpl", self::existsOnDisk()); $text = file_get_contents($this->templatePath . "if_test.tpl"); $source = new ezcTemplateSourceCode('if_test.tpl', 'mock:if_test.tpl', $text); $parser = new MockElement_ezcTemplateParser($source, $this->manager); // $parser->debug = true; if ($parser->debug) { echo "\nforeach_test.tpl\n"; } $program = $parser->parseIntoNodeTree(); if ($parser->debug) { echo "\n\n", ezcTemplateTstTreeOutput::output($program), "\n"; } $parser->verify(); }