Test Failed
Push — master ( 68ff63...99a915 )
by Vítězslav
08:13
created

FlexiBeeROTest::testPerformRequest()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 24
nc 10
nop 0
dl 0
loc 33
rs 4.909
c 2
b 1
f 0
1
<?php
2
3
namespace Test\FlexiPeeHP;
4
5
use FlexiPeeHP\FlexiBeeRO;
6
7
/**
8
 * Generated by PHPUnit_SkeletonGenerator on 2016-05-04 at 10:08:36.
9
 */
10
class FlexiBeeROTest extends \Test\Ease\BrickTest
11
{
12
    /**
13
     * @var FlexiBeeRO
14
     */
15
    protected $object;
16
17
    /**
18
     * Sets up the fixture, for example, opens a network connection.
19
     * This method is called before a test is executed.
20
     * @covers FlexiPeeHP\FlexiBeeRO::__construct
21
     */
22
    protected function setUp()
23
    {
24
        $this->object          = new FlexiBeeRO();
25
        $this->object->prefix  = '';
26
        $this->object->company = '';
27
        $this->object->debug   = true;
28
    }
29
30
    /**
31
     * Tears down the fixture, for example, closes a network connection.
32
     * This method is called after a test is executed.
33
     */
34
    protected function tearDown()
35
    {
36
        
37
    }
38
39
    public function testConstructor()
40
    {
41
        $classname = get_class($this->object);
42
        $evidence  = $this->object->getEvidence();
43
44
        // Get mock, without the constructor being called
45
        $mock = $this->getMockBuilder($classname)
46
            ->disableOriginalConstructor()
47
            ->getMockForAbstractClass();
48
        $mock->__construct(1, ['debug' => false]);
49
50
        if (!isset(\FlexiPeeHP\EvidenceList::$name[$evidence])) {
51
            $evidence = 'adresar';
52
        }
53
54
        $mock->__construct('',
55
            [
56
                'company' => 'Firma_s_r_o_',
57
                'url' => 'https://flexibee.firma.cz/',
58
                'user' => 'rest',
59
                'password' => '-dj3x21xaA_',
60
                'debug' => true,
61
                'prefix' => 'c',
62
                'evidence' => $evidence]);
63
    }
64
65
    /**
66
     * @covers FlexiPeeHP\FlexiBeeRO::logBanner
67
     */
68
    public function testLogBanner()
69
    {
70
        $this->object->logBanner(addslashes(get_class($this)));
71
    }
72
73
    /**
74
     * @covers FlexiPeeHP\FlexiBeeRO::curlInit
75
     */
76
    public function testSetupProperty()
77
    {
78
        $properties = ['debug' => true];
79
        $this->object->setupProperty($properties, 'debug');
80
        $this->assertTrue($this->object->debug);
81
        $this->object->setupProperty($properties, 'url', 'FLEXIBEE_URL');
82
    }
83
84
    /**
85
     * @covers FlexiPeeHP\FlexiBeeRO::curlInit
86
     */
87
    public function testCurlInit()
88
    {
89
        $this->object->curlInit();
90
        $this->assertTrue(is_resource($this->object->curl));
91
    }
92
93
    /**
94
     * @covers FlexiPeeHP\FlexiBeeRO::processInit
95
     */
96
    public function testProcessInit()
97
    {
98
        $this->object->processInit(['id' => 1]);
99
        $this->assertEquals(1, $this->object->getDataValue('id'));
100
101
        if (!is_null($this->object->evidence) && $this->object->evidence != 'test') {
102
103
104
            $firstID = $this->object->getColumnsFromFlexibee(['id', 'kod'],
105
                ['limit' => 1]);
106
107
            if (count($firstID) && isset($firstID[0]['id'])) {
108
109
                $this->object->processInit((int) current($firstID));
110
                $this->assertNotEmpty($this->object->__toString());
111
112
                if (isset($firstID[0]['kod'])) {
113
                    $this->object->processInit('code:'.$firstID[0]['kod']);
114
                    $this->assertNotEmpty($this->object->__toString());
115
                }
116
117
                $this->object->processInit($this->object->getEvidenceURL().'/'.$firstID[0]['id'].'.xml');
118
            } else {
119
                $this->markTestSkipped(sprintf('Evidence %s doed not contain first record',
120
                        $this->object->getEvidence()));
121
            }
122
        }
123
    }
124
125
    /**
126
     * @covers FlexiPeeHP\FlexiBeeRO::setUp
127
     */
128
    public function testSetUp()
129
    {
130
        $this->object->setUp(
131
            [
132
                'company' => 'cmp',
133
                'url' => 'url',
134
                'user' => 'usr',
135
                'password' => 'pwd',
136
                'prefix' => 'c',
137
                'debug' => true,
138
                'defaultUrlParams' => ['limit' => 10],
139
                'evidence' => 'smlouva'
140
            ]
141
        );
142
        $this->assertEquals('cmp', $this->object->company);
143
        $this->assertEquals('url', $this->object->url);
144
        $this->assertEquals('usr', $this->object->user);
145
        $this->assertEquals('/c/', $this->object->prefix);
146
        $this->assertEquals('pwd', $this->object->password);
147
    }
148
149
    /**
150
     * @covers FlexiPeeHP\FlexiBeeRO::setPrefix
151
     * @expectedException \Exception
152
     */
153
    public function testSetPrefix()
154
    {
155
        $this->object->setPrefix('c');
156
        $this->assertEquals('/c/', $this->object->prefix);
157
        $this->object->setPrefix(null);
158
        $this->assertEquals('', $this->object->prefix);
159
        $this->object->setPrefix('fail');
160
    }
161
162
    /**
163
     * @covers FlexiPeeHP\FlexiBeeRO::setFormat
164
     */
165
    public function testSetFormat()
166
    {
167
        $this->object->setFormat('xml');
168
        $this->assertEquals('xml', $this->object->format);
169
    }
170
171
    /**
172
     * We can set only evidence defined in EvidenceList class
173
     *
174
     * @covers FlexiPeeHP\FlexiBeeRO::setEvidence
175
     * @expectedException \Exception
176
     */
177
    public function testSetEvidence()
178
    {
179
        $this->object->setEvidence('adresar');
180
        $this->assertEquals('adresar', $this->object->evidence);
181
        $this->object->setPrefix('c');
182
        $this->object->debug = true;
183
        $this->object->setEvidence('fail');
184
    }
185
186
    /**
187
     * @covers FlexiPeeHP\FlexiBeeRO::setCompany
188
     */
189
    public function testSetCompany()
190
    {
191
        $this->object->setCompany('test_s_r_o_');
192
        $this->assertEquals('test_s_r_o_', $this->object->company);
193
    }
194
195
    /**
196
     * @covers FlexiPeeHP\FlexiBeeRO::object2array
197
     */
198
    public function testObject2array()
199
    {
200
        $this->assertNull($this->object->object2array(new \stdClass()));
201
        $this->assertEquals(
202
            [
203
                'item' => 1,
204
                'arrItem' => ['a', 'b' => 'c']
205
            ]
206
            , $this->object->object2array(new \Test\ObjectForTesting()));
207
    }
208
209
    /**
210
     * @covers FlexiPeeHP\FlexiBeeRO::objectToID
211
     */
212
    public function testObjectToID()
213
    {
214
        $id = \Ease\Sand::randomNumber(1, 9999);
215
        $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 214 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...
216
        $this->assertEquals([$id], $this->object->objectToID([$this->object]));
217
218
        $this->object->setDataValue('kod', 'TEST');
219
        $this->assertEquals('code:TEST',
220
            $this->object->objectToID($this->object));
221
222
        $this->assertEquals('TEST', $this->object->objectToID('TEST'));
0 ignored issues
show
Documentation introduced by
'TEST' is of type string, but the function expects a object|array.

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...
223
    }
224
225
    /**
226
     * @covers FlexiPeeHP\FlexiBeeRO::performRequest
227
     */
228
    public function testPerformRequest()
229
    {
230
        $evidence = $this->object->getEvidence();
231
        switch ($evidence) {
232
            case null:
233
            case '':
234
            case 'c':
235
            case 'test':
236
            case 'status':
237
            case 'nastaveni':
238
                $this->object->performRequest(null, 'GET', 'xml');
239
                break;
240
241
            default:
242
                $json = $this->object->performRequest(null, 'GET', 'json');
243
                if (array_key_exists('message', $json)) {
244
                    $this->assertArrayHasKey('@version', $json);
245
                } else {
246
                    $this->assertArrayHasKey($this->object->getResponseEvidence(),
247
                        $json);
248
                }
249
250
                $xml = $this->object->performRequest(null, 'GET', 'xml');
251
                if (!empty($xml)) {
252
                    $this->assertArrayHasKey($this->object->getResponseEvidence(),
253
                        $xml);
254
                }
255
                break;
256
        }
257
258
        //404 Test
259
        $this->assertNull($this->object->performRequest('error404.json'));
260
    }
261
262
    /**
263
     * @covers FlexiPeeHP\FlexiBeeRO::setAction
264
     */
265
    public function testSetAction()
266
    {
267
        switch ($this->object->getEvidence()) {
268
            case '':
269
            case 'c':
270
            case 'hooks':
271
            case 'status':
272
            case 'changes':
273
            case 'nastaveni':
274
            case 'evidence-list':
275
                $this->object->setAction('none');
276
                break;
277
            default:
278
                $this->assertTrue($this->object->setAction('new'));
279
                $this->object->actionsAvailable = [];
280
                $this->assertFalse($this->object->setAction('none'));
281
                $this->object->actionsAvailable = ['copy'];
282
                $this->assertFalse($this->object->setAction('none'));
283
                break;
284
        }
285
    }
286
287
    /**
288
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidence
289
     */
290
    public function testGetEvidence()
291
    {
292
        $this->assertEquals($this->object->evidence,
293
            $this->object->getEvidence());
294
    }
295
296
    /**
297
     * @covers FlexiPeeHP\FlexiBeeRO::getCompany
298
     */
299
    public function testGetCompany()
300
    {
301
        $this->assertEquals($this->object->company, $this->object->getCompany());
302
    }
303
304
    /**
305
     * @covers FlexiPeeHP\FlexiBeeRO::getResponseEvidence
306
     */
307 View Code Duplication
    public function testGetResponseEvidence()
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...
308
    {
309
        $responseEvidence = $this->object->getResponseEvidence();
310
        switch ($this->object->getEvidence()) {
311
            case '':
312
            case 'c':
313
            case 'hooks':
314
            case 'status':
315
            case 'changes':
316
            case 'nastaveni':
317
            case 'evidence-list':
318
                break;
319
            default:
320
                $this->assertEquals($this->object->getEvidence(),
321
                    $responseEvidence);
322
                break;
323
        }
324
    }
325
326
    /**
327
     * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId
328
     * @depends testInsertToFlexiBee
329
     */
330
    public function testGetLastInsertedId()
331
    {
332
        $this->assertNotEmpty($this->object->getLastInsertedId());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FlexiPeeHP\FlexiBeeRO as the method getLastInsertedId() does only exist in the following sub-classes of FlexiPeeHP\FlexiBeeRO: FlexiPeeHP\Adresar, FlexiPeeHP\Banka, FlexiPeeHP\Cenik, FlexiPeeHP\Company, FlexiPeeHP\DodavatelskaSmlouva, FlexiPeeHP\FakturaPrijata, FlexiPeeHP\FakturaVydana, FlexiPeeHP\FakturaVydanaPolozka, FlexiPeeHP\FlexiBeeRW, FlexiPeeHP\Hooks, FlexiPeeHP\Kontakt, FlexiPeeHP\Pokladna, FlexiPeeHP\PokladniPohyb, FlexiPeeHP\Priloha, FlexiPeeHP\RadaPokladniPohyb, FlexiPeeHP\SkladovaKarta, FlexiPeeHP\SkladovyPohyb, FlexiPeeHP\SkladovyPohybPolozka, FlexiPeeHP\SkupinaFirem, FlexiPeeHP\Stitek, FlexiPeeHP\Strom, FlexiPeeHP\StromCenik, FlexiPeeHP\UcetniObdobi, FlexiPeeHP\VyrobniCislo. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
333
    }
334
335
    /**
336
     * @covers FlexiPeeHP\FlexiBeeRO::xml2array
337
     */
338
    public function testXml2array()
339
    {
340
        $xml = '<card xmlns="http://businesscard.org">
341
   <name>John Doe</name>
342
   <title>CEO, Widget Inc.</title>
343
   <email>[email protected]</email>
344
   <phone>(202) 456-1414</phone>
345
   <logo url="widget.gif"/>
346
   <a><b>c</b></a>
347
 </card>';
348
349
        $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]',
350
            'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]];
351
352
353
        $this->assertEquals($data, $this->object->xml2array($xml));
354
    }
