Test Failed
Push — master ( 99a915...bca16c )
by Vítězslav
07:03
created

FlexiBeeROTest::testPerformRequest()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 16
nc 7
nop 0
dl 0
loc 22
rs 6.9811
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
    /**
40
     * @covers FlexiPeeHP\FlexiBeeRO::logBanner
41
     */
42
    public function testLogBanner()
43
    {
44
        $this->object->logBanner(addslashes(get_class($this)));
45
    }
46
47
    /**
48
     * Test Constructor
49
     *
50
     * @depends testLogBanner
51
     * @covers FlexiPeeHP\FlexiBeeRO::__construct
52
     */
53
    public function testConstructor()
54
    {
55
        $classname = get_class($this->object);
56
        $evidence  = $this->object->getEvidence();
57
58
        // Get mock, without the constructor being called
59
        $mock = $this->getMockBuilder($classname)
60
            ->disableOriginalConstructor()
61
            ->getMockForAbstractClass();
62
        $mock->__construct(1, ['debug' => false]);
63
64
        if (!isset(\FlexiPeeHP\EvidenceList::$name[$evidence])) {
65
            $evidence = 'adresar';
66
        }
67
68
        $mock->__construct('',
69
            [
70
                'company' => 'Firma_s_r_o_',
71
                'url' => 'https://flexibee.firma.cz/',
72
                'user' => 'rest',
73
                'password' => '-dj3x21xaA_',
74
                'debug' => true,
75
                'prefix' => 'c',
76
                'evidence' => $evidence]);
77
    }
78
79
    /**
80
     * @covers FlexiPeeHP\FlexiBeeRO::curlInit
81
     */
82
    public function testSetupProperty()
83
    {
84
        $properties = ['debug' => true];
85
        $this->object->setupProperty($properties, 'debug');
86
        $this->assertTrue($this->object->debug);
87
        $this->object->setupProperty($properties, 'url', 'FLEXIBEE_URL');
88
    }
89
90
    /**
91
     * @covers FlexiPeeHP\FlexiBeeRO::curlInit
92
     */
93
    public function testCurlInit()
94
    {
95
        $this->object->curlInit();
96
        $this->assertTrue(is_resource($this->object->curl));
97
    }
98
99
    /**
100
     * @covers FlexiPeeHP\FlexiBeeRO::processInit
101
     */
102
    public function testProcessInit()
103
    {
104
        $this->object->processInit(['id' => 1]);
105
        $this->assertEquals(1, $this->object->getDataValue('id'));
106
107
        if (!is_null($this->object->evidence) && $this->object->evidence != 'test') {
108
109
110
            $firstID = $this->object->getColumnsFromFlexibee(['id', 'kod'],
111
                ['limit' => 1]);
112
113
            if (count($firstID) && isset($firstID[0]['id'])) {
114
115
                $this->object->processInit((int) current($firstID));
116
                $this->assertNotEmpty($this->object->__toString());
117
118
                if (isset($firstID[0]['kod'])) {
119
                    $this->object->processInit('code:'.$firstID[0]['kod']);
120
                    $this->assertNotEmpty($this->object->__toString());
121
                }
122
123
                $this->object->processInit($this->object->getEvidenceURL().'/'.$firstID[0]['id'].'.xml');
124
            } else {
125
                $this->markTestSkipped(sprintf('Evidence %s doed not contain first record',
126
                        $this->object->getEvidence()));
127
            }
128
        }
129
    }
130
131
    /**
132
     * @covers FlexiPeeHP\FlexiBeeRO::setUp
133
     */
134
    public function testSetUp()
