testRenderDataTablesFunctionWithClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 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\JQuery\DataTablesBundle\Tests\Twig\Extension;
13
14
use Throwable;
15
use Twig\Extension\ExtensionInterface;
16
use Twig\Node\Node;
17
use Twig\TwigFilter;
18
use Twig\TwigFunction;
19
use WBW\Bundle\CoreBundle\Twig\Extension\AssetsTwigExtension;
20
use WBW\Bundle\JQuery\DataTablesBundle\Tests\AbstractTestCase;
21
use WBW\Bundle\JQuery\DataTablesBundle\Twig\Extension\DataTablesTwigExtension;
22
23
/**
24
 * DataTables Twig extension test.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Bundle\JQuery\DataTablesBundle\Tests\Twig\Extension
28
 */
29
class DataTablesTwigExtensionTest extends AbstractTestCase {
30
31
    /**
32
     * Asset Twig extension.
33
     *
34
     * @var AssetsTwigExtension
35
     */
36
    private $assetsTwigExtension;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected function setUp(): void {
42
        parent::setUp();
43
44
        // Set a Renderer Twig extension mock.
45
        $this->assetsTwigExtension = new AssetsTwigExtension($this->twigEnvironment);
46
    }
47
48
    /**
49
     * Test getFilters()
50
     *
51
     * @return void
52
     */
53
    public function testGetFilters(): void {
54
55
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
56
57
        $res = $obj->getFilters();
58
        $this->assertCount(2, $res);
59
60
        $this->assertInstanceOf(TwigFilter::class, $res[0]);
61
        $this->assertEquals("jQueryDataTablesName", $res[0]->getName());
62
        $this->assertEquals([$obj, "jQueryDataTablesNameFunction"], $res[0]->getCallable());
63
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
64
65
        $this->assertInstanceOf(TwigFilter::class, $res[1]);
66
        $this->assertEquals("jQueryDTName", $res[1]->getName());
67
        $this->assertEquals([$obj, "jQueryDataTablesNameFunction"], $res[1]->getCallable());
68
        $this->assertEquals(["html"], $res[1]->getSafe(new Node()));
69
    }
70
71
    /**
72
     * Test getFunctions()
73
     *
74
     * @return void
75
     */
76
    public function testGetFunctions(): void {
77
78
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
79
80
        $res = $obj->getFunctions();
81
        $this->assertCount(10, $res);
82
83
        $this->assertInstanceOf(TwigFunction::class, $res[0]);
84
        $this->assertEquals("jQueryDataTables", $res[0]->getName());
85
        $this->assertEquals([$obj, "jQueryDataTablesFunction"], $res[0]->getCallable());
86
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
87
88
        $this->assertInstanceOf(TwigFunction::class, $res[1]);
89
        $this->assertEquals("jQueryDT", $res[1]->getName());
90
        $this->assertEquals([$obj, "jQueryDataTablesFunction"], $res[1]->getCallable());
91
        $this->assertEquals(["html"], $res[1]->getSafe(new Node()));
92
93
        $this->assertInstanceOf(TwigFunction::class, $res[2]);
94
        $this->assertEquals("jQueryDataTablesName", $res[2]->getName());
95
        $this->assertEquals([$obj, "jQueryDataTablesNameFunction"], $res[2]->getCallable());
96
        $this->assertEquals(["html"], $res[2]->getSafe(new Node()));
97
98
        $this->assertInstanceOf(TwigFunction::class, $res[3]);
99
        $this->assertEquals("jQueryDTName", $res[3]->getName());
100
        $this->assertEquals([$obj, "jQueryDataTablesNameFunction"], $res[3]->getCallable());
101
        $this->assertEquals(["html"], $res[3]->getSafe(new Node()));
102
103
        $this->assertInstanceOf(TwigFunction::class, $res[4]);
104
        $this->assertEquals("jQueryDataTablesOptions", $res[4]->getName());
105
        $this->assertEquals([$obj, "jQueryDataTablesOptionsFunction"], $res[4]->getCallable());
106
        $this->assertEquals(["html"], $res[4]->getSafe(new Node()));
107
108
        $this->assertInstanceOf(TwigFunction::class, $res[5]);
109
        $this->assertEquals("jQueryDTOptions", $res[5]->getName());
110
        $this->assertEquals([$obj, "jQueryDataTablesOptionsFunction"], $res[5]->getCallable());
111
        $this->assertEquals(["html"], $res[5]->getSafe(new Node()));
112
113
        $this->assertInstanceOf(TwigFunction::class, $res[6]);
114
        $this->assertEquals("jQueryDataTablesStandalone", $res[6]->getName());
115
        $this->assertEquals([$obj, "jQueryDataTablesStandaloneFunction"], $res[6]->getCallable());
116
        $this->assertEquals(["html"], $res[6]->getSafe(new Node()));
117
118
        $this->assertInstanceOf(TwigFunction::class, $res[7]);
119
        $this->assertEquals("jQueryDTStandalone", $res[7]->getName());
120
        $this->assertEquals([$obj, "jQueryDataTablesStandaloneFunction"], $res[7]->getCallable());
121
        $this->assertEquals(["html"], $res[7]->getSafe(new Node()));
122
123
        $this->assertInstanceOf(TwigFunction::class, $res[8]);
124
        $this->assertEquals("renderDataTables", $res[8]->getName());
125
        $this->assertEquals([$obj, "renderDataTablesFunction"], $res[8]->getCallable());
126
        $this->assertEquals(["html"], $res[8]->getSafe(new Node()));
127
128
        $this->assertInstanceOf(TwigFunction::class, $res[9]);
129
        $this->assertEquals("renderDT", $res[9]->getName());
130
        $this->assertEquals([$obj, "renderDataTablesFunction"], $res[9]->getCallable());
131
        $this->assertEquals(["html"], $res[9]->getSafe(new Node()));
132
    }
133
134
    /**
135
     * Test the datatables-i18n-<version> directory.
136
     *
137
     * @return void
138
     */
139
    public function testI18n(): void {
140
141
        $directory = realpath(__DIR__ . "/../../../Resources/public/datatables-i18n");
142
143
        $found = 0;
144
145
        $stream = opendir($directory);
146
        while (false !== ($file = readdir($stream))) {
147
148
            if (true === in_array($file, [".", ".."])) {
149
                continue;
150
            }
151
152
            ++$found;
153
154
            $this->assertJson(file_get_contents($directory . "/" . $file), $file);
155
        }
156
157
        closedir($stream);
158
159
        $this->assertEquals(70, $found);
160
    }
161
162
    /**
163
     * Test jQueryDataTablesFunction()
164
     *
165
     * @return void
166
     * @throws Throwable Throws an exception if an error occurs.
167
     */
168
    public function testJQueryDataTablesFunction(): void {
169
170
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
171
172
        $arg = [
173
            "selector" => "#selector",
174
            "language" => "French",
175
        ];
176
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testJQueryDataTablesFunction.html.txt");
177
        $this->assertEquals($res, $obj->jQueryDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...eryDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

177
        $this->assertEquals($res, $obj->jQueryDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
178
    }
179
180
    /**
181
     * Test jQueryDataTablesFunction()
182
     *
183
     * @return void
184
     * @throws Throwable Throws an exception if an error occurs.
185
     */
186
    public function testJQueryDataTablesFunctionWithoutArguments(): void {
187
188
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
189
190
        $arg = [];
191
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testJQueryDataTablesFunctionWithoutArguments.html.txt");
192
        $this->assertEquals($res, $obj->jQueryDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...eryDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

192
        $this->assertEquals($res, $obj->jQueryDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
193
    }
194
195
    /**
196
     * Test jQueryDataTablesNameFunction()
197
     *
198
     * @return void
199
     */
200
    public function testJQueryDataTablesNameFunction(): void {
201
202
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
203
204
        $this->assertSame("dtemployee", $obj->jQueryDataTablesNameFunction($this->dtWrapper));
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...ataTablesNameFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

204
        $this->assertSame("dtemployee", $obj->jQueryDataTablesNameFunction(/** @scrutinizer ignore-type */ $this->dtWrapper));
Loading history...
205
    }
206
207
    /**
208
     * Test jQueryDataTablesOptionsFunction()
209
     *
210
     * @return void
211
     */
212
    public function testJQueryDataTablesOptionsFunction(): void {
213
214
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
215
216
        $res = [
217
            "ajax"       => [
218
                "type" => "POST",
219
                "url"  => "/datatables/employee/index",
220
            ],
221
            "columns"    => [
222
                [
223
                    "cellType" => "td",
224
                    "data"     => "name",
225
                    "name"     => "Name",
226
                ],
227
                [
228
                    "cellType" => "td",
229
                    "data"     => "position",
230
                    "name"     => "Position",
231
                ],
232
                [
233
                    "cellType" => "td",
234
                    "data"     => "office",
235
                    "name"     => "Office",
236
                ],
237
                [
238
                    "cellType" => "td",
239
                    "data"     => "age",
240
                    "name"     => "Age",
241
                ],
242
                [
243
                    "cellType" => "td",
244
                    "data"     => "startDate",
245
                    "name"     => "Start date",
246
                ],
247
                [
248
                    "cellType" => "td",
249
                    "data"     => "salary",
250
                    "name"     => "Salary",
251
                ],
252
                [
253
                    "cellType"   => "td",
254
                    "data"       => "actions",
255
                    "name"       => "Actions",
256
                    "orderable"  => false,
257
                    "searchable" => false,
258
                ],
259
            ],
260
            "processing" => true,
261
            "serverSide" => true,
262
        ];
263
        $this->assertSame($res, $obj->jQueryDataTablesOptionsFunction($this->dtWrapper));
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...TablesOptionsFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

263
        $this->assertSame($res, $obj->jQueryDataTablesOptionsFunction(/** @scrutinizer ignore-type */ $this->dtWrapper));
Loading history...
264
    }
265
266
    /**
267
     * Test jQueryDataTablesStandaloneFunction()
268
     *
269
     * @return void
270
     * @throws Throwable Throws an exception if an error occurs.
271
     */
272
    public function testJQueryDataTablesStandaloneFunction(): void {
273
274
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
275
276
        $arg = ["selector" => "#selector", "language" => "French", "options" => ["columnDefs" => [["orderable" => false, "targets" => -1]]]];
277
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testJQueryDataTablesStandaloneFunction.html.txt");
278
        $this->assertEquals($res, $obj->jQueryDataTablesStandaloneFunction($arg) . "\n");
279
    }
280
281
    /**
282
     * Test jQueryDataTablesStandaloneFunction()
283
     *
284
     * @return void
285
     * @throws Throwable Throws an exception if an error occurs.
286
     */
287
    public function testJQueryDataTablesStandaloneFunctionWithoutArguments(): void {
288
289
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
290
291
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testJQueryDataTablesStandaloneFunctionWithoutArguments.html.txt");
292
        $this->assertEquals($res, $obj->jQueryDataTablesStandaloneFunction() . "\n");
293
    }
294
295
    /**
296
     * Test renderDataTablesFunction()
297
     *
298
     * @return void
299
     */
300
    public function testRenderDataTablesFunction(): void {
301
302
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
303
304
        $i = 0;
305
        foreach ($this->dtWrapper->getColumns() as $dtColumn) {
0 ignored issues
show
Bug introduced by
The method getColumns() does not exist on null. ( Ignorable by Annotation )

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

305
        foreach ($this->dtWrapper->/** @scrutinizer ignore-call */ getColumns() as $dtColumn) {

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...
306
            $dtColumn->setClassname($dtColumn->getData());
307
            $dtColumn->setWidth((string) ++$i);
308
        }
309
310
        $arg = ["class" => "class", "thead" => true, "tfoot" => true];
311
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testRenderDataTablesFunction.html.txt");
312
        $this->assertEquals($res, $obj->renderDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...derDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

312
        $this->assertEquals($res, $obj->renderDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
313
    }
314
315
    /**
316
     * Test renderDataTablesFunction()
317
     *
318
     * @return void
319
     */
320
    public function testRenderDataTablesFunctionWithClass(): void {
321
322
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
323
324
        $arg = ["class" => "class"];
325
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testRenderDataTablesFunctionWithClass.html.txt");
326
        $this->assertEquals($res, $obj->renderDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...derDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

326
        $this->assertEquals($res, $obj->renderDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
327
    }
328
329
    /**
330
     * Test renderDataTablesFunction()
331
     *
332
     * @return void
333
     */
334
    public function testRenderDataTablesFunctionWithTFoot(): void {
335
336
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
337
338
        $arg = ["tfoot" => false];
339
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testRenderDataTablesFunctionWithTFoot.html.txt");
340
        $this->assertEquals($res, $obj->renderDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...derDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

340
        $this->assertEquals($res, $obj->renderDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
341
    }
342
343
    /**
344
     * Test renderDataTablesFunction()
345
     *
346
     * @return void
347
     */
348
    public function testRenderDataTablesFunctionWithTHead(): void {
349
350
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
351
352
        $arg = ["thead" => false];
353
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testRenderDataTablesFunctionWithTHead.html.txt");
354
        $this->assertEquals($res, $obj->renderDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...derDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

354
        $this->assertEquals($res, $obj->renderDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
355
    }
356
357
    /**
358
     * Test renderDataTablesFunction()
359
     *
360
     * @return void
361
     */
362
    public function testRenderDataTablesFunctionWithoutArguments(): void {
363
364
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
365
366
        $arg = [];
367
        $res = file_get_contents(__DIR__ . "/DataTablesTwigExtensionTest.testRenderDataTablesFunctionWithoutArguments.html.txt");
368
        $this->assertEquals($res, $obj->renderDataTablesFunction($this->dtWrapper, $arg) . "\n");
0 ignored issues
show
Bug introduced by
It seems like $this->dtWrapper can also be of type null; however, parameter $dtWrapper of WBW\Bundle\JQuery\DataTa...derDataTablesFunction() does only seem to accept WBW\Bundle\JQuery\DataTa...aTablesWrapperInterface, 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

368
        $this->assertEquals($res, $obj->renderDataTablesFunction(/** @scrutinizer ignore-type */ $this->dtWrapper, $arg) . "\n");
Loading history...
369
    }
370
371
    /**
372
     * Test __construct()
373
     *
374
     * @return void
375
     */
376
    public function test__construct(): void {
377
378
        $this->assertEquals("wbw.jquery.datatables.twig.extension", DataTablesTwigExtension::SERVICE_NAME);
379
380
        $obj = new DataTablesTwigExtension($this->twigEnvironment, $this->assetsTwigExtension, "test");
381
382
        $this->assertInstanceOf(ExtensionInterface::class, $obj);
383
384
        $this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment());
385
    }
386
}
387