/** * Returns an instance of the class ezcTemplateTranslationConfiguration * * @return ezcTemplateTranslationConfiguration Instance of ezcTemplateTranslationConfiguration */ public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new ezcTemplateTranslationConfiguration(); } return self::$instance; }
/** * Compiles the string $string from the context $context with $arguments as variables into executable code. * * This static method translates a string, but inserts special code as * replacements for the variables. * * @param string $string * @param string $context * @param array(string=>mixed) $arguments * @return string */ public static function compile($string, $context, $arguments) { $ttc = ezcTemplateTranslationConfiguration::getInstance(); $ctxt = $ttc->manager->getContext($ttc->locale, $context); $translation = $ctxt->compileTranslation($string, $arguments); return $translation; }
public function testCompileWithWrongContext() { $ttc = ezcTemplateTranslationConfiguration::getInstance(); $ttc->manager = new ezcTranslationManager(new ezcTranslationTsBackend(dirname(__FILE__) . '/translations')); try { ezcTemplateTranslationProvider::compile("Test 3", "test2", array()); self::fail('Expected exception not thrown.'); } catch (ezcTranslationContextNotAvailableException $e) { self::assertEquals("The context 'test2' does not exist.", $e->getMessage()); } }
public function testIssetUnknownProperty() { $t = ezcTemplateTranslationConfiguration::getInstance(); self::assertEquals(false, isset($t->notHere)); }
public function testRunRegression($directory) { $template = new ezcTemplate(); $dir = dirname($directory); $base = basename($directory); $template->configuration = new ezcTemplateConfiguration($dir, $this->getTempDir()); $template->configuration->targetCharset = "Latin1"; $template->configuration->translation = ezcTemplateTranslationConfiguration::getInstance(); $template->configuration->translation->locale = 'en_us'; $backend = new ezcTranslationTsBackend(dirname(__FILE__) . '/translations'); $backend->setOptions(array('format' => '[LOCALE].xml')); $manager = new ezcTranslationManager($backend); $template->configuration->translation->manager = $manager; // $template->configuration->cachePath = $this->getTempDir() . "/cached"; // $template->configuration->cachePath = "/tmp/cache"; /* if ( !is_dir( $template->configuration->cachePath ) ) { mkdir( $template->configuration->cachePath); } */ $template->configuration->addExtension("TestBlocks"); $template->configuration->addExtension("LinksCustomBlock"); $template->configuration->addExtension("cblockTemplateExtension"); $template->configuration->addExtension("Sha1CustomBlock"); if (preg_match("#^(\\w+)@(\\w+)\\..*\$#", $base, $match)) { $contextClass = "ezcTemplate" . ucfirst(strtolower($match[2])) . "Context"; $template->configuration->context = new $contextClass(); } else { $template->configuration->context = new ezcTemplateNoContext(); } $send = substr($directory, 0, -3) . ".send"; if (file_exists($send)) { $template->send = (include $send); } $out = ""; $exceptionText = ""; try { $out = $template->process($base); } catch (Exception $e) { $out = $e->getMessage(); $exceptionText = get_class($e) . "(" . $e->getCode() . "):\n" . $out; // Begin of the error message contains the full path. We replace this with 'mock' so that the // tests work on other systems as well. if (strncmp($out, $directory, strlen($directory)) == 0) { $out = "mock" . substr($out, strlen($directory)); } $exceptionText .= "\n" . $e->getTraceAsString(); } $expected = substr($directory, 0, -3) . ".out"; if (!file_exists($expected)) { $help = "The out file: '{$expected}' could not be found."; if (!self::$skipMissingTests && $this->interactiveMode) { echo "\n", $help, "\n"; self::interact($template, $directory, false, $out, $expected, $help, $exceptionText); return; } else { throw new PHPUnit_Framework_ExpectationFailedException($help); } } $expectedText = file_get_contents($expected); try { $this->assertEquals($expectedText, $out, "In: <{$expected}>\nOut: <{$directory}>"); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $help = "The evaluated template <" . $this->regressionDir . "/current.tmp> differs "; $help .= "from the expected output: <{$expected}>."; if ($this->interactiveMode) { // Touch the file. It will be run first, next time. if (isset($_ENV['EZC_TEST_TEMPLATE_SORT']) && $_ENV['EZC_TEST_TEMPLATE_SORT'] == 'mtime') { touch($directory); } $exceptionText = get_class($e) . "(" . $e->getCode() . "):\n" . $e->getMessage(); $exceptionText .= "\n" . $e->getTraceAsString(); echo "\n", $help, "\n"; self::interact($template, $directory, file_get_contents($expected), $out, $expected, $help, $exceptionText); return; } // Rethrow with new and more detailed message throw new PHPUnit_Framework_ExpectationFailedException($help, $e->getComparisonFailure()); } // check the receive variables. $receive = substr($directory, 0, -3) . ".receive"; if (file_exists($receive)) { $expectedVar = (include $receive); $foundVar = $template->receive; $this->assertEquals($expectedVar, $foundVar, "Received variables does not match"); } }