mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-10 12:32:14 +09:00
Add error checking directive and unit tests for it
This commit is contained in:
parent
d4654eb5cf
commit
0f14ad8ccf
5 changed files with 98 additions and 0 deletions
|
|
@ -751,4 +751,21 @@ class Template
|
||||||
default: return $grant->$type ?? false;
|
default: return $grant->$type ?? false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a validation error exists for v2.
|
||||||
|
*
|
||||||
|
* @param ...$args
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function _v2_errorExists(...$args): bool
|
||||||
|
{
|
||||||
|
$validator_id = \Context::get('XE_VALIDATOR_ID');
|
||||||
|
$validator_message = \Context::get('XE_VALIDATOR_MESSAGE');
|
||||||
|
if (empty($validator_id) || empty($validator_message))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return count($args) ? in_array((string)$validator_id, $args, true) : true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,10 @@ class TemplateParser_v2
|
||||||
'ob_start(); $__last_fragment_name = %s;',
|
'ob_start(); $__last_fragment_name = %s;',
|
||||||
"\$this->_fragments[\$__last_fragment_name] = ob_get_flush();",
|
"\$this->_fragments[\$__last_fragment_name] = ob_get_flush();",
|
||||||
],
|
],
|
||||||
|
'error' => [
|
||||||
|
'if ($this->_v2_errorExists(%s)):',
|
||||||
|
'endif;',
|
||||||
|
],
|
||||||
'isset' => ['if (isset(%s)):', 'endif;'],
|
'isset' => ['if (isset(%s)):', 'endif;'],
|
||||||
'unset' => ['if (!isset(%s)):', 'endif;'],
|
'unset' => ['if (!isset(%s)):', 'endif;'],
|
||||||
'empty' => ['if (empty(%s)):', 'endif;'],
|
'empty' => ['if (empty(%s)):', 'endif;'],
|
||||||
|
|
|
||||||
11
tests/_data/template/v2validation.executed.html
Normal file
11
tests/_data/template/v2validation.executed.html
Normal 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>
|
||||||
|
|
||||||
41
tests/_data/template/v2validation.html
Normal file
41
tests/_data/template/v2validation.html
Normal 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}
|
||||||
|
|
@ -723,6 +723,19 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
|
||||||
$target = strtr($target, ['$UNIQ' => $tmpvar]);
|
$target = strtr($target, ['$UNIQ' => $tmpvar]);
|
||||||
$this->assertEquals($target, $parsed);
|
$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
|
// @isset and @unset
|
||||||
$source = '<!--@isset($foo)--><!--@unset($bar)--><p></p><!--@end--><!--@endisset-->';
|
$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; ?>';
|
$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($expected),
|
||||||
$this->_normalizeWhitespace($executed_output)
|
$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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue