Completed
Push — master ( 14dc45...6a7b8b )
by Vítězslav
08:50
created

FlexiBeeROTest::testdestruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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->evidence  = 'c';
26
        $this->object->prefix    = '';
27
        $this->object->company   = '';
28
        $this->object->nameSpace = 'companies';
29
    }
30
31
    /**
32
     * Tears down the fixture, for example, closes a network connection.
33
     * This method is called after a test is executed.
34
     */
35
    protected function tearDown()
36
    {
37
        
38
    }
39
40
    /**
41
     * @covers FlexiPeeHP\FlexiBeeRO::curlInit
42
     */
43
    public function testCurlInit()
44
    {
45
        $this->object->curlInit();
46
        $this->assertTrue(is_resource($this->object->curl));
47
    }
48
49
    /**
50
     * @covers FlexiPeeHP\FlexiBeeRO::processInit
51
     */
52
    public function testProcessInit()
53
    {
54
        $this->object->processInit(['id' => 1]);
55
        $this->assertEquals(1, $this->object->getDataValue('id'));
56
        if (!is_null($this->object->evidence) && $this->object->evidence != 'test') {
57
            $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...
58
                ['limit' => 1]);
59
            $this->object->processInit((int) current($firstID));
60
            $this->assertNotEmpty($this->object->__toString());
61
        }
62
    }
63
64
    /**
65
     * @covers FlexiPeeHP\FlexiBeeRO::setEvidence
66
     */
67
    public function testSetEvidence()
68
    {
69
        $this->object->setEvidence('nastaveni');
70
        $this->assertEquals('nastaveni', $this->object->evidence);
71
    }
72
73
    /**
74
     * @covers FlexiPeeHP\FlexiBeeRO::object2array
75
     */
76
    public function testObject2array()
77
    {
78
        $this->assertNull($this->object->object2array(new \stdClass()));
79
        $this->assertEquals(
80
            [
81
            'item' => 1,
82
            'arrItem' => ['a', 'b' => 'c']
83
            ]
84
            , $this->object->object2array(new \Test\ObjectForTesting()));
85
    }
86
87
    /**
88
     * @covers FlexiPeeHP\FlexiBeeRO::objectToID
89
     */
90
    public function testObjectToID()
91
    {
92
        $id = \Ease\Sand::randomNumber(1, 9999);
93
        $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 92 can also be of type false; however, Ease\Brick::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...
94
        $this->assertEquals([$id], $this->object->objectToID([$this->object]));
95
96
        $this->object->setDataValue('kod', 'TEST');
97
        $this->assertEquals('code:TEST',
98
            $this->object->objectToID($this->object));
99
100
        $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...
101
    }
102
103
    /**
104
     * @covers FlexiPeeHP\FlexiBeeRO::performRequest
105
     */
106
    public function testPerformRequest()
107
    {
108
        $evidence = $this->object->getEvidence();
109
        switch ($evidence) {
110
            case null:
111
            case '':
112
            case 'test':
113
            case 'c':
114
                $this->object->evidence  = 'c';
115
                $this->object->prefix    = '';
116
                $this->object->company   = '';
117
                $this->object->nameSpace = 'companies';
118
                $json                    = $this->object->performRequest();
119
                $this->assertArrayHasKey('company', $json);
120
121
                $xml = $this->object->performRequest(null, 'GET', 'xml');
122
                $this->assertArrayHasKey('company', $xml);
123
                break;
124
125
            default:
126
                $json = $this->object->performRequest($evidence.'.json');
127
                if (array_key_exists('message', $json)) {
128
                    $this->assertArrayHasKey('@version', $json);
129
                } else {
130
                    $this->assertArrayHasKey($evidence, $json);
131
                }
132
                break;
133
        }
134
135
136
        $err = $this->object->performRequest('error.json');
137
        $this->assertArrayHasKey('success', $err);
138
        $this->assertEquals('false', $err['success']);
139
    }
140
141
    /**
142
     * @covers FlexiPeeHP\FlexiBeeRO::setAction
143
     */
144
    public function testSetAction()
145
    {
146
        $this->assertTrue($this->object->setAction('none'));
147
        $this->object->actionsAvailable = [];
148
        $this->assertFalse($this->object->setAction('none'));
149
        $this->object->actionsAvailable = ['copy'];
150
        $this->assertFalse($this->object->setAction('none'));
151
    }
152
153
    /**
154
     * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId
155
     * @depends testInsertToFlexiBee
156
     */
157
    public function testGetLastInsertedId()
