openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

commit 6037116938e4732b28eadf39463209986b6ab4c0
parent 79f67d098041c9548ed6afaf52349b8113b1fada
Author: Jan Dankert <develop@jandankert.de>
Date:   Thu, 10 Sep 2020 23:46:04 +0200

New: Simple Unit-Testing.

Diffstat:
dev-helper/test.php | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/security/test/PasswordTest.class.php | 21+++++++++++++++++++++
modules/util/test/MustacheTest.class.php | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/util/test/TestCase.class.php | 49+++++++++++++++++++++++++++++++++++++++++++++++++
modules/util/test/YAMLTest.class.php | 23+++++++++++++++++++++++
modules/util/text/variables/VariablesTest.class.php | 40++++++++++++++++++++++++++++------------
test/PasswordTest.php | 24------------------------
7 files changed, 262 insertions(+), 36 deletions(-)

diff --git a/dev-helper/test.php b/dev-helper/test.php @@ -0,0 +1,51 @@ +<?php + +echo "<html><body>"; + +ini_set('display_errors', 1); +ini_set('html_errors', 0); +error_reporting(E_ALL & ~E_NOTICE); + +require (__DIR__.'/../modules/autoload.php'); + +$tests = [ + new \util\text\variables\VariablesTest(), + new \util\test\YAMLTest(), + new \util\test\MustacheTest(), + new \security\test\PasswordTest() +]; + +echo '<h1>Running Tests</h1>'; + +$success = 0; +$failed = 0; + +foreach( $tests as $test ) { + $class = new ReflectionClass($test); + + echo '<h2>'.$class->getName().'</h2>'; + $methods = $class->getMethods(); + foreach( $methods as $method ) { + if ( strstr($method->getName(),'test') ) { + echo '<h3>'.$method->getName().'</h3>'; + try { + $method->invoke($test); + echo '<span style="color:green">OK</span>'; + $success++; + } + catch( \Exception $e ) { + echo '<strong style="color:red">Test failed: '.$e->getMessage().'</strong>'; + echo '<pre style="background-color:red">'.$e->getTraceAsString().'</pre>'; + $failed++; + } + } + } + + +} + +echo "<h1>Summary</h1>".($success+$failed).' Tests, '.$success.' success, '.$failed.' failed'; + +echo '<p><strong>'.(($failed==0)?"ALL TESTS OK":"THERE ARE TEST FAILURES").'</strong></p>'; + +echo "</body></html>";+ \ No newline at end of file diff --git a/modules/security/test/PasswordTest.class.php b/modules/security/test/PasswordTest.class.php @@ -0,0 +1,21 @@ +<?php + +namespace security\test; + +use security\Password; +use util\test\TestCase; + +class PasswordTest extends TestCase { + + public function testHash() { + $this->assertEquals("aadce520e20c2899f4ced228a79a3083",Password::hash("wtf",Password::ALGO_MD5)); + } + + + public function testCheck() { + + $this->assertEquals( true, Password::check("wtf",'$2y$10$LNY2qCb9elkMe/ITN09cB.6t5QqDzm9Uh9h/LV1I',Password::ALGO_CRYPT) ); + + $this->assertEquals( true,Password::check("wtf",'aadce520e20c2899f4ced228a79a3083',Password::ALGO_MD5)); + } +} diff --git a/modules/util/test/MustacheTest.class.php b/modules/util/test/MustacheTest.class.php @@ -0,0 +1,89 @@ +<?php + +namespace util\test; + +use util\Mustache; + +class MustacheTest extends TestCase { + + + public function testMustache() { + + $source = <<<SRC + Hello {{planet}}, {{& planet }}.{{! Simple example with a simple property }} + +{{#test}} + Yes, this is a {{test}}. {{! yes, it is}} +{{/test}} +{{^test}} +No, this is not a {{test}}. {{ ! will not be displayed, because test is not false }} +{{/test}} + +{{#car}} + My Car is {{color}}. {{! this is a property of the array car }} +It drives on {{& planet }}.{{! this property is inherited from the upper context }} +{{/}} + +{{#house}} + My House is {{size}}. {{! this property is read from an object }} +{{/}} {{! short closing tags are allowed }} + +Some names: +{{#names}} + my name is {{ name }}.{{! yes, spaces are allowed}} +{{/names}} + +{{#empty}} + this is not displayed {{! because the list is empty }} +{{/empty}} + +{{#upper}} + Hello again, {{planet}}. {{!displayed in uppercase}} +{{/}} + +<h1>Partials</h1> +{{> mycoolpartial}} + +<h1>Changing Delimiters</h1> +Default: {{name}} +{{=$( )=}} +Bash-Style: $(name) +Default should not work here: {{name}} + +$(={{ }}=) +Default again: {{name}} + +<h1>Dot notation</h1> + this will not work: {{building}} +but this is the color of the roof: {{building.roof.color}} + + +SRC; + + $m = new Mustache(); + $m->partialLoader = function($name) { + return "\nThis is a partial named ".$name.". It may include variables, like the name '{{name}}'.\n\n"; + }; + $m->parse( $source ); + + //echo 'Object: <pre><code>'; print_r($m); echo '</code></pre>'; + + $data = array( + 'planet' => '<b>world</b>', + 'test' => 'Test', + 'name' => 'Mallory', + 'car' => array('color'=>'red'), + 'house' => (object) array('size'=>'big' ), + 'names' => array( + array('name'=>'Alice'), + array('name'=>'Bob') + ), + 'empty' => array(), + 'upper' => static function($text) { return strtoupper($text); }, + 'building' => array('roof'=>array('color'=>'gray')) + + ); + + $this->assertNotEmpty( $m->render( $data ) ); + } +} diff --git a/modules/util/test/TestCase.class.php b/modules/util/test/TestCase.class.php @@ -0,0 +1,48 @@ +<?php + + +namespace util\test; + + +class TestCase +{ + public function run() { + + } + + + protected function assertEquals( $expected,$reality ) + { + if ( $expected != $reality ) + throw new \LogicException("Expected '$expected', but got '$reality'."); + } + + protected function assertNotEmpty( $reality ) + { + if ( empty( $reality ) ) + throw new \LogicException("Expected not empty, but got '$reality'."); + } + + protected function assertEmpty( $reality ) + { + if ( !empty( $reality ) ) + throw new \LogicException("Expected empty, but got '$reality'."); + } + + protected function assertFalse( $reality ) + { + if ( $reality ) + throw new \LogicException("Expected FALSE, but got '$reality'."); + } + + protected function assertTrue( $reality ) + { + if ( ! $reality ) + throw new \LogicException("Expected TRUE, but got '$reality'."); + } + + protected function fail() + { + throw new \LogicException("This should not happen."); + } +}+ \ No newline at end of file diff --git a/modules/util/test/YAMLTest.class.php b/modules/util/test/YAMLTest.class.php @@ -0,0 +1,23 @@ +<?php + +namespace util\test; + +use util\YAML; + +class YAMLTest extends TestCase { + + public function testYAMLParser() + { + + $yaml = <<<EOF +test : 'blabla' +a: '\\n\i\x\\nnux' +EOF; + + $arr = YAML::parse($yaml); + $this->assertEquals('blabla', $arr['test']); + } +} + + + diff --git a/modules/util/text/variables/VariablesTest.class.php b/modules/util/text/variables/VariablesTest.class.php @@ -1,17 +1,34 @@ <?php -require_once('../../../autoload.php'); +namespace util\text\variables; -header('Content-Type: text/plain'); -set_time_limit(2); +use util\test\TestCase; -$res = new \util\text\variables\VariableResolver(); +class VariablesTest extends TestCase { -$example = 'Hello ${planet:unknown planet}! Are you ok? My name is ${me.name:unnamed} and robots name is ${me.${nix.nada:name}}, i was born ${me.date:before some years}'; -#$example = 'Hello ${planet:unknown planet}! Are you ok? My name is ${me.name:unnamed}. I was born ${me.date:before some years}.'; -$res->addDefaultResolver( function($x) {return 'world';} ); -$res->addResolver('me', function($t) {if ($t == 'name') return 'alice';return '';}); + public function testResolver() { + $res = new \util\text\variables\VariableResolver(); + + $example = <<<SRC +Hello \${planet:unknown planet}! + +Are you ok? My name is \${me.name:unnamed} and robots name is \${me.\${nix.nada:name}}, i was born \${me.date:before some years}. +Message: \${message.somemessage:defaultMessage} +SRC; + + $res->addDefaultResolver( function($x) {return 'world';} ); + $res->addResolver('me', function($t) {if ($t == 'name') return 'alice';return '';}); + $res->addResolver('message', function($t) {return 'this is a message';}); + +// echo "Input:\n\n"; +// echo $example."\n\n"; +// +// echo "Output:\n\n"; +// echo $res->resolveVariables( $example )."\n\n"; +// +// echo "Resolver:\n\n"; + //print_r($res); + $this->assertNotEmpty($res->resolveVariables( $example ) ); + } +} -echo 'result: '.$res->resolveVariables( $example ); -echo "\n\n"; -print_r($res);- \ No newline at end of file diff --git a/test/PasswordTest.php b/test/PasswordTest.php @@ -1,23 +0,0 @@ -<?php - -include('../modules/security/Password.class.php'); -use security\Password; - -?> - -<p> -<?php -echo "Hash: ".Password::hash("wtf"); -?> -</p> - - -<p> -<?php -echo "Check: ".Password::check("wtf",'$2y$10$LNY2qCb9elkMe/ITN09cB.6t5QqDzm9Uh9h/LV1I'); - -echo "Check: ".Password::check("wtf",'aadce520e20c2899f4ced228a79a3083'); - - -?> -</p>- \ No newline at end of file