Completed
Push — master ( fea5ff...68f225 )
by Vítězslav
04:39
created

FlexiBeeROTest::testPerformRequest()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 26
rs 8.5806
1
<?php
2
3
namespace Test\FlexiPeeHP;
4
5
use FlexiPeeHP\FlexiBeeRO;
6
7
/**
8
 * Class used to test Object To Array Conversion
9
 */
10
class objTest extends \stdClass
11
{
12
    /**
13
     * Simple Item
14
     * @var integer
15
     */
16
    public $item = 1;
17
18
    /**
19
     * Array item
20
     * @var array
21
     */
22
    public $arrItem = ['a', 'b' => 'c'];
23
24
    /**
25
     * Simple method
26
     * 
27
     * @return boolean
28
     */
29
    public function method()
30
    {
31
        return true;
32
    }
33
}
34
35
/**
36
 * Generated by PHPUnit_SkeletonGenerator on 2016-05-04 at 10:08:36.
37
 */
38
class FlexiBeeROTest extends \Test\Ease\BrickTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
39
{
40
    /**
41
     * @var FlexiBeeRO
42
     */
43
    protected $object;
44
45
    /**
46
     * Sets up the fixture, for example, opens a network connection.
47
     * This method is called before a test is executed.
48
     * @covers FlexiPeeHP\FlexiBeeRO::__construct
49
     */
50
    protected function setUp()
51
    {
52
        $this->object = new FlexiBeeRO();
53
    }
54
55
    /**
56
     * Tears down the fixture, for example, closes a network connection.
57
     * This method is called after a test is executed.
58
     */
59
    protected function tearDown()
60
    {
61
        
62
    }
63
64
    public function testCurlInit()
65
    {
66
        $this->object->curlInit();
67
        $this->assertTrue(is_resource($this->object->curl));
68
    }
69
70
    /**
71
     * @covers FlexiPeeHP\FlexiBeeRO::setEvidence
72
     */
73
    public function testSetEvidence()
74
    {
75
        $this->object->setEvidence('nastaveni');
76
        $this->assertEquals('nastaveni', $this->object->evidence);
77
    }
78
79
    /**
80
     * @covers FlexiPeeHP\FlexiBeeRO::object2array
81
     */
82
    public function testObject2array()
83
    {
84
        $this->assertNull($this->object->object2array(new \stdClass()));
85
        $this->assertEquals(
86
            [
87
            'item' => 1,
88
            'arrItem' => ['a', 'b' => 'c']
89
            ]
90
            , $this->object->object2array(new objTest()));
91
    }
92
93
    /**
94
     * @covers FlexiPeeHP\FlexiBeeRO::performRequest
95
     */
96
    public function testPerformRequest()
97
    {
98
99
        if (!is_null($this->object->evidence) && $this->object->evidence != 'test') {
100
            $json = $this->object->performRequest($this->object->evidence.'.json');
101
            if (array_key_exists('message', $json)) {
102
                $this->assertArrayHasKey('@version', $json);
103
            } else {
104
                $this->assertArrayHasKey($this->object->evidence, $json);
105
            }
106
        } else {
107
            $this->object->evidence  = 'c';
108
            $this->object->prefix    = '';
109
            $this->object->company   = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type object<FlexiPeeHP\type> of property $company.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
            $this->object->nameSpace = 'companies';
111
            $json                    = $this->object->performRequest();
112
            $this->assertArrayHasKey('company', $json);
113
114
            $xml = $this->object->performRequest(null, 'GET', 'xml');
115
            $this->assertArrayHasKey('company', $xml);
116
        }
117
118
        $err = $this->object->performRequest('error.json');
119
        $this->assertArrayHasKey('success', $err);
120
        $this->assertEquals('false', $err['success']);
121
    }
122
123
    /**
124
     * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId
125
     * @depends testInsertToFlexiBee
126
     */
127
    public function testGetLastInsertedId()
128
    {
129
        $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\FakturaVydana, FlexiPeeHP\FlexiBeeRW, FlexiPeeHP\Hooks, FlexiPeeHP\Kontakt, FlexiPeeHP\Pokladna, FlexiPeeHP\PokladniPohyb, FlexiPeeHP\RadaPokladniPohyb, FlexiPeeHP\SkladovyPohyb, FlexiPeeHP\SkladovyPohybPolozka, FlexiPeeHP\SkupinaFirem, 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...
130
    }
131
132
    /**
133
     * @covers FlexiPeeHP\FlexiBeeRO::xml2array
134
     */
135
    public function testXml2array()
136
    {
137
        $xml = '<card xmlns="http://businesscard.org">
138
   <name>John Doe</name>
139
   <title>CEO, Widget Inc.</title>
140
   <email>[email protected]</email>
141
   <phone>(202) 456-1414</phone>
142
   <logo url="widget.gif"/>
143
   <a><b>c</b></a>
144
 </card>';
145
146
        $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]',
147
            'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]];
148
149
150
        $this->assertEquals($data, $this->object->xml2array($xml));
151
    }
152
153
    /**
154
     * @covers FlexiPeeHP\FlexiBeeRO::disconnect
155
     *
156
     * @depends testPerformRequest
157
     * @depends testLoadFlexiData
158
     * @depends testGetFlexiRow
159
     * @depends testGetFlexiData
160
     * @depends testLoadFromFlexiBee
161
     * @depends testInsertToFlexiBee
162
     * @depends testIdExists
163
     * @depends testRecordExists
164
     * @depends testGetColumnsFromFlexibee
165
     * @depends testSearchString
166
     */
167
    public function testDisconnect()
168
    {
169
        $this->object->disconnect();
170
        $this->assertNull($this->object->curl);
171
    }
