Completed
Push — master ( b9fce4...98eb76 )
by Max
01:21
created

DataWrapperTest::testDummyArraySet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
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['field1'], 'value1');
31
        $this->assertEquals($wrapper['obj.field2'], 'value2');
32
        $this->assertEquals($wrapper['array.field1'], 'value1');
33
        $this->assertEquals($wrapper['arrayObj.field1'], 'value1');
34
        $this->assertEquals($wrapper['arrayObj.count()'], 2);
35
        //
36
        $this->expectException(\Exception::class);
37
        $this->expectExceptionMessage("Bad field name notExistField");        
38
        $wrapper['obj.notExistField'];
39
    }
40
    
41
    public function testNotSafe()
42
    {
43
        $wrapper = $this->prepareWrapper(FALSE);
44
        //
45
        $this->assertEquals($wrapper['field1'], 'value1');
46
        $this->assertEquals($wrapper['obj.field2'], 'value2');
47
        $this->assertEquals($wrapper['array.field1'], 'value1');
48
        $this->assertEquals($wrapper['arrayObj.field1'], 'value1');
49
        $this->assertEquals($wrapper['arrayObj.count()'], 2);
50
        //
51
        $this->assertEquals($wrapper['obj.notExistField'], null);
52
        $this->assertEquals($wrapper['obj.notExistMethod()'], null);
53
    }
54
    
55
    public function testMagic()
56
    {
57
        $wrapper = $this->prepareWrapper(FALSE);
58
        //
59
        $field2 = 'arrayObj.field1';
60
        $field3 = 'arrayObj.count()';
61
        $this->assertEquals($wrapper->field1, 'value1');
62
        $this->assertEquals($wrapper->$field2, 'value1');
63
        $this->assertEquals($wrapper->$field3, 2);
64
        $this->assertEquals($wrapper['field1'], 'value1');
65
        $this->assertEquals($wrapper[$field2], 'value1');
66
        $this->assertEquals($wrapper[$field3], 2);
67
        
68
        $this->assertFalse(isset($wrapper->notExistField));
69
        $this->assertTrue(isset($wrapper->field1));
70
        $this->assertFalse(isset($wrapper['notExistField']));
71
        $this->assertTrue(isset($wrapper['field1']));
72
        
73
        $this->assertEquals($wrapper['field1'], 'value1');
74
        unset($wrapper['field1']);
75
        $this->assertNull($wrapper['field1']);
76
        
77
        $wrapper['someField'] = 'some string';
78
        $this->assertEquals('some string', $wrapper['someField']);
79
        
80
        $this->assertEquals($wrapper->arrayObj->count(), 2);
81
        $wrapper2 = new DataWrapper(new \ArrayObject(['field1'=>'value1','field2'=>'value2']));
82
        $this->assertEquals($wrapper2->count(), 2);
83
        $this->assertEquals(json_encode(['field1'=>'value1','field2'=>'value2']), json_encode($wrapper2));
84
        
85
        $wrapper3 = new DataWrapper(new dummy\DummyModel(), false);
86
        $this->assertEquals($wrapper3->getSomeString(), 'some string');
87
        
88
        $wrapper3['someField'] = 'some string3';
89
        $this->assertEquals('some string3', $wrapper3['someField']);
90
        unset($wrapper3['someField']);
91
        $this->assertNull($wrapper3['someField']);
92
        
93
        $this->assertEquals('Array obj count', $wrapper->fieldLabel($field3));
94
        $wrapper4 = new DataWrapper($wrapper);
95
        $this->assertEquals('Array obj count', $wrapper4->fieldLabel($field3));
96
        
97
        $this->expectException(\RuntimeException::class);
98
        $this->expectExceptionMessage("DataWraper can wrap only array or object");        
99
        new DataWrapper('some string');
100
    }
101
    
102 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...
103
    {
104
        $wrapper = $this->prepareWrapper(FALSE);
105
        $this->assertTrue($wrapper->check(['=','field1', 'value1']));
106
        $this->assertFalse($wrapper->check(['=','field1', 'wrongValue']));
107
        $this->assertTrue($wrapper->check(['=','notExistField', null]));
108
        $this->assertTrue($wrapper->check(['=','arrayObj.count()', 2]));
109
        $this->assertTrue($wrapper->check(['>','arrayObj.count()', 1]));
110
        //
111
    }
112
    
113 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...
114
    {
115
        $wrapper = $this->prepareWrapper(FALSE);
116
        $this->assertTrue($wrapper->check(['*=','field1', 'array.field1']));
117
        $this->assertFalse($wrapper->check(['*=','field1', 'notExistField']));
118
        $this->assertTrue($wrapper->check(['*=','notExistField', 'notExistField2']));
119
        $this->assertTrue($wrapper->check(['*=','arrayObj.count()', 'arrayObj.count()']));
120
        $this->assertTrue($wrapper->check(['*>=','arrayObj.count()', 'arrayObj.count()']));
121
        //
122
    }
123
    
