|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSFS\tests\base\dto; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use PSFS\base\dto\Field; |
|
7
|
|
|
use PSFS\base\dto\Form; |
|
8
|
|
|
use PSFS\base\dto\FormAction; |
|
9
|
|
|
use PSFS\base\dto\JsonResponse; |
|
10
|
|
|
use PSFS\base\dto\Order; |
|
11
|
|
|
use PSFS\base\dto\ProfilingJsonResponse; |
|
12
|
|
|
use PSFS\tests\examples\ComplexDto; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class DtoTest |
|
16
|
|
|
* @package PSFS\tests\base\dto |
|
17
|
|
|
*/ |
|
18
|
|
|
class DtoTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
private function getExampleData() |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
|
|
'fields' => [ |
|
25
|
|
|
['name' => '3.hidden', 'label' => 'Hidden field', 'type' => Field::HIDDEN_TYPE, 'required' => false], |
|
26
|
|
|
['name' => '4.text', 'label' => 'Text field', 'type' => Field::TEXT_TYPE, 'required' => false], |
|
27
|
|
|
['name' => '2.number', 'label' => 'Number field', 'type' => Field::NUMBER_TYPE, 'required' => true], |
|
28
|
|
|
['name' => '5.textarea', 'label' => 'Textarea field', 'type' => Field::TEXTAREA_TYPE, 'required' => false], |
|
29
|
|
|
['name' => '6.date', 'label' => 'Date field', 'type' => Field::DATE, 'required' => false], |
|
30
|
|
|
['name' => '1.test6', 'label' => 'Test 1', 'type' => Field::TEXT_TYPE, 'required' => true], |
|
31
|
|
|
], |
|
32
|
|
|
'actions' => [ |
|
33
|
|
|
['label' => 'test1', 'url' => 'test1', 'method' => 'test1'], |
|
34
|
|
|
['label' => 'test2', 'url' => 'test2', 'method' => 'test2'], |
|
35
|
|
|
['label' => 'test3', 'url' => 'test3', 'method' => 'test3'], |
|
36
|
|
|
], |
|
37
|
|
|
'order' => [ |
|
38
|
|
|
'test' => Order::ASC, |
|
39
|
|
|
'test2' => Order::DESC, |
|
40
|
|
|
], |
|
41
|
|
|
'boolean' => true, |
|
42
|
|
|
'number' => 5, |
|
43
|
|
|
'decimal' => 3.1415, |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Test for Form and Field Dto |
|
49
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
50
|
|
|
* @throws \ReflectionException |
|
51
|
|
|
* @covers \PSFS\base\dto\Form |
|
52
|
|
|
* @covers \PSFS\base\dto\Field |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testFormDto() |
|
55
|
|
|
{ |
|
56
|
|
|
$form1 = new Form(true); |
|
57
|
|
|
$form2 = new Form(); |
|
58
|
|
|
$this->assertEquals($form1->toArray(), $form2->toArray(), 'Error on creation for basic dto'); |
|
59
|
|
|
$this->assertEquals($form1->actions, $form2->actions, 'Different actions in dto from scratch'); |
|
60
|
|
|
$this->assertEquals($form1->fieldExists('test'), $form2->fieldExists('test'), 'Different check for fields that not exists'); |
|
61
|
|
|
|
|
62
|
|
|
$field = new Field('test', 'test'); |
|
63
|
|
|
$form1->addField($field); |
|
64
|
|
|
$this->assertTrue($form1->fieldExists($field->name), 'Error adding new field to a form'); |
|
65
|
|
|
$this->assertNotEquals($form1->toArray(), $form2->toArray(), 'Bad extraction for form data'); |
|
66
|
|
|
|
|
67
|
|
|
$reflection = new \ReflectionClass(Field::class); |
|
68
|
|
|
foreach ($reflection->getConstants() as $constant) { |
|
69
|
|
|
$this->assertNotNull(Form::fieldsOrder(['type' => $constant])); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return void |
|
75
|
|
|
* @throws \ReflectionException |
|
76
|
|
|
* @covers \PSFS\base\dto\Form |
|
77
|
|
|
* @covers \PSFS\base\dto\FormAction |
|
78
|
|
|
* @covers \PSFS\base\dto\Field |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testPopulatingDto() |
|
81
|
|
|
{ |
|
82
|
|
|
// Populating data for more testing |
|
83
|
|
|
$form1 = new Form(true); |
|
84
|
|
|
$form2 = new Form(); |
|
85
|
|
|
$exampleData = $this->getExampleData(); |
|
86
|
|
|
$emptyAction = new FormAction(false); |
|
87
|
|
|
foreach ($exampleData['actions'] as $actionData) { |
|
88
|
|
|
$action = clone $emptyAction; |
|
89
|
|
|
$action->fromArray($actionData); |
|
90
|
|
|
$form1->actions[] = $action; |
|
91
|
|
|
$form2->actions[] = $action; |
|
92
|
|
|
} |
|
93
|
|
|
foreach ($exampleData['fields'] as $fieldData) { |
|
94
|
|
|
$field = new Field($fieldData['name'], $fieldData['label'], $fieldData['type']); |
|
95
|
|
|
$field->required = $fieldData['required']; |
|
96
|
|
|
$form1->addField($field); |
|
97
|
|
|
$form2->addField($field); |
|
98
|
|
|
$this->assertEquals($form1->fieldExists($field->name), $form2->fieldExists($field->name), 'Error adding new field'); |
|
99
|
|
|
} |
|
100
|
|
|
$formExportData = $form1->toArray(); |
|
101
|
|
|
$this->assertEquals($formExportData, $form2->toArray(), 'Error on export for complex dto'); |
|
102
|
|
|
$this->assertEquals($form1->actions, $form2->actions, 'Different actions in dto with populated data'); |
|
103
|
|
|
/** |
|
104
|
|
|
* Checking order |
|
105
|
|
|
* First required, then see into Form dto method |
|
106
|
|
|
* In this case, the order is before the field name --> 1.number, 6.test6, 3.hidden, 4.text, 5.textarea, 6.date |
|
107
|
|
|
*/ |
|
108
|
|
|
$order = ['1.test6', '2.number', '3.hidden', '4.text', '5.textarea', '6.date']; |
|
109
|
|
|
foreach ($order as $index => $field) { |
|
110
|
|
|
$this->assertEquals($field, $formExportData['fields'][$index]['name'], 'Order is not right'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Test Dto basics |
|
116
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
117
|
|
|
* @throws \ReflectionException |
|
118
|
|
|
* @covers \PSFS\base\dto\Form |
|
119
|
|
|
* @covers \PSFS\base\dto\Order |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testDtoBasics() |
|
122
|
|
|
{ |
|
123
|
|
|
// Initialize classes to test |
|
124
|
|
|
$action = new FormAction(false); |
|
125
|
|
|
$action2 = clone $action; |
|
126
|
|
|
$emptyAction = clone $action; |
|
127
|
|
|
$order = new Order(false); |
|
128
|
|
|
$order2 = clone $order; |
|
129
|
|
|
$emptyOrder = clone $order; |
|
130
|
|
|
$complextDto = new ComplexDto(false); |
|
131
|
|
|
$complextDto2 = clone $complextDto; |
|
132
|
|
|
|
|
133
|
|
|
$this->assertEquals($action, $action2, 'Error cloning dtos'); |
|
134
|
|
|
$this->assertEquals($order, $order2, 'Error cloning dtos'); |
|
135
|
|
|
$this->assertEquals($complextDto, $complextDto2, 'Error cloning dtos'); |
|
136
|
|
|
|
|
137
|
|
|
// Populate tests |
|
138
|
|
|
$exampleData = $this->getExampleData(); |
|
139
|
|
|
|
|
140
|
|
|
// Manual creation vs hydration |
|
141
|
|
|
$action->fromArray($exampleData['actions'][0]); |
|
142
|
|
|
$action2->label = 'test1'; |
|
143
|
|
|
$action2->url = 'test1'; |
|
144
|
|
|
$action2->method = 'test1'; |
|
145
|
|
|
$this->assertEquals($action, $action2, 'Different values from hydration'); |
|
146
|
|
|
$this->assertEquals($action->toArray(), $action2->toArray(), 'Different values on export'); |
|
147
|
|
|
|
|
148
|
|
|
$order->fromArray($exampleData['order']); |
|
149
|
|
|
foreach ($exampleData['order'] as $field => $ord) { |
|
150
|
|
|
$order2->addOrder($field, $ord); |
|
151
|
|
|
} |
|
152
|
|
|
$this->assertEquals($order, $order2, 'Different values from hydration'); |
|
153
|
|
|
$this->assertEquals($order->toArray(), $order2->toArray(), 'Different values on export'); |
|
154
|
|
|
$order2->removeOrder('test'); |
|
155
|
|
|
$this->assertNotEquals($order, $order2, 'Remove field order failed in object'); |
|
156
|
|
|
$this->assertNotEquals($order->toArray(), $order2->toArray(), 'Remove field order failed as array'); |
|
157
|
|
|
|
|
158
|
|
|
// Multiple creation for dtos from array to create complex dto |
|
159
|
|
|
$action1 = clone $emptyAction; |
|
160
|
|
|
$action1->fromArray($exampleData['actions'][0]); |
|
161
|
|
|
$complextDto->actions[] = $action1; |
|
162
|
|
|
$action2 = clone $emptyAction; |
|
163
|
|
|
$action2->fromArray($exampleData['actions'][1]); |
|
164
|
|
|
$complextDto->actions[] = $action2; |
|
165
|
|
|
$action3 = clone $emptyAction; |
|
166
|
|
|
$action3->fromArray($exampleData['actions'][2]); |
|
167
|
|
|
$complextDto->actions[] = $action3; |
|
168
|
|
|
$emptyOrder->setOrder('test', Order::ASC); |
|
169
|
|
|
$this->assertCount(1, $emptyOrder->getOrders(), 'Distinct number or orders created'); |
|
170
|
|
|
$emptyOrder->addOrder('test2', Order::DESC); |
|
171
|
|
|
$this->assertCount(2, $emptyOrder->getOrders(), 'Distinct number or orders added'); |
|
172
|
|
|
$emptyOrder->addOrder('test3', Order::DESC); |
|
173
|
|
|
$this->assertCount(3, $emptyOrder->getOrders(), 'Distinct number or orders added'); |
|
174
|
|
|
$emptyOrder->removeOrder('test3'); |
|
175
|
|
|
$this->assertCount(2, $emptyOrder->getOrders(), 'Distinct number or orders removed'); |
|
176
|
|
|
$complextDto->order = $emptyOrder; |
|
177
|
|
|
$complextDto->boolean = $exampleData['boolean']; |
|
178
|
|
|
$complextDto->number = $exampleData['number']; |
|
179
|
|
|
$complextDto->decimal = $exampleData['decimal']; |
|
180
|
|
|
// Creation from import |
|
181
|
|
|
$complextDto2->fromArray($exampleData); |
|
182
|
|
|
$this->assertEquals($complextDto->jsonSerialize(), $complextDto2->jsonSerialize(), 'Different values on export'); |
|
183
|
|
|
$this->assertEquals($complextDto->order->jsonSerialize(), $complextDto2->order->jsonSerialize(), 'Different order values on export'); |
|
184
|
|
|
$this->assertEquals($complextDto->__toString(), $complextDto2->__toString(), 'Different values fot toString'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
189
|
|
|
* @covers \PSFS\base\dto\JsonResponse |
|
190
|
|
|
* @covers \PSFS\base\dto\ProfilingJsonResponse |
|
191
|
|
|
*/ |
|
192
|
|
|
public function testJsonResponseDto() |
|
193
|
|
|
{ |
|
194
|
|
|
$jsonResponse = new JsonResponse(); |
|
195
|
|
|
$profilling = ProfilingJsonResponse::createFromPrevious($jsonResponse, ['test' => true]); |
|
196
|
|
|
$this->assertNotEquals($jsonResponse->toArray(), $profilling->toArray(), 'Profilling error creation'); |
|
197
|
|
|
$this->assertEquals($jsonResponse->data, $profilling->data, 'Error creating profilling dto'); |
|
198
|
|
|
$this->assertEquals($jsonResponse->success, $profilling->success, 'Error creating profilling dto'); |
|
199
|
|
|
$this->assertEquals($jsonResponse->message, $profilling->message, 'Error creating profilling dto'); |
|
200
|
|
|
$this->assertEquals($jsonResponse->total, $profilling->total, 'Error creating profilling dto'); |
|
201
|
|
|
$this->assertEquals($jsonResponse->pages, $profilling->pages, 'Error creating profilling dto'); |
|
202
|
|
|
$this->assertNotEmpty($profilling->profiling, 'Error creating profilling dto'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|