Test Failed
Push — master ( 1ea845...275c2e )
by Vítězslav
03:06
created

FlexiBeeRWTest   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 408
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 408
rs 8.2857
c 4
b 1
f 0
wmc 39
lcom 1
cbo 5

27 Methods

Rating   Name   Duplication   Size   Complexity  
B getDataForInsert() 0 24 6
A setUp() 0 5 1
A tearDown() 0 4 1
A testPerformAction() 0 16 4
A testTimestampToFlexiDate() 0 6 1
A testTimestampToFlexiDateTime() 0 7 1
A testObjectToID() 0 7 1
A testGetRecordID() 0 9 3
A testControlMandatoryColumns() 0 4 1
A testControlReadOnlyColumns() 0 4 1
A testAddArrayToBranch() 0 4 1
A testAddObjectToBranch() 0 4 1
A testVazbaAdd() 0 4 1
A testVazbaDel() 0 4 1
A testGetJsonizedData() 0 6 1
A testRefresh() 0 5 1
A testInsertToFlexiBee() 0 4 1
A testParseResponse() 0 55 1
A testAssignResultIDs() 0 21 1
B testExtractResultIDs() 0 41 1
A testGetLastInsertedId() 0 4 1
A testDeleteFromFlexiBee() 0 4 1
A testTakeData() 0 4 1
A testGetDataForJSON() 0 5 1
A testAddExternalID() 0 7 2
A testChangeExternalID() 0 5 1
A testSync() 0 9 2
1
<?php
2
3
namespace Test\FlexiPeeHP;
4
5
use FlexiPeeHP\FlexiBeeRW;
6
7
/**
8
 * Generated by PHPUnit_SkeletonGenerator on 2016-05-04 at 10:08:36.
9
 */
