Add error checking directive and unit tests for it

This commit is contained in:
Kijin Sung 2023-10-18 00:29:32 +09:00
parent d4654eb5cf
commit 0f14ad8ccf
5 changed files with 98 additions and 0 deletions

View file

@ -0,0 +1,11 @@
<div class="alert">
<p>You have an error!</p>
<p><strong>[Ref. #2]</strong></p>
</div>
<div class="alert">
<p>You have an error!</p>
<p><strong>[Ref. #4]</strong></p>
</div>

View file

@ -0,0 +1,41 @@
@version(2)
<!--// No error yet -->
@error('foo/bar/baz/1')
<div class="alert">
<p>{$XE_VALIDATOR_MESSAGE}</p>
<p><strong>[Ref. #1]</strong></p>
</div>
@enderror
<!--// We set error ID and message now -->
{@ $XE_VALIDATOR_ID = 'foo/bar/baz/1'}
{@ $XE_VALIDATOR_MESSAGE = 'You have an error!'}
<!--// This should work -->
@error('foo/bar/baz/3', 'foo/bar/baz/2', 'foo/bar/baz/1')
<div class="alert">
<p>{$XE_VALIDATOR_MESSAGE}</p>
<p><strong>[Ref. #2]</strong></p>
</div>
@enderror
<!--// Wrong error ID -->
@error('foo/bar/baz/6', 'foo/bar/baz/5', 'foo/bar/baz/4')
<div class="alert">
<p>{$XE_VALIDATOR_MESSAGE}</p>
<p><strong>[Ref. #3]</strong></p>
</div>
@enderror
<!--// Check for any error, this should also work -->
@error
<div class="alert">
<p>{$XE_VALIDATOR_MESSAGE}</p>
<p><strong>[Ref. #4]</strong></p>
</div>
@enderror
<!--// Cleanup -->
{@ $XE_VALIDATOR_ID = null}
{@ $XE_VALIDATOR_MESSAGE = null}

View file

@ -723,6 +723,19 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$target = strtr($target, ['$UNIQ' => $tmpvar]);
$this->assertEquals($target, $parsed);
// @error
$source = implode("\n", [
"@error('email', 'login')",
'{{ $message }}',
'@enderror',
]);
$target = implode("\n", [
"<?php if (\$this->_v2_errorExists('email', 'login')): ?>",
"<?php echo htmlspecialchars(\$__Context->message ?? '', \ENT_QUOTES, 'UTF-8', false); ?>",
'<?php endif; ?>',
]);
$this->assertEquals($target, $this->_parse($source));
// @isset and @unset
$source = '<!--@isset($foo)--><!--@unset($bar)--><p></p><!--@end--><!--@endisset-->';
$target = '<?php if (isset($__Context->foo)): ?><?php if (!isset($__Context->bar)): ?><p></p><?php endif; ?><?php endif; ?>';
@ -1024,6 +1037,18 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$this->_normalizeWhitespace($expected),
$this->_normalizeWhitespace($executed_output)
);
// Validation error check
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2validation.html');
$tmpl->disableCache();
$executed_output = $tmpl->compile();
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2validation.executed.html', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2validation.executed.html');
$this->assertEquals(
$this->_normalizeWhitespace($expected),
$this->_normalizeWhitespace($executed_output)
);
}
/**