Test Failed
Push — master ( cc2d13...fdb64f )
by Vítězslav
07:05
created

FlexiBeeROTest::testAddUrlParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
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
     */
176
    public function testSetEvidence()
177
    {
178
        $this->object->setEvidence('adresar');
179
        $this->assertEquals('adresar', $this->object->evidence);
180
        $this->object->setPrefix('c');
181
        $this->object->setEvidence('fail');
182
    }
183
184
    /**
185
     * @covers FlexiPeeHP\FlexiBeeRO::setCompany
186
     */
187
    public function testSetCompany()
188
    {
189
        $this->object->setCompany('test_s_r_o_');
190
        $this->assertEquals('test_s_r_o_', $this->object->company);
191
    }
192
193
    /**
194
     * @covers FlexiPeeHP\FlexiBeeRO::object2array
195
     */
196
    public function testObject2array()
197
    {
198
        $this->assertNull($this->object->object2array(new \stdClass()));
199
        $this->assertEquals(
200
            [
201
                'item' => 1,
202
                'arrItem' => ['a', 'b' => 'c']
203
            ]
204
            , $this->object->object2array(new \Test\ObjectForTesting()));
205
    }
206
207
    /**
208
     * @covers FlexiPeeHP\FlexiBeeRO::objectToID
209
     */
210
    public function testObjectToID()
211
    {
212
        $id = \Ease\Sand::randomNumber(1, 9999);
213
        $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 212 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...
214
        $this->assertEquals([$id], $this->object->objectToID([$this->object]));
215
216
        $this->object->setDataValue('kod', 'TEST');
217
        $this->assertEquals('code:TEST',
218
            $this->object->objectToID($this->object));
219
220
        $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...
221
    }
222
223
    /**
224
     * @covers FlexiPeeHP\FlexiBeeRO::performRequest
225
     */
226
    public function testPerformRequest()
227
    {
228
        $evidence = $this->object->getEvidence();
229
        switch ($evidence) {
230
            case null:
231
            case '':
232
            case 'c':
233
            case 'test':
234
            case 'status':
235
            case 'nastaveni':
236
                $this->object->performRequest(null, 'GET', 'xml');
237
                break;
238
239
            default:
240
                $json = $this->object->performRequest(null, 'GET', 'json');
241
                if (array_key_exists('message', $json)) {
242
                    $this->assertArrayHasKey('@version', $json);
243
                } else {
244
                    $this->assertArrayHasKey($this->object->getResponseEvidence(),
245
                        $json);
246
                }
247
248
                $xml = $this->object->performRequest(null, 'GET', 'xml');
249
                $this->assertArrayHasKey($this->object->getResponseEvidence(),
250
                    $xml);
251
252
                break;
253
        }
254
255
        //404 Test
256
        $this->assertNull($this->object->performRequest('error404.json'));
257
    }
258
259
    /**
260
     * @covers FlexiPeeHP\FlexiBeeRO::setAction
261
     */
262
    public function testSetAction()
263
    {
264
        switch ($this->object->getEvidence()) {
265
            case '':
266
            case 'c':
267
            case 'hooks':
268
            case 'status':
269
            case 'changes':
270
            case 'nastaveni':
271
            case 'evidence-list':
272
                $this->object->setAction('none');
273
                break;
274
            default:
275
                $this->assertTrue($this->object->setAction('new'));
276
                $this->object->actionsAvailable = [];
277
                $this->assertFalse($this->object->setAction('none'));
278
                $this->object->actionsAvailable = ['copy'];
279
                $this->assertFalse($this->object->setAction('none'));
280
                break;
281
        }
282
    }
283
284
    /**
285
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidence
286
     */
287
    public function testGetEvidence()
288
    {
289
        $this->assertEquals($this->object->evidence,
290
            $this->object->getEvidence());
291
    }
292
293
    /**
294
     * @covers FlexiPeeHP\FlexiBeeRO::getCompany
295
     */