10
class FlexiBeeRWTest extends FlexiBeeROTest
11
{
12
    /**
13
     * Poznámka vkládaná do záznamů vytvářených během testů
14
     * @var strig 
15
     */
16
    public $poznam = 'Generováno UnitTestem PHP Knihovny https://github.com/Spoje-NET/FlexiPeeHP';
17
18
    /**
19
     *
20
     * @var array
21
     */
22
    public $insertableData = [];
23
24
    /**
25
     * @var FlexiBeeRW
26
     */
27
    protected $object;
28
29
    /**
30
     * Gives You data able to insert into current evidence
31
     *
32
     * @param string $code custom record code
33
     *
34
     * @return array
35
     */
36
    public function getDataForInsert($code = 'UnitTest')
37
    {
38
        $dataForInsert = [];
39
        $structure     = $this->object->getColumnsInfo();
40
        if (count($structure) && array_key_exists('typDokl', $structure)) {
41
            if ($structure['typDokl']['type'] == 'relation') {
42
                $relatedEvidence          = basename($structure['typDokl']['url']);
43
                $loader                   = new \FlexiPeeHP\FlexiBeeRO(null,
44
                    ['evidence' => $relatedEvidence]);
45
                $typDoklRaw               = $loader->getColumnsFromFlexibee([
46
                    'kod'], ['limit' => 1]);
47
                $dataForInsert['typDokl'] = \FlexiPeeHP\FlexiBeeRO::code($typDoklRaw[0]['kod']);
48
            }
49
50
            if (array_key_exists('poznam', $structure)) {
51
                $dataForInsert['poznam'] = $this->poznam;
52
            }
53
54
            if (array_key_exists('kod', $structure)) {
55
                $dataForInsert['kod'] = \FlexiPeeHP\FlexiBeeRO::uncode($this->object->getKod($code));
56
            }
57
        }
58
        return $dataForInsert;
59
    }
60
61
    /**
62
     * Sets up the fixture, for example, opens a network connection.
63
     * This method is called before a test is executed.
64
     * @covers FlexiPeeHP\FlexiBeeRW::__construct
65
     */
66
    protected function setUp()
67
    {
68
        $this->object = new FlexiBeeRW(null,
69
            ['atomic' => false, 'debug' => false]);
70
    }
71
72
    /**
73
     * Tears down the fixture, for example, closes a network connection.
74
     * This method is called after a test is executed.
75
     */
76
    protected function tearDown()
77
    {
78
        
79
    }
80
81
    /**
82
     * @covers FlexiPeeHP\FlexiBeeRW::performAction
83
     * @expectedException \Exception
84
     */
85
    public function testPerformAction()
86
    {
87
        $actions = $this->object->getActionsInfo();
88
89
        if (count($actions)) {
90
            if (array_key_exists('new', $actions)) {
91
                $this->object->performAction('new', 'ext');
92
            }
93
94
            if (array_key_exists('storno', $actions)) {
95
                $this->object->insertToFlexiBee($this->getDataForInsert('StornoTest_'.time()));
96
                $this->object->performAction('storno', 'int');
97
            }
98
        }
99
        $this->object->performAction('nonexitst');
100
    }
101
102
    /**
103
     * @covers FlexiPeeHP\FlexiBeeRW::timestampToFlexiDate
104
     */
105
    public function testTimestampToFlexiDate()
106
    {
107
        $this->assertNull($this->object->timestampToFlexiDate());
108
        $this->assertEquals('2016-09-16',
109
            $this->object->timestampToFlexiDate('1474040506'));
110
    }
111
112
    /**
113
     * @covers FlexiPeeHP\FlexiBeeRW::timestampToFlexiDateTime
114
     */
115
    public function testTimestampToFlexiDateTime()
116
    {
117
        $this->assertNull($this->object->timestampToFlexiDateTime());
118
        $flexiDateTime = $this->object->timestampToFlexiDateTime('1474040506');
119
        $this->assertEquals('2016-09-16', substr($flexiDateTime, 0, 10));
120
        $this->assertEquals(':41:46', substr($flexiDateTime, -6));
121
    }
122
123
    /**
124
     * @covers FlexiPeeHP\FlexiBeeRW::objectToID
125
     */
126
    public function testObjectToID()
127
    {
128
        $id = \Ease\Sand::randomNumber(1, 9999);
129
        $this->object->setMyKey($id);
0 ignored issues
show
Security Bug introduced by
It seems like $id defined by \Ease\Sand::randomNumber(1, 9999) on line 128 can also be of type false; however, FlexiPeeHP\FlexiBeeRO::setMyKey() does only seem to accept integer|string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
130
        $this->assertEquals([$id], $this->object->objectToID([$this->object]));
131
        parent::testObjectToID();
132
    }
133
134
    /**
135
     * @covers FlexiPeeHP\FlexiBeeRW::getRecordID
136
     */
137
    public function testGetRecordID()
138
    {
139
        parent::testGetRecordID();
140
        $structure = $this->object->getEvidenceInfo();
141
        if (count($structure) && array_key_exists('kod', $structure)) {
142
            $this->object->setData(['kod' => 'KOD']);
143
            $this->assertEquals('code:KOD', $this->object->getRecordID());
144
        }
145
    }
146
147
    /**
148
     * @covers FlexiPeeHP\FlexiBeeRW::controlMandatoryColumns
149
     */
150
    public function testControlMandatoryColumns()
151
    {
152
        $this->object->controlMandatoryColumns();
0 ignored issues
show
Deprecated Code introduced by
The method FlexiPeeHP\FlexiBeeRW::controlMandatoryColumns() has been deprecated with message: since version 1.8.7

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
    }
154
155
    /**
156
     * @covers FlexiPeeHP\FlexiBeeRW::controlReadOnlyColumns
157
     */
158
    public function testControlReadOnlyColumns()
159
    {
160
        $this->object->controlReadOnlyColumns(['id' => 1]);
161
    }
162
163
    /**
164
     * @covers FlexiPeeHP\FlexiBeeRW::addArrayToBranch
165
     */
166
    public function testAddArrayToBranch()
167
    {
168
        $this->object->addArrayToBranch(['nazev' => 'test'], 'podEvidence');
169
    }
170
171
    /**
172
     * @covers FlexiPeeHP\FlexiBeeRW::addObjectToBranch
173
     */
174
    public function testAddObjectToBranch()
175
    {
176
        $this->object->addObjectToBranch(new \FlexiPeeHP\FakturaVydanaPolozka(['nazev' => 'test']));
177
    }
178
179
    /**
180
     * @covers FlexiPeeHP\FlexiBeeRW::vazbaAdd
181
     */
182
    public function testVazbaAdd()
183
    {
184
        $this->object->vazbaAdd(\Ease\Sand::randomNumber());
0 ignored issues
show
Documentation introduced by
\Ease\Sand::randomNumber() is of type false|integer, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
    }
186
187
    /**
188
     * @covers FlexiPeeHP\FlexiBeeRW::vazbaDel
189
     */
190
    public function testVazbaDel()
191
    {
192
        $this->object->vazbaDel(\Ease\Sand::randomNumber());
0 ignored issues
show
Documentation introduced by
\Ease\Sand::randomNumber() is of type false|integer, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
    }
194
195
    /**
196
     * @covers FlexiPeeHP\FlexiBeeRW::getJsonizedData
197
     */
198
    public function testGetJsonizedData()
199
    {
200
        $this->object->setData(['id' => time(), 'nazev' => \Ease\Sand::randomString(),
201
            'stitky' => ['TEST', 'TESTING']]);
202
        $this->object->getJsonizedData();
203
    }
204
205
    /**
206
     * @covers FlexiPeeHP\FlexiBeeRW::refresh
207
     */
208
    public function testRefresh()
209
    {
210
        $this->object->setMyKey('ext:test:'.\Ease\Sand::randomNumber());
211
        $this->object->refresh();
0 ignored issues
show
Deprecated Code introduced by
The method FlexiPeeHP\FlexiBeeRW::refresh() has been deprecated with message: since version 1.8.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
212
    }
213
214
    /**
215
     * @covers FlexiPeeHP\FlexiBeeRW::insertToFlexiBee
216
     */
217
    public function testInsertToFlexiBee()
218
    {
219
        $this->object->insertToFlexiBee(['id' => 'ext:test:'.time()]);
220
    }
221
222
    /**
223
     * @covers FlexiPeeHP\FlexiBeeRW::parseResponse
224
     */
225
    public function testParseResponse()
226
    {
227
        $responseDecoded = array(
228
            '@version' => '1.0',
229
            'success' => 'true',
230
            'stats' =>
231
            array(
232
                'created' => '1',
233
                'updated' => '5',
234
                'deleted' => '0',
235
                'skipped' => '0',
236
                'failed' => '0',
237
            ),
238
            'results' =>
239
            array(
240
                0 =>
241
                array(
242
                    'id' => '792',
243
                    'request-id' => 'EXT:APP:100',
244
                    'ref' => '/c/flexipeehp/adresar/792',
245
                ),
246
                1 =>
247
                array(
248
                    'id' => '793',
249
                    'request-id' => 'EXT:APP:200',
250
                    'ref' => '/c/flexipeehp/adresar/793',
251
                ),
252
                2 =>
253
                array(
254
                    'id' => '794',
255
                    'request-id' => 'EXT:APP:300',
256
                    'ref' => '/c/flexipeehp/adresar/794',
257
                ),
258
                3 =>
259
                array(
260
                    'id' => '795',
261
                    'request-id' => 'EXT:APP:400',
262
                    'ref' => '/c/flexipeehp/adresar/795',
263
                ),
264
                4 =>
265
                array(
266
                    'id' => '830',
267
                    'request-id' => 'EXT:APP:500',
268
                    'ref' => '/c/flexipeehp/banka/830',
269
                ),
270
                5 =>
271
                array(
272
                    'id' => '28',
273
                    'ref' => '/c/flexipeehp/adresar-bankovni-ucet/28',
274
                ),
275
            ),
276
            )
277
        ;
278
        $this->object->parseResponse($responseDecoded, 201);
279
    }
280
281
    /**
282
     * @covers FlexiPeeHP\FlexiBeeRW::assignResultIDs
283
     */
284
    public function testAssignResultIDs()
285
    {
286
        $ids = array(
287
            'adresar' =>
288
            array(
289
                'EXT:APP:100' => '792',
290
                'EXT:APP:200' => '793',
291
                'EXT:APP:300' => '794',
292
                'EXT:APP:400' => '795',
293
            ),
294
            'banka' =>
295
            array(
296
                'EXT:APP:500' => '830',
297
            ),
298
            'adresar-bankovni-ucet' =>
299
            array(
300
                '' => '27',
301
            ),
302
        );
303
        $this->object->assignResultIDs($ids);
304
    }
305
306
    /**
307
     * @covers FlexiPeeHP\FlexiBeeRW::extractResultIDs
308
     */
309
    public function testExtractResultIDs()
310
    {
311
        $resultInfo = array(
312
            0 =>
313
            array(
314
                'id' => '792',
315
                'request-id' => 'EXT:APP:100',
316
                'ref' => '/c/flexipeehp/adresar/792',
317
            ),
318
            1 =>
319
            array(
320
                'id' => '793',
321
                'request-id' => 'EXT:APP:200',
322
                'ref' => '/c/flexipeehp/adresar/793',
323
            ),
324
            2 =>
325
            array(
326
                'id' => '794',
327
                'request-id' => 'EXT:APP:300',
328
                'ref' => '/c/flexipeehp/adresar/794',
329
            ),
330
            3 =>
331
            array(
332
                'id' => '795',
333
                'request-id' => 'EXT:APP:400',
334
                'ref' => '/c/flexipeehp/adresar/795',
335
            ),
336
            4 =>
337
            array(
338
                'id' => '830',
339
                'request-id' => 'EXT:APP:500',
340
                'ref' => '/c/flexipeehp/banka/830',
341
            ),
342
            5 =>
343
            array(
344
                'id' => '26',
345
                'ref' => '/c/flexipeehp/adresar-bankovni-ucet/26',
346
            ),
347
        );
348
        $this->object->extractResultIDs($resultInfo);
349
    }
350
351
    /**
352
     * @covers FlexiPeeHP\FlexiBeeRW::getLastInsertedId
353
     */
354
    public function testGetLastInsertedId()
355
    {
356
        $this->object->getLastInsertedId();
0 ignored issues
show
Unused Code introduced by
The call to the method FlexiPeeHP\FlexiBeeRW::getLastInsertedId() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
357
    }
358
359
    /**
360
     * @covers FlexiPeeHP\FlexiBeeRW::deleteFromFlexiBee
361
     */
362
    public function testDeleteFromFlexiBee()
363
    {
364
        $this->object->deleteFromFlexiBee();
365
    }
366
367
    /**
368
     * @covers FlexiPeeHP\FlexiBeeRW::takeData
369
     */
370
    public function testTakeData()
371
    {
372
        $this->object->takeData(['id' => \Ease\Sand::randomNumber()]);
373
    }
374
375
    /**
376
     * @after 
377
     * @covers FlexiPeeHP\FlexiBeeRW::getDataForJSON
378
     */
379
    public function testGetDataForJSON()
380
    {
381
        $this->object->setData(['name' => 'test']);
382
        $this->object->getDataForJSON();
383
    }
384
385
    /**
386
     * @covers FlexiPeeHP\FlexiBeeRW::addExternalID
387
     */
388
    public function testAddExternalID()
389
    {
390
        if (empty($this->object->getEvidence())) {
391
            $this->object->setEvidence('banka');
392
        }
393
        $this->object->addExternalID('ext:test:'.\Ease\Sand::randomNumber());
394
    }
395
396
    /**
397
     * @covers FlexiPeeHP\FlexiBeeRW::changeExternalID
398
     */
399
    public function testChangeExternalID()
400
    {
401
        $this->object->changeExternalID('test', \Ease\Sand::randomNumber(),
0 ignored issues
show
Security Bug introduced by
It seems like \Ease\Sand::randomNumber() targeting Ease\Sand::randomNumber() can also be of type false; however, FlexiPeeHP\FlexiBeeRW::changeExternalID() does only seem to accept string|integer, did you maybe forget to handle an error condition?
Loading history...
402
            \Ease\Sand::randomNumber());
0 ignored issues
show
Security Bug introduced by
It seems like \Ease\Sand::randomNumber() targeting Ease\Sand::randomNumber() can also be of type false; however, FlexiPeeHP\FlexiBeeRW::changeExternalID() does only seem to accept string|integer|null, did you maybe forget to handle an error condition?
Loading history...
403
    }
404
405
    /**
406
     * @covers FlexiPeeHP\FlexiBeeRW::sync
407
     */
408
    public function testSync()
409
    {
410
        $syncResult = $this->object->sync(['kod' => \Ease\Sand::randomString()]);
0 ignored issues
show
Unused Code introduced by
The call to FlexiBeeRW::sync() has too many arguments starting with array('kod' => \Ease\Sand::randomString()).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
411
        if ($this->object->getEvidence()) {
412
            $this->assertTrue($syncResult);
413
        } else {
414
            $this->assertFalse($syncResult);
415
        }
416
    }
417
}
418