Passed
Push — master ( 2d4adf...cc1fa4 )
by WEBEWEB
13:18
created

testStringUniqidFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2021 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 Throwable;
16
use Twig\Extension\ExtensionInterface;
17
use Twig\Node\Node;
18
use Twig\TwigFilter;
19
use Twig\TwigFunction;
20
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
21
use WBW\Bundle\CoreBundle\Twig\Extension\StringTwigExtension;
22
23
/**
24
 * String Twig extension test.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Bundle\CoreBundle\Tests\Twig\Extension
28
 */
29
class StringTwigExtensionTest extends AbstractTestCase {
30
31
    /**
32
     * Test dateFormat()
33
     *
34
     * @return void
35
     * @throws Throwable Throws an exception if an error occurs.
36
     */
37
    public function testDateFormat(): void {
38
39
        $obj = new StringTwigExtension($this->twigEnvironment);
40
41
        $this->assertNull($obj->dateFormat(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->dateFormat(null) targeting WBW\Bundle\CoreBundle\Tw...Extension::dateFormat() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
        $this->assertEquals("2018-01-14 18:00", $obj->dateFormat(new DateTime("2018-01-14 18:00")));
43
        $this->assertEquals("14/01/2018 18:00", $obj->dateFormat(new DateTime("2018-01-14 18:00"), "d/m/Y H:i"));
44
    }
45
46
    /**
47
     * Test getFilters()
48
     *
49
     * @return void
50
     */
51
    public function testGetFilters() {
52
53
        $obj = new StringTwigExtension($this->twigEnvironment);
54
55
        $res = $obj->getFilters();
56
        $this->assertCount(11, $res);
57
58
        $i = -1;
59
60
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
61
        $this->assertEquals("dateFormat", $res[$i]->getName());
62
        $this->assertEquals([$obj, "dateFormat"], $res[$i]->getCallable());
63
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
64
65
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
66
        $this->assertEquals("htmlEntityDecode", $res[$i]->getName());
67
        $this->assertEquals([$obj, "htmlEntityDecode"], $res[$i]->getCallable());
68
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
69
70
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
71
        $this->assertEquals("htmlEntityEncode", $res[$i]->getName());
72
        $this->assertEquals([$obj, "htmlEntityEncode"], $res[$i]->getCallable());
73
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
74
75
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
76
        $this->assertEquals("jsonDecode", $res[$i]->getName());
77
        $this->assertEquals([$obj, "jsonDecode"], $res[$i]->getCallable());
78
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
79
80
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
81
        $this->assertEquals("md5", $res[$i]->getName());
82
        $this->assertEquals([$obj, "md5"], $res[$i]->getCallable());
83
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
84
85
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
86
        $this->assertEquals("stringExtractUpperCase", $res[$i]->getName());
87
        $this->assertEquals([$obj, "stringExtractUpperCase"], $res[$i]->getCallable());
88
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
89
90
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
91
        $this->assertEquals("stringFormat", $res[$i]->getName());
92
        $this->assertEquals([$obj, "stringFormat"], $res[$i]->getCallable());
93
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
94
95
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
96
        $this->assertEquals("stringHumanReadable", $res[$i]->getName());
97
        $this->assertEquals([$obj, "stringHumanReadable"], $res[$i]->getCallable());
98
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
99
100
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
101
        $this->assertEquals("stringLowerCamelCase", $res[$i]->getName());
102
        $this->assertEquals([$obj, "stringLowerCamelCase"], $res[$i]->getCallable());
103
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
104
105
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
106
        $this->assertEquals("stringSnakeCase", $res[$i]->getName());
107
        $this->assertEquals([$obj, "stringSnakeCase"], $res[$i]->getCallable());
108
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
109
110
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
111
        $this->assertEquals("stringUpperCamelCase", $res[$i]->getName());
112
        $this->assertEquals([$obj, "stringUpperCamelCase"], $res[$i]->getCallable());
113
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
114
    }
115
116
    /**
117
     * Test getFunctions()
118
     *
119
     * @return void
120
     */
121
    public function testGetFunctions() {
122
123
        $obj = new StringTwigExtension($this->twigEnvironment);
124
125
        $res = $obj->getFunctions();
126
        $this->assertCount(12, $res);
127
128
        $i = -1;
129
130
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
131
        $this->assertEquals("dateFormat", $res[$i]->getName());
132
        $this->assertEquals([$obj, "dateFormat"], $res[$i]->getCallable());
133
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
134
135
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
136
        $this->assertEquals("htmlEntityDecode", $res[$i]->getName());
137
        $this->assertEquals([$obj, "htmlEntityDecode"], $res[$i]->getCallable());
138
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
139
140
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
141
        $this->assertEquals("htmlEntityEncode", $res[$i]->getName());
142
        $this->assertEquals([$obj, "htmlEntityEncode"], $res[$i]->getCallable());
143
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
144
145
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
146
        $this->assertEquals("jsonDecode", $res[$i]->getName());
147
        $this->assertEquals([$obj, "jsonDecode"], $res[$i]->getCallable());
148
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
149
150
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
151
        $this->assertEquals("md5", $res[$i]->getName());
152
        $this->assertEquals([$obj, "md5"], $res[$i]->getCallable());
153
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
154
155
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
156
        $this->assertEquals("stringExtractUpperCase", $res[$i]->getName());
157
        $this->assertEquals([$obj, "stringExtractUpperCase"], $res[$i]->getCallable());
158
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
159
160
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
161
        $this->assertEquals("stringFormat", $res[$i]->getName());
162
        $this->assertEquals([$obj, "stringFormat"], $res[$i]->getCallable());
163
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
164
165
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
166
        $this->assertEquals("stringHumanReadable", $res[$i]->getName());
167
        $this->assertEquals([$obj, "stringHumanReadable"], $res[$i]->getCallable());
168
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
169
170
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
171
        $this->assertEquals("stringLowerCamelCase", $res[$i]->getName());
172
        $this->assertEquals([$obj, "stringLowerCamelCase"], $res[$i]->getCallable());
173
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
174
175
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
176
        $this->assertEquals("stringSnakeCase", $res[$i]->getName());
177
        $this->assertEquals([$obj, "stringSnakeCase"], $res[$i]->getCallable());
178
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
179
180
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
181
        $this->assertEquals("stringUniqid", $res[$i]->getName());
182
        $this->assertEquals([$obj, "stringUniqidFunction"], $res[$i]->getCallable());
183
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
184
185
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
186
        $this->assertEquals("stringUpperCamelCase", $res[$i]->getName());
187
        $this->assertEquals([$obj, "stringUpperCamelCase"], $res[$i]->getCallable());
188
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
189
    }
190
191
    /**
192
     * Test htmlEntityDecode()
193
     *
194
     * @return void
195
     */
196
    public function testHtmlEntityDecode(): void {
197
198
        $obj = new StringTwigExtension($this->twigEnvironment);
199
200
        $this->assertNull($obj->htmlEntityDecode(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->htmlEntityDecode(null) targeting WBW\Bundle\CoreBundle\Tw...ion::htmlEntityDecode() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
201
        $this->assertEquals("&", $obj->htmlEntityDecode("&amp;"));
202
    }
203
204
    /**
205
     * Test htmlEntityEncode()
206
     *
207
     * @return void
208
     */
209
    public function testHtmlEntityEncode(): void {
210
211
        $obj = new StringTwigExtension($this->twigEnvironment);
212
213
        $this->assertNull($obj->htmlEntityEncode(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->htmlEntityEncode(null) targeting WBW\Bundle\CoreBundle\Tw...ion::htmlEntityEncode() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
214
        $this->assertEquals("&amp;", $obj->htmlEntityEncode("&"));
215
    }
216
217
    /**
218
     * Test jsonDecode()
219
     *
220
     * @return void
221
     */
222
    public function testJsonDecode(): void {
223
224
        $obj = new StringTwigExtension($this->twigEnvironment);
225
226
        $this->assertNull($obj->jsonDecode(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->jsonDecode(null) targeting WBW\Bundle\CoreBundle\Tw...Extension::jsonDecode() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
227
        $this->assertEquals([], $obj->jsonDecode("{}"));
228
    }
229
230
    /**
231
     * Test md5()
232
     *
233
     * @return void
234
     */
235
    public function testMd5(): void {
236
237
        $obj = new StringTwigExtension($this->twigEnvironment);
238
239
        $this->assertNull($obj->md5(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->md5(null) targeting WBW\Bundle\CoreBundle\Tw...ingTwigExtension::md5() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
240
        $this->assertEquals("d41d8cd98f00b204e9800998ecf8427e", $obj->md5(""));
241
    }
242
243
    /**
244
     * Test the stringExtractUpperCase() method
245
     *
246
     * @return void
247
     * @throws Throwable Throws an exception if an error occurs.
248
     */
249
    public function testStringExtractUpperCase() {
250
251
        $obj = new StringTwigExtension($this->twigEnvironment);
252
253
        $this->assertNull($obj->stringExtractUpperCase(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringExtractUpperCase(null) targeting WBW\Bundle\CoreBundle\Tw...tringExtractUpperCase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
254
        $this->assertEquals("STE", $obj->stringExtractUpperCase("StringTwigExtension"));
255
        $this->assertEquals("ste", $obj->stringExtractUpperCase("StringTwigExtension", true));
256
    }
257
258
    /**
259
     * Test the stringFormat() method
260
     *
261
     * @return void
262
     * @throws Throwable Throws an exception if an error occurs.
263
     */
264
    public function testStringFormat() {
265
266
        $obj = new StringTwigExtension($this->twigEnvironment);
267
        $this->assertEquals("", $obj->stringFormat(null, "_____ _____ _"));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringFormat(null, '_____ _____ _') targeting WBW\Bundle\CoreBundle\Tw...tension::stringFormat() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
268
        $this->assertEquals("", $obj->stringFormat("Helloworld!", null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringFormat('Helloworld!', null) targeting WBW\Bundle\CoreBundle\Tw...tension::stringFormat() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
269
270
        $this->assertEquals("Hello world !", $obj->stringFormat("Helloworld!", "_____ _____ _"));
271
        $this->assertEquals("+33 6 12 34 56 78", $obj->stringFormat("612345678", "+33 _ __ __ __ __"));
272
    }
273
274
    /**
275
     * Test the stringHumanReadable() method
276
     *
277
     * @return void
278
     * @throws Throwable Throws an exception if an error occurs.
279
     */
280
    public function testStringHumanReadable() {
281
282
        $obj = new StringTwigExtension($this->twigEnvironment);
283
284
        $this->assertNull($obj->stringHumanReadable(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringHumanReadable(null) targeting WBW\Bundle\CoreBundle\Tw...::stringHumanReadable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
285
        $this->assertEquals("String twig extension", $obj->stringHumanReadable("StringTwigExtension"));
286
    }
287
288
    /**
289
     * Test the stringLowerCamelCase() method
290
     *
291
     * @return void
292
     * @throws Throwable Throws an exception if an error occurs.
293
     */
294
    public function testStringLowerCamelCase() {
295
296
        $obj = new StringTwigExtension($this->twigEnvironment);
297
298
        $this->assertNull($obj->stringLowerCamelCase(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringLowerCamelCase(null) targeting WBW\Bundle\CoreBundle\Tw...:stringLowerCamelCase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
299
        $this->assertEquals("stringTwigExtension", $obj->stringLowerCamelCase("StringTwigExtension"));
300
    }
301
302
    /**
303
     * Test the stringSnakeCase() method
304
     *
305
     * @return void
306
     * @throws Throwable Throws an exception if an error occurs.
307
     */
308
    public function testStringSnakeCase() {
309
310
        $obj = new StringTwigExtension($this->twigEnvironment);
311
312
        $this->assertNull($obj->stringSnakeCase(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringSnakeCase(null) targeting WBW\Bundle\CoreBundle\Tw...sion::stringSnakeCase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
313
        $this->assertEquals("string_twig_extension", $obj->stringSnakeCase("StringTwigExtension"));
314
        $this->assertEquals("string-twig-extension", $obj->stringSnakeCase("StringTwigExtension", "-"));
315
    }
316
317
    /**
318
     * Test the stringUniqidFunction() method
319
     *
320
     * @return void
321
     * @throws Throwable Throws an exception if an error occurs.
322
     */
323
    public function testStringUniqidFunction() {
324
325
        $obj = new StringTwigExtension($this->twigEnvironment);
326
327
        $this->assertNull($obj->stringUniqidFunction(-1));
328
329
        $res = $obj->stringUniqidFunction();
330
        $this->assertRegExp("/^[a-z0-9]{13}$/", $res);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertRegExp() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/4086 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

330
        /** @scrutinizer ignore-deprecated */ $this->assertRegExp("/^[a-z0-9]{13}$/", $res);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
It seems like $res can also be of type null; however, parameter $string of PHPUnit\Framework\Assert::assertRegExp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

330
        $this->assertRegExp("/^[a-z0-9]{13}$/", /** @scrutinizer ignore-type */ $res);
Loading history...
331
332
        $map = [];
333
        for ($i = 0; $i < 20; ++$i) {
334
335
            $tmp = $obj->stringUniqidFunction();
336
            $this->assertArrayNotHasKey($tmp, $map);
337
338
            $map[$tmp] = $tmp;
339
        }
340
    }
341
342
    /**
343
     * Test the stringUpperCamelCase() method
344
     *
345
     * @return void
346
     * @throws Throwable Throws an exception if an error occurs.
347
     */
348
    public function testStringUpperCamelCase() {
349
350
        $obj = new StringTwigExtension($this->twigEnvironment);
351
352
        $this->assertNull($obj->stringUpperCamelCase(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->stringUpperCamelCase(null) targeting WBW\Bundle\CoreBundle\Tw...:stringUpperCamelCase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
353
        $this->assertEquals("StringTwigExtension", $obj->stringUpperCamelCase("StringTwigExtension"));
354
    }
355
356
    /**
357
     * Test __construct()
358
     *
359
     * @return void
360
     */
361
    public function test__construct() {
362
363
        $this->assertEquals("wbw.core.twig.extension.string", StringTwigExtension::SERVICE_NAME);
364
365
        $obj = new StringTwigExtension($this->twigEnvironment);
366
367
        $this->assertInstanceOf(ExtensionInterface::class, $obj);
368
369
        $this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment());
370
    }
371
372
}
373