355
356
    /**
357
     * @covers FlexiPeeHP\FlexiBeeRO::disconnect
358
     *
359
     * @depends testPerformRequest
360
     * @depends testLoadFlexiData
361
     * @depends testGetFlexiRow
362
     * @depends testGetFlexiData
363
     * @depends testLoadFromFlexiBee
364
     * @depends testInsertToFlexiBee
365
     * @depends testIdExists
366
     * @depends testRecordExists
367
     * @depends testGetColumnsFromFlexibee
368
     * @depends testSearchString
369
     */
370
    public function testDisconnect()
371
    {
372
        $this->object->disconnect();
373
        $this->assertNull($this->object->curl);
374
    }
375
376
    /**
377
     * @covers FlexiPeeHP\FlexiBeeRO::__destruct
378
     * @depends testDisconnect
379
     */
380
    public function testdestruct()
381
    {
382
        $this->markTestSkipped();
383
    }
384
385
    /**
386
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow
387
     */
388
    public function testGetFlexiRow()
389
    {
390
        $this->object->getFlexiRow(0);
391
        $this->object->getFlexiRow(1);
392
    }
393
394
    /**
395
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData
396
     */
397
    public function testGetFlexiData()
398
    {
399
        $evidence = $this->object->getEvidence();
400
        switch ($evidence) {
401
            case null:
402
                $this->object->getFlexiData();
403
                break;
404
            case 'c':
405
                $this->object->evidence  = 'c';
406
                $this->object->prefix    = '';
407
                $this->object->company   = '';
408
                $this->object->nameSpace = 'companies';
409
                $flexidata               = $this->object->getFlexiData();
410
                $this->assertArrayHasKey('company', $flexidata);
411
                break;
412
            case 'evidence-list':
413
                $flexidata               = $this->object->getFlexiData(null,
414
                    ['detail' => 'id']);
415
                $this->assertArrayHasKey('evidenceType', $flexidata[0]);
416
                break;
417
418
            default:
419
                $flexidata = $this->object->getFlexiData(null,
420
                    ['detail' => 'id']);
421
422
                if (is_array($flexidata)) {
423
                    if (count($flexidata)) {
424
                        $this->markTestSkipped('Empty evidence');
425
                    } else {
426
                        $this->assertArrayHasKey(0, $flexidata);
427
428
                        $this->assertArrayHasKey('id', $flexidata[0]);
429
                        $filtrered = $this->object->getFlexiData(null,
430
                            ["id = ".$flexidata[0]['id'], 'detail' => 'full']);
431
                        $this->assertArrayHasKey(0, $filtrered);
432
                        $this->assertArrayHasKey('id', $filtrered[0]);
433
                    }
434
                }
435
                break;
436
        }
437
    }