158
    {
159
        $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\FakturaVydana, FlexiPeeHP\FakturaVydanaPolozka, FlexiPeeHP\FlexiBeeRW, FlexiPeeHP\Hooks, FlexiPeeHP\Kontakt, FlexiPeeHP\Pokladna, FlexiPeeHP\PokladniPohyb, FlexiPeeHP\RadaPokladniPohyb, 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...
160
    }
161
162
    /**
163
     * @covers FlexiPeeHP\FlexiBeeRO::xml2array
164
     */
165
    public function testXml2array()
166
    {
167
        $xml = '<card xmlns="http://businesscard.org">
168
   <name>John Doe</name>
169
   <title>CEO, Widget Inc.</title>
170
   <email>[email protected]</email>
171
   <phone>(202) 456-1414</phone>
172
   <logo url="widget.gif"/>
173
   <a><b>c</b></a>
174
 </card>';
175
176
        $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]',
177
            'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]];
178
179
180
        $this->assertEquals($data, $this->object->xml2array($xml));
181
    }
182
183
    /**
184
     * @covers FlexiPeeHP\FlexiBeeRO::disconnect
185
     *
186
     * @depends testPerformRequest
187
     * @depends testLoadFlexiData
188
     * @depends testGetFlexiRow
189
     * @depends testGetFlexiData
190
     * @depends testLoadFromFlexiBee
191
     * @depends testInsertToFlexiBee
192
     * @depends testIdExists
193
     * @depends testRecordExists
194
     * @depends testGetColumnsFromFlexibee
195
     * @depends testSearchString
196
     */
197
    public function testDisconnect()
198
    {
199
        $this->object->disconnect();
200
        $this->assertNull($this->object->curl);
201
    }
202
203
    /**
204
     * @covers FlexiPeeHP\FlexiBeeRO::__destruct
205
     * @depends testDisconnect
206
     */
207
    public function testdestruct()
208
    {
209
        $this->markTestSkipped();
210
    }
211
212
    /**
213
     * @covers FlexiPeeHP\FlexiBeeRO::loadFlexiData
214
     * @todo   Implement testLoadFlexiData().
215
     */
216
    public function testLoadFlexiData()
217
    {
218
        // Remove the following lines when you implement this test.
219
        $this->markTestIncomplete(
220
            'This test has not been implemented yet.'
221
        );
222
    }
223
224
    /**
225
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow
226
     */
227
    public function testGetFlexiRow()
228
    {
229
        $this->object->getFlexiRow(0);
230
        $this->object->getFlexiRow(1);
231
    }
232
233
    /**
234
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData
235
     */
236
    public function testGetFlexiData()
237
    {
238
        $evidence = $this->object->getEvidence();
239
240
        switch ($evidence) {
241
            case null:
242
                $this->markTestSkipped('Unsupported evidence');
243
                break;
244
            case 'c':
245
                $this->object->evidence  = 'c';
246
                $this->object->prefix    = '';
247
                $this->object->company   = '';
248
                $this->object->nameSpace = 'companies';
249
                $flexidata               = $this->object->getFlexiData();
250
                $this->assertArrayHasKey('company', $flexidata);
251
                break;
252
253
            default:
254
                $flexidata = $this->object->getFlexiData();
255
                if (is_array($flexidata) && !count($flexidata)) {
256
                    $this->markTestSkipped('Empty evidence');
257 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
258
                    $this->assertArrayHasKey(0, $flexidata);
259
                    $this->assertArrayHasKey('id', $flexidata[0]);
260
                    $filtrered = $this->object->getFlexiData(null,
261
                        key($flexidata[0])." = ".current($flexidata[0]));
262
                    $this->assertArrayHasKey(0, $filtrered);
263
                    $this->assertArrayHasKey('id', $filtrered[0]);
264
                }
265
                break;
266
        }
267
    }
268
269
    /**
270
     * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee
271
     */
272
    public function testLoadFromFlexiBee()
273
    {
274
        $this->object->loadFromFlexiBee();
275
        $this->object->loadFromFlexiBee(222);
276
    }
277
278
    /**
279
     * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData
280
     */
281
    public function testJsonizeData()
282
    {
283
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}',
284
            $this->object->jsonizeData(['key' => 'value']));
285
        $this->object->setAction('copy');
286
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"},"'.$this->object->evidence.'@action":"copy"}}',
287
            $this->object->jsonizeData(['key' => 'value']));
288
    }
289
290
    /**
291
     * @covers FlexiPeeHP\FlexiBeeRO::idExists
292
     * @todo   Implement testIdExists().
293
     */
294
    public function testIdExists()
295
    {
296
        // Remove the following lines when you implement this test.
297
        $this->markTestIncomplete(
298
            'This test has not been implemented yet.'
299
        );
300
    }
301
302
    /**
303
     * @covers FlexiPeeHP\FlexiBeeRO::recordExists
304
     */
