Completed
Push — master ( a7ba10...1f74b8 )
by Vítězslav
03:34
created

FlexiBeeTest::testDisconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Test\FlexiPeeHP;
4
5
use FlexiPeeHP\FlexiBee;
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 FlexiBeeTest 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 FlexiBee
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\FlexiBee::__construct
49
     */
50
    protected function setUp()
51
    {
52
        $this->object = new FlexiBee;
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\FlexiBee::setAgenda
72
     */
73
    public function testSetAgenda()
74
    {
75
        $this->object->setAgenda('nastaveni');
76
        $this->assertEquals('nastaveni', $this->object->agenda);
77
    }
78
79
    /**
80
     * @covers FlexiPeeHP\FlexiBee::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\FlexiBee::performRequest
95
     */
96
    public function testPerformRequest()
97
    {
98
99
        if (!is_null($this->object->agenda) && $this->object->agenda != 'test') {
100
            $json = $this->object->performRequest($this->object->agenda.'.json');
101
            if (array_key_exists('message', $json)) {
102
                $this->assertArrayHasKey('@version', $json);
103
            } else {
104
                $this->assertArrayHasKey($this->object->agenda, $json);
105
            }
106
        } else {
107
            $this->object->agenda    = '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, null, '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\FlexiBee::getLastInsertedId
125
     * @depends testInsertToFlexiBee
126
     */
127
    public function testGetLastInsertedId()
128
    {
129
        $this->assertNotEmpty($this->object->getLastInsertedId());
130
    }
131
132
    /**
133
     * @covers FlexiPeeHP\FlexiBee::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\FlexiBee::disconnect
155
     *
156
     * @depends testPerformRequest
157
     * @depends testLoadFlexiData
158
     * @depends testGetFlexiRow
159
     * @depends testGetFlexiData
160
     * @depends testLoadFromFlexiBee
161
     * @depends testSaveToFlexiBee
162
     * @depends testInsertToFlexiBee
163
     * @depends testIdExists
164
     * @depends testRecordExists
165
     * @depends testGetColumnsFromFlexibee
166
     * @depends testSearchString
167
     */
168
    public function testDisconnect()
169
    {
170
        $this->object->disconnect();
171
        $this->assertNull($this->object->curl);
172
    }
173
174
    /**
175
     * @covers FlexiPeeHP\FlexiBee::__destruct
176
     * @depends testDisconnect
177
     */
178
    public function test__destruct()
179
    {
180
        $this->markTestSkipped();
181
    }
182
183
    /**
184
     * @covers FlexiPeeHP\FlexiBee::loadFlexiData
185
     * @todo   Implement testLoadFlexiData().
186
     */
187
    public function testLoadFlexiData()
188
    {
189
        // Remove the following lines when you implement this test.
190
        $this->markTestIncomplete(
191
            'This test has not been implemented yet.'
192
        );
193
    }
194
195
    /**
196
     * @covers FlexiPeeHP\FlexiBee::getFlexiRow
197
     */
198
    public function testGetFlexiRow()
199
    {
200
        $this->object->getFlexiRow(0);
201
        $this->object->getFlexiRow(1);
202
    }
203
204
    /**
205
     * @covers FlexiPeeHP\FlexiBee::getFlexiData
206
     */
207
    public function testGetFlexiData()
208
    {
209
        if (is_null($this->object->agenda) || ($this->object->agenda == 'test')) {
210
            $this->object->agenda    = 'c';
211
            $this->object->prefix    = '';
212
            $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...
213
            $this->object->nameSpace = 'companies';
214
            $flexidata               = $this->object->getFlexiData();
215
            $this->assertArrayHasKey('company', $flexidata);
216 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...
217
            $flexidata = $this->object->getFlexiData();
218
            $this->assertArrayHasKey(0, $flexidata);
219
            $this->assertArrayHasKey('id', $flexidata[0]);
220
            $filtrered = $this->object->getFlexiData(null,
221
                key($flexidata[0])." = ".current($flexidata[0]));
222
            $this->assertArrayHasKey(0, $filtrered);
223
            $this->assertArrayHasKey('id', $filtrered[0]);
224
        }
225
    }
226
227
    /**
228
     * @covers FlexiPeeHP\FlexiBee::loadFromFlexiBee
229
     */
230
    public function testLoadFromFlexiBee()
231
    {
232
        $this->object->loadFromFlexiBee();
233
        $this->object->loadFromFlexiBee(222);
234
    }
235
236
    /**
237
     * @covers FlexiPeeHP\FlexiBee::saveToFlexiBee
238
     * @todo   Implement testSaveToFlexiBee().
239
     */
240
    public function testSaveToFlexiBee()
241
    {
242
        // Remove the following lines when you implement this test.
243
        $this->markTestIncomplete(
244
            'This test has not been implemented yet.'
245
        );
246
    }
247
248
    /**
249
     * @covers FlexiPeeHP\FlexiBee::jsonizeData
250
     */
251
    public function testJsonizeData()
252
    {
253
        $this->assertEquals('{"winstrom":{"@version":"1.0","'.$this->object->agenda.'":{"key":"value"}}}',
254
            $this->object->jsonizeData(['key' => 'value']));
255
    }
256
257
    /**
258
     * @covers FlexiPeeHP\FlexiBee::insertToFlexiBee
259
     * @todo   Implement testInsertToFlexiBee().
260
     */
261
    public function testInsertToFlexiBee()
262
    {
263
        // Remove the following lines when you implement this test.
264
        $this->markTestIncomplete(
265
            'This test has not been implemented yet.'
266
        );
267
    }
268
269
    /**
270
     * @covers FlexiPeeHP\FlexiBee::idExists
271
     * @todo   Implement testIdExists().
272
     */
273
    public function testIdExists()
274
    {
275
        // Remove the following lines when you implement this test.
276
        $this->markTestIncomplete(
277
            'This test has not been implemented yet.'
278
        );
279
    }
280
281
    /**
282
     * @covers FlexiPeeHP\FlexiBee::recordExists
283
     */
284
    public function testRecordExists()
285
    {
286
//        $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...
287
//        $this->assertFalse($this->object->recordExists([]));
288
    }
289
290
    /**
291
     * @covers FlexiPeeHP\FlexiBee::getColumnsFromFlexibee
292
     * @todo   Implement testGetColumnsFromFlexibee().
293
     */
294
    public function testGetColumnsFromFlexibee()
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\FlexiBee::getKod
304
     */
305
    public function testGetKod()
306
    {
307
308
        $this->assertEquals('CODE',
309
            $this->object->getKod([$this->object->myKeyColumn => 'code']));
310
311
        $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...
312
            .'(3.5 mm)';
313
        $code0                                 = $this->object->getKod($testString);
314
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code0);
315
        $code1                                 = $this->object->getKod($testString,
316
            false);
317
        $this->assertEquals('FISHCLAMPUCHYTKAPR', $code1);
318
        $code2                                 = $this->object->getKod($testString);
319
        $this->assertEquals('FISHCLAMPUCHYTKAPR1', $code2);
320
        $this->object->setData($testString);
321
        $code3                                 = $this->object->getKod();
322
        $this->assertEquals('FISHCLAMPUCHYTKAPR2', $code3);
323
324
        $this->assertEquals('TEST',
325
            $this->object->getKod([$this->object->nameColumn => 'test']));
326
327
        $this->assertEquals('TEST1', $this->object->getKod('test'));
328
329
        $this->assertEquals('test', $this->object->getKod(['kod' => 'test']));
330
    }
331
332
    /**
333
     * @covers FlexiPeeHP\FlexiBee::searchString
334
     * @todo   Implement testSearchString().
335
     */
336
    public function testSearchString()
337
    {
338
        // Remove the following lines when you implement this test.
339
        $this->markTestIncomplete(
340
            'This test has not been implemented yet.'
341
        );
342
    }
343
344
    /**
345
     * @covers FlexiPeeHP\FlexiBee::logResult
346
     */
347
    public function testLogResult()
348
    {
349
        $this->object->cleanMessages();
350
        $success = json_decode('{"winstrom":{"@version":"1.0","success":"true",'
351
            .'"stats":{"created":"0","updated":"1","deleted":"0","skipped":"0"'
352
            .',"failed":"0"},"results":[{"id":"1","request-id":"ext:SōkoMan.item'
353
            .':5271","ref":"/c/spoje_net_s_r_o_1/skladovy-pohyb/1.json"}]}}');
354
        $this->object->logResult(current($this->object->object2array($success)),
355
            'http://test');
356
357
        $this->assertArrayHasKey('info', $this->object->getStatusMessages(true));
358
359
        $error = json_decode('{"winstrom":{"@version":"1.0","success":"false",'
360
            .'"stats":{"created":"0","updated":"0","deleted":"0","skipped":"0"'
361
            .',"failed":"0"},"results":[{"errors":[{"message":"cz.winstrom.'
362
            .'service.WSBusinessException: Zadaný kód není unikátní.\nZadaný'
363
            .' kód není unikátní."}]}]}}');
364
        $this->object->logResult(current($this->object->object2array($error)));
365
        $this->assertArrayHasKey('error', $this->object->getStatusMessages(true));
366
    }
367
368
    /**
369
     * @covers FlexiPeeHP\FlexiBee::flexiUrl
370
     */
371
    public function testFlexiUrl()
372
    {
373
        $this->assertEquals("a = 1 and b = 'foo'",
374
            $this->object->flexiUrl(['a' => 1, 'b' => 'foo'], 'and'));
375
        $this->assertEquals("a = 1 or b = 'bar'",
376
            $this->object->flexiUrl(['a' => 1, 'b' => 'bar'], 'or'));
377
    }
378
}