438
439
    /**
440
     * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee
441
     */
442
    public function testLoadFromFlexiBee()
443
    {
444
        $this->object->loadFromFlexiBee();
445
        $this->object->loadFromFlexiBee(222);
446
    }
447
448
    /**
449
     * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData
450
     */
451
    public function testJsonizeData()
452
    {
453
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}',
454
            $this->object->jsonizeData(['key' => 'value']));
455
456
        switch ($this->object->getEvidence()) {
457
            case '':
458
            case 'c':
459
            case 'hooks':
460
            case 'status':
461
            case 'changes':
462
            case 'nastaveni':
463
            case 'evidence-list':
464
                $this->object->jsonizeData(['key' => 'value']);
465
                break;
466
            default:
467
                $this->object->setAction('copy');
468
                $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"},"'.$this->object->evidence.'@action":"copy"}}',
469
                    $this->object->jsonizeData(['key' => 'value']));
470
                break;
471
        }
472
    }
473
474
    /**
475
     * @covers FlexiPeeHP\FlexiBeeRO::idExists
476
     */
477
    public function testIdExists()
478
    {
479
        $this->assertFalse($this->object->idExists('nonexistent'));
0 ignored issues
show
Documentation introduced by
'nonexistent' is of type string, but the function expects a boolean|null.

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...
480
        switch ($this->object->getEvidence()) {
481
            case '':
482
            case 'c':
483
            case 'hooks':
484
            case 'status':
485
            case 'changes':
486
            case 'nastaveni':
487
            case 'evidence-list':
488
                break;
489
            default:
490
                $first = $this->object->getColumnsFromFlexibee(['id'],
491
                    ['limit' => 1], 'id');
492
                if (empty($first)) {
493
                    $this->markTestSkipped('empty evidence ?');
494
                } else {
495
                    $this->object->setData($first);
496
                    $this->assertTrue($this->object->idExists());
497
                }
498
                break;
499
        }
500
    }