135
    {
136
        $this->object->setUp(
137
            [
138
                'company' => 'cmp',
139
                'url' => 'url',
140
                'user' => 'usr',
141
                'password' => 'pwd',
142
                'prefix' => 'c',
143
                'debug' => true,
144
                'defaultUrlParams' => ['limit' => 10],
145
                'evidence' => 'smlouva'
146
            ]
147
        );
148
        $this->assertEquals('cmp', $this->object->company);
149
        $this->assertEquals('url', $this->object->url);
150
        $this->assertEquals('usr', $this->object->user);
151
        $this->assertEquals('/c/', $this->object->prefix);
152
        $this->assertEquals('pwd', $this->object->password);
153
    }
154
155
    /**
156
     * @covers FlexiPeeHP\FlexiBeeRO::setPrefix
157
     * @expectedException \Exception
158
     */
159
    public function testSetPrefix()
160
    {
161
        $this->object->setPrefix('c');
162
        $this->assertEquals('/c/', $this->object->prefix);
163
        $this->object->setPrefix(null);
164
        $this->assertEquals('', $this->object->prefix);
165
        $this->object->setPrefix('fail');
166
    }
167
168
    /**
169
     * @covers FlexiPeeHP\FlexiBeeRO::setFormat
170
     */
171
    public function testSetFormat()
172
    {
173
        $this->object->setFormat('xml');
174
        $this->assertEquals('xml', $this->object->format);
175
    }
176
177
    /**
178
     * We can set only evidence defined in EvidenceList class
179
     *
180
     * @covers FlexiPeeHP\FlexiBeeRO::setEvidence
181
     * @expectedException \Exception
182
     */
183
    public function testSetEvidence()
184
    {
185
        $this->object->setEvidence('adresar');
186
        $this->assertEquals('adresar', $this->object->evidence);
187
        $this->object->setPrefix('c');
188
        $this->object->debug = true;
189
        $this->object->setEvidence('fail');
190
    }
191
192
    /**
193
     * @covers FlexiPeeHP\FlexiBeeRO::setCompany
194
     */
195
    public function testSetCompany()
196
    {
197
        $this->object->setCompany('test_s_r_o_');
198
        $this->assertEquals('test_s_r_o_', $this->object->company);
199
    }
200
201
    /**
202
     * @covers FlexiPeeHP\FlexiBeeRO::object2array
203
     */
204
    public function testObject2array()
205
    {
206
        $this->assertNull($this->object->object2array(new \stdClass()));
207
        $this->assertEquals(
208
            [
209
                'item' => 1,
210
                'arrItem' => ['a', 'b' => 'c']
211
            ]
212
            , $this->object->object2array(new \Test\ObjectForTesting()));
213
    }
214
215
    /**
216
     * @covers FlexiPeeHP\FlexiBeeRO::objectToID
217
     */
218 View Code Duplication
    public function testObjectToID()
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...
219
    {
220
        $this->object->setDataValue('kod', 'TEST');
221
        $this->assertEquals('code:TEST',
222
            $this->object->objectToID($this->object));
223
224
        $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...
225
    }
226
227
    /**
228
     * @covers FlexiPeeHP\FlexiBeeRO::performRequest
229
     */
230
    public function testPerformRequest()
231
    {
232
        $evidence = $this->object->getEvidence();
233
        switch ($evidence) {
234
            case null:
235
            case '':
236
            case 'c':
237
            case 'test':
238
            case 'status':
239
            case 'nastaveni':
240
                $this->object->performRequest(null, 'GET', 'xml');
241
                break;
242
243
            default:
244
                $json = $this->object->performRequest(null, 'GET', 'json');
0 ignored issues
show
Unused Code introduced by
$json is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
245
                $xml  = $this->object->performRequest(null, 'GET', 'xml');
0 ignored issues
show
Unused Code introduced by
$xml is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
246
                break;
247
        }
248
249
        //404 Test
250
        $this->assertNull($this->object->performRequest('error404.json'));
251
    }
252
253
    /**
254
     * @covers FlexiPeeHP\FlexiBeeRO::setAction
255
     */
256
    public function testSetAction()
