TplBlock/test/TplBlockTest.php

188 lines
4.2 KiB
PHP
Raw Normal View History

2018-03-19 22:32:58 +01:00
<?php
/**
* Gniearks TplBlock unit tests.
*
* PHP version 5
*
* @category Template
* @package TplBlock
* @author gnieark <gnieark@tinad.fr>
* @license GNU General Public License V3
* @link https://github.com/gnieark/tplBlock/
*/
namespace TplBlockTest;
2018-03-19 22:32:58 +01:00
use PHPUnit\Framework\TestCase;
use TplBlock\TplBlock;
2018-03-19 22:32:58 +01:00
/**
* The TplBlockTest class.
*
* @category Template
* @package TplBlock
* @author gnieark <gnieark@tinad.fr>
* @license GNU General Public License V3
* @link https://github.com/gnieark/tplBlock/
*/
class TplBlockTest extends TestCase
{
2018-03-19 22:32:58 +01:00
/**
* A template cannot accept a sub template with no name.
*
* @return void
*
* @expectedException UnexpectedValueException
*/
public function testSendEmptyNameOnSubFunction()
{
$template = new TplBlock();
$subTemplate = new TplBlock();
$template->addSubBlock($subTemplate);
2018-03-19 22:32:58 +01:00
}
/**
* Verify that variable replacement takes place.
*
* @return void
*/
public function testSimpleVar()
{
$template = new TplBlock();
$variables = [
"name" => "Gnieark",
"title" => "Monsieur",
"firstname" => "Grouik",
];
$actual = $template
->addVars($variables)
->applyTplStr("Hello {{name}}");
$this->assertEquals("Hello Gnieark", $actual);
2018-03-19 22:32:58 +01:00
}
/**
* Test from a file.
*
* @return void
*/
public function testParseFromFile()
{
file_put_contents("temp.txt", "Hello {{name}}");
$template = new TplBlock();
$variables = [
"name" => "Gnieark",
"title" => "Monsieur",
"firstname" => "Grouik",
];
$actual = $template
->addVars($variables)
->applyTplFile("temp.txt");
$this->assertEquals("Hello Gnieark", $actual);
unlink("temp.txt");
2018-03-20 19:03:06 +01:00
}
/**
* Test blocs.
*
* @return void
*/
public function testBlocs()
{
$model = "
2018-03-20 19:03:06 +01:00
Bhah blah wpooie456
<!-- BEGIN bloc -->
have to be shown
<!-- END bloc -->
<!-- BEGIN blocTwo -->
WONT to be shown
<!-- END blocTwo -->
";
$template = new TplBlock();
$actual = $template
->addSubBlock(new TplBlock("bloc"))
->applyTplStr($model);
$this->assertContains("have", $actual);
2018-03-26 21:57:35 +02:00
$this->assertFalse(strpos("WONT", $actual));
}
/**
* Test blocs with tabs spaces etc..
*
* @return void
*/
public function testBlocsWithsWeirdSpaces()
{
$model = "
Bhah blah wpooie456
<!-- BEGIN bloc -->
have to be shown
<!-- END bloc -->
<!-- BEGIN blocTwo -->
WONT to be shown
<!-- END blocTwo -->
";
$template = new TplBlock();
$actual = $template
->addSubBlock(new TplBlock("bloc"))
->applyTplStr($model);
$this->assertContains("have", $actual);
$this->assertFalse(strpos("WONT", $actual));
2018-03-20 19:03:06 +01:00
}
2018-03-19 22:32:58 +01:00
2018-03-21 19:03:56 +01:00
/**
* Test if error on blocks names WTF.
*
* @return void
*
* @expectedException UnexpectedValueException
*/
public function testIfErrorOnForbiddenName()
{
new TplBlock("kjsd54 65");
2018-03-21 19:03:56 +01:00
}
/**
* Test if error on blocks names WTF.
*
* @return void
*
* @expectedException UnexpectedValueException
*/
public function testIfErrorOnForbiddenNameAgain()
{
new TplBlock("kjsd54.5");
2018-03-21 19:03:56 +01:00
}
2018-03-26 07:48:39 +02:00
public function testIfRemoveNonGivenVarsWorks(){
$tpl = new TplBlock();
$resultWithReplace = $tpl
->doReplaceNonGivenVars()
->applyTplStr("Hello {{name}}");
2018-03-26 07:48:39 +02:00
$resultWithoutReplace = $tpl
->dontReplaceNonGivenVars()
->applyTplStr("Hello {{name}}");
$this->assertContains("name",$resultWithoutReplace);
$this->assertFalse(strpos("name", $resultWithReplace));
}
}