Test Failed
Push — master ( db00ed...66a8dd )
by Vítězslav
10:56
created

FlexiBeeROTest::testGetResponseEvidence()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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