257
    {
258
        switch ($this->object->getEvidence()) {
259
            case '':
260
            case 'c':
261
            case 'hooks':
262
            case 'status':
263
            case 'changes':
264
            case 'nastaveni':
265
            case 'evidence-list':
266
                $this->object->setAction('none');
267
                break;
268
            default:
269
                $this->assertTrue($this->object->setAction('new'));
270
                $this->object->actionsAvailable = [];
271
                $this->assertFalse($this->object->setAction('none'));
272
                $this->object->actionsAvailable = ['copy'];
273
                $this->assertFalse($this->object->setAction('none'));
274
                break;
275
        }
276
    }
277
278
    /**
279
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidence
280
     */
281
    public function testGetEvidence()
282
    {
283
        $this->assertEquals($this->object->evidence,
284
            $this->object->getEvidence());
285
    }
286
287
    /**
288
     * @covers FlexiPeeHP\FlexiBeeRO::getCompany
289
     */
290
    public function testGetCompany()
291
    {
292
        $this->assertEquals($this->object->company, $this->object->getCompany());
293
    }
294
295
    /**
296
     * @covers FlexiPeeHP\FlexiBeeRO::getResponseEvidence
297
     */
298 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...
299
    {
300
        $responseEvidence = $this->object->getResponseEvidence();
301
        switch ($this->object->getEvidence()) {
302
            case '':
303
            case 'c':
304
            case 'hooks':
305
            case 'status':
306
            case 'changes':
307
            case 'nastaveni':
308
            case 'evidence-list':
309
                break;
310
            default:
311
                $this->assertEquals($this->object->getEvidence(),
312
                    $responseEvidence);
313
                break;
314
        }
315
    }
316
317
    /**
318
     * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId
319
     * @depends testInsertToFlexiBee
320
     */
321
    public function testGetLastInsertedId()
322
    {
323
        $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...
324
    }
325
326
    /**
327
     * @covers FlexiPeeHP\FlexiBeeRO::xml2array
328
     */
329
    public function testXml2array()
330
    {
331
        $xml = '<card xmlns="http://businesscard.org">
332
   <name>John Doe</name>
333
   <title>CEO, Widget Inc.</title>
334
   <email>[email protected]</email>
335
   <phone>(202) 456-1414</phone>
336
   <logo url="widget.gif"/>
337
   <a><b>c</b></a>
338
 </card>';
339
340
        $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]',
341
            'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]];
342
343
344
        $this->assertEquals($data, $this->object->xml2array($xml));
345
    }
346
347
    /**
348
     * @covers FlexiPeeHP\FlexiBeeRO::disconnect
349
     *
350
     * @depends testPerformRequest
351
     * @depends testLoadFlexiData
352
     * @depends testGetFlexiRow
353
     * @depends testGetFlexiData
354
     * @depends testLoadFromFlexiBee
355
     * @depends testInsertToFlexiBee
356
     * @depends testIdExists
357
     * @depends testRecordExists
358
     * @depends testGetColumnsFromFlexibee
359
     * @depends testSearchString
360
     */
361
    public function testDisconnect()
362
    {
363
        $this->object->disconnect();
364
        $this->assertNull($this->object->curl);
365
    }
366
367
    /**
368
     * @covers FlexiPeeHP\FlexiBeeRO::__destruct
369
     * @depends testDisconnect
370
     */
371
    public function testdestruct()
372
    {
373
        $this->markTestSkipped();
374
    }
375
376
    /**
377
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow
378
     */
379
    public function testGetFlexiRow()
380
    {
381
        $this->object->getFlexiRow(0);
382
        $this->object->getFlexiRow(1);
383
    }
384
385
    /**
386
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData
387
     */
388
    public function testGetFlexiData()
