Completed
Push — master ( 0dc5c4...fea5ff )
by Vítězslav
03:55
created

FlexiBeeTest   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 329
Duplicated Lines 2.74 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 15
Bugs 1 Features 1
Metric Value
c 15
b 1
f 1
dl 9
loc 329
rs 10
wmc 28
lcom 2
cbo 3

23 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
A testCurlInit() 0 5 1
A testObject2array() 0 10 1
A testGetLastInsertedId() 0 4 1
A testXml2array() 0 17 1
A testSetEvidence() 0 5 1
B testPerformRequest() 0 26 4
A testDisconnect() 0 5 1
A test__destruct() 0 4 1
A testLoadFlexiData() 0 7 1
A testGetFlexiRow() 0 5 1
A testGetFlexiData() 9 19 3
A testLoadFromFlexiBee() 0 5 1
A testJsonizeData() 0 5 1
A testInsertToFlexiBee() 0 7 1
A testIdExists() 0 7 1
A testRecordExists() 0 5 1
A testGetColumnsFromFlexibee() 0 7 1
B testGetKod() 0 27 1
A testSearchString() 0 7 1
A testLogResult() 0 20 1
A testFlexiUrl() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Test\FlexiPeeHP;
4
5
use FlexiPeeHP\FlexiBee;
6
7
/**
8
 * Class used to test Object To Array Conversion
9
 */
10
class objTest extends \stdClass
11
{
12
    /**
13
     * Simple Item
14
     * @var integer
15
     */
16
    public $item = 1;
17
18
    /**
19
     * Array item
20
     * @var array
21
     */
22
    public $arrItem = ['a', 'b' => 'c'];
23
24
    /**
25
     * Simple method
26
     * 
27
     * @return boolean
28
     */
29
    public function method()
30
    {
31
        return true;
32
    }
33
}
34
35
/**
36
 * Generated by PHPUnit_SkeletonGenerator on 2016-05-04 at 10:08:36.
37
 */
38
class FlexiBeeTest extends \Test\Ease\BrickTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
39
{
40
    /**
41
     * @var FlexiBee
42
     */
43
    protected $object;
44
45
    /**
46
     * Sets up the fixture, for example, opens a network connection.
47
     * This method is called before a test is executed.
48
     * @covers FlexiPeeHP\FlexiBee::__construct
49
     */
50
    protected function setUp()
51
    {
52
        $this->object = new FlexiBee;
53
    }
54
55
    /**
56
     * Tears down the fixture, for example, closes a network connection.
57
     * This method is called after a test is executed.
58
     */
59
    protected function tearDown()
60
    {
61
        
62
    }
63
64
    public function testCurlInit()
65
    {
66
        $this->object->curlInit();
67
        $this->assertTrue(is_resource($this->object->curl));
68
    }
69
70
    /**
71
     * @covers FlexiPeeHP\FlexiBee::setEvidence
72
     */
73
    public function testSetEvidence()
74
    {
75
        $this->object->setEvidence('nastaveni');
76
        $this->assertEquals('nastaveni', $this->object->evidence);
77
    }
78
79
    /**
80
     * @covers FlexiPeeHP\FlexiBee::object2array
81
     */
82
    public function testObject2array()
83
    {
84
        $this->assertNull($this->object->object2array(new \stdClass()));
85
        $this->assertEquals(
86
            [
87
            'item' => 1,
88
            'arrItem' => ['a', 'b' => 'c']
89
            ]
90
            , $this->object->object2array(new objTest()));
91
    }
92
93
    /**
94
     * @covers FlexiPeeHP\FlexiBee::performRequest
95
     */
96
    public function testPerformRequest()
97
    {
98
99
        if (!is_null($this->object->evidence) && $this->object->evidence != 'test') {
100
            $json = $this->object->performRequest($this->object->evidence.'.json');
101
            if (array_key_exists('message', $json)) {
102
                $this->assertArrayHasKey('@version', $json);
103
            } else {
104
                $this->assertArrayHasKey($this->object->evidence, $json);
105
            }
106
        } else {
107
            $this->object->evidence  = 'c';
108
            $this->object->prefix    = '';
109
            $this->object->company   = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type object<FlexiPeeHP\type> of property $company.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
            $this->object->nameSpace = 'companies';
111
            $json                    = $this->object->performRequest();
112
            $this->assertArrayHasKey('company', $json);
113
114
            $xml = $this->object->performRequest(null, 'GET', 'xml');
115
            $this->assertArrayHasKey('company', $xml);
116
        }
117
118
        $err = $this->object->performRequest('error.json');
119
        $this->assertArrayHasKey('success', $err);
120
        $this->assertEquals('false', $err['success']);
121
    }
122
123
    /**
124
     * @covers FlexiPeeHP\FlexiBee::getLastInsertedId
125
     * @depends testInsertToFlexiBee
126
     */
127
    public function testGetLastInsertedId()
128
    {
129
        $this->assertNotEmpty($this->object->getLastInsertedId());
130
    }
131
132
    /**
133
     * @covers FlexiPeeHP\FlexiBee::xml2array
134
     */
135
    public function testXml2array()
136
    {
137
        $xml = '<card xmlns="http://businesscard.org">
138
   <name>John Doe</name>
139
   <title>CEO, Widget Inc.</title>
140
   <email>[email protected]</email>
141
   <phone>(202) 456-1414</phone>
142
   <logo url="widget.gif"/>
143
   <a><b>c</b></a>
144
 </card>';
145
146
        $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]',
147
            'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]];
148
149
150
        $this->assertEquals($data, $this->object->xml2array($xml));
151
    }