296
    public function testGetCompany()
297
    {
298
        $this->assertEquals($this->object->company, $this->object->getCompany());
299
    }
300
301
    /**
302
     * @covers FlexiPeeHP\FlexiBeeRO::getResponseEvidence
303
     */
304 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...
305
    {
306
        $responseEvidence = $this->object->getResponseEvidence();
307
        switch ($this->object->getEvidence()) {
308
            case '':
309
            case 'c':
310
            case 'hooks':
311
            case 'status':
312
            case 'changes':
313
            case 'nastaveni':
314
            case 'evidence-list':
315
                break;
316
            default:
317
                $this->assertEquals($this->object->getEvidence(),
318
                    $responseEvidence);
319
                break;
320
        }
321
    }
322
323
    /**
324
     * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId
325
     * @depends testInsertToFlexiBee
326
     */
327
    public function testGetLastInsertedId()
328
    {
329
        $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...
330
    }
331
332
    /**
333
     * @covers FlexiPeeHP\FlexiBeeRO::xml2array
334
     */
335
    public function testXml2array()
336
    {
337
        $xml = '<card xmlns="http://businesscard.org">
338
   <name>John Doe</name>
339
   <title>CEO, Widget Inc.</title>
340
   <email>[email protected]</email>
341
   <phone>(202) 456-1414</phone>
342
   <logo url="widget.gif"/>
343
   <a><b>c</b></a>
344
 </card>';
345
346
        $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]',
347
            'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]];
348
349
350
        $this->assertEquals($data, $this->object->xml2array($xml));
351
    }
352
353
    /**
354
     * @covers FlexiPeeHP\FlexiBeeRO::disconnect
355
     *
356
     * @depends testPerformRequest
357
     * @depends testLoadFlexiData
358
     * @depends testGetFlexiRow
359
     * @depends testGetFlexiData
360
     * @depends testLoadFromFlexiBee
361
     * @depends testInsertToFlexiBee
362
     * @depends testIdExists
363
     * @depends testRecordExists
364
     * @depends testGetColumnsFromFlexibee
365
     * @depends testSearchString
366
     */
367
    public function testDisconnect()
368
    {
369
        $this->object->disconnect();
370
        $this->assertNull($this->object->curl);
371
    }
372
373
    /**
374
     * @covers FlexiPeeHP\FlexiBeeRO::__destruct
375
     * @depends testDisconnect
376
     */
377
    public function testdestruct()
378
    {
379
        $this->markTestSkipped();
380
    }
381
382
    /**
383
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow
384
     */
385
    public function testGetFlexiRow()
386
    {
387
        $this->object->getFlexiRow(0);
388
        $this->object->getFlexiRow(1);
389
    }
390
391
    /**
392
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData
393
     */
394
    public function testGetFlexiData()
395
    {
396
        $evidence = $this->object->getEvidence();
397
        switch ($evidence) {
398
            case null:
399
                $this->object->getFlexiData();
400
                break;
401
            case 'c':
402
                $this->object->evidence  = 'c';
403
                $this->object->prefix    = '';
404
                $this->object->company   = '';
405
                $this->object->nameSpace = 'companies';
406
                $flexidata               = $this->object->getFlexiData();
407
                $this->assertArrayHasKey('company', $flexidata);
408
                break;
409
            case 'evidence-list':
410
                $flexidata               = $this->object->getFlexiData(null,
411
                    ['detail' => 'id']);
412
                $this->assertArrayHasKey('evidenceType', $flexidata[0]);
413
                break;
414
415
            default:
416
                $flexidata = $this->object->getFlexiData(null,
417
                    ['detail' => 'id']);
418
419
                if (is_array($flexidata)) {
420
                    if (count($flexidata)) {
421
                        $this->markTestSkipped('Empty evidence');
422
                    } else {
423
                        $this->assertArrayHasKey(0, $flexidata);
424
425
                        $this->assertArrayHasKey('id', $flexidata[0]);
426
                        $filtrered = $this->object->getFlexiData(null,
427
                            ["id = ".$flexidata[0]['id'], 'detail' => 'full']);
428
                        $this->assertArrayHasKey(0, $filtrered);
429
                        $this->assertArrayHasKey('id', $filtrered[0]);
430
                    }
431
                }
432
                break;
433
        }
434
    }