389
    {
390
        $evidence = $this->object->getEvidence();
391
        switch ($evidence) {
392
            case null:
393
                $this->object->getFlexiData();
394
                break;
395
            case 'c':
396
                $this->object->evidence  = 'c';
397
                $this->object->prefix    = '';
398
                $this->object->company   = '';
399
                $this->object->nameSpace = 'companies';
400
                $flexidata               = $this->object->getFlexiData();
401
                $this->assertArrayHasKey('company', $flexidata);
402
                break;
403
            case 'evidence-list':
404
                $flexidata               = $this->object->getFlexiData(null,
405
                    ['detail' => 'id']);
406
                $this->assertArrayHasKey('evidenceType', $flexidata[0]);
407
                break;
408
409
            default:
410
                $flexidata = $this->object->getFlexiData(null,
411
                    ['detail' => 'id']);
412
413
                if (is_array($flexidata)) {
414
                    if (count($flexidata)) {
415
                        $this->markTestSkipped('Empty evidence');
416
                    } else {
417
                        $this->assertArrayHasKey(0, $flexidata);
418
419
                        $this->assertArrayHasKey('id', $flexidata[0]);
420
                        $filtrered = $this->object->getFlexiData(null,
421
                            ["id = ".$flexidata[0]['id'], 'detail' => 'full']);
422
                        $this->assertArrayHasKey(0, $filtrered);
423
                        $this->assertArrayHasKey('id', $filtrered[0]);
424
                    }
425
                }
426
                break;
427
        }
428
    }
429
430
    /**
431
     * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee
432
     */
433
    public function testLoadFromFlexiBee()
434
    {
435
        $this->object->loadFromFlexiBee();
436
        $this->object->loadFromFlexiBee(222);
437
    }
438
439
    /**
440
     * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData
441
     */
442
    public function testJsonizeData()
443
    {
444
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}',
445
            $this->object->jsonizeData(['key' => 'value']));
446
447
        switch ($this->object->getEvidence()) {
448
            case '':
449
            case 'c':
450
            case 'hooks':
451
            case 'status':
452
            case 'changes':
453
            case 'nastaveni':
454
            case 'evidence-list':
455
                $this->object->jsonizeData(['key' => 'value']);
456
                break;
457
            default:
458
                $this->object->setAction('copy');
459
                $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"},"'.$this->object->evidence.'@action":"copy"}}',
460
                    $this->object->jsonizeData(['key' => 'value']));
461
                break;
462
        }
463
    }
464
465
    /**
466
     * @covers FlexiPeeHP\FlexiBeeRO::idExists
467
     */
468
    public function testIdExists()
469
    {
470
        $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...
471
        switch ($this->object->getEvidence()) {
472
            case '':
473
            case 'c':
474
            case 'hooks':
475
            case 'status':
476
            case 'changes':
477
            case 'nastaveni':
478
            case 'evidence-list':
479
                break;
480
            default:
481
                $first = $this->object->getColumnsFromFlexibee(['id'],
482
                    ['limit' => 1], 'id');
483
                if (empty($first)) {
484
                    $this->markTestSkipped('empty evidence ?');
485
                } else {
486
                    $this->object->setData($first);
487
                    $this->assertTrue($this->object->idExists());
488
                }
489
                break;
490
        }
491
    }
492
493
    /**
494
     * @covers FlexiPeeHP\FlexiBeeRO::getRecordID
495
     */
496
    public function testGetRecordID()
497
    {
498
        $this->object->setData([$this->object->getmyKeyColumn() => 10]);
499
        $this->assertEquals(10, $this->object->getRecordID());
500
    }
501
502
    /**
503
     * @covers FlexiPeeHP\FlexiBeeRO::recordExists
504
     */
505
    public function testRecordExists()
