Completed
Push — master ( 0926c2...16eb90 )
by WEBEWEB
01:52
created

UtilityTwigExtensionTest::testGetFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Tests\Twig\Extension;
13
14
use DateTime;
15
use Exception;
16
use Twig\Node\Node;
17
use Twig\TwigFilter;
18
use Twig\TwigFunction;
19
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
20
use WBW\Bundle\CoreBundle\Twig\Extension\UtilityTwigExtension;
21
22
/**
23
 * Utility Twig extension test.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Bundle\CoreBundle\Tests\Twig\Extension
27
 */
28
class UtilityTwigExtensionTest extends AbstractTestCase {
29
30
    /**
31
     * Tests the calcAge() method.
32
     *
33
     * @return void
34
     * @throws Exception Throws an exception if an error occurs.
35
     */
36
    public function testCalcAge(): void {
37
38
        $obj = new UtilityTwigExtension($this->twigEnvironment);
39
40
        $this->assertGreaterThanOrEqual(19, $obj->calcAge(new DateTime("2000-02-13")));
41
        $this->assertGreaterThanOrEqual(19, $obj->calcAge(new DateTime("2000-02-14")));
42
        $this->assertGreaterThanOrEqual(18, $obj->calcAge(new DateTime("2000-02-15")));
43
    }
44
45
    /**
46
     * Tests the formatDate() method.
47
     *
48
     * @return void
49
     * @throws Exception Throws an exception if an error occurs.
50
     */
51
    public function testFormatDate(): void {
52
53
        $obj = new UtilityTwigExtension($this->twigEnvironment);
54
55
        $this->assertEquals("", $obj->formatDate());
56
        $this->assertEquals("2018-01-14 18:00", $obj->formatDate(new DateTime("2018-01-14 18:00")));
57
        $this->assertEquals("14/01/2018 18:00", $obj->formatDate(new DateTime("2018-01-14 18:00"), "d/m/Y H:i"));
58
    }
59
60
    /**
61
     * Tests the formatString() method.
62
     *
63
     * @return void
64
     */
65
    public function testFormatString(): void {
66
67
        $obj = new UtilityTwigExtension($this->twigEnvironment);
68
69
        $this->assertEquals("", $obj->formatString(null, "_____ _____ _"));
70
        $this->assertEquals("", $obj->formatString("Helloworld!", null));
71
72
        $this->assertEquals("Hello world !", $obj->formatString("Helloworld!", "_____ _____ _"));
73
        $this->assertEquals("+33 6 12 34 56 78", $obj->formatString("612345678", "+33 _ __ __ __ __"));
74
    }
75
76
    /**
77
     * Tests the getFilters() method.
78
     *
79
     * @return void
80
     */
81
    public function testGetFilters(): void {
82
83
        $obj = new UtilityTwigExtension($this->twigEnvironment);
84
85
        $res = $obj->getFilters();
86
        $this->assertCount(8, $res);
87
88
        $this->assertInstanceOf(TwigFilter::class, $res[0]);
89
        $this->assertEquals("calcAge", $res[0]->getName());
90
        $this->assertEquals([$obj, "calcAge"], $res[0]->getCallable());
91
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
92
93
        $this->assertInstanceOf(TwigFilter::class, $res[1]);
94
        $this->assertEquals("formatDate", $res[1]->getName());
95
        $this->assertEquals([$obj, "formatDate"], $res[1]->getCallable());
96
        $this->assertEquals(["html"], $res[1]->getSafe(new Node()));
97
98
        $this->assertInstanceOf(TwigFilter::class, $res[2]);
99
        $this->assertEquals("fmtDate", $res[2]->getName());
100
        $this->assertEquals([$obj, "formatDate"], $res[2]->getCallable());
101
        $this->assertEquals(["html"], $res[2]->getSafe(new Node()));
102
103
        $this->assertInstanceOf(TwigFilter::class, $res[3]);
104
        $this->assertEquals("formatString", $res[3]->getName());
105
        $this->assertEquals([$obj, "formatString"], $res[3]->getCallable());
106
        $this->assertEquals(["html"], $res[3]->getSafe(new Node()));
107
108
        $this->assertInstanceOf(TwigFilter::class, $res[4]);
109
        $this->assertEquals("fmtString", $res[4]->getName());
110
        $this->assertEquals([$obj, "formatString"], $res[4]->getCallable());
111
        $this->assertEquals(["html"], $res[4]->getSafe(new Node()));
112
113
        $this->assertInstanceOf(TwigFilter::class, $res[5]);
114
        $this->assertEquals("htmlEntityDecode", $res[5]->getName());
115
        $this->assertEquals([$obj, "htmlEntityDecode"], $res[5]->getCallable());
116
        $this->assertEquals(["html"], $res[5]->getSafe(new Node()));
117
118
        $this->assertInstanceOf(TwigFilter::class, $res[6]);
119
        $this->assertEquals("htmlEntityEncode", $res[6]->getName());
120
        $this->assertEquals([$obj, "htmlEntityEncode"], $res[6]->getCallable());
121
        $this->assertEquals(["html"], $res[6]->getSafe(new Node()));
122
123
        $this->assertInstanceOf(TwigFilter::class, $res[7]);
124
        $this->assertEquals("md5", $res[7]->getName());
125
        $this->assertEquals([$obj, "md5"], $res[7]->getCallable());
126
        $this->assertEquals(["html"], $res[7]->getSafe(new Node()));
127
    }
128
129
    /**
130
     * Tests the getFunctions() method.
131
     *
132
     * @return void
133
     */
134
    public function testGetFunctions(): void {
135
136
        $obj = new UtilityTwigExtension($this->twigEnvironment);
137
138
        $res = $obj->getFunctions();
139
        $this->assertCount(8, $res);
140
141
        $this->assertInstanceOf(TwigFunction::class, $res[0]);
142
        $this->assertEquals("calcAge", $res[0]->getName());
143
        $this->assertEquals([$obj, "calcAge"], $res[0]->getCallable());
144
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
145
146
        $this->assertInstanceOf(TwigFunction::class, $res[1]);
147
        $this->assertEquals("formatDate", $res[1]->getName());
148
        $this->assertEquals([$obj, "formatDate"], $res[1]->getCallable());
149
        $this->assertEquals(["html"], $res[1]->getSafe(new Node()));
150
151
        $this->assertInstanceOf(TwigFunction::class, $res[2]);
152
        $this->assertEquals("fmtDate", $res[2]->getName());
153
        $this->assertEquals([$obj, "formatDate"], $res[2]->getCallable());
154
        $this->assertEquals(["html"], $res[2]->getSafe(new Node()));
155
156
        $this->assertInstanceOf(TwigFunction::class, $res[3]);
157
        $this->assertEquals("formatString", $res[3]->getName());
158
        $this->assertEquals([$obj, "formatString"], $res[3]->getCallable());
159
        $this->assertEquals(["html"], $res[3]->getSafe(new Node()));
160
161
        $this->assertInstanceOf(TwigFunction::class, $res[4]);
162
        $this->assertEquals("fmtString", $res[4]->getName());
163
        $this->assertEquals([$obj, "formatString"], $res[4]->getCallable());
164
        $this->assertEquals(["html"], $res[4]->getSafe(new Node()));
165
166
        $this->assertInstanceOf(TwigFunction::class, $res[5]);
167
        $this->assertEquals("htmlEntityDecode", $res[5]->getName());
168
        $this->assertEquals([$obj, "htmlEntityDecode"], $res[5]->getCallable());
169
        $this->assertEquals(["html"], $res[5]->getSafe(new Node()));
170
171
        $this->assertInstanceOf(TwigFunction::class, $res[6]);
172
        $this->assertEquals("htmlEntityEncode", $res[6]->getName());
173
        $this->assertEquals([$obj, "htmlEntityEncode"], $res[6]->getCallable());
174
        $this->assertEquals(["html"], $res[6]->getSafe(new Node()));
175
176
        $this->assertInstanceOf(TwigFunction::class, $res[7]);
177
        $this->assertEquals("md5", $res[7]->getName());
178
        $this->assertEquals([$obj, "md5"], $res[7]->getCallable());
179
        $this->assertEquals(["html"], $res[7]->getSafe(new Node()));
180
    }
181
182
    /**
183
     * Tests the htmlEntityDecode() method.
184
     *
185
     * @return void
186
     */
187
    public function testHtmlEntityDecode(): void {
188
189
        $obj = new UtilityTwigExtension($this->twigEnvironment);
190
191
        $this->assertEquals("", $obj->htmlEntityDecode(null));
192
        $this->assertEquals("&", $obj->htmlEntityDecode("&amp;"));
193
    }
194
195
    /**
196
     * Tests the htmlEntityEncode() method.
197
     *
198
     * @return void
199
     */
200
    public function testHtmlEntityEncode(): void {
201
202
        $obj = new UtilityTwigExtension($this->twigEnvironment);
203
204
        $this->assertEquals("", $obj->htmlEntityEncode(null));
205
        $this->assertEquals("&amp;", $obj->htmlEntityEncode("&"));
206
    }
207
208
    /**
209
     * Tests the md5() method.
210
     *
211
     * @return void
212
     */
213
    public function testMd5(): void {
214
215
        $obj = new UtilityTwigExtension($this->twigEnvironment);
216
217
        $this->assertEquals("", $obj->md5(null));
218
        $this->assertEquals("d41d8cd98f00b204e9800998ecf8427e", $obj->md5(""));
219
    }
220
221
    /**
222
     * Tests the __construct() method.
223
     *
224
     * @return void
225
     */
226
    public function test__construct(): void {
227
228
        $this->assertEquals("wbw.core.twig.extension.utility", UtilityTwigExtension::SERVICE_NAME);
229
230
        $obj = new UtilityTwigExtension($this->twigEnvironment);
231
232
        $this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment());
233
    }
234
}
235