AssetsTwigExtensionTest::testAssetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 1
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) 2020 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 Twig\Extension\ExtensionInterface;
15
use Twig\Node\Node;
16
use Twig\TwigFilter;
17
use Twig\TwigFunction;
18
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
19
use WBW\Bundle\CoreBundle\Tests\TestCaseHelper;
20
use WBW\Bundle\CoreBundle\Twig\Extension\AssetsTwigExtension;
21
22
/**
23
 * Assets Twig extension test.
24
 *
25
 * @author webeweb <https://github.com/webeweb>
26
 * @package WBW\Bundle\CoreBundle\Tests\Twig\Extension
27
 */
28
class AssetsTwigExtensionTest extends AbstractTestCase {
29
30
    /**
31
     * Test assetExists()
32
     *
33
     * @return void
34
     */
35
    public function testAssetExists(): void {
36
37
        $obj = new AssetsTwigExtension($this->twigEnvironment);
38
        $obj->setPublicDirectory(__DIR__ . "/../../Fixtures/app/public");
39
40
        $this->assertNull($obj->assetExists(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->assetExists(null) targeting WBW\Bundle\CoreBundle\Tw...xtension::assetExists() 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...
41
        $this->assertFalse($obj->assetExists("/.gitignore"));
42
        $this->assertTrue($obj->assetExists("/.gitkeep"));
43
    }
44
45
    /**
46
     * Test coreGtag()
47
     *
48
     * @returns void
49
     */
50
    public function testCoreGtag(): void {
51
52
        $obj = new AssetsTwigExtension($this->twigEnvironment);
53
54
        $exp = file_get_contents(__DIR__ . "/AssetsTwigExtensionTest.testCoreGtag.js.txt");
55
56
        $this->assertEquals($exp, $obj->coreGtag("UA-123456789-0"));
57
        $this->assertNull($obj->coreGtag(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->coreGtag(null) targeting WBW\Bundle\CoreBundle\Tw...igExtension::coreGtag() 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...
58
        $this->assertNull($obj->coreGtag(""));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->coreGtag('') targeting WBW\Bundle\CoreBundle\Tw...igExtension::coreGtag() 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...
59
    }
60
61
    /**
62
     * Test coreImageFunction()
63
     *
64
     * @return void
65
     */
66
    public function testCoreImageFunction(): void {
67
68
        $arg = [
69
            "src"    => "src",
70
            "alt"    => "alt",
71
            "width"  => 1024,
72
            "height" => 768,
73
            "class"  => "class",
74
            "usemap" => "#usemap",
75
        ];
76
        $exp = '<img src="src" alt="alt" width="1024" height="768" class="class" usemap="#usemap"/>';
77
78
        $obj = new AssetsTwigExtension($this->twigEnvironment);
79
80
        $this->assertEquals($exp, $obj->coreImageFunction($arg));
81
    }
82
83
    /**
84
     * Test coreImageFunction()
85
     *
86
     * @return void
87
     */
88
    public function testCoreImageFunctionWithoutArguments(): void {
89
90
        $arg = [];
91
        $exp = "<img />";
92
93
        $obj = new AssetsTwigExtension($this->twigEnvironment);
94
95
        $this->assertEquals($exp, $obj->coreImageFunction($arg));
96
    }
97
98
    /**
99
     * Test coreRenderIconFunction()
100
     *
101
     * @return void
102
     */
103
    public function testCoreRenderIconFunction(): void {
104
105
        $obj = new AssetsTwigExtension($this->twigEnvironment);
106
107
        $this->assertNull($obj->coreRenderIconFunction(null));
108
        $this->assertNull($obj->coreRenderIconFunction("::"));
109
    }
110
111
    /**
112
     * Test coreResourceScriptFunction()
113
     */
114
    public function testCoreResourceScriptFunction(): void {
115
116
        $generate = TestCaseHelper::getRouterGenerateFunction();
117
118
        // Set the Router mock.
119
        $this->router->expects($this->any())->method("generate")->willReturnCallback($generate);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Routing\RouterInterface. ( Ignorable by Annotation )

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

119
        $this->router->/** @scrutinizer ignore-call */ 
120
                       expects($this->any())->method("generate")->willReturnCallback($generate);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
121
        $obj = new AssetsTwigExtension($this->twigEnvironment);
122
        $obj->setRouter($this->router);
123
124
        $this->assertEquals('<script type="text/javascript" src="wbw_core_twig_resource"></script>', $obj->coreResourceScriptFunction("test", ["v" => 1]));
125
    }
126
127
    /**
128
     * Test coreResourceStyleFunction()
129
     */
130
    public function testCoreResourceStyleFunction(): void {
131
132
        $generate = TestCaseHelper::getRouterGenerateFunction();
133
134
        // Set the Router mock.
135
        $this->router->expects($this->any())->method("generate")->willReturnCallback($generate);
136
137
        $obj = new AssetsTwigExtension($this->twigEnvironment);
138
        $obj->setRouter($this->router);
139
140
        $this->assertEquals('<link type="text/css" rel="stylesheet" href="wbw_core_twig_resource">', $obj->coreResourceStyleFunction("test", ["v" => 1]));
141
    }
142
143
    /**
144
     * Test coreScriptFilter()
145
     *
146
     * @return void
147
     */
148
    public function testCoreScriptFilter(): void {
149
150
        $exp = file_get_contents(__DIR__ . "/AssetsTwigExtensionTest.testCoreScriptFilter.html.txt");
151
152
        $obj = new AssetsTwigExtension($this->twigEnvironment);
153
154
        $this->assertEquals($exp, $obj->coreScriptFilter("content") . "\n");
155
    }
156
157
    /**
158
     * Test coreStyleFilter()
159
     *
160
     * @return void
161
     */
162
    public function testCoreStyleFilter(): void {
163
164
        $exp = file_get_contents(__DIR__ . "/AssetsTwigExtensionTest.testCoreStyleFilter.html.txt");
165
166
        $obj = new AssetsTwigExtension($this->twigEnvironment);
167
168
        $this->assertEquals($exp, $obj->coreStyleFilter("content") . "\n");
169
    }
170
171
    /**
172
     * Test cssRgba()
173
     *
174
     * @return void
175
     */
176
    public function testCssRgba(): void {
177
178
        $obj = new AssetsTwigExtension($this->twigEnvironment);
179
180
        $this->assertNull($obj->cssRgba(null));
181
        $this->assertNull($obj->cssRgba(""));
182
        $this->assertEquals("rgba(255, 255, 255, 0.00)", $obj->cssRgba("#FFFFFF", 0.00));
183
    }
184
185
    /**
186
     * Test getFilters()
187
     *
188
     * @return void
189
     */
190
    public function testGetFilters(): void {
191
192
        $obj = new AssetsTwigExtension($this->twigEnvironment);
193
194
        $res = $obj->getFilters();
195
        $this->assertCount(5, $res);
196
197
        $i = -1;
198
199
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
200
        $this->assertEquals("assetExists", $res[$i]->getName());
201
        $this->assertEquals([$obj, "assetExists"], $res[$i]->getCallable());
202
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
203
204
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
205
        $this->assertEquals("coreGtag", $res[$i]->getName());
206
        $this->assertEquals([$obj, "coreGtag"], $res[$i]->getCallable());
207
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
208
209
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
210
        $this->assertEquals("coreScript", $res[$i]->getName());
211
        $this->assertEquals([$obj, "coreScriptFilter"], $res[$i]->getCallable());
212
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
213
214
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
215
        $this->assertEquals("coreStyle", $res[$i]->getName());
216
        $this->assertEquals([$obj, "coreStyleFilter"], $res[$i]->getCallable());
217
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
218
219
        $this->assertInstanceOf(TwigFilter::class, $res[++$i]);
220
        $this->assertEquals("cssRgba", $res[$i]->getName());
221
        $this->assertEquals([$obj, "cssRgba"], $res[$i]->getCallable());
222
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
223
    }
224
225
    /**
226
     * Test getFunctions()
227
     *
228
     * @return void
229
     */
230
    public function testGetFunctions(): void {
231
232
        $obj = new AssetsTwigExtension($this->twigEnvironment);
233
234
        $res = $obj->getFunctions();
235
        $this->assertCount(8, $res);
236
237
        $i = -1;
238
239
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
240
        $this->assertEquals("assetExists", $res[$i]->getName());
241
        $this->assertEquals([$obj, "assetExists"], $res[$i]->getCallable());
242
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
243
244
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
245
        $this->assertEquals("coreGtag", $res[$i]->getName());
246
        $this->assertEquals([$obj, "coreGtag"], $res[$i]->getCallable());
247
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
248
249
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
250
        $this->assertEquals("coreImage", $res[$i]->getName());
251
        $this->assertEquals([$obj, "coreImageFunction"], $res[$i]->getCallable());
252
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
253
254
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
255
        $this->assertEquals("coreRenderIcon", $res[$i]->getName());
256
        $this->assertEquals([$obj, "coreRenderIconFunction"], $res[$i]->getCallable());
257
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
258
259
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
260
        $this->assertEquals("coreResourcePath", $res[$i]->getName());
261
        $this->assertEquals([$obj, "coreResourcePathFunction"], $res[$i]->getCallable());
262
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
263
264
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
265
        $this->assertEquals("coreResourceScript", $res[$i]->getName());
266
        $this->assertEquals([$obj, "coreResourceScriptFunction"], $res[$i]->getCallable());
267
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
268
269
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
270
        $this->assertEquals("coreResourceStyle", $res[$i]->getName());
271
        $this->assertEquals([$obj, "coreResourceStyleFunction"], $res[$i]->getCallable());
272
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
273
274
        $this->assertInstanceOf(TwigFunction::class, $res[++$i]);
275
        $this->assertEquals("cssRgba", $res[$i]->getName());
276
        $this->assertEquals([$obj, "cssRgba"], $res[$i]->getCallable());
277
        $this->assertEquals(["html"], $res[$i]->getSafe(new Node()));
278
    }
279
280
    /**
281
     * Test renderIcon()
282
     *
283
     * @return void
284
     */
285
    public function testRenderIcon(): void {
286
287
        $this->assertNull(AssetsTwigExtension::renderIcon($this->twigEnvironment, null));
288
        $this->assertNull(AssetsTwigExtension::renderIcon($this->twigEnvironment, "::"));
289
    }
290
291
    /**
292
     * Test renderIcon()
293
     *
294
     * @return void
295
     */
296
    public function testRenderIconWithFontAwesome(): void {
297
298
        $this->assertEquals('<i class="fa fa-home"></i>', AssetsTwigExtension::renderIcon($this->twigEnvironment, "fa:home"));
299
        $this->assertEquals('<i class="fas fa-home"></i>', AssetsTwigExtension::renderIcon($this->twigEnvironment, "fas:home"));
300
        $this->assertEquals('<i class="far fa-home"></i>', AssetsTwigExtension::renderIcon($this->twigEnvironment, "far:home"));
301
        $this->assertEquals('<i class="fal fa-home"></i>', AssetsTwigExtension::renderIcon($this->twigEnvironment, "fal:home"));
302
        $this->assertEquals('<i class="fad fa-home"></i>', AssetsTwigExtension::renderIcon($this->twigEnvironment, "fad:home"));
303
    }
304
305
    /**
306
     * Test renderIcon()
307
     *
308
     * @return void
309
     */
310
    public function testRenderIconWithMaterialDesignIconicFont(): void {
311
312
        $exp = '<i class="zmdi zmdi-home"></i>';
313
314
        $this->assertEquals($exp, AssetsTwigExtension::renderIcon($this->twigEnvironment, "zmdi:home"));
315
    }
316
317
    /**
318
     * Test renderIcon()
319
     *
320
     * @return void
321
     */
322
    public function testRenderIconWithMeteocons(): void {
323
324
        $exp = '<i class="meteocons" data-meteocons="A"></i>';
325
326
        $this->assertEquals($exp, AssetsTwigExtension::renderIcon($this->twigEnvironment, "mc:A"));
327
    }
328
329
    /**
330
     * Test __construct()
331
     *
332
     * @return void
333
     */
334
    public function test__construct(): void {
335
336
        $this->assertEquals("wbw.core.twig.extension.assets", AssetsTwigExtension::SERVICE_NAME);
337
338
        $obj = new AssetsTwigExtension($this->twigEnvironment);
339
340
        $this->assertInstanceOf(ExtensionInterface::class, $obj);
341
342
        $this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment());
343
        $this->assertNull($obj->getPublicDirectory());
344
    }
345
}
346