Issues (117)

Tests/Provider/AbstractDataTablesProviderTest.php (7 issues)

Labels
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-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\JQuery\DataTablesBundle\Tests\Provider;
13
14
use DateTime;
15
use InvalidArgumentException;
16
use Throwable;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension;
18
use WBW\Bundle\CoreBundle\Tests\TestCaseHelper;
19
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOptionsInterface;
20
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
21
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
22
use WBW\Bundle\JQuery\DataTablesBundle\Renderer\ColumnWidthInterface;
23
use WBW\Bundle\JQuery\DataTablesBundle\Tests\AbstractTestCase;
24
use WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Entity\Employee;
25
use WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Provider\TestDataTablesProvider;
26
27
/**
28
 * Abstract DataTables provider test.
29
 *
30
 * @author webeweb <https://github.com/webeweb>
31
 * @package WBW\Bundle\JQuery\DataTablesBundle\Tests\Provider
32
 */
33
class AbstractDataTablesProviderTest extends AbstractTestCase {
34
35
    /**
36
     * Button Twig extension.
37
     *
38
     * @var ButtonTwigExtension
39
     */
40
    private $buttonTwigExtension;
41
42
    /**
43
     * DataTable provider.
44
     *
45
     * @var TestDataTablesProvider
46
     */
47
    private $dataTablesProvider;
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    protected function setUp(): void {
53
        parent::setUp();
54
55
        // Set a generate() callback.
56
        $generate = TestCaseHelper::getRouterGenerateFunction();
57
58
        // Set the Router mock.
59
        $this->router->expects($this->any())->method("generate")->willReturnCallback($generate);
0 ignored issues
show
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

59
        $this->router->/** @scrutinizer ignore-call */ 
60
                       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...
60
61
        // Set a Button Twig extension mock.
62
        $this->buttonTwigExtension = new ButtonTwigExtension($this->twigEnvironment);
63
64
        // Set a DataTables provider mock.
65
        $this->dataTablesProvider = new TestDataTablesProvider($this->router, $this->translator, $this->buttonTwigExtension);
66
    }
67
68
    /**
69
     * Test getCSVExporter()
70
     *
71
     * @return void
72
     */
73
    public function testGetCSVExporter(): void {
74
75
        $obj = $this->dataTablesProvider;
76
77
        $this->assertNull($obj->getCSVExporter());
0 ignored issues
show
Are you sure the usage of $obj->getCSVExporter() targeting WBW\Bundle\JQuery\DataTa...vider::getCSVExporter() 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...
78
    }
79
80
    /**
81
     * Test getEditor()
82
     *
83
     * @return void
84
     */
85
    public function testGetEditor(): void {
86
87
        $obj = $this->dataTablesProvider;
88
89
        $this->assertNull($obj->getEditor());
0 ignored issues
show
Are you sure the usage of $obj->getEditor() targeting WBW\Bundle\JQuery\DataTa...esProvider::getEditor() 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...
90
    }
91
92
    /**
93
     * Test getMethod()
94
     *
95
     * @return void
96
     */
97
    public function testGetMethod(): void {
98
99
        $obj = $this->dataTablesProvider;
100
101
        $this->assertNull($obj->getMethod());
0 ignored issues
show
Are you sure the usage of $obj->getMethod() targeting WBW\Bundle\JQuery\DataTa...esProvider::getMethod() 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...
102
    }
103
104
    /**
105
     * Test getOptions()
106
     *
107
     * @return void
108
     */
109
    public function testGetOptions(): void {
110
111
        $obj = $this->dataTablesProvider;
112
113
        $res = $obj->getOptions();
114
        $this->assertInstanceOf(DataTablesOptionsInterface::class, $res);
115
116
        $this->assertCount(3, $res->getOptions());
117
118
        $this->assertEquals([[0, "asc"]], $res->getOption("order"));
119
        $this->assertTrue($res->getOption("responsive"));
120
        $this->assertEquals(1000, $res->getOption("searchDelay"));
121
    }
122
123
    /**
124
     * Test renderActionButtonComment()
125
     *
126
     * @return void
127
     */
128
    public function testRenderActionButtonCommentWithComment(): void {
129
130
        $obj = $this->dataTablesProvider;
131
132
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonCommentWithComment.html.txt");
133
        $this->assertEquals($res, $obj->renderActionButtonComment(new Employee(), "commentRoute", "comment"));
134
    }
135
136
    /**
137
     * Test renderActionButtonComment()
138
     *
139
     * @return void
140
     */
141
    public function testRenderActionButtonCommentWithoutComment(): void {
142
143
        $obj = $this->dataTablesProvider;
144
145
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonCommentWithoutComment.html.txt");
146
        $this->assertEquals($res, $obj->renderActionButtonComment(new Employee(), "commentRoute", null));
147
    }
148
149
    /**
150
     * Test renderActionButtonDelete()
151
     *
152
     * @return void
153
     */
154
    public function testRenderActionButtonDelete(): void {
155
156
        $obj = $this->dataTablesProvider;
157
158
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonDelete.html.txt");
159
        $this->assertEquals($res, $obj->renderActionButtonDelete(new Employee(), "deleteRoute"));
160
    }
161
162
    /**
163
     * Test renderActionButtonDuplicate()
164
     *
165
     * @return void
166
     */
167
    public function testRenderActionButtonDuplicate(): void {
168
169
        $obj = $this->dataTablesProvider;
170
171
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonDuplicate.html.txt");
172
        $this->assertEquals($res, $obj->renderActionButtonDuplicate(new Employee(), "duplicateRoute"));
173
    }
174
175
    /**
176
     * Test renderActionButtonEdit()
177
     *
178
     * @return void
179
     */
180
    public function testRenderActionButtonEdit(): void {
181
182
        $obj = $this->dataTablesProvider;
183
184
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonEdit.html.txt");
185
        $this->assertEquals($res, $obj->renderActionButtonEdit(new Employee(), "editRoute"));
186
    }
187
188
    /**
189
     * Test renderActionButtonNew()
190
     *
191
     * @return void
192
     */
193
    public function testRenderActionButtonNew(): void {
194
195
        $obj = $this->dataTablesProvider;
196
197
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonNew.html.txt");
198
        $this->assertEquals($res, $obj->renderActionButtonNew(new Employee(), "newRoute"));
199
    }
200
201
    /**
202
     * Test renderActionButtonNew()
203
     *
204
     * @return void
205
     */
206
    public function testRenderActionButtonNewWithNullEntity(): void {
207
208
        $obj = $this->dataTablesProvider;
209
210
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonNew.html.txt");
211
        $this->assertEquals($res, $obj->renderActionButtonNew(null, "newRoute"));
212
    }
213
214
    /**
215
     * Test renderActionButtonPdf()
216
     *
217
     * @return void
218
     */
219
    public function testRenderActionButtonPdf(): void {
220
221
        $obj = $this->dataTablesProvider;
222
223
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonPdf.html.txt");
224
        $this->assertEquals($res, $obj->renderActionButtonPdf(new Employee(), "pdfRoute"));
225
    }
226
227
    /**
228
     * Test renderActionButtonShow()
229
     *
230
     * @return void
231
     */
232
    public function testRenderActionButtonShow(): void {
233
234
        $obj = $this->dataTablesProvider;
235
236
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonShow.html.txt");
237
        $this->assertEquals($res, $obj->renderActionButtonShow(new Employee(), "showRoute"));
238
    }
239
240
    /**
241
     * Test renderActionButtonSwitch()
242
     *
243
     * @return void
244
     */
245
    public function testRenderActionButtonSwitchWithFalse(): void {
246
247
        $obj = $this->dataTablesProvider;
248
249
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonSwitchWithFalse.html.txt");
250
        $this->assertEquals($res, $obj->renderActionButtonSwitch(new Employee(), "switchRoute", false));
251
    }
252
253
    /**
254
     * Test renderActionButtonSwitch()
255
     *
256
     * @return void
257
     */
258
    public function testRenderActionButtonSwitchWithTrue(): void {
259
260
        $obj = $this->dataTablesProvider;
261
262
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonSwitchWithTrue.html.txt");
263
        $this->assertEquals($res, $obj->renderActionButtonSwitch(new Employee(), "switchRoute", true));
264
    }
265
266
    /**
267
     * Test renderActionButton()
268
     *
269
     * @return void
270
     */
271
    public function testRenderActionButtonWithInvalidArgumentException(): void {
272
273
        $obj = $this->dataTablesProvider;
274
275
        try {
276
277
            $obj->renderActionButtonDelete($this, "route");
278
        } catch (Throwable $ex) {
279
280
            $this->assertInstanceOf(InvalidArgumentException::class, $ex);
281
            $this->assertEquals("The entity must implements DataTablesEntityInterface or declare a getId() method", $ex->getMessage());
282
        }
283
    }
284
285
    /**
286
     * Test renderButtons()
287
     *
288
     * @return void
289
     */
290
    public function testRenderButtons(): void {
291
292
        $obj = $this->dataTablesProvider;
293
294
        $res = implode(" ", [
295
            file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonEdit.html.txt"),
296
            file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonDelete.html.txt"),
297
        ]);
298
299
        $res = str_replace("deleteRoute", "wbw_jquery_datatables_delete", $res);
300
        $this->assertEquals($res, $obj->renderButtons(new Employee(), "editRoute"));
301
    }
302
303
    /**
304
     * Test renderDate()
305
     *
306
     * @return void
307
     * @throws Throwable Throws an exception if an error occurs.
308
     */
309
    public function testRenderDate(): void {
310
311
        $obj = $this->dataTablesProvider;
312
313
        $this->assertNull($obj->renderDate(null));
0 ignored issues
show
Are you sure the usage of $obj->renderDate(null) targeting WBW\Bundle\JQuery\DataTa...sProvider::renderDate() 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...
314
        $this->assertEquals("14/01/2019", $obj->renderDate(new DateTime("2019-01-14")));
315
        $this->assertEquals("14-01-2019", $obj->renderDate(new DateTime("2019-01-14"), "d-m-Y"));
316
    }
317
318
    /**
319
     * Test renderDateTime()
320
     *
321
     * @return void
322
     * @throws Throwable Throws an exception if an error occurs.
323
     */
324
    public function testRenderDateTime(): void {
325
326
        $obj = $this->dataTablesProvider;
327
328
        $this->assertNull($obj->renderDateTime(null));
0 ignored issues
show
Are you sure the usage of $obj->renderDateTime(null) targeting WBW\Bundle\JQuery\DataTa...vider::renderDateTime() 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...
329
        $this->assertEquals("14/01/2019 18:15", $obj->renderDateTime(new DateTime("2019-01-14 18:15:00")));
330
        $this->assertEquals("14-01-2019 18h15", $obj->renderDateTime(new DateTime("2019-01-14 18:15:00"), "d-m-Y H\hi"));
331
    }
332
333
    /**
334
     * Test the renderFloat() methods.
335
     *
336
     * @return void
337
     */
338
    public function testRenderFloat(): void {
339
340
        $obj = $this->dataTablesProvider;
341
342
        $this->assertNull($obj->renderFloat(null));
0 ignored issues
show
Are you sure the usage of $obj->renderFloat(null) targeting WBW\Bundle\JQuery\DataTa...Provider::renderFloat() 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...
343
        $this->assertEquals("1,000.00", $obj->renderFloat(1000));
344
        $this->assertEquals("1,000.000", $obj->renderFloat(1000, 3));
345
        $this->assertEquals("1 000,000", $obj->renderFloat(1000, 3, ",", " "));
346
    }
347
348
    /**
349
     * Test renderRow()
350
     *
351
     * @return void
352
     */
353
    public function testRenderRow(): void {
354
355
        // Set an Entity mock.
356
        $entity = $this->getMockBuilder(Employee::class)->getMock();
357
        $entity->expects($this->any())->method("getId")->willReturn(1);
358
359
        $obj = $this->dataTablesProvider;
360
361
        $this->assertEquals(["data-id" => 1], $obj->renderRow(DataTablesResponseInterface::DATATABLES_ROW_ATTR, $entity, 0));
362
        $this->assertNull($obj->renderRow(DataTablesResponseInterface::DATATABLES_ROW_CLASS, $entity, 0));
363
        $this->assertEquals(["pkey" => 1], $obj->renderRow(DataTablesResponseInterface::DATATABLES_ROW_DATA, $entity, 0));
364
        $this->assertEquals("test-1", $obj->renderRow(DataTablesResponseInterface::DATATABLES_ROW_ID, $entity, 0));
365
    }
366
367
    /**
368
     * Test renderRowButtons()
369
     *
370
     * @return void
371
     */
372
    public function testRenderRowButtons(): void {
373
374
        $obj = $this->dataTablesProvider;
375
376
        $res = implode(" ", [
377
            file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonShow.html.txt"),
378
            file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonEdit.html.txt"),
379
            file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonDelete.html.txt"),
380
        ]);
381
        $this->assertEquals($res, $obj->renderRowButtons(new Employee(), "editRoute", "deleteRoute", "showRoute"));
382
    }
383
384
    /**
385
     * Test renderRowButtons()
386
     *
387
     * @return void
388
     */
389
    public function testRenderRowButtonsWithDeleteRoute(): void {
390
391
        $obj = $this->dataTablesProvider;
392
393
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonDelete.html.txt");
394
        $this->assertEquals($res, $obj->renderRowButtons(new Employee(), null, "deleteRoute"));
395
    }
396
397
    /**
398
     * Test renderRowButtons()
399
     *
400
     * @return void
401
     */
402
    public function testRenderRowButtonsWithEditRoute(): void {
403
404
        $obj = $this->dataTablesProvider;
405
406
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonEdit.html.txt");
407
        $this->assertEquals($res, $obj->renderRowButtons(new Employee(), "editRoute"));
408
    }
409
410
    /**
411
     * Test renderRowButtons()
412
     *
413
     * @return void
414
     */
415
    public function testRenderRowButtonsWithShowRoute(): void {
416
417
        $obj = $this->dataTablesProvider;
418
419
        $res = file_get_contents(__DIR__ . "/AbstractDataTablesProviderTest.testRenderActionButtonShow.html.txt");
420
        $this->assertEquals($res, $obj->renderRowButtons(new Employee(), null, null, "showRoute"));
421
    }
422
423
    /**
424
     * Test wrapContent()
425
     *
426
     * @return void
427
     */
428
    public function testWrapContent(): void {
429
430
        $obj = $this->dataTablesProvider;
431
432
        $this->assertEquals("content", $obj->wrapContent(null, "content", null));
433
        $this->assertEquals("prefix-content", $obj->wrapContent("prefix-", "content", null));
434
        $this->assertEquals("prefix-content-suffix", $obj->wrapContent("prefix-", "content", "-suffix"));
435
        $this->assertEquals("content-suffix", $obj->wrapContent(null, "content", "-suffix"));
436
    }
437
438
    /**
439
     * Test __construct()
440
     *
441
     * @return void
442
     */
443
    public function test__construct(): void {
444
445
        $obj = $this->dataTablesProvider;
446
447
        $this->assertInstanceOf(DataTablesProviderInterface::class, $obj);
448
        $this->assertInstanceOf(ColumnWidthInterface::class, $obj);
449
450
        $this->assertSame($this->buttonTwigExtension, $obj->getButtonTwigExtension());
451
        $this->assertSame($this->router, $obj->getRouter());
452
        $this->assertSame($this->translator, $obj->getTranslator());
453
    }
454
}
455