435
436
    /**
437
     * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee
438
     */
439
    public function testLoadFromFlexiBee()
440
    {
441
        $this->object->loadFromFlexiBee();
442
        $this->object->loadFromFlexiBee(222);
443
    }
444
445
    /**
446
     * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData
447
     */
448
    public function testJsonizeData()
449
    {
450
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}',
451
            $this->object->jsonizeData(['key' => 'value']));
452
453
        switch ($this->object->getEvidence()) {
454
            case '':
455
            case 'c':
456
            case 'hooks':
457
            case 'status':
458
            case 'changes':
459
            case 'nastaveni':
460
            case 'evidence-list':
461
                $this->object->jsonizeData(['key' => 'value']);
462
                break;
463
            default:
464
                $this->object->setAction('copy');
465
                $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"},"'.$this->object->evidence.'@action":"copy"}}',
466
                    $this->object->jsonizeData(['key' => 'value']));
467
                break;
468
        }
469
    }
470
471
    /**
472
     * @covers FlexiPeeHP\FlexiBeeRO::idExists
473
     */
474
    public function testIdExists()
475
    {
476
        $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...
477
        switch ($this->object->getEvidence()) {
478
            case '':
479
            case 'c':
480
            case 'hooks':
481
            case 'status':
482
            case 'changes':
483
            case 'nastaveni':
484
            case 'evidence-list':
485
                break;
486
            default:
487
                $first = $this->object->getColumnsFromFlexibee(['id'],
488
                    ['limit' => 1], 'id');
489
                if (empty($first)) {
490
                    $this->markTestSkipped('empty evidence ?');
491
                } else {
492
                    $this->object->setData($first);
493
                    $this->assertTrue($this->object->idExists());
494
                }
495
                break;
496
        }
497
    }
498
499
    /**
500
     * @covers FlexiPeeHP\FlexiBeeRO::getRecordID
501
     */
502
    public function testGetRecordID()
503
    {
504
        $this->object->setData(['id' => 10]);
505
        $this->assertEquals(10, $this->object->getRecordID());
506
        $this->object->setData(['kod' => 'KOD']);
507
        $this->assertEquals('code:KOD', $this->object->getRecordID());
508
    }
509
510
    /**
511
     * @covers FlexiPeeHP\FlexiBeeRO::recordExists
512
     */
513
    public function testRecordExists()
514
    {
515
        $evidence = $this->object->getEvidence();
516
517
        switch ($evidence) {
518
            case null:
519
            case 'c':
520
            case 'status':
521
            case 'evidence-list':
522
                $this->object->recordExists(['id' => 1]);
523
                break;
524
525
            default:
526
                $flexidata = $this->object->getFlexiData(null,
527
                    ['limit' => 1, 'detail' => 'id']);
528
                if (is_array($flexidata) && !count($flexidata)) {
529
                    $this->assertFalse($this->object->recordExists(['id' => 1]),
530
                        'Record ID 1 exists in empty evidence ?');
531
                } else {
532
                    if (isset($flexidata['success']) && ($flexidata['success'] == 'false')) {
533
                        $this->markTestSkipped($flexidata['message']);
534
                    } else {
535
                        $this->object->setData(['id' => (int) $flexidata[0]['id']]);
536
                        $this->assertTrue($this->object->recordExists(),
537
                            'First record exists test failed');
538
                        $this->assertFalse($this->object->recordExists(['id' => 0]),
539
                            'Record ID 0 exists');
540
                        $this->assertFalse($this->object->recordExists(['unexistent' => 1]),
541
                            'Unexistent Record exist ?');
542
                    }
543
                }
544
                break;
545
        }
546
    }