172
173
    /**
174
     * @covers FlexiPeeHP\FlexiBeeRO::__destruct
175
     * @depends testDisconnect
176
     */
177
    public function test__destruct()
178
    {
179
        $this->markTestSkipped();
180
    }
181
182
    /**
183
     * @covers FlexiPeeHP\FlexiBeeRO::loadFlexiData
184
     * @todo   Implement testLoadFlexiData().
185
     */
186
    public function testLoadFlexiData()
187
    {
188
        // Remove the following lines when you implement this test.
189
        $this->markTestIncomplete(
190
            'This test has not been implemented yet.'
191
        );
192
    }
193
194
    /**
195
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow
196
     */
197
    public function testGetFlexiRow()
198
    {
199
        $this->object->getFlexiRow(0);
200
        $this->object->getFlexiRow(1);
201
    }
202
203
    /**
204
     * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData
205
     */
206
    public function testGetFlexiData()
207
    {
208
        if (is_null($this->object->evidence) || ($this->object->evidence == 'test')) {
209
            $this->object->evidence  = 'c';
210
            $this->object->prefix    = '';
211
            $this->object->company   = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type object<FlexiPeeHP\type> of property $company.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
212
            $this->object->nameSpace = 'companies';
213
            $flexidata               = $this->object->getFlexiData();
214
            $this->assertArrayHasKey('company', $flexidata);
215 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...
216
            $flexidata = $this->object->getFlexiData();
217
            $this->assertArrayHasKey(0, $flexidata);
218
            $this->assertArrayHasKey('id', $flexidata[0]);
219
            $filtrered = $this->object->getFlexiData(null,
220
                key($flexidata[0])." = ".current($flexidata[0]));
221
            $this->assertArrayHasKey(0, $filtrered);
222
            $this->assertArrayHasKey('id', $filtrered[0]);
223
        }
224
    }
225
226
    /**
227
     * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee
228
     */
229
    public function testLoadFromFlexiBee()
230
    {
231
        $this->object->loadFromFlexiBee();
232
        $this->object->loadFromFlexiBee(222);
233
    }
234
235
    /**
236
     * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData
237
     */
238
    public function testJsonizeData()
239
    {
240
        $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}',
241
            $this->object->jsonizeData(['key' => 'value']));
242
    }
243
244
    /**
245
     * @covers FlexiPeeHP\FlexiBeeRO::idExists
246
     * @todo   Implement testIdExists().
247
     */
248
    public function testIdExists()
249
    {
250
        // Remove the following lines when you implement this test.
251
        $this->markTestIncomplete(
252
            'This test has not been implemented yet.'
253
        );
254
    }
255
256
    /**
257
     * @covers FlexiPeeHP\FlexiBeeRO::recordExists
258
     */
259
    public function testRecordExists()
260
    {
261
//        $this->assertTrue($this->object->recordExists([]));
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
262
//        $this->assertFalse($this->object->recordExists([]));
263
    }
264
265
    /**
266
     * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee
267
     * @todo   Implement testGetColumnsFromFlexibee().
268
     */
269
    public function testGetColumnsFromFlexibee()
270
    {
271
        // Remove the following lines when you implement this test.
272
        $this->markTestIncomplete(
273
            'This test has not been implemented yet.'
274
        );
275
    }
276
277
    /**
278
     * @covers FlexiPeeHP\FlexiBeeRO::getKod
279
     */
280
    public function testGetKod()
281
    {
282
283
        $this->assertEquals('CODE',
284
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
285
286
        $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...
287
            .'(3.5 mm)';
288
        $code0                                 = $this->object->getKod($testString);
289
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code0);
290
        $code1                                 = $this->object->getKod($testString,
291
            false);
292
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code1);
293
        $code2                                 = $this->object->getKod($testString);
294
        $this->assertEquals('FISHCLAMPUCHYTKAPR1', $code2);
295
        $this->object->setData($testString);
296
        $code3                                 = $this->object->getKod();
297
        $this->assertEquals('FISHCLAMPUCHYTKAPR2', $code3);
298
299
        $this->assertEquals('TEST',
300
            $this->object->getKod([$this->object->nameColumn => 'test']));
301
302
        $this->assertEquals('TEST1', $this->object->getKod('test'));
303
304
        $this->assertEquals('TEST2', $this->object->getKod(['kod' => 'test']));
305
        $this->assertEquals('NOTSET', $this->object->getKod(['kod' => '']));
306
    }
307
308
    /**
309
     * @covers FlexiPeeHP\FlexiBeeRO::searchString
310
     * @todo   Implement testSearchString().
311
     */
312
    public function testSearchString()
313
    {
314
        // Remove the following lines when you implement this test.
315
        $this->markTestIncomplete(
316
            'This test has not been implemented yet.'
317
        );
318
    }
319
320
    /**
321
     * @covers FlexiPeeHP\FlexiBeeRO::logResult
322
     */
323
    public function testLogResult()
324
    {
325
        $this->object->cleanMessages();
326
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
327
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
328
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
329
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
330
        $this->object->logResult(current($this->object->object2array($success)),
331
            'http://test');
332
333
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
334
335
        $error = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
336
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
337
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
338
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
339
            .' kód není unikátní."}]}]}}');
340
        $this->object->logResult(current($this->object->object2array($error)));
341
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
342
    }
343
344
    /**
345
     * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl
346
     */
347
    public function testFlexiUrl()
348
    {
349
        $this->assertEquals("a = 1 and b = 'foo'",
350
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
351
        $this->assertEquals("a = 1 or b = 'bar'",
352
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
353
    }
354
}