501
502
    /**
503
     * @covers FlexiPeeHP\FlexiBeeRO::getRecordID
504
     */
505
    public function testGetRecordID()
506
    {
507
        $this->object->setData(['id' => 10]);
508
        $this->assertEquals(10, $this->object->getRecordID());
509
        $this->object->setData(['kod' => 'KOD']);
510
        $this->assertEquals('code:KOD', $this->object->getRecordID());
511
    }
512
513
    /**
514
     * @covers FlexiPeeHP\FlexiBeeRO::recordExists
515
     */
516
    public function testRecordExists()
517
    {
518
        $evidence = $this->object->getEvidence();
519
520
        switch ($evidence) {
521
            case null:
522
            case 'c':
523
            case 'status':
524
            case 'evidence-list':
525
                $this->object->recordExists(['id' => 1]);
526
                break;
527
528
            default:
529
                $flexidata = $this->object->getFlexiData(null,
530
                    ['limit' => 1, 'detail' => 'id']);
531
                if (is_array($flexidata) && !count($flexidata)) {
532
                    $this->assertFalse($this->object->recordExists(['id' => 1]),
533
                        'Record ID 1 exists in empty evidence ?');
534
                } else {
535
                    if (isset($flexidata['success']) && ($flexidata['success'] == 'false')) {
536
                        $this->markTestSkipped($flexidata['message']);
537
                    } else {
538
                        $this->object->setData(['id' => (int) $flexidata[0]['id']]);
539
                        $this->assertTrue($this->object->recordExists(),
540
                            'First record exists test failed');
541
                        $this->assertFalse($this->object->recordExists(['id' => 0]),
542
                            'Record ID 0 exists');
543
                        $this->assertFalse($this->object->recordExists(['unexistent' => 1]),
544
                            'Unexistent Record exist ?');
545
                    }
546
                }
547
                break;
548
        }
549
    }