152
153
    /**
154
     * @covers FlexiPeeHP\FlexiBee::disconnect
155
     *
156
     * @depends testPerformRequest
157
     * @depends testLoadFlexiData
158
     * @depends testGetFlexiRow
159
     * @depends testGetFlexiData
160
     * @depends testLoadFromFlexiBee
161
     * @depends testInsertToFlexiBee
162
     * @depends testIdExists
163
     * @depends testRecordExists
164
     * @depends testGetColumnsFromFlexibee
165
     * @depends testSearchString
166
     */
167
    public function testDisconnect()
168
    {
169
        $this->object->disconnect();
170
        $this->assertNull($this->object->curl);
171
    }
172
173
    /**
174
     * @covers FlexiPeeHP\FlexiBee::__destruct
175
     * @depends testDisconnect
176
     */
177
    public function test__destruct()
178
    {
179
        $this->markTestSkipped();
180
    }
181
182
    /**
183
     * @covers FlexiPeeHP\FlexiBee::loadFlexiData
184
     * @todo   Implement testLoadFlexiData().
185
     */
186
    public function testLoadFlexiData()
187
    {
188
        // Remove the following lines when you implement this test.
189
        $this->markTestIncomplete(
190
            'This test has not been implemented yet.'
191
        );
192
    }
193
194
    /**
195
     * @covers FlexiPeeHP\FlexiBee::getFlexiRow
196
     */
197
    public function testGetFlexiRow()
198
    {
199
        $this->object->getFlexiRow(0);
200
        $this->object->getFlexiRow(1);
201
    }
202
203
    /**
204
     * @covers FlexiPeeHP\FlexiBee::getFlexiData
205
     */
206
    public function testGetFlexiData()
207
    {
208
        if (is_null($this->object->evidence) || ($this->object->evidence == 'test')) {
209
            $this->object->evidence  = 'c';
210
            $this->object->prefix    = '';
211
            $this->object->company   = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type object<FlexiPeeHP\type> of property $company.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
212
            $this->object->nameSpace = 'companies';
213
            $flexidata               = $this->object->getFlexiData();
214
            $this->assertArrayHasKey('company', $flexidata);
215 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
216
            $flexidata = $this->object->getFlexiData();
217
            $this->assertArrayHasKey(0, $flexidata);
218
            $this->assertArrayHasKey('id', $flexidata[0]);
219
            $filtrered = $this->object->getFlexiData(null,
220
                key($flexidata[0])." = ".current($flexidata[0]));
221
            $this->assertArrayHasKey(0, $filtrered);
222
            $this->assertArrayHasKey('id', $filtrered[0]);
223
        }
224
    }
225
226
    /**
227
     * @covers FlexiPeeHP\FlexiBee::loadFromFlexiBee
228
     */
229
    public function testLoadFromFlexiBee()
230
    {
231
        $this->object->loadFromFlexiBee();
232
        $this->object->loadFromFlexiBee(222);
233
    }
