mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-04-02 06:14:51 -04:00
Fix: Pass parameter to generate() and add composite format tests
- Fixed bug where render() was not passing caller-supplied to generate(), causing ad-hoc tokens to be ignored - Added %F (yyyy-MM-dd) and %D (MM/dd/yy) composite date formats to the IntlDateFormatter pattern map - Added test coverage for composite date format directives (%F, %D, %T, %R)
This commit is contained in:
@@ -20,7 +20,9 @@ class Token_lib
|
||||
'%b' => 'MMM',
|
||||
'%B' => 'MMMM',
|
||||
'%d' => 'dd',
|
||||
'%D' => 'MM/dd/yy',
|
||||
'%e' => 'd',
|
||||
'%F' => 'yyyy-MM-dd',
|
||||
'%j' => 'D',
|
||||
'%m' => 'MM',
|
||||
'%U' => 'w',
|
||||
@@ -71,7 +73,7 @@ class Token_lib
|
||||
|
||||
$token_values = [];
|
||||
$tokens_to_replace = [];
|
||||
$this->generate($token_tree, $tokens_to_replace, $token_values, $save);
|
||||
$this->generate($token_tree, $tokens, $tokens_to_replace, $token_values, $save);
|
||||
|
||||
return str_replace($tokens_to_replace, $token_values, $tokened_text);
|
||||
}
|
||||
@@ -192,10 +194,10 @@ class Token_lib
|
||||
return $results;
|
||||
}
|
||||
|
||||
private function generate(array $used_tokens, array &$tokens_to_replace, array &$token_values, bool $save = true): void
|
||||
private function generate(array $used_tokens, array $tokens, array &$tokens_to_replace, array &$token_values, bool $save = true): void
|
||||
{
|
||||
foreach ($used_tokens as $token_code => $token_info) {
|
||||
$token_value = $this->resolve_token($token_code, [], $save);
|
||||
$token_value = $this->resolve_token($token_code, $tokens, $save);
|
||||
|
||||
foreach ($token_info as $length => $token_spec) {
|
||||
$tokens_to_replace[] = $token_spec;
|
||||
|
||||
@@ -228,4 +228,40 @@ class Token_libTest extends CIUnitTestCase
|
||||
$this->assertArrayHasKey('token1', $result);
|
||||
$this->assertArrayHasKey('token2', $result);
|
||||
}
|
||||
|
||||
public function testRenderReplacesCompositeDirectivePercentF(): void
|
||||
{
|
||||
$input = 'Date: %F';
|
||||
$result = $this->tokenLib->render($input, [], false);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertStringNotContainsString('%F', $result);
|
||||
$this->assertMatchesRegularExpression('/Date: \d{4}-\d{2}-\d{2}/', $result);
|
||||
}
|
||||
|
||||
public function testRenderReplacesCompositeDirectivePercentD(): void
|
||||
{
|
||||
$input = 'Date: %D';
|
||||
$result = $this->tokenLib->render($input, [], false);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertStringNotContainsString('%D', $result);
|
||||
$this->assertMatchesRegularExpression('/Date: \d{2}\/\d{2}\/\d{2}/', $result);
|
||||
}
|
||||
|
||||
public function testRenderHandlesPercentT(): void
|
||||
{
|
||||
$input = 'Time: %T';
|
||||
$result = $this->tokenLib->render($input, [], false);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertStringNotContainsString('%T', $result);
|
||||
$this->assertMatchesRegularExpression('/Time: \d{2}:\d{2}:\d{2}/', $result);
|
||||
}
|
||||
|
||||
public function testRenderHandlesPercentR(): void
|
||||
{
|
||||
$input = 'Time: %R';
|
||||
$result = $this->tokenLib->render($input, [], false);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertStringNotContainsString('%R', $result);
|
||||
$this->assertMatchesRegularExpression('/Time: \d{2}:\d{2}/', $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user