Completed
Push — master ( c4e219...f561f8 )
by Andrii
03:15
created

ConnectionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
A tearDown() 0 3 1
A testGet() 0 9 1
A testErrorChecker() 0 6 1
1
<?php
2
3
/*
4
 * Tools to use API as ActiveRecord for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-hiart
7
 * @package   yii2-hiart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\hiart\tests\unit;
13
14
use GuzzleHttp\Psr7\Response;
15
use hiqdev\hiart\Connection;
16
use hiqdev\hiart\tests\Mock;
17
use Yii;
18
19
/**
20
 * Connection test class.
21
 */
22
class ConnectionTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var Connection
26
     */
27
    protected $object;
28
29
    /**
30
     * @var Mock
31
     */
32
    protected $mock;
33
34
    protected $site   = 'http://test.api.site/';
35
    protected $url    = 'test/url';
36
    protected $query  = ['a' => 'b'];
37
    protected $body   = ['c' => 'd'];
38
    protected $result = 'xyz result string';
39
40
    protected function setUp()
41
    {
42
        $response     = new Response(200, [], $this->result);
43
        $this->mock   = new Mock($response);
44
        $this->object = Yii::createObject([
45
            'class'   => Connection::className(),
46
            'handler' => $this->mock,
47
            'config'  => [
48
                'base_uri' => $this->site,
49
            ],
50
            'errorChecker' => function ($res) {
0 ignored issues
show
Unused Code introduced by
The parameter $res is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
                return null;
52
            },
53
        ]);
54
    }
55
56
    protected function tearDown()
57
    {
58
    }
59
60
    public function testGet()
61
    {
62
        $result = $this->object->get($this->url, [], $this->body, false);
0 ignored issues
show
Documentation introduced by
$this->body is of type array, but the function expects a string|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...
63
        $this->assertSame($this->result, $result);
64
        $this->assertSame('request',   $this->mock->name);
65
        $this->assertSame('GET',       $this->mock->args[0]);
66
        $this->assertSame($this->url,  $this->mock->args[1]);
67
        $this->assertSame($this->body, $this->mock->args[2]['form_params']);
68
    }
69
70
    public function testErrorChecker()
71
    {
72
        $this->object->setErrorChecker(function ($res) { return $res; });
73
        $this->setExpectedException('hiqdev\hiart\ErrorResponseException', $this->result);
74
        $this->object->get($this->url, [], $this->body, false);
0 ignored issues
show
Documentation introduced by
$this->body is of type array, but the function expects a string|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...
75
    }
76
}
77