550
551
    /**
552
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee
553
     */
554
    public function testGetColumnsFromFlexibee()
555
    {
556
        $columns = $this->object->getColumnsFromFlexibee(['id'], ['limit' => 1],
557
            'id');
558
559
        switch ($this->object->getEvidence()) {
560
            case '':
561
            case 'c':
562
            case 'hooks':
563
            case 'status':
564
            case 'changes':
565
            case 'nastaveni':
566
            case 'evidence-list':
567
                break;
568
            default:
569
                $this->assertNotEmpty($columns);
570
                break;
571
        }
572
    }
573
574
    /**
575
     * @covers FlexiPeeHP\FlexiBeeRO::getExternalID
576
     */
577
    public function testGetExternalID()
578
    {
579
        $this->assertTrue(empty($this->object->getExternalID('ext:test:10'))); //ext: does not exist
580
581
        $this->object->setDataValue('external-ids',
582
            ['ext:doe:22', 'ext:test:10']);
583
584
        $this->assertEquals('ext:doe:22', $this->object->getExternalID());
585
        $this->assertEquals('10', $this->object->getExternalID('test'));
586
    }
587
588
    /**
589
     * @covers FlexiPeeHP\FlexiBeeRO::getGlobalVersion
590
     */
591 View Code Duplication
    public function testGetGlobalVersion()
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...
592
    {
593
        switch ($this->object->getEvidence()) {
594
            case '':
595
            case 'c':
596
            case 'hooks':
597
            case 'status':
598
            case 'changes':
599
            case 'nastaveni':
600
            case 'evidence-list':
601
                $this->object->getGlobalVersion();
602
                break;
603
            default:
604
                $this->assertInternalType("int",
605
                    $this->object->getGlobalVersion(),
606
                    'error obtaining of GlobalVersion');
607
                break;
608
        }
609
    }
610
611
    /**
612
     * @covers FlexiPeeHP\FlexiBeeRO::getResponseFormat
613
     */
614
    public function testGetResponseFormat()
615
    {
616
        $this->object->performRequest(null, 'GET', 'json');
617
        $this->assertEquals('application/json',
618
            $this->object->getResponseFormat());
619
        $this->object->performRequest(null, 'GET', 'xml');
620
        $this->assertEquals('application/xml',
621
            $this->object->getResponseFormat());
622
    }
623
624
    /**
625
     * @covers FlexiPeeHP\FlexiBeeRO::getKod
626
     */
627
    public function testGetKod()
