Skip to content

Commit

Permalink
Filter test
Browse files Browse the repository at this point in the history
  • Loading branch information
zzgab committed Sep 10, 2015
1 parent 435765f commit 2b3cc69
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 130 deletions.
9 changes: 9 additions & 0 deletions test/FeedTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/**
* @author Gabriel Zerbib <gabriel@figdice.org>
* @copyright 2004-2015, Gabriel Zerbib.
* @version 2.1.2
* @package FigDice
*
* This file is part of FigDice.
* http://figdice.org/
*/

class FeedTest extends PHPUnit_Framework_TestCase
{
Expand Down
53 changes: 53 additions & 0 deletions test/FilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* @author Gabriel Zerbib <gabriel@figdice.org>
* @copyright 2004-2015, Gabriel Zerbib.
* @version 2.1.2
* @package FigDice
*
* This file is part of FigDice.
* http://figdice.org/
*/

use figdice\Filter;
use figdice\View;

class FilterTest extends PHPUnit_Framework_TestCase
{
public function testSpecialHTMLCharacterInFigTextIsNotAltered()
{
$view = new View();
$view->loadString(
'<test><span fig:text="/data"/></test>'
);
$view->mount('data', '<TAG>');
$this->assertEquals('<test><span><TAG></span></test>', $view->render());
}

public function testSpecialHTMLCharacterAfterFilter()
{
$view = new View();
$view->loadString(
'<test><span fig:text="/data" fig:filter="EscapeHtmlFilter"/></test>'
);
$view->mount('data', '<TAG>');
$this->assertEquals('<test><span>&lt;TAG&gt;</span></test>', $view->render());
}
}

class EscapeHtmlFilter implements Filter
{

/**
* Operates the transform on the input string,
* returns the filtered output.
* This test filter simply performs php's native HTML escapes.
*
* @param string $buffer
* @return string
*/
public function transform($buffer)
{
return htmlspecialchars($buffer);
}
}
260 changes: 130 additions & 130 deletions test/ViewParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,201 +28,201 @@
*/
class ViewParserTest extends PHPUnit_Framework_TestCase {

/**
* @expectedException \figdice\exceptions\XMLParsingException
*/
public function testRenderBeforeLoadFails() {
$view = new View();
$result = $view->render();
$this->assertFail();
}

public function testSourceWithSimpleXml() {
$view = new View();
$source = <<<ENDXML
/**
* @expectedException \figdice\exceptions\XMLParsingException
*/
public function testRenderBeforeLoadFails() {
$view = new View();
$result = $view->render();
$this->assertFail();
}

public function testSourceWithSimpleXml() {
$view = new View();
$source = <<<ENDXML
<xml></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml></xml>', $result);
}

public function testAutoClosingTagGetsSpaceBeforeSlash() {
$view = new View();
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml></xml>', $result);
}

public function testAutoClosingTagGetsSpaceBeforeSlash() {
$view = new View();
$source = <<<ENDXML
<xml><node attr="12"/></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node attr="12" /></xml>', $result);
}

public function testFigMuteRemovesTag() {
$view = new View();
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node attr="12" /></xml>', $result);
}

public function testFigMuteRemovesTag() {
$view = new View();
$source = <<<ENDXML
<xml><node fig:mute="true"/></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml></xml>', $result);
}

public function testFigTextStatic() {
$view = new View();
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml></xml>', $result);
}

public function testFigTextStatic() {
$view = new View();
$source = <<<ENDXML
<xml><node fig:text="12"/></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>12</node></xml>', $result);
}

public function testFigTextWithSimpleExpression() {
$view = new View();
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>12</node></xml>', $result);
}

public function testFigTextWithSimpleExpression() {
$view = new View();
$source = <<<ENDXML
<xml><node fig:text="12 + 3"/></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>15</node></xml>', $result);
}
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>15</node></xml>', $result);
}


public function testMountRoot() {
$view = new View();
$view->mount('someKey', '12');
$source = <<<ENDXML
public function testMountRoot() {
$view = new View();
$view->mount('someKey', '12');
$source = <<<ENDXML
<xml><node fig:text="/someKey + 4"/></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>16</node></xml>', $result);
}

public function testMountWithSubpath() {
$view = new View();
$view->mount('someKey', array( 'sub' => 'value') );
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>16</node></xml>', $result);
}

public function testMountWithSubpath() {
$view = new View();
$view->mount('someKey', array( 'sub' => 'value') );
$source = <<<ENDXML
<xml><node fig:text="'cool' + /someKey/sub" /></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>coolvalue</node></xml>', $result);
}

public function testMountWithRelativeAtRootContext() {
$view = new View();
$view->mount('someKey', array( 'sub' => 'value') );
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>coolvalue</node></xml>', $result);
}

public function testMountWithRelativeAtRootContext() {
$view = new View();
$view->mount('someKey', array( 'sub' => 'value') );
$source = <<<ENDXML
<xml><node fig:text="'cool' + someKey/sub" /></xml>
ENDXML;
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>coolvalue</node></xml>', $result);
}

public function testSimpleVoidTag() {
$view = new View();
$source = <<<ENDXML
$view->loadString($source);
$result = $view->render();
$this->assertEquals('<xml><node>coolvalue</node></xml>', $result);
}

public function testSimpleVoidTag() {
$view = new View();
$source = <<<ENDXML
<div>
<img src="image.jpg" fig:void="true"/>
</div>
ENDXML;

$view->loadString($source);
$result = $view->render();
$expected = <<<ENDHTML
$view->loadString($source);
$result = $view->render();
$expected = <<<ENDHTML
<div>
<img src="image.jpg">
</div>
ENDHTML;

$this->assertEquals($expected, $result);
}
$this->assertEquals($expected, $result);
}

public function testVoidTagWithInnerAttributes() {
$view = new View();
$source = <<<ENDXML
public function testVoidTagWithInnerAttributes() {
$view = new View();
$source = <<<ENDXML
<div>
<img src="image.jpg" fig:void="true">
<fig:attr name="border" value="1 + 1" />
</img>
</div>
ENDXML;

$view->loadString($source);
$result = $view->render();
$expected = <<<ENDHTML
$view->loadString($source);
$result = $view->render();
$expected = <<<ENDHTML
<div>
<img src="image.jpg" border="2">
</div>
ENDHTML;

$this->assertEquals($expected, $result);
}
$this->assertEquals($expected, $result);
}

public function testFigTagIsAlwaysMute() {
$view = new View();
$source = <<<ENDXML
public function testFigTagIsAlwaysMute() {
$view = new View();
$source = <<<ENDXML
<div>
<fig:sometag someattr="1">sometext</fig:sometag>
</div>
ENDXML;

$view->loadString($source);
$result = $view->render();
$expected = <<<ENDHTML
$view->loadString($source);
$result = $view->render();
$expected = <<<ENDHTML
<div>
sometext
</div>
ENDHTML;

$this->assertEquals($expected, $result);
}
$this->assertEquals($expected, $result);
}

/**
* @expectedException figdice\exceptions\FileNotFoundException
*/
public function testIncludeWithFileNotFoundThrowsException()
{
/**
* @expectedException figdice\exceptions\FileNotFoundException
*/
public function testIncludeWithFileNotFoundThrowsException()
{

$view = new View();
$view->loadFile('resources/FigXmlIncludeNotFound.xml');
$view = new View();
$view->loadFile('resources/FigXmlIncludeNotFound.xml');

// will raise an exception
$result = $view->render();
$this->assertFalse(true);
}
// will raise an exception
$result = $view->render();
$this->assertFalse(true);
}

public function testParseAfterRenderHasNoEffect() {
$view = new View();
$source = <<<ENDXML
public function testParseAfterRenderHasNoEffect() {
$view = new View();
$source = <<<ENDXML
<div>
</div>
ENDXML;
$view->loadString($source);
$output = $view->render();
$view->parse();
$this->assertEquals($output, $view->render());
}
$view->loadString($source);
$output = $view->render();
$view->parse();
$this->assertEquals($output, $view->render());
}

public function testAdHocEval()
public function testAdHocEval()
{
$view = new View();
$view->loadString('<xml attr="some {adhoc} here"></xml>');
$view->mount('adhoc', 'test');
$this->assertEquals('<xml attr="some test here"></xml>', $view->render());
}

public function testWalkOnNonCountableObjectRunsOnArrayWithObject()
{
$view = new View();
$view->loadString(
'<test fig:walk="/obj"><fig:mute fig:text="property" /></test>'
);
$obj = new stdClass();
$obj->property = 12;
$view->mount('obj', $obj);
$this->assertEquals('<test>12</test>', $view->render());
}
public function testWalkOnNonCountableObjectRunsOnArrayWithObject()
{
$view = new View();
$view->loadString(
'<test fig:walk="/obj"><fig:mute fig:text="property" /></test>'
);
$obj = new stdClass();
$obj->property = 12;
$view->mount('obj', $obj);
$this->assertEquals('<test>12</test>', $view->render());
}
}

0 comments on commit 2b3cc69

Please sign in to comment.