Completed
Push — master ( 0f5459...b9fce4 )
by Max
01:22
created

DataWrapperTest::testCheckFieldRelated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * @copyright (c) 2020 Mendel <[email protected]>
4
 * @license see license.txt
5
 */
6
namespace drycart\data\tests;
7
use drycart\data\DataWrapper;
8
9
/**
10
 * @author mendel
11
 */
12
class DataWrapperTest extends \PHPUnit\Framework\TestCase
13
{
14
    protected function prepareWrapper(bool $safe) : DataWrapper
15
    {
16
        $data = [
17
            'field1'=>'value1',
18
            'field2'=>'value2',
19
            'obj'=>(object) ['field1'=>'value1','field2'=>'value2'],
20
            'array'=>['field1'=>'value1','field2'=>'value2'],
21
            'arrayObj'=> new \ArrayObject(['field1'=>'value1','field2'=>'value2'])
22
        ];
23
        return new DataWrapper($data, $safe);
24
    }
25
    
26
    public function testSafe()
27
    {
28
        $wrapper = $this->prepareWrapper(TRUE);
29
        //
30
        $this->assertEquals($wrapper->get('field1'), 'value1');
31
        $this->assertEquals($wrapper->get('obj.field2'), 'value2');
32
        $this->assertEquals($wrapper->get('array.field1'), 'value1');
33
        $this->assertEquals($wrapper->get('arrayObj.field1'), 'value1');
34
        $this->assertEquals($wrapper->get('arrayObj.count()'), 2);
35
        //
36
        $this->expectException(\Exception::class);
37
        $this->expectExceptionMessage("Bad field name notExistField");        
38
        $wrapper->get('obj.notExistField');
39
    }
40
    
41
    public function testNotSafe()
42
    {
43
        $wrapper = $this->prepareWrapper(FALSE);
44
        //
45
        $this->assertEquals($wrapper->get('field1'), 'value1');
46
        $this->assertEquals($wrapper->get('obj.field2'), 'value2');
47
        $this->assertEquals($wrapper->get('array.field1'), 'value1');
48
        $this->assertEquals($wrapper->get('arrayObj.field1'), 'value1');
49
        $this->assertEquals($wrapper->get('arrayObj.count()'), 2);
50
        //
51
        $this->assertEquals($wrapper->get('obj.notExistField'), null);
52
        $this->assertEquals($wrapper->get('obj.notExistMethod()'), null);
53
        //
54
        $this->assertEquals($wrapper->get('obj.notExistField', 'default'), 'default');
55
        $this->assertEquals($wrapper->get('obj.notExistMethod()', 'default'), 'default');
56
    }
57
    
58
    public function testMagic()
59
    {
60
        $wrapper = $this->prepareWrapper(FALSE);
61
        //
62
        $field2 = 'arrayObj.field1';
63
        $field3 = 'arrayObj.count()';
64
        $this->assertEquals($wrapper->field1, 'value1');
65
        $this->assertEquals($wrapper->$field2, 'value1');
66
        $this->assertEquals($wrapper->$field3, 2);
67
        $this->assertEquals($wrapper['field1'], 'value1');
68
        $this->assertEquals($wrapper[$field2], 'value1');
69
        $this->assertEquals($wrapper[$field3], 2);
70
        
71
        $this->assertFalse(isset($wrapper->notExistField));
72
        $this->assertTrue(isset($wrapper->field1));
73
        $this->assertFalse(isset($wrapper['notExistField']));
74
        $this->assertTrue(isset($wrapper['field1']));
75
        
76
        $this->assertEquals($wrapper->arrayObj->count(), 2);
77
        $wrapper2 = new DataWrapper(new \ArrayObject(['field1'=>'value1','field2'=>'value2']));
78
        $this->assertEquals($wrapper2->count(), 2);
79
        $this->assertEquals(json_encode(['field1'=>'value1','field2'=>'value2']), json_encode($wrapper2));
80
        
81
        $wrapper3 = new DataWrapper(new dummy\DummyModel());
82
        $this->assertEquals($wrapper3->getSomeString(), 'some string');
83
        
84
        $this->assertEquals('Array obj count', $wrapper->fieldLabel($field3));
85
        $wrapper4 = new DataWrapper($wrapper);
86
        $this->assertEquals('Array obj count', $wrapper4->fieldLabel($field3));
87
        
88
        $this->expectException(\RuntimeException::class);
89
        $this->expectExceptionMessage("DataWraper can wrap only array or object");        
90
        new DataWrapper('some string');
91
    }
92
    
93 View Code Duplication
    public function testDummyArrayUnset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $wrapper = $this->prepareWrapper(FALSE);
96
        
97
        $this->expectException(\RuntimeException::class);
98
        $this->expectExceptionMessage("DataWraper is just read-only wrapper");        
99
        
100
        unset($wrapper['someField']);
101
    }
102
    
103 View Code Duplication
    public function testDummyArraySet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $wrapper = $this->prepareWrapper(FALSE);
106
        
107
        $this->expectException(\RuntimeException::class);
108
        $this->expectExceptionMessage("DataWraper is just read-only wrapper");        
109
        
110
        $wrapper['someField'] = 'some string';
111
    }
112
    
113 View Code Duplication
    public function testCheckFieldDirect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $wrapper = $this->prepareWrapper(FALSE);
116
        $this->assertTrue($wrapper->check(['=','field1', 'value1']));
117
        $this->assertFalse($wrapper->check(['=','field1', 'wrongValue']));
118
        $this->assertTrue($wrapper->check(['=','notExistField', null]));
119
        $this->assertTrue($wrapper->check(['=','arrayObj.count()', 2]));
120
        $this->assertTrue($wrapper->check(['>','arrayObj.count()', 1]));