305
    public function testRecordExists()
306
    {
307
        $evidence = $this->object->getEvidence();
308
309
        switch ($evidence) {
310
            case null:
311
            case 'c':
312
                $this->markTestSkipped('Unsupported evidence');
313
                break;
314
315
            default:
316
                $flexidata = $this->object->getFlexiData(null, ['limit' => 1]);
317
                if (is_array($flexidata) && !count($flexidata)) {
318
                    $this->markTestSkipped('Empty evidence');
319
                } else {
320
                    $this->object->setData(['id' => (int) $flexidata[0]['id']]);
321
                    $this->assertTrue($this->object->recordExists());
322
                    $this->assertFalse($this->object->recordExists(['id' => 0]));
323
                    $this->assertFalse($this->object->recordExists(['unexistent' => 1]));
324
                }
325
                break;
326
        }
327
    }
328
329
    /**
330
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee
331
     * @todo   Implement testGetColumnsFromFlexibee().
332
     */
333
    public function testGetColumnsFromFlexibee()
334
    {
335
        // Remove the following lines when you implement this test.
336
        $this->markTestIncomplete(
337
            'This test has not been implemented yet.'
338
        );
339
    }
340
341
    /**
342
     * @covers FlexiPeeHP\FlexiBeeRO::getKod
343
     */
344
    public function testGetKod()
345
    {
346
347
        $this->assertEquals('CODE',
348
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
349
350
        $testString[$this->object->nameColumn] = 'Fish clamp -  Úchytka pro instalaci samonosných kabelů '
0 ignored issues
show
Coding Style Comprehensibility introduced by
$testString was never initialized. Although not strictly required by PHP, it is generally a good practice to add $testString = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
351
            .'(3.5 mm)';
352
        $code0                                 = $this->object->getKod($testString);
353
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code0);
354
        $code1                                 = $this->object->getKod($testString,
355
            false);
356
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code1);
357
        $code2                                 = $this->object->getKod($testString);
358
        $this->assertEquals('FISHCLAMPUCHYTKAPR1', $code2);
359
        $this->object->setData($testString);
360
        $code3                                 = $this->object->getKod();
361
        $this->assertEquals('FISHCLAMPUCHYTKAPR2', $code3);
362
363
        $this->assertEquals('TEST',
364
            $this->object->getKod([$this->object->nameColumn => 'test']));
365
366
        $this->assertEquals('TEST1', $this->object->getKod('test'));
367
368
        $this->assertEquals('TEST2', $this->object->getKod(['kod' => 'test']));
369
        $this->assertEquals('NOTSET', $this->object->getKod(['kod' => '']));
370
    }
371
372
    /**
373
     * @covers FlexiPeeHP\FlexiBeeRO::logResult
374
     */
375
    public function testLogResult()
376
    {
377
        $this->object->cleanMessages();
378
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
379
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
380
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
381
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
382
        $this->object->logResult(current($this->object->object2array($success)),
383
            'http://test');
384
385
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
386
387
        $error                          = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
388
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
389
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
390
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
391
            .' kód není unikátní."}]}]}}');
392
        $this->object->lastResponseCode = 500;
393
        $this->object->logResult(current($this->object->object2array($error)));
394
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
395
    }
396
397
    /**
398
     * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl
399
     */
400
    public function testFlexiUrl()
401
    {
402
        $this->assertEquals("a eq 1 and b eq 'foo'",
403
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
404
        $this->assertEquals("a eq 1 or b eq 'bar'",
405
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
406
        $this->assertEquals("a eq true or b eq false",
407
            $this->object->flexiUrl(['a' => true, 'b' => false], 'or'));
408
        $this->assertEquals("a is null and b is not null",
409
            $this->object->flexiUrl(['a' => null, 'b' => '!null'], 'and'));
410
    }
411
412
    /**
413
     * @covers FlexiPeeHP\FlexiBeeRO::__toString
414
     */
415
    public function testtoString()
416
    {
417
418
        $identifer = 'ext:test:123';
419
        $this->object->setDataValue('id', $identifer);
420
        $this->assertEquals($identifer, (string) $this->object);
421
422
        $code = 'test';
423
        $this->object->setDataValue('kod', $code);
424
        $this->assertEquals('code:'.$code, (string) $this->object);
425
426
        $this->object->dataReset();
427
        $this->assertNull($this->object->__toString());
428
    }
429
430
    /**
431
     * @covers FlexiPeeHP\FlexiBeeRO::draw
432
     */
433
    public function testDraw($whatWant = NULL)
434
    {
435
        $this->object->setDataValue('kod', 'test');
436
        $this->assertEquals('code:test', $this->object->draw());
437
    }
438
439
}
440