unit tests

This commit is contained in:
Gnieark 2018-03-19 22:32:58 +01:00
parent 132f169963
commit c330732c1b
3 changed files with 89 additions and 0 deletions

47
sample/sample.php Normal file
View File

@ -0,0 +1,47 @@
<?php
include("../class.TplBlock.php");
$tpl = new TplBlock();
//simples vars
$tpl->add_vars(array(
"name" => "Gnieark",
"title" => "Monsieur",
"firstname" => "Grouik"
)
);
$primes = array(1,2,3,5,7,11);
// a sub bloc
foreach($primes as $prime){
$tplPrime = new TplBlock('primes');
$tplPrime->add_vars(array('number' => $prime));
$tpl->add_sub_block($tplPrime);
}
// test sub - sub blocs
for ($i = 2; $i < 121; $i++){
$tplNumber = new TplBlock('number');
$tplNumber->add_vars( array("value" => $i));
$index = 1;
$number = $i;
while ( $number > 1 && $index < count($primes)){
if($number % $primes[$index] == 0){
$number = $number / $primes[$index];
$tplDivisor = new TplBlock("divisor");
$tplDivisor->add_vars( array("value" => $primes[$index]));
$tplNumber->add_sub_block($tplDivisor);
}else{
$index++;
}
}
$tpl->add_sub_block($tplNumber);
}
echo $tpl->apply_tpl_file("tpl.txt",true);

15
sample/tpl.txt Normal file
View File

@ -0,0 +1,15 @@
hello {{title}} {{name}} {{firstname}}
The first primes numbers are:
<!-- BEGIN primes -->
- {{primes.number}}
<!-- END primes -->
Lets list all divisors of:
<!-- BEGIN number -->
{{number.value}} Have those divisors:
<!-- BEGIN number.divisor -->
- {{number.divisor.value}}
<!-- END number.divisor -->
<!-- END number -->

27
test/TplBlockTest.php Normal file
View File

@ -0,0 +1,27 @@
<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__.'/../class.TplBlock.php';
class TplBlockTest extends TestCase{
/**
* @expectedException InvalidTemplateException
*/
public function testSendEmptyNameOnSubFunction(){
$tpl = new TplBlock();
$subTpl = new TplBlock();
$tpl->add_sub_block($subTpl);
}
public function testsimpleVar(){
$tpl = new TplBlock();
$tpl->add_vars(array(
"name" => "Gnieark",
"title" => "Monsieur",
"firstname" => "Grouik"
)
);
$this->assertEquals("Hello Gnieark", $tpl->apply_tpl_str("Hello {{name}}"));
}
}