506
    {
507
        $evidence = $this->object->getEvidence();
508
509
        switch ($evidence) {
510
            case null:
511
            case 'c':
512
            case 'status':
513
            case 'evidence-list':
514
                $this->object->recordExists(['id' => 1]);
515
                break;
516
517
            default:
518
                $flexidata = $this->object->getFlexiData(null,
519
                    ['limit' => 1, 'detail' => 'id']);
520
                if (is_array($flexidata) && !count($flexidata)) {
521
                    $this->assertFalse($this->object->recordExists(['id' => 1]),
522
                        'Record ID 1 exists in empty evidence ?');
523
                } else {
524
                    if (!is_null($flexidata)) {
525
                        $this->object->setData(['id' => (int) $flexidata[0]['id']]);
526
                        $this->assertTrue($this->object->recordExists(),
527
                            'First record exists test failed');
528
                        $this->assertFalse($this->object->recordExists(['id' => 0]),
529
                            'Record ID 0 exists');
530
                        $this->assertFalse($this->object->recordExists(['unexistent' => 1]),
531
                            'Unexistent Record exist ?');
532
                    }
533
                }
534
                break;
535
        }
536
    }
537
538
    /**
539
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee
540
     */
541
    public function testGetColumnsFromFlexibee()
542
    {
543
544
        switch ($this->object->getEvidence()) {
545
            case '':
546
            case 'c':
547
            case 'hooks':
548
            case 'status':
549
            case 'changes':
550
            case 'nastaveni':
551
            case 'evidence-list':
552
                break;
553
            default:
554
                $structure = $this->object->getColumnsInfo();
555
556
                $columns = $this->object->getColumnsFromFlexibee([current(array_keys($structure))],
0 ignored issues
show
Documentation introduced by
array(current(array_keys($structure))) is of type array<integer,integer|st...integer|string|false"}>, but the function expects a array<integer,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...
Unused Code introduced by
$columns is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
557
                    ['limit' => 1], 'id');
558
                break;
559
        }
560
    }
561
562
    /**
563
     * @covers FlexiPeeHP\FlexiBeeRO::getExternalID
564
     */
565
    public function testGetExternalID()
566
    {
567
        $this->assertTrue(empty($this->object->getExternalID('ext:test:10'))); //ext: does not exist
568
569
        $this->object->setDataValue('external-ids',
570
            ['ext:doe:22', 'ext:test:10']);
571
572
        $this->assertEquals('ext:doe:22', $this->object->getExternalID());
573
        $this->assertEquals('10', $this->object->getExternalID('test'));
574
    }
575
576
    /**
577
     * @covers FlexiPeeHP\FlexiBeeRO::getGlobalVersion
578
     */
579 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...
580
    {
581
        switch ($this->object->getEvidence()) {
582
            case '':
583
            case 'c':
584
            case 'hooks':
585
            case 'status':
586
            case 'changes':
587
            case 'nastaveni':
588
            case 'evidence-list':
589
                $this->object->getGlobalVersion();
590
                break;
591
            default:
592
                $this->assertInternalType("int",
593
                    $this->object->getGlobalVersion(),
594
                    'error obtaining of GlobalVersion');
595
                break;
596
        }
597
    }
598
599
    /**
600
     * @covers FlexiPeeHP\FlexiBeeRO::getResponseFormat
601
     */
602
    public function testGetResponseFormat()
603
    {
604
        $this->object->performRequest(null, 'GET', 'json');
605
        $this->assertEquals('application/json',
606
            $this->object->getResponseFormat());
607
        $this->object->performRequest(null, 'GET', 'xml');
608
        $this->assertEquals('application/xml',
609
            $this->object->getResponseFormat());
610
    }
611
612
    /**
613
     * @covers FlexiPeeHP\FlexiBeeRO::getKod
614
     */
615
    public function testGetKod()