628
    {
629
        $testString = [];
630
        $this->assertEquals('code:CODE',
631
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
632
633
        $testString[$this->object->nameColumn] = 'Fish clamp -  Úchytka pro instalaci samonosných kabelů '
634
            .'(3.5 mm)';
635
        $code0                                 = $this->object->getKod($testString);
636
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR', $code0);
637
        $code1                                 = $this->object->getKod($testString,
638
            false);
639
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR', $code1);
640
        $code2                                 = $this->object->getKod($testString);
641
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR1', $code2);
642
        $this->object->setData($testString);
643
        $code3                                 = $this->object->getKod();
644
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR2', $code3);
645
646
        $this->assertEquals('code:TEST',
647
            $this->object->getKod([$this->object->nameColumn => 'test']));
648
649
        $this->assertEquals('code:TEST1', $this->object->getKod('test'));
650
651
        $this->assertEquals('code:TEST2',
652
            $this->object->getKod(['kod' => 'test']));
653
        $this->assertEquals('code:NOTSET', $this->object->getKod(['kod' => '']));
654
    }
655
656
    /**
657
     * @covers FlexiPeeHP\FlexiBeeRO::logResult
658
     */
659
    public function testLogResult()
660
    {
661
        $this->object->cleanMessages();
662
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
663
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
664
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
665
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
666
        $this->object->logResult(current($this->object->object2array($success)),
667
            'http://test');
668
669
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
670
671
        $error                          = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
672
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
673
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
674
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
675
            .' kód není unikátní."}]}]}}');
676
        $this->object->lastResponseCode = 500;
677
        $this->object->logResult(current($this->object->object2array($error)));
678
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
679
    }
680
681
    /**
682
     * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl
683
     */
684
    public function testFlexiUrl()