124
    public function testCheckLogical()
125
    {
126
        $wrapper = $this->prepareWrapper(FALSE);
127
        $this->assertFalse($wrapper->check([
128
            'NOT',
129
            ['*=','field1', 'array.field1']
130
        ]));
131
        $this->assertFalse($wrapper->check([
132
            'AND',
133
            ['*=','field1', 'array.field1'],
134
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
135
            ['*=','field1', 'notExistField']
136
        ]));
137
        $this->assertTrue($wrapper->check([
138
            'OR',
139
            ['*=','field1', 'array.field1'],
140
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
141
            ['*=','field1', 'notExistField']
142
        ]));
143
        $this->assertFalse($wrapper->check([
144
            'OR',
145
            ['*!=','field1', 'array.field1'],
146
            ['*>','arrayObj.count()', 'arrayObj.count()'],
147
            ['*=','field1', 'notExistField']
148
        ]));
149
        $this->assertTrue($wrapper->check([
150
            'and',
151
            ['*=','field1', 'array.field1'],
152
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
153
            [
154
                'not',
155
                ['*=','field1', 'notExistField']
156
            ],
157
            [
158
                'or',
159
                ['*=','field1', 'array.field1'],
160
                ['*>=','arrayObj.count()', 'arrayObj.count()'],
161
                ['*=','field1', 'notExistField']
162
            ]
163
        ]));
164
        $this->assertTrue($wrapper->check([
165
            '*=field1' => 'array.field1',
166
            '*>=arrayObj.count()' => 'arrayObj.count()',
167
            '*<>field1' => 'notExistField'
168
        ]));
169
        $this->assertTrue($wrapper->check([
170
            'field1' => 'value1',
171
            '>=arrayObj.count()' => 1,
172
            '<>field1' => 'notExist'
173
        ]));
174
    }
175
    
176
    public function testIterator()
177
    {
178
        $wrapper = $this->prepareWrapper(FALSE);
179
        $array = iterator_to_array($wrapper);
180
        $this->assertArrayHasKey('field1', $array);
181
        $this->assertEquals('value1',$array['field1']);
182
        
183
        $wrapper2 = new DataWrapper(new \ArrayObject(['field1'=>'value1']));
184
        $array2 = iterator_to_array($wrapper2);
185
        $this->assertArrayHasKey('field1', $array2);
186
        $this->assertEquals('value1',$array2['field1']);
187
        
188
        $wrapper3 = new DataWrapper((object) ['field1'=>'value1']);
189
        $array3 = iterator_to_array($wrapper3);
190
        $this->assertArrayHasKey('field1', $array3);
191
        $this->assertEquals('value1',$array3['field1']);
192
    }
193
    
194
    public function testKeys()
195
    {
196
        $wrapper = $this->prepareWrapper(FALSE);
197
        $this->assertEquals([
198
            'field1','field2','obj','array','arrayObj'
199
        ],$wrapper->keys());
200
        
201
        $wrapper2 = new DataWrapper(new \ArrayObject(['field1'=>'value1']));
202
        $this->assertEquals([
203
            'field1'
204
        ],$wrapper2->keys());
205
        
206
        $wrapper3 = new DataWrapper((object) ['field1'=>'value1']);
207
        $this->assertEquals([
208
            'field1'
209
        ],$wrapper3->keys());
210
        
211
        $arrayObj = new \ArrayObject(['field1'=>'value1']);
212
        $wrapper4 = new DataWrapper($arrayObj->getIterator());
213
        $this->assertEquals([
214
            'field1'
215
        ],$wrapper4->keys());
216
        
217
        $wrapper5 = new DataWrapper($wrapper2);
218
        $this->assertEquals([
219
            'field1'
220
        ],$wrapper5->keys());
221
    }
222
    
223
    public function testTitle()
224
    {
225
        $wrapper = $this->prepareWrapper(FALSE);
226
        $this->assertEquals('Some array...',$wrapper->title());
227
        
228
        $arrayObj = new \ArrayObject(['field1'=>'value1']);
229
        $wrapper2 = new DataWrapper($arrayObj);
230
        $this->assertEquals('Object #'.spl_object_id($arrayObj),$wrapper2->title());
231
        
232
        $wrapper3 = new DataWrapper($wrapper2);
233
        $this->assertEquals('Object #'.spl_object_id($arrayObj),$wrapper3->title());
234
    }
235
    
236
    public function testFieldsInfo()
237
    {
238
        $wrapper = $this->prepareWrapper(FALSE);
239
        $this->assertEquals([
240
            'field1'=>[],'field2'=>[],'obj'=>[],'array'=>[],'arrayObj'=>[]
241
        ],$wrapper->fieldsInfo());
242
        
243
        $wrapper2 = new DataWrapper($wrapper);
244
        $this->assertEquals([
245
            'field1'=>[],'field2'=>[],'obj'=>[],'array'=>[],'arrayObj'=>[]
246
        ],$wrapper2->fieldsInfo());
247
    }
248
}
249