Skip to content

Commit

Permalink
Merge pull request #29 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Loading Dynamic PHP Pages as Virtual DOM
  • Loading branch information
usernane authored Feb 24, 2023
2 parents 1662756 + 3b9f988 commit 55279f3
Show file tree
Hide file tree
Showing 9 changed files with 1,013 additions and 823 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<file>./webfiori/ui/HTMLDoc.php</file>
<file>./webfiori/ui/Input.php</file>
<file>./webfiori/ui/HTMLTable.php</file>
<file>./webfiori/ui/TemplateCompiler.php</file>
</whitelist>
</filter>
<logging>
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@
require_once $libPath.'TableRow.php';
require_once $libPath.'HTMLTable.php';
require_once $libPath.'RadioGroup.php';
require_once $libPath.'TemplateCompiler.php';

18 changes: 18 additions & 0 deletions tests/test-templates/php-template-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div>
<?php
if (count($posts) != 0) {?>
<ul>
<?php
foreach ($posts as $postTitle) {?>
<li><?= $postTitle;?></li>
<?php
}
?>
</ul>
<?php
} else {
echo "No posts.\n";
}
?>
</div>

4 changes: 4 additions & 0 deletions tests/test-templates/php-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<?= 'This is a test on php';?>
</div>

7 changes: 7 additions & 0 deletions tests/test-templates/t00.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
*/

87 changes: 44 additions & 43 deletions tests/webfiori/test/ui/HTMLNodeTest.php

Large diffs are not rendered by default.

71 changes: 58 additions & 13 deletions tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
namespace webfiori\test\ui;

use webfiori\ui\HTMLNode;
use PHPUnit\Framework\TestCase;
use webfiori\ui\HeadNode;
use webfiori\ui\HTMLDoc;
use PHPUnit\Framework\TestCase;
use webfiori\ui\HTMLNode;
use webfiori\ui\TemplateCompiler;
/**
* Description of TestLoadTemplate
*
Expand All @@ -17,13 +18,14 @@ class LoadTemplateTest extends TestCase {
*/
public function test00() {
$this->expectException('Exception');
HTMLNode::loadComponent('');
$compiler = new TemplateCompiler('');
}
/**
* @test
*/
public function test01() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'t00.html');
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'t00.html');
$node = $compiler->getCompiled();
$this->assertTrue($node instanceof HTMLDoc);
$this->assertEquals(3, $node->getHeadNode()->childrenCount());
$this->assertEquals('TODO supply a title', $node->getHeadNode()->getTitle());
Expand All @@ -35,25 +37,27 @@ public function test01() {
* @test
*/
public function test03() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'vue-00.html', [
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'vue-00.html', [
'vue-src' => 'https://cdn.jsdelivr.net/npm/vue',
'php-message' => 'This is PHP var.'
]);
$node = $compiler->getCompiled();
$this->assertEquals('This is PHP var.', $node->getChildByID('php-message')->getChild(0)->getText());
$this->assertEquals('{{ message }}', $node->getChildByID('vue-message')->getChild(0)->getText());
}
/**
* @test
*/
public function test02() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'t01.html', [
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'t01.html', [
'title' => 'Users Status',
'desc' => 'A page that shows the status of users accounts.',
'top-paragraph' => 'All users.',
'username' => 'The username of the user.',
'email' => 'The email of the user.',
'ajax lib' => 'https://example.com/ajaxlib.js'
]);
$node = $compiler->getCompiled();
$this->assertTrue($node instanceof HTMLDoc);
$this->assertEquals(5, $node->getHeadNode()->childrenCount());
$this->assertEquals('https://example.com/ajaxlib.js', $node->getChildByID('my-script')->getAttribute('src'));
Expand All @@ -69,14 +73,51 @@ public function test02() {
* @test
*/
public function test04() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'super-00.html');
$this->assertEquals('object', gettype($node));
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'super-00.html');
$this->assertEquals('object', gettype($compiler->getCompiled()));
}
/**
* @test
*/
public function test05() {
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'php-template.php');
$node = $compiler->getCompiled();
$this->assertEquals("<div>\n This is a test on php</div>", $node->toHTML());
}
/**
* @test
*/
public function test06() {
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'php-template-2.php', [
'posts' => [
'One',
'Two',
'Three'
]]);
$this->assertEquals("<div>"
. "<ul>"
. "<li>One</li>"
. "<li>Two</li>"
. "<li>Three</li>"
. "</ul>"
. "</div>", $compiler->getCompiled()->toHTML());
}
/**
* @test
*/
public function test07() {
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'php-template-2.php', [
'posts' => []
]);
$node = $compiler->getCompiled();
$this->assertEquals("<div>\n No posts.\n</div>", $node->toHTML());
}
/**
* @test
*/
public function testHeadTemplate00() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'head-template-00.html');
$c = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'head-template-00.html');
$node = $c->getCompiled();
$this->assertTrue($node instanceof HeadNode);
$this->assertEquals(3, $node->childrenCount());
$this->assertEquals('TODO supply a title', $node->getPageTitle());
Expand All @@ -86,7 +127,8 @@ public function testHeadTemplate00() {
* @test
*/
public function testHeadTemplate01() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'head-template-01.html');
$c = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'head-template-01.html');
$node = $c->getCompiled();
$this->assertTrue($node instanceof HeadNode);
$this->assertEquals(3, $node->childrenCount());
$this->assertEquals('{{title}}', $node->getPageTitle());
Expand All @@ -96,9 +138,10 @@ public function testHeadTemplate01() {
* @test
*/
public function testHeadTemplate02() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'head-template-01.html', [
$c = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'head-template-01.html', [
'title' => 'This is page title.'
]);
$node = $c->getCompiled();
$this->assertTrue($node instanceof HeadNode);
$this->assertEquals(3, $node->childrenCount());
$this->assertEquals('This is page title.', $node->getPageTitle());
Expand All @@ -108,10 +151,11 @@ public function testHeadTemplate02() {
* @test
*/
public function testHeadTemplate03() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'head-template-02.html', [
$c = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'head-template-02.html', [
'title' => 'This is page title.',
'description'=>'This is the description of the page.'
]);
$node = $c->getCompiled();
$this->assertTrue($node instanceof HeadNode);
$this->assertEquals(4, $node->childrenCount());
$this->assertEquals('This is page title.', $node->getPageTitle());
Expand All @@ -122,7 +166,8 @@ public function testHeadTemplate03() {
* @test
*/
public function testHeadTemplate04() {
$node = HTMLNode::loadComponent(self::TEST_TEMPLATES_PATH.'head-template-03.html');
$c = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'head-template-03.html');
$node = $c->getCompiled();
$this->assertTrue($node instanceof HeadNode);
$this->assertEquals(6, $node->childrenCount());
$this->assertEquals('{{title}}', $node->getPageTitle());
Expand Down
Loading

0 comments on commit 55279f3

Please sign in to comment.