Issues (117)

Tests/Factory/DataTablesFactoryTest.php (1 issue)

Labels
Severity
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\Factory;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesColumnInterface;
17
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOptionsInterface;
18
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOrderInterface;
19
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesRequestInterface;
20
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
21
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesSearchInterface;
22
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesWrapperInterface;
23
use WBW\Bundle\JQuery\DataTablesBundle\Tests\AbstractTestCase;
24
use WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Factory\TestDataTablesFactory;
25
use WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\TestFixtures;
26
27
/**
28
 * DataTables factory test.
29
 *
30
 * @author webeweb <https://github.com/webeweb>
31
 * @package WBW\Bundle\JQuery\DataTablesBundle\Tests\Factory
32
 */
33
class DataTablesFactoryTest extends AbstractTestCase {
34
35
    /**
36
     * Test newResponse()
37
     *
38
     * @return void
39
     */
40
    public function testNewOptions(): void {
41
42
        $obj = TestDataTablesFactory::newOptions();
43
44
        $this->assertInstanceOf(DataTablesOptionsInterface::class, $obj);
45
    }
46
47
    /**
48
     * Test newResponse()
49
     *
50
     * @return void
51
     */
52
    public function testNewResponse(): void {
53
54
        // Set a DataTables wrapper mock.
55
        $wrapper = TestFixtures::getWrapper();
56
        $wrapper->setRequest($this->dtRequest);
57
58
        $obj = TestDataTablesFactory::newResponse($wrapper);
59
60
        $this->assertInstanceOf(DataTablesResponseInterface::class, $obj);
61
62
        $this->assertEquals([], $obj->getData());
63
        $this->assertEquals(0, $obj->getDraw());
64
        $this->assertNull($obj->getError());
65
        $this->assertEquals(0, $obj->getRecordsFiltered());
66
        $this->assertEquals(0, $obj->getRecordsTotal());
67
        $this->assertSame($wrapper, $obj->getWrapper());
68
        $this->assertEquals(0, $obj->rowsCount());
69
    }
70
71
    /**
72
     * Test newWrapper()
73
     *
74
     * @return void
75
     */
76
    public function testNewWrapper(): void {
77
78
        // Set a User mock.
79
        $user = $this->getMockBuilder(UserInterface::class)->getMock();
80
81
        $obj = TestDataTablesFactory::newWrapper("url", $this->dtProvider, $user);
0 ignored issues
show
It seems like $this->dtProvider can also be of type null; however, parameter $provider of WBW\Bundle\JQuery\DataTa...esFactory::newWrapper() does only seem to accept WBW\Bundle\JQuery\DataTa...TablesProviderInterface, 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

81
        $obj = TestDataTablesFactory::newWrapper("url", /** @scrutinizer ignore-type */ $this->dtProvider, $user);
Loading history...
82
83
        $this->assertInstanceOf(DataTablesWrapperInterface::class, $obj);
84
85
        $this->assertEquals([], $obj->getColumns());
86
        $this->assertEquals("", $obj->getMapping()->getPrefix());
87
        $this->assertEquals("POST", $obj->getMethod());
88
        $this->assertNull($obj->getOptions());
89
        $this->assertTrue($obj->getProcessing());
90
        $this->assertSame($this->dtProvider, $obj->getProvider());
91
        $this->assertNull($obj->getRequest());
92
        $this->assertNull($obj->getResponse());
93
        $this->assertTrue($obj->getServerSide());
94
        $this->assertEquals("url", $obj->getUrl());
95
        $this->assertSame($user, $obj->getUser());
96
    }
97
98
    /**
99
     * Test parseColumn()
100
     *
101
     * @return void
102
     */
103
    public function testParseColumn(): void {
104
105
        // Set a DataTables wrapper mock.
106
        $wrapper = TestFixtures::getWrapper();
107
108
        // Set the POST data mock.
109
        $postData = TestFixtures::getPostData();
110
111
        $obj = TestDataTablesFactory::parseColumn($postData["columns"][0], $wrapper);
112
113
        $this->assertInstanceOf(DataTablesColumnInterface::class, $obj);
114
115
        $this->assertEquals("name", $obj->getData());
116
        $this->assertEquals("Name", $obj->getName());
117
        $this->assertTrue($obj->getOrderable());
118
        $this->assertFalse($obj->getSearch()->getRegex());
119
        $this->assertEquals("", $obj->getSearch()->getValue());
120
        $this->assertTrue($obj->getSearchable());
121
    }
122
123
    /**
124
     * Test parseColumn()
125
     *
126
     * @return void
127
     */
128
    public function testParseColumnWithBadData(): void {
129
130
        // Set a DataTables wrapper mock.
131
        $wrapper = TestFixtures::getWrapper();
132
133
        // Set the POST data mock.
134
        $postData                       = TestFixtures::getPostData();
135
        $postData["columns"][0]["data"] = "exception";
136
137
        $obj = TestDataTablesFactory::parseColumn($postData["columns"][0], $wrapper);
138
139
        $this->assertNull($obj);
140
    }
141
142
    /**
143
     * Test parseColumn()
144
     *
145
     * @return void
146
     */
147
    public function testParseColumnWithBadName(): void {
148
149
        // Set a DataTables wrapper mock.
150
        $wrapper = TestFixtures::getWrapper();
151
152
        // Set the POST data mock.
153
        $postData                       = TestFixtures::getPostData();
154
        $postData["columns"][0]["name"] = "exception";
155
156
        $obj = TestDataTablesFactory::parseColumn($postData["columns"][0], $wrapper);
157
158
        $this->assertNull($obj);
159
    }
160
161
    /**
162
     * Test parseColumn()
163
     *
164
     * @return void
165
     */
166
    public function testParseColumnWithNoData(): void {
167
168
        // Set a DataTables wrapper mock.
169
        $wrapper = TestFixtures::getWrapper();
170
171
        // Set the POST data mock.
172
        $postData = TestFixtures::getPostData();
173
        unset($postData["columns"][0]["data"]);
174
175
        $obj = TestDataTablesFactory::parseColumn($postData["columns"][0], $wrapper);
176
177
        $this->assertNull($obj);
178
    }
179
180
    /**
181
     * Test parseColumn()
182
     *
183
     * @return void
184
     */
185
    public function testParseColumnWithNoName(): void {
186
187
        // Set a DataTables wrapper mock.
188
        $wrapper = TestFixtures::getWrapper();
189
190
        // Set the POST data mock.
191
        $postData = TestFixtures::getPostData();
192
        unset($postData["columns"][0]["name"]);
193
194
        $obj = TestDataTablesFactory::parseColumn($postData["columns"][0], $wrapper);
195
196
        $this->assertNull($obj);
197
    }
198
199
    /**
200
     * Test parseColumn()
201
     *
202
     * @return void
203
     */
204
    public function testParseColumnWithNoSearchable(): void {
205
206
        // Set a DataTables wrapper mock.
207
        $wrapper = TestFixtures::getWrapper();
208
209
        // Set the POST data mock.
210
        $postData                             = TestFixtures::getPostData();
211
        $postData["columns"][0]["searchable"] = "false";
212
213
        $obj = TestDataTablesFactory::parseColumn($postData["columns"][0], $wrapper);
214
215
        $this->assertEquals("name", $obj->getData());
216
        $this->assertEquals("Name", $obj->getName());
217
        $this->assertTrue($obj->getOrderable());
218
        $this->assertFalse($obj->getSearch()->getRegex());
219
        $this->assertEquals("", $obj->getSearch()->getValue());
220
        $this->assertTrue($obj->getSearchable());
221
    }
222
223
    /**
224
     * Test parseColumns()
225
     *
226
     * @return void
227
     */
228
    public function testParseColumns(): void {
229
230
        // Set a DataTables wrapper mock.
231
        $wrapper = TestFixtures::getWrapper();
232
233
        // Set the POST data mock.
234
        $postData = TestFixtures::getPostData();
235
236
        $obj = TestDataTablesFactory::parseColumns($postData["columns"], $wrapper);
237
238
        $this->assertCount(7, $obj);
239
    }
240
241
    /**
242
     * Test parseColumns()
243
     *
244
     * @return void
245
     */
246
    public function testParseColumnsWithNoData(): void {
247
248
        // Set a DataTables wrapper mock.
249
        $wrapper = TestFixtures::getWrapper();
250
251
        // Set the POST data mock.
252
        $postData = TestFixtures::getPostData();
253
        unset($postData["columns"][6]["name"]);
254
255
        $obj = TestDataTablesFactory::parseColumns($postData["columns"], $wrapper);
256
257
        $this->assertCount(6, $obj);
258
    }
259
260
    /**
261
     * Test parseOrder()
262
     *
263
     * @return void
264
     */
265
    public function testParseOrder(): void {
266
267
        // Set the POST data mock.
268
        $postData                       = TestFixtures::getPostData();
269
        $postData["order"][0]["column"] = "0";
270
        $postData["order"][0]["dir"]    = "asc";
271
272
        $obj = TestDataTablesFactory::parseOrder($postData["order"][0]);
273
274
        $this->assertInstanceOf(DataTablesOrderInterface::class, $obj);
275
276
        $this->assertEquals(0, $obj->getColumn());
277
        $this->assertEquals("ASC", $obj->getDir());
278
    }
279
280
    /**
281
     * Test parseOrder()
282
     *
283
     * @return void
284
     */
285
    public function testParseOrderWithInvalidDir(): void {
286
287
        // Set the POST data mock.
288
        $postData                    = TestFixtures::getPostData();
289
        $postData["order"][0]["dir"] = "exception";
290
291
        $obj = TestDataTablesFactory::parseOrder($postData["order"][0]);
292
293
        $this->assertEquals(0, $obj->getColumn());
294
        $this->assertEquals("ASC", $obj->getDir());
295
    }
296
297
    /**
298
     * Test parseOrder()
299
     *
300
     * @return void
301
     */
302
    public function testParseOrderWithNoColumn(): void {
303
304
        // Set the POST data mock.
305
        $postData = TestFixtures::getPostData();
306
        unset($postData["order"][0]["column"]);
307
308
        $obj = TestDataTablesFactory::parseOrder($postData["order"][0]);
309
310
        $this->assertNull($obj->getColumn());
311
        $this->assertNull($obj->getDir());
312
    }
313
314
    /**
315
     * Test parseOrder()
316
     *
317
     * @return void
318
     */
319
    public function testParseOrderWithNoDir(): void {
320
321
        // Set the POST data mock.
322
        $postData = TestFixtures::getPostData();
323
        unset($postData["order"][0]["dir"]);
324
325
        $obj = TestDataTablesFactory::parseOrder($postData["order"][0]);
326
327
        $this->assertNull($obj->getColumn());
328
        $this->assertNull($obj->getDir());
329
    }
330
331
    /**
332
     * Test parseOrders()
333
     *
334
     * @return void
335
     */
336
    public function testParseOrders(): void {
337
338
        // Set the POST data mock.
339
        $postData                    = TestFixtures::getPostData();
340
        $postData["order"][0]["dir"] = "exception";
341
342
        $obj = TestDataTablesFactory::parseOrders($postData["order"]);
343
344
        $this->assertCount(1, $obj);
345
        $this->assertEquals(0, $obj[0]->getColumn());
346
        $this->assertEquals("ASC", $obj[0]->getDir());
347
    }
348
349
    /**
350
     * Test parseRequest()
351
     *
352
     * @return void
353
     */
354
    public function testParseRequestWithGET(): void {
355
356
        // Set a DataTables wrapper mock.
357
        $wrapper = TestFixtures::getWrapper();
358
359
        // Set a Request mock.
360
        $request = new Request(array_merge(TestFixtures::getPostData(), ["query" => "query"]), ["request" => "request"], [], [], [], ["REQUEST_METHOD" => "GET"]);
361
362
        $obj = TestDataTablesFactory::parseRequest($wrapper, $request);
363
364
        $this->assertInstanceOf(DataTablesRequestInterface::class, $obj);
365
366
        $this->assertEquals(1, $obj->getDraw());
367
        $this->assertEquals(10, $obj->getLength());
368
        $this->assertEquals("query", $obj->getQuery()->get("query"));
369
        $this->assertEquals("request", $obj->getRequest()->get("request"));
370
        $this->assertFalse($obj->getSearch()->getRegex());
371
        $this->assertEquals("", $obj->getSearch()->getValue());
372
        $this->assertEquals(0, $obj->getStart());
373
374
        $this->assertCount(7, $obj->getColumns());
375
    }
376
377
    /**
378
     * Test parseRequest()
379
     *
380
     * @return void
381
     */
382
    public function testParseRequestWithPOST(): void {
383
384
        // Set a DataTables wrapper mock.
385
        $wrapper = TestFixtures::getWrapper();
386
387
        // Set a Request mock.
388
        $request = new Request(["query" => "query"], array_merge(TestFixtures::getPostData(), ["request" => "request"]), [], [], [], ["REQUEST_METHOD" => "POST"]);
389
390
        $obj = TestDataTablesFactory::parseRequest($wrapper, $request);
391
392
        $this->assertInstanceOf(DataTablesRequestInterface::class, $obj);
393
394
        $this->assertEquals(1, $obj->getDraw());
395
        $this->assertEquals(10, $obj->getLength());
396
        $this->assertEquals("query", $obj->getQuery()->get("query"));
397
        $this->assertEquals("request", $obj->getRequest()->get("request"));
398
        $this->assertFalse($obj->getSearch()->getRegex());
399
        $this->assertEquals("", $obj->getSearch()->getValue());
400
        $this->assertEquals(0, $obj->getStart());
401
402
        $this->assertCount(7, $obj->getColumns());
403
    }
404
405
    /**
406
     * Test parseSearch()
407
     *
408
     * @return void
409
     */
410
    public function testParseSearch(): void {
411
412
        // Set the POST data mock.
413
        $postData                    = TestFixtures::getPostData();
414
        $postData["search"]["regex"] = "true";
415
        $postData["search"]["value"] = "value";
416
417
        $obj = TestDataTablesFactory::parseSearch($postData["search"]);
418
419
        $this->assertInstanceOf(DataTablesSearchInterface::class, $obj);
420
421
        $this->assertTrue($obj->getRegex());
422
        $this->assertEquals("value", $obj->getValue());
423
    }
424
425
    /**
426
     * Test parseSearch()
427
     *
428
     * @return void
429
     */
430
    public function testParseSearchWithNoRegex(): void {
431
432
        // Set the POST data mock.
433
        $postData                    = TestFixtures::getPostData();
434
        $postData["search"]["regex"] = "false";
435
        $postData["search"]["value"] = "value";
436
        unset($postData["search"]["regex"]); // Set an invalid search.
437
438
        $obj = TestDataTablesFactory::parseSearch($postData["search"]);
439
440
        $this->assertFalse($obj->getRegex());
441
        $this->assertEquals("", $obj->getValue());
442
    }
443
444
    /**
445
     * Test parseSearch()
446
     *
447
     * @return void
448
     */
449
    public function testParseSearchWithNoValue(): void {
450
451
        // Set the POST data mock.
452
        $postData                    = TestFixtures::getPostData();
453
        $postData["search"]["regex"] = "true";
454
        $postData["search"]["value"] = "value";
455
        unset($postData["search"]["value"]); // Set an invalid search.
456
457
        $obj = TestDataTablesFactory::parseSearch($postData["search"]);
458
459
        $this->assertFalse($obj->getRegex());
460
        $this->assertEquals("", $obj->getValue());
461
    }
462
463
    /**
464
     * Test parseWrapper()
465
     *
466
     * @return void
467
     */
468
    public function testParseWrapper(): void {
469
470
        // Set a DataTables wrapper mock.
471
        $wrapper = TestFixtures::getWrapper();
472
473
        TestDataTablesFactory::parseWrapper($wrapper, $this->request);
474
475
        $this->assertNotNull($wrapper->getRequest());
476
        $this->assertNotNull($wrapper->getResponse());
477
    }
478
}
479