616
    {
617
        $testString = [];
618
        $this->assertEquals('code:CODE',
619
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
620
621
        $testString[$this->object->nameColumn] = 'Fish clamp -  Úchytka pro instalaci samonosných kabelů '
622
            .'(3.5 mm)';
623
        $code0                                 = $this->object->getKod($testString);
624
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR', $code0);
625
        $code1                                 = $this->object->getKod($testString,
626
            false);
627
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR', $code1);
628
        $code2                                 = $this->object->getKod($testString);
629
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR1', $code2);
630
        $this->object->setData($testString);
631
        $code3                                 = $this->object->getKod();
632
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR2', $code3);
633
634
        $this->assertEquals('code:TEST',
635
            $this->object->getKod([$this->object->nameColumn => 'test']));
636
637
        $this->assertEquals('code:TEST1', $this->object->getKod('test'));
638
639
        $this->assertEquals('code:TEST2',
640
            $this->object->getKod(['kod' => 'test']));
641
        $this->assertEquals('code:NOTSET', $this->object->getKod(['kod' => '']));
642
    }
643
644
    /**
645
     * @covers FlexiPeeHP\FlexiBeeRO::logResult
646
     */
647
    public function testLogResult()
648
    {
649
        $this->object->cleanMessages();
650
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
651
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
652
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
653
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
654
        $this->object->logResult(current($this->object->object2array($success)),
655
            'http://test');
656
657
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
658
659
        $error                          = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
660
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
661
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
662
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
663
            .' kód není unikátní."}]}]}}');
664
        $this->object->lastResponseCode = 500;
665
        $this->object->logResult(current($this->object->object2array($error)));
666
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
667
    }
668
669
    /**
670
     * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl
671
     */
672
    public function testFlexiUrl()
