Example #1
0
 public function testDictionaryLoadedFromIncludedTemplate()
 {
     $this->view->loadFile(__DIR__ . '/resources/DictionaryTestParent.xml');
     $this->view->setTranslationPath(__DIR__ . '/resources/dict');
     $this->view->setLanguage('fr');
     $output = trim($this->view->render());
     $output = preg_replace('#^[ \\t]+#m', '', $output);
     $output = preg_replace('#[ \\t]+$#m', '', $output);
     $output = preg_replace('#\\n+#', ';', $output);
     $this->assertEquals('key1-child;Ma chaƮne traduite;key1-parent;key1-parent', $output);
     // Now check that the key1-parent has been cached, because used twice
     // But because the $cache array is private in Dictionary class,
     // we use Reflection to break into it.
     $reflector = new ReflectionClass(get_class($dict = $this->view->getRootNode()->getCurrentFile()->getDictionary('inParent')));
     $cacheProp = $reflector->getProperty('cache');
     $cacheProp->setAccessible(true);
     $cache = $cacheProp->getValue($dict);
     // And I know for a fact that it translates twice the "key1" key.
     $this->assertEquals('key1-parent', $cache['key1']);
 }
Example #2
0
 public function testPlugInIncludedFileUsesParentViewOption()
 {
     // New behaviour for plugs: rendered in local context.
     $view = new View();
     $view->loadFile(__DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'slot-in-parent.xml');
     $output = trim($view->render());
     $this->assertEquals(11, $output);
     // Legacy behaviour: rendered in final global context
     // What we are trying to test is: the plug is defined in an included template, and yet its rendering process
     // checks properly that the top view has the GLOBAL_PLUGS defined.
     $view = new View([View::GLOBAL_PLUGS]);
     $view->loadFile(__DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'slot-in-parent.xml');
     $output = trim($view->render());
     $this->assertEquals(12, $output);
 }
Example #3
0
    public function testScriptTagWithoutContentInIncludedFile()
    {
        vfsStream::setup('root');
        $template = <<<TEMPLATE
<fig:template xmlns:fig="http://www.figdice.org">
  <link href="/assets/style.css" rel="stylesheet" />
  <fig:include file="inner.html" />
</fig:template>
TEMPLATE;
        vfsStream::newFile('outer.html')->withContent($template)->at(vfsStreamWrapper::getRoot());
        $template = <<<TEMPLATE
<fig:template>
  <script src="/assets/require.js"></script>
</fig:template>
TEMPLATE;
        $innerVFile = vfsStream::newFile('inner.html');
        $innerVFile->withContent($template)->at(vfsStreamWrapper::getRoot());
        $filename = vfsStream::url('root/outer.html');
        $view = new View();
        $view->loadFile($filename);
        $output = $view->render();
        $expected = <<<EXPECTED
  <link href="/assets/style.css" rel="stylesheet" />
  <script src="/assets/require.js"></script>
EXPECTED;
        $this->assertEquals(trim($expected), trim($output));
        // Now test by inverting the script and link, so that the script is no longer
        // the last tag in the template (Bolek's test)
        $template = <<<TEMPLATE
<fig:template>
  <script src="/assets/require.js"></script>
  <link href="/assets/style.css" rel="stylesheet" />
</fig:template>
TEMPLATE;
        $innerVFile->setContent($template);
        $expected = <<<EXPECTED
  <link href="/assets/style.css" rel="stylesheet" />
  <script src="/assets/require.js"></script>
  <link href="/assets/style.css" rel="stylesheet" />
EXPECTED;
        $view = new View();
        $view->loadFile($filename);
        $output = $view->render();
        $this->assertEquals(trim($expected), trim($output));
    }
Example #4
0
 public function testFigInclude()
 {
     $view = new View();
     $view->loadFile(__DIR__ . '/resources/FigXmlInclude1.xml');
     $output = $view->render();
     $expected = file_get_contents(__DIR__ . '/resources/FigXmlIncludeExpect.html');
     $this->assertEquals(trim($expected), trim($output));
 }
Example #5
0
 /**
  * Renders template from file to the ResponseInterface stream.
  *
  * @param ResponseInterface $response
  * @param $template
  * @param array $data
  * @return ResponseInterface
  */
 public function render(ResponseInterface $response, $template, array $data = array())
 {
     $this->view->loadFile($this->templatesPath . DIRECTORY_SEPARATOR . $template);
     $this->renderWithData($response, $data);
     return $response;
 }