685
    {
686
        $this->assertEquals("a eq '1' and b eq 'foo'",
687
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
688
        $this->assertEquals("a eq '1' or b eq 'bar'",
689
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
690
        $this->assertEquals("a eq true or b eq false",
691
            $this->object->flexiUrl(['a' => true, 'b' => false], 'or'));
692
        $this->assertEquals("a is null and b is not null",
693
            $this->object->flexiUrl(['a' => null, 'b' => '!null'], 'and'));
694
    }
695
696
    /**
697
     * @covers FlexiPeeHP\FlexiBeeRO::unifyResponseFormat
698
     */
699
    public function testunifyResponseFormat()
700
    {
701
        $this->assertNull($this->object->unifyResponseFormat(null));
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
702
        //One Row Test
703
704
        $responseEvidence = $this->object->getResponseEvidence();
705
        if (empty($responseEvidence)) {
706
            $responseEvidence       = $this->object->evidence = 'test';
707
        }
708
709
        $test1raw = [$responseEvidence =>
710
            ['id' => 1, 'name' => 'value']
711
        ];
712
713
        $test1expected = [$responseEvidence =>
714
            [
715
                ['id' => 1, 'name' => 'value']
716
            ]
717
        ];
718
719
        $this->assertEquals($test1expected,
720
            $this->object->unifyResponseFormat($test1raw));
721
722
        //Two Row Test
723
        $test2Raw = [$this->object->getResponseEvidence() =>
724
            [
725
                ['id' => 1, 'name' => 'value'],
726
                ['id' => 2, 'name' => 'value2']
727
            ]
728
        ];
729
730
        $test2expected = [$this->object->getResponseEvidence() =>
731
            [
732
                ['id' => 1, 'name' => 'value'],
733
                ['id' => 2, 'name' => 'value2']
734
            ]
735
        ];
736
737
        $this->assertEquals($test2expected,
738
            $this->object->unifyResponseFormat($test2Raw));
739
    }
740
741
    /**
742
     * @covers FlexiPeeHP\FlexiBeeRO::__toString
743
     */
744
    public function testtoString()
745
    {
746
747
        $identifer = 'ext:test:123';
748
        $this->object->setDataValue('id', $identifer);
749
        $this->assertEquals($identifer, (string) $this->object);
750
751
        $code = 'test';
752
        $this->object->setDataValue('kod', $code);
753
        $this->assertEquals('code:'.$code, (string) $this->object);
754
755
        $this->object->dataReset();
756
        $this->assertEquals('', $this->object->__toString());
757
    }
758
759
    /**
760
     * @covers FlexiPeeHP\FlexiBeeRO::draw
761
     */
762
    public function testDraw($whatWant = NULL)
763
    {
764
        $this->object->setDataValue('kod', 'test');
765
        $this->assertEquals('code:test', $this->object->draw());
766
    }
767
768
    /**
769
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsInfo
770
     */
771 View Code Duplication
    public function testgetColumnsInfo()
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...
772
    {
773
        switch ($this->object->getEvidence()) {
774
            case '':
775
            case 'c':
776
            case 'hooks':
777
            case 'status':
778
            case 'changes':
779
            case 'nastaveni':
780
            case 'evidence-list':
781
                $this->assertNull($this->object->getColumnsInfo());
782
                break;
783
            default:
784
                $this->assertNotEmpty($this->object->getColumnsInfo(),
785
                    'Cannot obtain structure for '.$this->object->getEvidence());
786
                break;
787
        }
788
    }
789
790
    /**
791
     * @covers FlexiPeeHP\FlexiBeeRO::getActionsInfo
792
     */
793
    public function testgetActionsInfo()
794
    {
795
        switch ($this->object->getEvidence()) {
796
            case '':
797
            case 'c':
798
            case 'hooks':
799
            case 'status':
800
            case 'changes':
801
            case 'nastaveni':
802
            case 'evidence-list':
803
                $this->assertNull($this->object->getActionsInfo());
804
                $this->assertNotEmpty($this->object->getActionsInfo('faktura-vydana'),
805
                    'Cannot obtain actions for na evidence');
806
                break;
807
            default:
808
                $this->assertNotEmpty($this->object->getActionsInfo(),
809
                    'Cannot obtain actions for '.$this->object->getEvidence());
810
                break;
811
        }
812
    }
813
814
    /**
815
     * @covers FlexiPeeHP\FlexiBeeRO::getRelationsInfo
816
     */
817 View Code Duplication
    public function testgetRelationsInfo()
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...
818
    {
819
        switch ($this->object->getEvidence()) {
820
            case '':
821
            case 'c':
822
            case 'hooks':
823
            case 'status':
824
            case 'changes':
825
            case 'nastaveni':
826
            case 'strom-cenik':
827
            case 'ucetni-obdobi':
828
            case 'evidence-list':
829
                $this->assertNull($this->object->getRelationsInfo());
830
                break;
831
            default:
832
                $this->assertNotEmpty($this->object->getRelationsInfo(),
833
                    'Cannot obtain relations for '.$this->object->getEvidence());
834
                break;
835
        }
836
    }
837
838
    /**
839
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceUrl
840
     */
841
    public function testgetEvidenceUrl()
842
    {
843
        $this->assertNotEmpty($this->object->getEvidenceUrl());
844
        $this->assertNotEmpty($this->object->getEvidenceUrl('/properties'));
0 ignored issues
show
Unused Code introduced by
The call to FlexiBeeRO::getEvidenceURL() has too many arguments starting with '/properties'.

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...
845
    }
846
847
    /**
848
     * @covers FlexiPeeHP\FlexiBeeRO::evidenceToClassName
849
     */
850
    public function testevidenceToClassName()
851
    {
852
        $this->assertEquals('FakturaVydana',
853
            $this->object->evidenceToClassName('faktura-vydana'));
854
    }
855
856
    /**
857
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceInfo
858
     */
859
    public function testGetEvidenceInfo()
860
    {
861
        $eInfo = $this->object->getEvidenceInfo();
862
        switch ($this->object->getEvidence()) {
863
            case '':
864
            case 'c':
865
            case 'hooks':
866
            case 'status':
867
            case 'changes':
868
            case 'nastaveni':
869
            case 'evidence-list':
870
                break;
871
            default:
872
                $this->assertArrayHasKey('evidencePath', $eInfo);
873
                break;
874
        }
875
    }
876
877
    /**
878
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceName
879
     */
880
    public function testGetEvidenceName()
881
    {
882
        $evidenceName = $this->object->getEvidenceName();
883
        switch ($this->object->getEvidence()) {
884
            case '':
885
            case 'c':
886
            case 'hooks':
887
            case 'status':
888
            case 'changes':
889
            case 'nastaveni':
890
            case 'evidence-list':
891
                break;
892
            default:
893
                $this->assertNotEmpty($evidenceName);
894
                break;
895
        }
896
    }
897
898
    /**
899
     * @covers FlexiPeeHP\FlexiBeeRO::performAction
900
     * @expectedException \Exception
901
     */
902
    public function testPerformAction()
903
    {
904
        $actions = $this->object->getActionsInfo();
905
906
        if (count($actions)) {
907
            if (array_key_exists('new', $actions)) {
908
                $this->object->performAction('new', 'ext');
909
            }
910
911
            if (array_key_exists('storno', $actions)) {
912
                $this->assertTrue($this->object->performAction('storno'));
913
            }
914
        }
915
        $this->object->performAction('nonexitst');
916
    }
917
918
    /**
919
     * @covers FlexiPeeHP\FlexiBeeRO::saveResponseToFile
920
     */
921
    public function testSaveResponseToFile()
922
    {
923
        $tmp = sys_get_temp_dir().'/'.tmpfile();
924
        $this->object->saveResponseToFile($tmp);
925
        $this->assertFileExists($tmp);
926
    }
927
928
    /**
929
     * @covers FlexiPeeHP\FlexiBeeRO::getFirstRecordID()
930
     */
931
    public function testgetFirstRecordID()
932
    {
933
        $firstID = $this->object->getFirstRecordID();
934
        switch ($this->object->getEvidence()) {
935
            case '':
936
            case 'c':
937
            case 'hooks':
938
            case 'status':
939
            case 'changes':
940
            case 'nastaveni':
941
            case 'evidence-list':
942
                break;
943
            default:
944
                if (is_null($firstID)) {
945
                    $this->markTestSkipped('Empty evidence');
946
                } else {
947
                    $this->assertFalse(empty($firstID));
948
                }
949
                break;
950
        }
951
    }
952
953
    /**
954
     * @covers FlexiPeeHP\FlexiBeeRO::getVazby
955
     * @expectedException \Exception
956
     */
957
    public function testGetVazby()
958
    {
959
        switch ($this->object->getEvidence()) {
960
            case '':
961
            case 'c':
962
            case 'hooks':
963
            case 'status':
964
            case 'changes':
965
            case 'nastaveni':
966
                break;
967
            default:
968
                //       $this->expectException($this->object->getVazby());
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
969
                $first = $this->object->getFirstRecordID();
970
                if (!empty($first)) {
971
                    $this->object->setMyKey($first);
972
                    $vazby = $this->object->getVazby();
973
                    if (is_null($vazby)) {
974
                        $this->markTestSkipped('No bonds to check');
975
                    } else {
976
                        $this->assertTrue(is_array($vazby));
977
                    }
978
                }
979
                $this->object->setMyKey(null);
980
                $this->object->getVazby(); //Get Exception
981
                break;
982
        }
983
    }
984
985
    /**
986
     * @covers FlexiPeeHP\FlexiBeeRO::evidenceUrlWithSuffix
987
     */
988
    public function testEvidenceUrlWithSuffix()
989
    {
990
        $urlraw = $this->object->getEvidenceURL();
991
        $lala   = $this->object->evidenceUrlWithSuffix('lala');
992
        $this->assertEquals($urlraw.'/lala', $lala);
993
        $lolo   = $this->object->evidenceUrlWithSuffix('?lele');
994
        $this->assertEquals($urlraw.'?lele', $lolo);
995
        $lulu   = $this->object->evidenceUrlWithSuffix(';lulu');
996
        $this->assertEquals($urlraw.';lulu', $lulu);
997
    }
998
999
    /**
1000
     * @covers FlexiPeeHP\FlexiBeeRO::addUrlParams
1001
     */
1002
    public function testAddUrlParams()
1003
    {
1004
        $this->assertEquals('http://vitexsoftware.cz/path?id=1&a=b',
1005
            $this->object->addUrlParams('http://vitexsoftware.cz/path?a=b',
1006
                ['id' => 1], TRUE));
1007
    }
1008
1009
    /**
1010
     * @covers FlexiPeeHP\FlexiBeeRO::addDefaultUrlParams
1011
     */
1012
    public function testAddDefaultUrlParams()
1013
    {
1014
        $this->object->defaultUrlParams       = [];
1015
        $this->assertEquals('http://vitexsoftware.cz?a=b',
1016
            $this->object->addDefaultUrlParams('http://vitexsoftware.cz?a=b'));
1017
        $this->object->defaultUrlParams['id'] = 1;
1018
        $this->assertEquals('http://vitexsoftware.cz/path?a=b&id=1',
1019
            $this->object->addDefaultUrlParams('http://vitexsoftware.cz/path?a=b'));
1020
    }
1021
1022
    public function testFlexiDateToDateTime()
1023
    {
1024
        $this->assertEquals(1495749600,
1025
            FlexiBeeRO::flexiDateToDateTime('2017-05-26+02:00')->getTimestamp());
1026
    }
1027
1028
    public function testFlexiDateTimeToDateTime()
1029
    {
1030
        $this->assertEquals(1506412853,
1031
            FlexiBeeRO::flexiDateTimeToDateTime('2017-09-26T10:00:53.755+02:00')->getTimestamp());
1032
    }
1033
}
1034