Add phpunit test to add all additions to smarty

This commit is contained in:
2024-12-27 10:30:27 +09:00
parent fd369a7115
commit 39bfcecd3f
9 changed files with 328 additions and 0 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
vendor
composer.lock
.phpunit.cache/
tools-libs/

4
.phive/phars.xml Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="^10.3.5" installed="10.3.5" location="./tools/phpunit" copy="false"/>
</phive>

View File

@@ -47,6 +47,33 @@ Alternative setup composer local zip file repot:
1) Update and leep the label/pos changes
5) Create new release version as official relase number
## Test
After any update run `tools/phpunit` to test the compare output, also a visual check can be done by accessing the `test/` folder
- Translations should work
- pos/label names for checkbox/options should work
- get var output should work
- plugin test load should work
### For intelephense users when phpunit was installed as phar
Intelephense cannot directly access the phar file, if phpunit was installed as a phar file (eg via phive) then the following commands must be used to setup the intelephense parser
In the base folder:
```sh
php -r "(new Phar('/path/to/phive/folder/.phive/phars/phpunit-10.3.5.phar'))->extractTo('tools-libs/phpunit');"
```
Then open the vscode settings and set for the Folder (if multiple folders are in the workspace) or the Workspace the following setting
```txt
"intelephense.environment.includePaths": [
"/tools-libs/phpunit/"
]
```
## Updated files (different from master)
### New