547
548
    /**
549
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee
550
     */
551
    public function testGetColumnsFromFlexibee()
552
    {
553
        $columns = $this->object->getColumnsFromFlexibee(['id'], ['limit' => 1],
554
            'id');
555
556
        switch ($this->object->getEvidence()) {
557
            case '':
558
            case 'c':
559
            case 'hooks':
560
            case 'status':
561
            case 'changes':
562
            case 'nastaveni':
563
            case 'evidence-list':
564
                break;
565
            default:
566
                $this->assertNotEmpty($columns);
567
                break;
568
        }
569
    }
570
571
    /**
572
     * @covers FlexiPeeHP\FlexiBeeRO::getExternalID
573
     */
574
    public function testGetExternalID()
575
    {
576
        $this->assertTrue(empty($this->object->getExternalID('ext:test:10'))); //ext: does not exist
577
578
        $this->object->setDataValue('external-ids',
579
            ['ext:doe:22', 'ext:test:10']);
580
581
        $this->assertEquals('ext:doe:22', $this->object->getExternalID());
582
        $this->assertEquals('10', $this->object->getExternalID('test'));
583
    }
584
585
    /**
586
     * @covers FlexiPeeHP\FlexiBeeRO::getGlobalVersion
587
     */
588 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...
589
    {
590
        switch ($this->object->getEvidence()) {
591
            case '':
592
            case 'c':
593
            case 'hooks':
594
            case 'status':
595
            case 'changes':
596
            case 'nastaveni':
597
            case 'evidence-list':
598
                $this->object->getGlobalVersion();
599
                break;
600
            default:
601
                $this->assertInternalType("int",
602
                    $this->object->getGlobalVersion(),
603
                    'error obtaining of GlobalVersion');
604
                break;
605
        }
606
    }
607
608
    /**
609
     * @covers FlexiPeeHP\FlexiBeeRO::getResponseFormat
610
     */
611
    public function testGetResponseFormat()
612
    {
613
        $this->object->performRequest(null, 'GET', 'json');
614
        $this->assertEquals('application/json',
615
            $this->object->getResponseFormat());
616
        $this->object->performRequest(null, 'GET', 'xml');
617
        $this->assertEquals('application/xml',
618
            $this->object->getResponseFormat());
619
    }
620
621
    /**
622
     * @covers FlexiPeeHP\FlexiBeeRO::getKod
623
     */
624
    public function testGetKod()
625
    {
626
        $testString = [];
627
        $this->assertEquals('code:CODE',
628
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
629
630
        $testString[$this->object->nameColumn] = 'Fish clamp -  Úchytka pro instalaci samonosných kabelů '
631
            .'(3.5 mm)';
632
        $code0                                 = $this->object->getKod($testString);
633
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR', $code0);
634
        $code1                                 = $this->object->getKod($testString,
635
            false);
636
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR', $code1);
637
        $code2                                 = $this->object->getKod($testString);
638
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR1', $code2);
639
        $this->object->setData($testString);
640
        $code3                                 = $this->object->getKod();
641
        $this->assertEquals('code:FISHCLAMPUCHYTKAPR2', $code3);
642
643
        $this->assertEquals('code:TEST',
644
            $this->object->getKod([$this->object->nameColumn => 'test']));
645
646
        $this->assertEquals('code:TEST1', $this->object->getKod('test'));
647
648
        $this->assertEquals('code:TEST2',
649
            $this->object->getKod(['kod' => 'test']));
650
        $this->assertEquals('code:NOTSET', $this->object->getKod(['kod' => '']));
651
    }
652
653
    /**
654
     * @covers FlexiPeeHP\FlexiBeeRO::logResult
655
     */
656
    public function testLogResult()
657
    {
658
        $this->object->cleanMessages();
659
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
660
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
661
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
662
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
663
        $this->object->logResult(current($this->object->object2array($success)),
664
            'http://test');
665
666
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
667
668
        $error                          = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
669
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
670
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
671
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
672
            .' kód není unikátní."}]}]}}');
673
        $this->object->lastResponseCode = 500;
674
        $this->object->logResult(current($this->object->object2array($error)));
675
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
676
    }