673
    {
674
        $this->assertEquals("a eq '1' and b eq 'foo'",
675
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
676
        $this->assertEquals("a eq '1' or b eq 'bar'",
677
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
678
        $this->assertEquals("a eq true or b eq false",
679
            $this->object->flexiUrl(['a' => true, 'b' => false], 'or'));
680
        $this->assertEquals("a is null and b is not null",
681
            $this->object->flexiUrl(['a' => null, 'b' => '!null'], 'and'));
682
    }
683
684
    /**
685
     * @covers FlexiPeeHP\FlexiBeeRO::unifyResponseFormat
686
     */
687
    public function testunifyResponseFormat()
688
    {
689
        $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...
690
        //One Row Test
691
692
        $responseEvidence = $this->object->getResponseEvidence();
693
        if (empty($responseEvidence)) {
694
            $responseEvidence       = $this->object->evidence = 'test';
695
        }
696
697
        $test1raw = [$responseEvidence =>
698
            ['id' => 1, 'name' => 'value']
699
        ];
700
701
        $test1expected = [$responseEvidence =>
702
            [
703
                ['id' => 1, 'name' => 'value']
704
            ]
705
        ];
706
707
        $this->assertEquals($test1expected,
708
            $this->object->unifyResponseFormat($test1raw));
709
710
        //Two Row Test
711
        $test2Raw = [$this->object->getResponseEvidence() =>
712
            [
713
                ['id' => 1, 'name' => 'value'],
714
                ['id' => 2, 'name' => 'value2']
715
            ]
716
        ];
717
718
        $test2expected = [$this->object->getResponseEvidence() =>
719
            [
720
                ['id' => 1, 'name' => 'value'],
721
                ['id' => 2, 'name' => 'value2']
722
            ]
723
        ];
724
725
        $this->assertEquals($test2expected,
726
            $this->object->unifyResponseFormat($test2Raw));
727
    }
728
729
    /**
730
     * @covers FlexiPeeHP\FlexiBeeRO::__toString
731
     */
732
    public function testtoString()
733
    {
734
735
        $identifer = 'ext:test:123';
736
        $this->object->setDataValue('id', $identifer);
737
        $this->assertEquals($identifer, (string) $this->object);
738
739
        $code = 'test';
740
        $this->object->setDataValue('kod', $code);
741
        $this->assertEquals('code:'.$code, (string) $this->object);
742
743
        $this->object->dataReset();
744
        $this->assertEquals('', $this->object->__toString());
745
    }
746
747
    /**
748
     * @covers FlexiPeeHP\FlexiBeeRO::draw
749
     */
750
    public function testDraw($whatWant = NULL)
751
    {
752
        $this->object->setDataValue('kod', 'test');
753
        $this->assertEquals('code:test', $this->object->draw());
754
    }
755
756
    /**
757
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsInfo
758
     */
759
    public function testgetColumnsInfo()
760
    {
761
        switch ($this->object->getEvidence()) {
762
            case '':
763
            case 'c':
764
            case 'hooks':
765
            case 'status':
766
            case 'changes':
767
            case 'evidence-list':
768
                $this->assertNull($this->object->getColumnsInfo());
769
                break;
770
            default:
771
                $this->assertNotEmpty($this->object->getColumnsInfo(),
772
                    'Cannot obtain structure for '.$this->object->getEvidence());
773
                break;
774
        }
775
    }
776
777
    /**
778
     * @covers FlexiPeeHP\FlexiBeeRO::getActionsInfo
779
     */
780
    public function testgetActionsInfo()
781
    {
782
        switch ($this->object->getEvidence()) {
783
            case '':
784
            case 'c':
785
            case 'hooks':
786
            case 'status':
787
            case 'changes':
788
            case 'nastaveni':
789
            case 'evidence-list':
790
                $this->assertNull($this->object->getActionsInfo());
791
                $this->assertNotEmpty($this->object->getActionsInfo('faktura-vydana'),
792
                    'Cannot obtain actions for na evidence');
793
                break;
794
            default:
795
                $this->assertNotEmpty($this->object->getActionsInfo(),
796
                    'Cannot obtain actions for '.$this->object->getEvidence());
797
                break;
798
        }
799
    }
800
801
    /**
802
     * @covers FlexiPeeHP\FlexiBeeRO::getRelationsInfo
803
     */
804
    public function testgetRelationsInfo()
805
    {
806
        switch ($this->object->getEvidence()) {
807
            case '':
808
            case 'c':
809
            case 'hooks':
810
            case 'status':
811
            case 'changes':
812
            case 'nastaveni':
813
            case 'strom-cenik':
814
            case 'ucetni-obdobi':
815
            case 'evidence-list':
816
                $this->assertNull($this->object->getRelationsInfo());
817
                break;
818
            default:
819
                $this->assertNotEmpty($this->object->getRelationsInfo(),
820
                    'Cannot obtain relations for '.$this->object->getEvidence());
821
                break;
822
        }
823
    }
824
825
    /**
826
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceUrl
827
     */
828
    public function testgetEvidenceUrl()
829
    {
830
        $this->assertNotEmpty($this->object->getEvidenceUrl());
831
        $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...
832
    }
833
834
    /**
835
     * @covers FlexiPeeHP\FlexiBeeRO::evidenceToClassName
836
     */
837
    public function testevidenceToClassName()
838
    {
839
        $this->assertEquals('FakturaVydana',
840
            $this->object->evidenceToClassName('faktura-vydana'));
841
    }
842
843
    /**
844
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceInfo
845
     */
846 View Code Duplication
    public function testGetEvidenceInfo()
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...
847
    {
848
        $eInfo = $this->object->getEvidenceInfo();
849
        switch ($this->object->getEvidence()) {
850
            case '':
851
            case 'c':
852
            case 'hooks':
853
            case 'status':
854
            case 'changes':
855
            case 'nastaveni':
856
            case 'evidence-list':
857
                break;
858
            default:
859
                $this->assertArrayHasKey('evidencePath', $eInfo);
860
                break;
861
        }
862
    }
863
864
    /**
865
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceName
866
     */
867 View Code Duplication
    public function testGetEvidenceName()
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...
868
    {
869
        $evidenceName = $this->object->getEvidenceName();
870
        switch ($this->object->getEvidence()) {
871
            case '':
872
            case 'c':
873
            case 'hooks':
874
            case 'status':
875
            case 'changes':
876
            case 'nastaveni':
877
            case 'evidence-list':
878
                break;
879
            default:
880
                $this->assertNotEmpty($evidenceName);
881
                break;
882
        }
883
    }
884
885
    /**
886
     * @covers FlexiPeeHP\FlexiBeeRO::saveResponseToFile
887
     */
888
    public function testSaveResponseToFile()
889
    {
890
        $tmp = sys_get_temp_dir().'/'.tmpfile();
891
        $this->object->saveResponseToFile($tmp);
892
        $this->assertFileExists($tmp);
893
    }
894
895
    /**
896
     * @covers FlexiPeeHP\FlexiBeeRO::getFirstRecordID()
897
     */
898 View Code Duplication
    public function testgetFirstRecordID()
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...
899
    {
900
        switch ($this->object->getEvidence()) {
901
            case '':
902
            case 'c':
903
            case 'hooks':
904
            case 'status':
905
            case 'changes':
906
            case 'nastaveni':
907
            case 'evidence-list':
908
                break;
909
            default:
910
                $firstID = $this->object->getFirstRecordID();
0 ignored issues
show
Unused Code introduced by
$firstID is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
911
                break;
912
        }
913
    }
914
915
    /**
916
     * @covers FlexiPeeHP\FlexiBeeRO::getVazby
917
     * @expectedException \Exception
918
     */
919 View Code Duplication
    public function testGetVazby()
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...
920
    {
921
        switch ($this->object->getEvidence()) {
922
            case '':
923
            case 'c':
924
            case 'hooks':
925
            case 'status':
926
            case 'changes':
927
            case 'nastaveni':
928
            case 'evidence-list':
929
                break;
930
            default:
931
                $this->object->getVazby($this->object->getMyKey());
932
                break;
933
        }
934
        $this->object->getVazby();
935
    }
936
937
    /**
938
     * @covers FlexiPeeHP\FlexiBeeRO::evidenceUrlWithSuffix
939
     */
940
    public function testEvidenceUrlWithSuffix()
941
    {
942
        $urlraw = $this->object->getEvidenceURL();
943
        $lala   = $this->object->evidenceUrlWithSuffix('lala');
944
        $this->assertEquals($urlraw.'/lala', $lala);
945
        $lolo   = $this->object->evidenceUrlWithSuffix('?lele');
946
        $this->assertEquals($urlraw.'?lele', $lolo);
947
        $lulu   = $this->object->evidenceUrlWithSuffix(';lulu');
948
        $this->assertEquals($urlraw.';lulu', $lulu);
949
    }
950
951
    /**
952
     * @covers FlexiPeeHP\FlexiBeeRO::addUrlParams
953
     */
954
    public function testAddUrlParams()
955
    {
956
        $this->assertEquals('http://vitexsoftware.cz/path?id=1&a=b',
957
            $this->object->addUrlParams('http://vitexsoftware.cz/path?a=b',
958
                ['id' => 1], TRUE));
959
    }
960
961
    /**
962
     * @covers FlexiPeeHP\FlexiBeeRO::addDefaultUrlParams
963
     */
964
    public function testAddDefaultUrlParams()
965
    {
966
        $this->object->defaultUrlParams       = [];
967
        $this->assertEquals('http://vitexsoftware.cz?a=b',
968
            $this->object->addDefaultUrlParams('http://vitexsoftware.cz?a=b'));
969
        $this->object->defaultUrlParams['id'] = 1;
970
        $this->assertEquals('http://vitexsoftware.cz/path?a=b&id=1',
971
            $this->object->addDefaultUrlParams('http://vitexsoftware.cz/path?a=b'));
972
    }
973
974
    public function testFlexiDateToDateTime()
975
    {
976
        $this->assertEquals(1495749600,
977
            FlexiBeeRO::flexiDateToDateTime('2017-05-26+02:00')->getTimestamp());
978
    }
979
980
    public function testFlexiDateTimeToDateTime()
981
    {
982
        $this->assertEquals(1506412853,
983
            FlexiBeeRO::flexiDateTimeToDateTime('2017-09-26T10:00:53.755+02:00')->getTimestamp());
984
    }
985
}
986