234
235
    /**
236
     * @covers FlexiPeeHP\FlexiBee::jsonizeData
237
     */
238
    public function testJsonizeData()
239
    {
240
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}',
241
            $this->object->jsonizeData(['key' => 'value']));
242
    }
243
244
    /**
245
     * @covers FlexiPeeHP\FlexiBee::insertToFlexiBee
246
     * @todo   Implement testInsertToFlexiBee().
247
     */
248
    public function testInsertToFlexiBee()
249
    {
250
        // Remove the following lines when you implement this test.
251
        $this->markTestIncomplete(
252
            'This test has not been implemented yet.'
253
        );
254
    }
255
256
    /**
257
     * @covers FlexiPeeHP\FlexiBee::idExists
258
     * @todo   Implement testIdExists().
259
     */
260
    public function testIdExists()
261
    {
262
        // Remove the following lines when you implement this test.
263
        $this->markTestIncomplete(
264
            'This test has not been implemented yet.'
265
        );
266
    }
267
268
    /**
269
     * @covers FlexiPeeHP\FlexiBee::recordExists
270
     */
271
    public function testRecordExists()
272
    {
273
//        $this->assertTrue($this->object->recordExists([]));
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
274
//        $this->assertFalse($this->object->recordExists([]));
275
    }
276
277
    /**
278
     * @covers FlexiPeeHP\FlexiBee::getColumnsFromFlexibee
279
     * @todo   Implement testGetColumnsFromFlexibee().
280
     */
281
    public function testGetColumnsFromFlexibee()
282
    {
283
        // Remove the following lines when you implement this test.
284
        $this->markTestIncomplete(
285
            'This test has not been implemented yet.'
286
        );
287
    }
288
289
    /**
290
     * @covers FlexiPeeHP\FlexiBee::getKod
291
     */
292
    public function testGetKod()
293
    {
294
295
        $this->assertEquals('CODE',
296
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
297
298
        $testString[$this->object->nameColumn] = 'Fish clamp -  Úchytka pro instalaci samonosných kabelů '
0 ignored issues
show
Coding Style Comprehensibility introduced by
$testString was never initialized. Although not strictly required by PHP, it is generally a good practice to add $testString = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
299
            .'(3.5 mm)';
300
        $code0                                 = $this->object->getKod($testString);
301
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code0);
302
        $code1                                 = $this->object->getKod($testString,
303
            false);
304
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code1);
305
        $code2                                 = $this->object->getKod($testString);
306
        $this->assertEquals('FISHCLAMPUCHYTKAPR1', $code2);
307
        $this->object->setData($testString);
308
        $code3                                 = $this->object->getKod();
309
        $this->assertEquals('FISHCLAMPUCHYTKAPR2', $code3);
310
311
        $this->assertEquals('TEST',
312
            $this->object->getKod([$this->object->nameColumn => 'test']));
313
314
        $this->assertEquals('TEST1', $this->object->getKod('test'));
315
316
        $this->assertEquals('TEST2', $this->object->getKod(['kod' => 'test']));
317
        $this->assertEquals('NOTSET', $this->object->getKod(['kod' => '']));
318
    }
319
320
    /**
321
     * @covers FlexiPeeHP\FlexiBee::searchString
322
     * @todo   Implement testSearchString().
323
     */
324
    public function testSearchString()
325
    {
326
        // Remove the following lines when you implement this test.
327
        $this->markTestIncomplete(
328
            'This test has not been implemented yet.'
329
        );
330
    }
331
332
    /**
333
     * @covers FlexiPeeHP\FlexiBee::logResult
334
     */
335
    public function testLogResult()
336
    {
337
        $this->object->cleanMessages();
338
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
339
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
340
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
341
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
342
        $this->object->logResult(current($this->object->object2array($success)),
343
            'http://test');
344
345
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
346
347
        $error = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
348
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
349
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
350
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
351
            .' kód není unikátní."}]}]}}');
352
        $this->object->logResult(current($this->object->object2array($error)));
353
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
354
    }
355
356
    /**
357
     * @covers FlexiPeeHP\FlexiBee::flexiUrl
358
     */
359
    public function testFlexiUrl()
360
    {
361
        $this->assertEquals("a = 1 and b = 'foo'",
362
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
363
        $this->assertEquals("a = 1 or b = 'bar'",
364
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
365
    }
366
}