Skip to content

Commit 91a71ee

Browse files
committed
#16 - Unit tested
1 parent f26733d commit 91a71ee

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ composer.phar
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock
77
/composer.lock
8+
/nbproject/private/

src/TgUtils/Templating/DateFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function format($value, $params, Processor $processor) {
2121
switch ($params[0]) {
2222
case 'unix': return $value->toUnix();
2323
case 'iso8601': return $value->toISO8601(TRUE);
24-
case 'rfc8601': return $value->toRFC822(TRUE);
24+
case 'rfc822': return $value->toRFC822(TRUE);
2525
}
2626
return $value->format(\TgI18n\I18N::_($params[0]), TRUE, TRUE, $processor->language);
2727
}

src/TgUtils/Templating/Processor.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TgUtils\Templating;
44

5+
use TgI18n\I18N;
6+
57
/** Provides some basic templating mechanism */
68
class Processor {
79

@@ -89,13 +91,13 @@ public function getObject($name) {
8991
* Returns the formatter with the given key or NULL.
9092
*/
9193
public function getFormatter($name) {
92-
return isset($this->formatter[$name]) ? $this->formatter[$name] : NULL;
94+
return isset($this->formatters[$name]) ? $this->formatters[$name] : NULL;
9395
}
9496

9597
/**
9698
* Returns the snippet with the given key or NULL.
9799
*/
98-
protected function getSnippet($name) {
100+
public function getSnippet($name) {
99101
return isset($this->snippets[$name]) ? $this->snippets[$name] : NULL;
100102
}
101103

@@ -105,7 +107,7 @@ protected function getSnippet($name) {
105107
* The formatter "datetime" will be used then.
106108
* More arguments for the formatter can follow, separated with : again
107109
*/
108-
protected function getAttribute($objName, $attr) {
110+
public function getAttribute($objName, $attr) {
109111
$object = $this->getObject($objName);
110112
$rc = '';
111113
if ($object != null) {
@@ -120,13 +122,13 @@ protected function getAttribute($objName, $attr) {
120122
if ($attrFormat != 'plain') {
121123
$formatter = $this->getFormatter($attrFormat);
122124
if ($formatter != NULL) {
123-
$rc = $formatter->format($object->$attrName, $attrDef, $this);
125+
$rc = $formatter->format($value, $attrDef, $this);
124126
}
125127
} else if (is_object($value)) {
126128
if (is_a($value, 'TgUtils\\Date')) {
127129
$formatter = $this->getFormatter('date');
128130
if ($formatter == NULL) $formatter = new DateFormatter();
129-
$rc = $formatter->format($object->$attrName, $attrDef, $this);
131+
$rc = $formatter->format($value, $attrDef, $this);
130132
} else {
131133
$rc = $value->__toString();
132134
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgUtils\Templating;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Test the templating
9+
* @author ralph
10+
*
11+
*/
12+
class ProcessorTest extends TestCase {
13+
14+
public function testSnippetWithString(): void {
15+
$processor = self::createProcessor();
16+
$template = 'This is the output: {{textSnippet}}';
17+
$result = $processor->process($template);
18+
$this->assertEquals('This is the output: This is text snippet.', $result);
19+
}
20+
21+
public function testSnippetWithSnippet(): void {
22+
$processor = self::createProcessor();
23+
$template = 'This is the output: {{mySnippet}}';
24+
$result = $processor->process($template);
25+
$this->assertEquals('This is the output: This is my-snippet.', $result);
26+
}
27+
28+
public function testSimpleAttribute(): void {
29+
$processor = self::createProcessor();
30+
$template = 'This is the output: {{testObject.name}}';
31+
$result = $processor->process($template);
32+
$this->assertEquals('This is the output: testObjectName', $result);
33+
}
34+
35+
public function testFormatter(): void {
36+
$processor = self::createProcessor();
37+
$template = 'This is the output: {{testObject.aDate:date:rfc822}}';
38+
$result = $processor->process($template);
39+
$this->assertEquals('This is the output: Fri, 01 Jan 2021 00:00:00 +0000', $result);
40+
}
41+
42+
protected static function createProcessor(): Processor {
43+
$testObject = new \stdClass;
44+
$testObject->name = 'testObjectName';
45+
$testObject->aDate = new \TgUtils\Date(1609459200, 'UTC');
46+
47+
$objects = array(
48+
'testObject' => $testObject,
49+
);
50+
$snippets = array(
51+
'textSnippet' => 'This is text snippet.',
52+
'mySnippet' => new TestSnippet(),
53+
);
54+
$formatters = array(
55+
'date' => new DateFormatter(),
56+
);
57+
return new Processor($objects, $snippets, $formatters, 'en');
58+
}
59+
}
60+
61+
class TestSnippet implements Snippet {
62+
63+
public function getOutput($processor) {
64+
return 'This is my-snippet.';
65+
}
66+
}

0 commit comments

Comments
 (0)