Example #6
0
 /**
  * Creates a sub-view object, invokes its parsing phase,
  * and renders it as the child of the current tag.
  * @return string or false
  */
 private function fig_include()
 {
     //Extract from the attributes the file to include.
     if (!$this->hasAttribute('file')) {
         throw new RequiredAttributeException($this->name, $this->getCurrentFilename(), $this->getLineNumber(), 'Missing required attribute: "file" in tag: "' . $this->name . '"' . ' in file: ' . $this->getCurrentFilename() . '(' . $this->getLineNumber() . ')');
     }
     $file = $this->attributes['file'];
     //Create a sub-view, attached to the current element.
     $view = new View();
     $view->inherit($this);
     $view->loadFile(dirname($this->getCurrentFilename()) . '/' . $file, $this->getCurrentFile());
     //Make the current node aware that it is being rendered
     //as an include directive (therefore, it will be skipped
     //when the subview tries to render it).
     $this->bRendering = true;
     //Parse the subview (build its own tree).
     $view->parse();
     $result = $view->render();
     unset($this->bRendering);
     return $result;
 }
Example #7
0
 * @version 2.0.4
 * @package FigDice
 *
 * This file is part of FigDice.
 *
 * FigDice is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * FigDice is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FigDice.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * In this example we will learn to:
 *
 * - parse and use pieces of XML data.
 */
// Autoload the Figdice lib
require_once '../../vendor/autoload.php';
use figdice\View;
$view = new View();
// Well, everything is explained in the template.xml... Go see!
$view->loadFile('template.xml');
$output = $view->render();
echo $output;
Example #8
0
 public function testFeedWithoutFactoryWithAutoload()
 {
     $view = new View();
     $view->loadFile(__DIR__ . '/resources/FigXmlFeedAutoload.xml');
     $autoloadFuncrion = function ($className) {
         if ('some\\figdice\\test\\ns\\CustomAutoloadFeed' == $className) {
             require_once __DIR__ . '/resources/CustomAutoloadFeed.php';
         }
     };
     spl_autoload_register($autoloadFuncrion);
     $this->assertEquals(13, trim($view->render()));
     spl_autoload_unregister($autoloadFuncrion);
 }
Example #9
0
 /**
  * @expectedException figdice\exceptions\FileNotFoundException
  */
 public function testIncludeWithFileNotFoundThrowsException()
 {
     $view = new View();
     $view->loadFile('resources/FigXmlIncludeNotFound.xml');
     // will raise an exception
     $result = $view->render();
     $this->assertFalse(true);
 }
Example #10
0
 * - play with conditions
 * - play with complex attributes
 */
// Autoload the Figdice lib
require_once '../../vendor/autoload.php';
use figdice\View;
use figdice\exceptions\FileNotFoundException;
// Create a Fig View object
$view = new View();
// This time we do not load the outer page:
// rather, our template's "entry-point" is going to be an inner block.
// The inner block different on every URL of our site, but it sits at a
// specific location on the generic page, which remains (almost) identical
// on every URL.
// The inner template is responsible for loading its container.
try {
    $view->loadFile('template-inner.xml');
} catch (FileNotFoundException $ex) {
    die('template file not found');
}
// Mount some data into our View
// You can play with this true/false value and re-run the example,
// to see the difference in output.
$view->mount('isLogged', false);
// Render the template!
try {
    $output = $view->render();
} catch (FileNotFoundException $ex) {
    die('some include went wrong at rendering-time: ' . PHP_EOL . $ex->getMessage());
}
echo $output;
Example #11
0
    public function testVfsIncludeParentDir()
    {
        vfsStream::setup('root');
        $template = <<<ENDXML
<xml>
  <fig:include file="../someFile.xml" />
</xml>
ENDXML;
        vfsStream::newFile('template.xml')->at(vfsStream::newDirectory('subDir')->at(vfsStreamWrapper::getRoot()))->withContent($template);
        $template = <<<ENDXML
<included>
  Hello
</included>
ENDXML;
        vfsStream::newFile('someFile.xml')->at(vfsStreamWrapper::getRoot())->withContent($template);
        $filename = vfsStream::url('root/subDir/template.xml');
        $view = new View();
        $view->loadFile($filename);
        $output = $view->render();
        $expected = <<<ENDHTML
<xml>
  <included>
  Hello
</included>
</xml>
ENDHTML;
        $this->assertEquals($expected, $output);
    }