677
678
    /**
679
     * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl
680
     */
681
    public function testFlexiUrl()
682
    {
683
        $this->assertEquals("a eq '1' and b eq 'foo'",
684
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
685
        $this->assertEquals("a eq '1' or b eq 'bar'",
686
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
687
        $this->assertEquals("a eq true or b eq false",
688
            $this->object->flexiUrl(['a' => true, 'b' => false], 'or'));
689
        $this->assertEquals("a is null and b is not null",
690
            $this->object->flexiUrl(['a' => null, 'b' => '!null'], 'and'));
691
    }
692
693
    /**
694
     * @covers FlexiPeeHP\FlexiBeeRO::unifyResponseFormat
695
     */
696
    public function testunifyResponseFormat()
697
    {
698
        $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...
699
        //One Row Test
700
701
        $responseEvidence = $this->object->getResponseEvidence();
702
        if (empty($responseEvidence)) {
703
            $responseEvidence       = $this->object->evidence = 'test';
704
        }
705
706
        $test1raw = [$responseEvidence =>
707
            ['id' => 1, 'name' => 'value']
708
        ];
709
710
        $test1expected = [$responseEvidence =>
711
            [
712
                ['id' => 1, 'name' => 'value']
713
            ]
714
        ];
715
716
        $this->assertEquals($test1expected,
717
            $this->object->unifyResponseFormat($test1raw));
718
719
        //Two Row Test
720
        $test2Raw = [$this->object->getResponseEvidence() =>
721
            [
722
                ['id' => 1, 'name' => 'value'],
723
                ['id' => 2, 'name' => 'value2']
724
            ]
725
        ];
726
727
        $test2expected = [$this->object->getResponseEvidence() =>
728
            [
729
                ['id' => 1, 'name' => 'value'],
730
                ['id' => 2, 'name' => 'value2']
731
            ]
732
        ];
733
734
        $this->assertEquals($test2expected,
735
            $this->object->unifyResponseFormat($test2Raw));
736
    }
737
738
    /**
739
     * @covers FlexiPeeHP\FlexiBeeRO::__toString
740
     */
741
    public function testtoString()
742
    {
743
744
        $identifer = 'ext:test:123';
745
        $this->object->setDataValue('id', $identifer);
746
        $this->assertEquals($identifer, (string) $this->object);
747
748
        $code = 'test';
749
        $this->object->setDataValue('kod', $code);
750
        $this->assertEquals('code:'.$code, (string) $this->object);
751
752
        $this->object->dataReset();
753
        $this->assertEquals('', $this->object->__toString());
754
    }
755
756
    /**
757
     * @covers FlexiPeeHP\FlexiBeeRO::draw
758
     */
759
    public function testDraw($whatWant = NULL)
760
    {
761
        $this->object->setDataValue('kod', 'test');
762
        $this->assertEquals('code:test', $this->object->draw());
763
    }
764
765
    /**
766
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsInfo
767
     */
768 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...
769
    {
770
        switch ($this->object->getEvidence()) {
771
            case '':
772
            case 'c':
773
            case 'hooks':
774
            case 'status':
775
            case 'changes':
776
            case 'nastaveni':
777
            case 'evidence-list':
778
                $this->assertNull($this->object->getColumnsInfo());
779
                break;
780
            default:
781
                $this->assertNotEmpty($this->object->getColumnsInfo(),
782
                    'Cannot obtain structure for '.$this->object->getEvidence());
783
                break;
784
        }
785
    }
786
787
    /**
788
     * @covers FlexiPeeHP\FlexiBeeRO::getActionsInfo
789
     */
790
    public function testgetActionsInfo()
791
    {
792
        switch ($this->object->getEvidence()) {
793
            case '':
794
            case 'c':
795
            case 'hooks':
796
            case 'status':
797
            case 'changes':
798
            case 'nastaveni':
799
            case 'evidence-list':
800
                $this->assertNull($this->object->getActionsInfo());
801
                $this->assertNotEmpty($this->object->getActionsInfo('faktura-vydana'),
802
                    'Cannot obtain actions for na evidence');
803
                break;
804
            default:
805
                $this->assertNotEmpty($this->object->getActionsInfo(),
806
                    'Cannot obtain actions for '.$this->object->getEvidence());
807
                break;
808
        }
809
    }
810
811
    /**
812
     * @covers FlexiPeeHP\FlexiBeeRO::getRelationsInfo
813
     */
814 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...
815
    {
816
        switch ($this->object->getEvidence()) {
817
            case '':
818
            case 'c':
819
            case 'hooks':
820
            case 'status':
821
            case 'changes':
822
            case 'nastaveni':
823
            case 'strom-cenik':
824
            case 'ucetni-obdobi':
825
            case 'evidence-list':
826
                $this->assertNull($this->object->getRelationsInfo());
827
                break;
828
            default:
829
                $this->assertNotEmpty($this->object->getRelationsInfo(),
830
                    'Cannot obtain relations for '.$this->object->getEvidence());
831
                break;
832
        }
833
    }
834
835
    /**
836
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceUrl
837
     */
838
    public function testgetEvidenceUrl()
839
    {
840
        $this->assertNotEmpty($this->object->getEvidenceUrl());
841
        $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...
842
    }
843
844
    /**
845
     * @covers FlexiPeeHP\FlexiBeeRO::evidenceToClassName
846
     */
847
    public function testevidenceToClassName()
848
    {
849
        $this->assertEquals('FakturaVydana',
850
            $this->object->evidenceToClassName('faktura-vydana'));
851
    }
852
853
    /**
854
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceInfo
855
     */
856
    public function testGetEvidenceInfo()
857
    {
858
        $eInfo = $this->object->getEvidenceInfo();
859
        switch ($this->object->getEvidence()) {
860
            case '':
861
            case 'c':
862
            case 'hooks':
863
            case 'status':
864
            case 'changes':
865
            case 'nastaveni':
866
            case 'evidence-list':
867
                break;
868
            default:
869
                $this->assertArrayHasKey('evidencePath', $eInfo);
870
                break;
871
        }
872
    }
873
874
    /**
875
     * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceName
876
     */
877
    public function testGetEvidenceName()
878
    {
879
        $evidenceName = $this->object->getEvidenceName();
880
        switch ($this->object->getEvidence()) {
881
            case '':
882
            case 'c':
883
            case 'hooks':
884
            case 'status':
885
            case 'changes':
886
            case 'nastaveni':
887
            case 'evidence-list':
888
                break;
889
            default:
890
                $this->assertNotEmpty($evidenceName);
891
                break;
892
        }
893
    }
894
895
    /**
896
     * @covers FlexiPeeHP\FlexiBeeRO::performAction
897
     * @expectedException \Exception
898
     */
899
    public function testPerformAction()
900
    {
901
        $actions = $this->object->getActionsInfo();
902
903
        if (count($actions)) {
904
            if (array_key_exists('new', $actions)) {
905
                $this->object->performAction('new', 'ext');
906
            }
907
908
            if (array_key_exists('storno', $actions)) {
909
                $this->assertTrue($this->object->performAction('storno'));
910
            }
911
        }
912
        $this->object->performAction('nonexitst');
913
    }
914
915
    /**
916
     * @covers FlexiPeeHP\FlexiBeeRO::saveResponseToFile
917
     */
918
    public function testSaveResponseToFile()
919
    {
920
        $tmp = sys_get_temp_dir().'/'.tmpfile();
921
        $this->object->saveResponseToFile($tmp);
922
        $this->assertFileExists($tmp);
923
    }
924
925
    /**
926
     * @covers FlexiPeeHP\FlexiBeeRO::getFirstRecordID()
927
     */
928
    public function testgetFirstRecordID()
929
    {
930
        $firstID = $this->object->getFirstRecordID();
931
        switch ($this->object->getEvidence()) {
932
            case '':
933
            case 'c':
934
            case 'hooks':
935
            case 'status':
936
            case 'changes':
937
            case 'nastaveni':
938
            case 'evidence-list':
939
                break;
940
            default:
941
                if (is_null($firstID)) {
942
                    $this->markTestSkipped('Empty evidence');
943
                } else {
944
                    $this->assertFalse(empty($firstID));
945
                }
946
                break;
947
        }
948
    }
949
950
    /**
951
     * @covers FlexiPeeHP\FlexiBeeRO::getVazby
952
     * @expectedException \Exception
953
     */
954
    public function testGetVazby()
955
    {
956
        switch ($this->object->getEvidence()) {
957
            case '':
958
            case 'c':
959
            case 'hooks':
960
            case 'status':
961
            case 'changes':
962
            case 'nastaveni':
963
                break;
964
            default:
965
                //       $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...
966
                $first = $this->object->getFirstRecordID();
967
                if (!empty($first)) {
968
                    $this->object->setMyKey($first);
969
                    $vazby = $this->object->getVazby();
970
                    if (is_null($vazby)) {
971
                        $this->markTestSkipped('No bonds to check');
972
                    } else {
973
                        $this->assertTrue(is_array($vazby));
974
                    }
975
                }
976
                $this->object->setMyKey(null);
977
                $this->object->getVazby(); //Get Exception
978
                break;
979
        }
980
    }
981
982
    /**
983
     * @covers FlexiPeeHP\FlexiBeeRO::evidenceUrlWithSuffix
984
     */
985
    public function testEvidenceUrlWithSuffix()
986
    {
987
        $urlraw = $this->object->getEvidenceURL();
988
        $lala   = $this->object->evidenceUrlWithSuffix('lala');
989
        $this->assertEquals($urlraw.'/lala', $lala);
990
        $lolo   = $this->object->evidenceUrlWithSuffix('?lele');
991
        $this->assertEquals($urlraw.'?lele', $lolo);
992
        $lulu   = $this->object->evidenceUrlWithSuffix(';lulu');
993
        $this->assertEquals($urlraw.';lulu', $lulu);
994
    }
995
996
    /**
997
     * @covers FlexiPeeHP\FlexiBeeRO::addUrlParams
998
     */
999
    public function testAddUrlParams()
1000
    {
1001
        $this->assertEquals('http://vitexsoftware.cz/path?id=1&a=b',
1002
            $this->object->addUrlParams('http://vitexsoftware.cz/path?a=b',
1003
                ['id' => 1], TRUE));
1004
    }
1005
1006
    /**
1007
     * @covers FlexiPeeHP\FlexiBeeRO::addDefaultUrlParams
1008
     */
1009
    public function testAddDefaultUrlParams()
1010
    {
1011
        $this->object->defaultUrlParams       = [];
1012
        $this->assertEquals('http://vitexsoftware.cz?a=b',
1013
            $this->object->addDefaultUrlParams('http://vitexsoftware.cz?a=b'));
1014
        $this->object->defaultUrlParams['id'] = 1;
1015
        $this->assertEquals('http://vitexsoftware.cz/path?a=b&id=1',
1016
            $this->object->addDefaultUrlParams('http://vitexsoftware.cz/path?a=b'));
1017
    }
1018
}