Passed
Pull Request — master (#28)
by Fran
08:31
created

DtoTest::testDtoBasics()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 64
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 51
nc 2
nop 0
dl 0
loc 64
rs 9.069
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     */
50
    public function testFormDto()
51
    {
52
        $form1 = new Form(true);
53
        $form2 = new Form();
54
        $this->assertEquals($form1->toArray(), $form2->toArray(), 'Error on creation for basic dto');
55
        $this->assertEquals($form1->actions, $form2->actions, 'Different actions in dto from scratch');
56
        $this->assertEquals($form1->fieldExists('test'), $form2->fieldExists('test'), 'Different check for fields that not exists');
57
58
        $field = new Field('test', 'test');
59
        $form1->addField($field);
60
        $this->assertTrue($form1->fieldExists($field->name), 'Error adding new field to a form');
61
        $this->assertNotEquals($form1->toArray(), $form2->toArray(), 'Bad extraction for form data');
62
63
        $reflection = new \ReflectionClass(Field::class);
64
        foreach ($reflection->getConstants() as $constant) {
65
            $this->assertNotNull(Form::fieldsOrder(['type' => $constant]));
66
        }
67
    }
68
69
    /**
70
     * @return void
71
     * @throws \ReflectionException
72
     */
73
    public function testPopulatingDto()
74
    {
75
        // Populating data for more testing
76
        $form1 = new Form(true);
77
        $form2 = new Form();
78
        $exampleData = $this->getExampleData();
79
        $emptyAction = new FormAction(false);
80
        foreach ($exampleData['actions'] as $actionData) {
81
            $action = clone $emptyAction;
82
            $action->fromArray($actionData);
83
            $form1->actions[] = $action;
84
            $form2->actions[] = $action;
85
        }
86
        foreach ($exampleData['fields'] as $fieldData) {
87
            $field = new Field($fieldData['name'], $fieldData['label'], $fieldData['type']);
88
            $field->required = $fieldData['required'];
89
            $form1->addField($field);
90
            $form2->addField($field);
91
            $this->assertEquals($form1->fieldExists($field->name), $form2->fieldExists($field->name), 'Error adding new field');
92
        }
93
        $formExportData = $form1->toArray();
94
        $this->assertEquals($formExportData, $form2->toArray(), 'Error on export for complex dto');
95
        $this->assertEquals($form1->actions, $form2->actions, 'Different actions in dto with populated data');
96
        /**
97
         * Checking order
98
         * First required, then see into Form dto method
99
         * In this case, the order is before the field name --> 1.number, 6.test6, 3.hidden, 4.text, 5.textarea, 6.date
100
         */
101
        $order = ['1.test6', '2.number', '3.hidden', '4.text', '5.textarea', '6.date'];
102
        foreach ($order as $index => $field) {
103
            $this->assertEquals($field, $formExportData['fields'][$index]['name'], 'Order is not right');
104
        }
105
    }
106
107
    /**
108
     * Test Dto basics
109
     * @throws \ReflectionException
110
     */
111
    public function testDtoBasics()
112
    {
113
        // Initialize classes to test
114
        $action = new FormAction(false);
115
        $action2 = clone $action;
116
        $emptyAction = clone $action;
117
        $order = new Order(false);
118
        $order2 = clone $order;
119
        $emptyOrder = clone $order;
120
        $complextDto = new ComplexDto(false);
121
        $complextDto2 = clone $complextDto;
122
123
        $this->assertEquals($action, $action2, 'Error cloning dtos');
124
        $this->assertEquals($order, $order2, 'Error cloning dtos');
125
        $this->assertEquals($complextDto, $complextDto2, 'Error cloning dtos');
126
127
        // Populate tests
128
        $exampleData = $this->getExampleData();
129
130
        // Manual creation vs hydration
131
        $action->fromArray($exampleData['actions'][0]);
132
        $action2->label = 'test1';
133
        $action2->url = 'test1';
134
        $action2->method = 'test1';
135
        $this->assertEquals($action, $action2, 'Different values from hydration');
136
        $this->assertEquals($action->toArray(), $action2->toArray(), 'Different values on export');
137
138
        $order->fromArray($exampleData['order']);
139
        foreach ($exampleData['order'] as $field => $ord) {
140
            $order2->addOrder($field, $ord);
141
        }
142
        $this->assertEquals($order, $order2, 'Different values from hydration');
143
        $this->assertEquals($order->toArray(), $order2->toArray(), 'Different values on export');
144
        $order2->removeOrder('test');
145
        $this->assertNotEquals($order, $order2, 'Remove field order failed in object');
146
        $this->assertNotEquals($order->toArray(), $order2->toArray(), 'Remove field order failed as array');
147
148
        // Multiple creation for dtos from array to create complex dto
149
        $action1 = clone $emptyAction;
150
        $action1->fromArray($exampleData['actions'][0]);
151
        $complextDto->actions[] = $action1;
152
        $action2 = clone $emptyAction;
153
        $action2->fromArray($exampleData['actions'][1]);
154
        $complextDto->actions[] = $action2;
155
        $action3 = clone $emptyAction;
156
        $action3->fromArray($exampleData['actions'][2]);
157
        $complextDto->actions[] = $action3;
158
        $emptyOrder->setOrder('test', Order::ASC);
159
        $this->assertCount(1, $emptyOrder->getOrders(), 'Distinct number or orders created');
160
        $emptyOrder->addOrder('test2', Order::DESC);
161
        $this->assertCount(2, $emptyOrder->getOrders(), 'Distinct number or orders added');
162
        $emptyOrder->addOrder('test3', Order::DESC);
163
        $this->assertCount(3, $emptyOrder->getOrders(), 'Distinct number or orders added');
164
        $emptyOrder->removeOrder('test3');
165
        $this->assertCount(2, $emptyOrder->getOrders(), 'Distinct number or orders removed');
166
        $complextDto->order = $emptyOrder;
167
        $complextDto->boolean = $exampleData['boolean'];
168
        $complextDto->number = $exampleData['number'];
169
        $complextDto->decimal = $exampleData['decimal'];
170
        // Creation from import
171
        $complextDto2->fromArray($exampleData);
172
        $this->assertEquals($complextDto->jsonSerialize(), $complextDto2->jsonSerialize(), 'Different values on export');
173
        $this->assertEquals($complextDto->order->jsonSerialize(), $complextDto2->order->jsonSerialize(), 'Different order values on export');
174
        $this->assertEquals($complextDto->__toString(), $complextDto2->__toString(), 'Different values fot toString');
175
    }
176
177
    public function testJsonResponseDto()
178
    {
179
        $jsonResponse = new JsonResponse();
180
        $profilling = ProfilingJsonResponse::createFromPrevious($jsonResponse, ['test' => true]);
181
        $this->assertNotEquals($jsonResponse->toArray(), $profilling->toArray(), 'Profilling error creation');
182
        $this->assertEquals($jsonResponse->data, $profilling->data, 'Error creating profilling dto');
183
        $this->assertEquals($jsonResponse->success, $profilling->success, 'Error creating profilling dto');
184
        $this->assertEquals($jsonResponse->message, $profilling->message, 'Error creating profilling dto');
185
        $this->assertEquals($jsonResponse->total, $profilling->total, 'Error creating profilling dto');
186
        $this->assertEquals($jsonResponse->pages, $profilling->pages, 'Error creating profilling dto');
187
        $this->assertNotEmpty($profilling->profiling, 'Error creating profilling dto');
188
    }
189
}
190