121
        //
122
    }
123
    
124 View Code Duplication
    public function testCheckFieldRelated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $wrapper = $this->prepareWrapper(FALSE);
127
        $this->assertTrue($wrapper->check(['*=','field1', 'array.field1']));
128
        $this->assertFalse($wrapper->check(['*=','field1', 'notExistField']));
129
        $this->assertTrue($wrapper->check(['*=','notExistField', 'notExistField2']));
130
        $this->assertTrue($wrapper->check(['*=','arrayObj.count()', 'arrayObj.count()']));
131
        $this->assertTrue($wrapper->check(['*>=','arrayObj.count()', 'arrayObj.count()']));
132
        //
133
    }
134
    
135
    public function testCheckLogical()
136
    {
137
        $wrapper = $this->prepareWrapper(FALSE);
138
        $this->assertFalse($wrapper->check([
139
            'NOT',
140
            ['*=','field1', 'array.field1']
141
        ]));
142
        $this->assertFalse($wrapper->check([
143
            'AND',
144
            ['*=','field1', 'array.field1'],
145
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
146
            ['*=','field1', 'notExistField']
147
        ]));
148
        $this->assertTrue($wrapper->check([
149
            'OR',
150
            ['*=','field1', 'array.field1'],
151
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
152
            ['*=','field1', 'notExistField']
153
        ]));
154
        $this->assertFalse($wrapper->check([
155
            'OR',
156
            ['*<>','field1', 'array.field1'],
157
            ['*>','arrayObj.count()', 'arrayObj.count()'],
158
            ['*=','field1', 'notExistField']
159
        ]));
160
        $this->assertTrue($wrapper->check([
161
            'and',
162
            ['*=','field1', 'array.field1'],
163
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
164
            [
165
                'not',
166
                ['*=','field1', 'notExistField']
167
            ],
168
            [
169
                'or',
170
                ['*=','field1', 'array.field1'],
171
                ['*>=','arrayObj.count()', 'arrayObj.count()'],
172
                ['*=','field1', 'notExistField']
173
            ]
174
        ]));
175
        $this->assertTrue($wrapper->check([
176
            '*=field1' => 'array.field1',
177
            '*>=arrayObj.count()' => 'arrayObj.count()',
178
            '*<>field1' => 'notExistField'
179
        ]));
180
        $this->assertTrue($wrapper->check([
181
            'field1' => 'value1',
182
            '>=arrayObj.count()' => 1,
183
            '<>field1' => 'notExist'
184
        ]));
185
    }
186
    
187
    public function testIterator()
188
    {
189
        $wrapper = $this->prepareWrapper(FALSE);
190
        $array = iterator_to_array($wrapper);
191
        $this->assertArrayHasKey('field1', $array);
192
        $this->assertEquals('value1',$array['field1']);
193
        
194
        $wrapper2 = new DataWrapper(new \ArrayObject(['field1'=>'value1']));
195
        $array2 = iterator_to_array($wrapper2);
196
        $this->assertArrayHasKey('field1', $array2);
197
        $this->assertEquals('value1',$array2['field1']);
198
        
199
        $wrapper3 = new DataWrapper((object) ['field1'=>'value1']);
200
        $array3 = iterator_to_array($wrapper3);
201
        $this->assertArrayHasKey('field1', $array3);
202
        $this->assertEquals('value1',$array3['field1']);
203
    }
204
    
205
    public function testKeys()
206
    {
207
        $wrapper = $this->prepareWrapper(FALSE);
208
        $this->assertEquals([
209
            'field1','field2','obj','array','arrayObj'
210
        ],$wrapper->keys());
211
        
212
        $wrapper2 = new DataWrapper(new \ArrayObject(['field1'=>'value1']));
213
        $this->assertEquals([
214
            'field1'
215
        ],$wrapper2->keys());
216
        
217
        $wrapper3 = new DataWrapper((object) ['field1'=>'value1']);
218
        $this->assertEquals([
219
            'field1'
220
        ],$wrapper3->keys());
221
        
222
        $arrayObj = new \ArrayObject(['field1'=>'value1']);
223
        $wrapper4 = new DataWrapper($arrayObj->getIterator());
224
        $this->assertEquals([
225
            'field1'
226
        ],$wrapper4->keys());
227
        
228
        $wrapper5 = new DataWrapper($wrapper2);
229
        $this->assertEquals([
230
            'field1'
231
        ],$wrapper5->keys());
232
    }
233
    
234
    public function testTitle()
235
    {
236
        $wrapper = $this->prepareWrapper(FALSE);
237
        $this->assertEquals('Some array...',$wrapper->title());
238
        
239
        $arrayObj = new \ArrayObject(['field1'=>'value1']);
240
        $wrapper2 = new DataWrapper($arrayObj);
241
        $this->assertEquals('Object #'.spl_object_id($arrayObj),$wrapper2->title());
242
        
243
        $wrapper3 = new DataWrapper($wrapper2);
244
        $this->assertEquals('Object #'.spl_object_id($arrayObj),$wrapper3->title());
245
    }
246
    
247
    public function testFieldsInfo()
248
    {
249
        $wrapper = $this->prepareWrapper(FALSE);
250
        $this->assertEquals([
251
            'field1'=>[],'field2'=>[],'obj'=>[],'array'=>[],'arrayObj'=>[]
252
        ],$wrapper->fieldsInfo());
253
        
254
        $wrapper2 = new DataWrapper($wrapper);
255
        $this->assertEquals([
256
            'field1'=>[],'field2'=>[],'obj'=>[],'array'=>[],'arrayObj'=>[]
257
        ],$wrapper2->fieldsInfo());
258
    }
259
}
260