14
phpunit.xml Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
bootstrap="test/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="deploy">
<directory>test</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -154,6 +154,7 @@ class Template extends TemplateBase {
if ($this->smarty->debugging) {
$this->smarty->getDebug()->start_template($this, $display);
}
// exit;
// checks if template exists
if ($this->compile_check && !$this->getSource()->exists) {
throw new Exception(

258
test/SmartyExtendedTest.php Normal file
View File

@@ -0,0 +1,258 @@
<?php
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
/**
* Test class for Smarty
* @coversDefaultClass \Smarty
* @testdox \Smarty method tests for extended smarty plugins
*/
final class SmartyExtendedTest extends TestCase
{
/**
* Undocumented function
*
* @testdox Output\Form\Elements Class tests
*
* @return void
*/
public function testSmartyExtended()
{
define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR);
$locale = 'ja_JP.UTF-8';
$domain = 'frontend';
$locale_path = BASE . 'includes/locale/';
$encoding = 'UTF-8';
$l10n = new \CoreLibs\Language\L10n(
$locale,
$domain,
$locale_path,
$encoding
);
$locale = $l10n->getLocaleAsArray();
\CoreLibs\Language\L10n::loadFunctions();
_setlocale(LC_MESSAGES, $locale['locale']);
_textdomain($locale['domain']);
_bindtextdomain($locale['domain'], $locale['path']);
_bind_textdomain_codeset($locale['domain'], $locale['encoding']);
$template_name = "test.full.tpl";
$CACHE_ID = 'SmartyV5PHPUnit';
$COMPILE_ID = 'SmartyV5PHPUnit';
$CONTENT_DATA = [
'BASE' => BASE,
// 'TEST_INSTALL' => $smarty->testInstall(),
'HTML_TITLE' => 'Smarty v5 tst',
// smarty test
'SMARTY_TEST' => 'Test Data',
'TRANSLATE_TEST' => $l10n->__('Are we translated?'),
'TRANSLATE_TEST_FUNCTION' => _gettext('Are we translated?'),
'TRANSLATE_TEST_SMARTY' => $l10n->__('Are we translated?'),
'replace' => 'Replaced',
// variable variables
'test' => 'foo',
'foo' => 'bar',
// loop
'loop_start' => 5,
// drop down test with optgroups
'drop_down_test' => [
'foo' => 'Foo',
'bar' => 'Bar',
'foobar' => 'Foo Bar',
],
'drop_down_test_selected' => 'bar',
'drop_down_test_nested' => [
'' => '選択してください',
'4/25(木)' => [
'4/25(木) 11:00-11:50' => '4/25(木) 11:00-11:50',
'4/25(木) 12:20-13:00' => '4/25(木) 12:20-13:00'
],
'4/26(金)' => [
'4/26(金) 11:00-11:50' => '4/26(金) 11:00-11:50',
'4/26(金) 12:20-13:00' => '4/26(金) 12:20-13:00'
],
'4/27(土)' => [
'4/27(土) 11:00-11:50' => '4/27(土) 11:00-11:50',
'4/27(土) 12:20-13:00' => '4/27(土) 12:20-13:00'
],
],
'drop_down_test_nested_selected' => '',
'radio_test' => [
'0' => 'On',
'1' => 'Off',
'-1' => 'Undefined'
],
'radio_test_selected' => -1,
'checkbox_test' => [
'0' => 'On',
'1' => 'Off',
'-1' => 'Undefined'
],
'checkbox_test_pos' => [
'0' => 'A',
'1' => 'B'
],
'checkbox_test_selected' => ['1', '-1'],
'checkbox_test_pos_selected' => ['0', '-1'],
];
$smarty = new \Smarty\Smarty();
$smarty->setTemplateDir(BASE . 'includes/templates/frontend');
$smarty->setConfigDir(BASE . 'configs');
$smarty->setCompileDir(BASE . 'templates_c');
$smarty->setCacheDir(BASE . 'cache');
$smarty->setEscapeHtml(true);
// get var test
$smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'getvar', [&$smarty, 'getTemplateVars']);
// for testing plugin tyoes
require 'plugins/block.t.php';
$smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'tp', 'smarty_block_t');
require 'plugins/function.popup_init.php';
require 'plugins/function.popup.php';
$smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'popinit', 'smarty_function_popup_init');
$smarty->registerPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, 'pop', 'smarty_function_popup');
foreach ($CONTENT_DATA as $key => $value) {
$smarty->assign($key, $value);
}
// the data as it should look like
// base created by running the index.php and copy the data from the tmp folder for the template
// eg "test.full.tpl" is "tmp/test.full.tpl.html"
$compare_smarty_compiled = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Smarty v5 tst</title>
<meta http-equiv="Content-Type" content="text/html; charset=">
<script type="text/javascript" language="JavaScript" src="./js/overlib/overlib.js"></script>
</head>
<body>
BASE: /storage/var/www/html/developers/clemens/core_data/composer-packages/Smarty-Extended/test/<br>
<hr>
<div>
SMARTY_TEST: Test Data
</div>
<div>
TEST INSTALL
</div>
<div>
MERGE DATA:
</div>
<div onmouseover="return overlib('Text block<br>Control<br>SMARTY 5',WIDTH,250,CAPTION,'Info v5',BGCOLOR,'#ece86d',FGCOLOR,'#ecb96d',CAPCOLOR,'black');" onmouseout="nd();">This is a test for Smarty 5</div>
<div onmouseover="return overlib('Text block<br>Control',WIDTH,250,CAPTION,'Info');" onmouseout="nd();" style="border: 1px solid black; margin: 5px 0 5px 0; padding: 5px;">
POPUP HERE (hover mouse)
</div>
<div onmouseover="return overlib('Text block<br>Control<br>Plugin',WIDTH,250,CAPTION,'Info');" onmouseout="nd();" style="border: 1px solid black; margin: 5px 0 5px 0; padding: 5px;">
POPUP PLUGIN HERE (hover mouse)
</div>
<div>
<b>Outside translation test</b><br>
TRANSLATION CLASS (OUT): Are we translated?<br>
TRANSLATION CLASS (OUT FUNCTION): Are we translated?<br>
TRANSLATION CLASS (SMARTY): Are we translated?<br>
</div>
<div>
<b>Translate Test with replace:</b><br>
ORIGINAL: Original with string: %1 (Replaced)<br>
TRANSLATED: Translated with string: Replaced<br>
TRANSLATED (escape): Translated with string: on<br>
Capture test: INPUT TEST<br>
Plural test 0: multi<br>
Plural test 1: single<br>
Plural test 2: multi<br>
</div>
<div>
<b>Plugin Test:</b><br>
ORIGINAL: Original with string: %1 (Replaced)<br>
TRANSLATED: Translated with string: Replaced<br>
</div>
<div>
<b>Variable variables:</b><br>
Test: foo<br>
Foo: bar<br>
vFoo (\$test = \$foo = bar): bar<br>
vFoo (\$bar = \$test = foo): foo
</div>
<div class="jq-container">
<div id="jq-test" class="jp-test">
<div id="test-div" class="test-div">
Some content here or asdfasdfasf
</div>
<div id="translate-div">
TRANSLATION SMARTY: I should be translated </div>
</div>
</div>
<div class="loop-test">
<div><b>LOOP TEST</b></div>
<div>LOOP OUTPUT: 1</div>
<div>LOOP OUTPUT: 2</div>
<div>LOOP OUTPUT: 3</div>
<div>LOOP OUTPUT: 4</div>
<div>LOOP OUTPUT: 5</div>
</div>
<div>
<select id="drop_down_test" name="drop_down_test">
<option label="Foo" value="foo">Foo</option>
<option label="Bar" value="bar" selected="selected">Bar</option>
<option label="Foo Bar" value="foobar">Foo Bar</option>
</select>
</div>
<div>
<select id="drop_down_test_nested" name="drop_down_test_nested">
<option label="選択してください" value="" selected="selected">選択してください</option>
<optgroup label="4/25(木)">
<option label="4/25(木) 11:00-11:50" value="4/25(木) 11:00-11:50">4/25(木) 11:00-11:50</option>
<option label="4/25(木) 12:20-13:00" value="4/25(木) 12:20-13:00">4/25(木) 12:20-13:00</option>
</optgroup>
<optgroup label="4/26(金)">
<option label="4/26(金) 11:00-11:50" value="4/26(金) 11:00-11:50">4/26(金) 11:00-11:50</option>
<option label="4/26(金) 12:20-13:00" value="4/26(金) 12:20-13:00">4/26(金) 12:20-13:00</option>
</optgroup>
<optgroup label="4/27(土)">
<option label="4/27(土) 11:00-11:50" value="4/27(土) 11:00-11:50">4/27(土) 11:00-11:50</option>
<option label="4/27(土) 12:20-13:00" value="4/27(土) 12:20-13:00">4/27(土) 12:20-13:00</option>
</optgroup>
</select>
</div>
<div>
<label><input type="radio" name="radio_test" value="0" />On</label>
<label><input type="radio" name="radio_test" value="1" />Off</label>
<label><input type="radio" name="radio_test" value="-1" checked="checked" />Undefined</label>
</div>
<div>
<label><input type="checkbox" name="checkbox_test[]" value="0" />On</label>
<label><input type="checkbox" name="checkbox_test[]" value="1" checked="checked" />Off</label>
<label><input type="checkbox" name="checkbox_test[]" value="-1" checked="checked" />Undefined</label>
</div>
<div>
<label><input type="checkbox" name="checkbox_test_pos[]" value="0" checked="checked" />On</label>
<label><input type="checkbox" name="checkbox_test_pos[]" value="1" />Off</label>
<label><input type="checkbox" name="checkbox_test_pos[]" value="-1" checked="checked" />Undefined</label>
</div>
<div>
</div>
</body>
</html>
HTML;
$this->assertEquals(
$compare_smarty_compiled,
trim($smarty->fetch(
$template_name,
$CACHE_ID,
$COMPILE_ID
))
);
}
}
// __END__

15
test/bootstrap.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
$set = 0;
foreach (['', '/../..', '/..', '/../../src', '/../src'] as $src) {
if (is_file(dirname(__DIR__) . $src . '/vendor/autoload.php')) {
require dirname(__DIR__) . $src . '/vendor/autoload.php';
$set = 1;
break;
}
}
if (!$set) {
die("Cannot find /vendor/autoload.php in reference to: " . dirname(__DIR__) . "\n");
}
// __END__

View File

@@ -129,6 +129,12 @@ foreach ($CONTENT_DATA as $key => $value) {
$smarty->assign($key, $value);
}
file_put_contents('tmp/' . $template_name . '.html', $smarty->fetch(
$template_name,
$CACHE_ID,
$COMPILE_ID
));
// write to display
$smarty->display(
$template_name,
$CACHE_ID,

1
tools/phpunit Symbolic link
View File

@@ -0,0 +1 @@
/home/clemens/.phive/phars/phpunit-10.3.5.phar