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\Helper; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
15
|
|
|
use Throwable; |
16
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Entity\DataTablesEntityInterface; |
17
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesEntityHelper; |
18
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Tests\AbstractTestCase; |
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Tests\Fixtures\Entity\Employee; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* DataTables entity helper. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb> |
25
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Tests\Helper |
26
|
|
|
*/ |
27
|
|
|
class DataTablesEntityHelperTest extends AbstractTestCase { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Test indexEntities() |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
34
|
|
|
*/ |
35
|
|
|
public function testIndexEntities(): void { |
36
|
|
|
|
37
|
|
|
$count = 57; |
38
|
|
|
|
39
|
|
|
// Set the entities mock. |
40
|
|
|
$entities = []; |
41
|
|
|
for ($i = $count; 0 < $i; --$i) { |
42
|
|
|
|
43
|
|
|
$id = rand(); |
44
|
|
|
|
45
|
|
|
$buffer = $this->getMockBuilder(DataTablesEntityInterface::class)->getMock(); |
46
|
|
|
$buffer->expects($this->any())->method("getId")->willReturn($id); |
47
|
|
|
|
48
|
|
|
$entities[] = $buffer; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$entities[] = $entities[$count - 1]; |
52
|
|
|
|
53
|
|
|
$res = DataTablesEntityHelper::indexEntities($entities); |
54
|
|
|
$this->assertCount($count, $res); |
55
|
|
|
|
56
|
|
|
foreach ($entities as $current) { |
57
|
|
|
$this->assertSame($current, $res[$current->getId()]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Test isCompatible() |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function testIsCompatible(): void { |
67
|
|
|
|
68
|
|
|
// Set a DataTables entity mock. |
69
|
|
|
$dtEntity = $this->getMockBuilder(DataTablesEntityInterface::class)->getMock(); |
70
|
|
|
|
71
|
|
|
$this->assertTrue(DataTablesEntityHelper::isCompatible($dtEntity)); |
72
|
|
|
$this->assertTrue(DataTablesEntityHelper::isCompatible(new Employee())); |
73
|
|
|
$this->assertFalse(DataTablesEntityHelper::isCompatible($this)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test newSerializer() |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function testNewSerializer(): void { |
82
|
|
|
|
83
|
|
|
$res = DataTablesEntityHelper::newSerializer(); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(SerializerInterface::class, $res); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|