Test Failed
Push — master ( 9ca4a5...db00ed )
by Vítězslav
11:19
created

FlexiBeeROTest::testIdExists()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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