ClientTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 8
dl 0
loc 90
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testVerify() 0 17 1
A testKickboxApiException() 0 14 1
A testEmptyContentException() 0 11 1
A getResponse() 0 8 1
A getErrorResponse() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Kickbox Bundle.
5
 *
6
 * (c) Abdoul Ndiaye <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Andi\KickBoxBundle\Tests\Http;
13
14
use Andi\KickBoxBundle\Exception\EmptyContentException;
15
use Andi\KickBoxBundle\Exception\KickBoxApiException;
16
use Andi\KickBoxBundle\Factory\ResponseFactory;
17
use Andi\KickBoxBundle\Http\Client;
18
use Andi\KickBoxBundle\Http\Response;
19
use Andi\KickBoxBundle\Tests\Factory\ResponseFactoryTest;
20
use GuzzleHttp\Psr7\Response as HttpResponse;
21
use GuzzleHttp\Psr7\Request;
22
use GuzzleHttp\Psr7\Stream;
23
use Phake;
24
use GuzzleHttp\Client as HttpClient;
25
26
/**
27
 * Class ClientTest
28
 *
29
 * @author Abdoul Ndiaye <[email protected]>
30
 *
31
 * @see    Andi\KickBoxBundle\Http\Client
32
 */
33
class ClientTest extends \PHPUnit_Framework_TestCase
34
{
35
    /**
36
     * @var HttpClient|\Phake_IMock
37
     */
38
    protected $httpClient;
39
40
    /**
41
     * @var ResponseFactory
42
     */
43
    protected $responseFactory;
44
45
    /**
46
     * @{@inheritdoc}
47
     */
48
    protected function setUp()
49
    {
50
        $this->httpClient      = Phake::mock(HttpClient::class);
51
        $this->responseFactory = new ResponseFactory();
52
    }
53
54
    public function testVerify()
55
    {
56
        Phake::when($this->httpClient)->get(
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<GuzzleHttp\Client>; however, Phake::when() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57
            'my_end_point',
58
            [
59
                'query' => [
60
                    'email'   => '[email protected]',
61
                    'apikey'  => 'my_key',
62
                    'timeout' => 7000,
63
                ]
64
            ]
65
        )->thenReturn($this->getResponse());
66
67
        $client = new Client($this->httpClient, $this->responseFactory, 'my_end_point', 'my_key');
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Phake_IMock>; however, Andi\KickBoxBundle\Http\Client::__construct() does only seem to accept object<GuzzleHttp\Client>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
69
        $this->assertInstanceOf(Response::class, $client->verify('[email protected]', 7000));
70
    }
71
72
    public function testKickboxApiException()
73
    {
74
        Phake::when($this->httpClient)->get(Phake::anyParameters())->thenThrow(
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<GuzzleHttp\Client>; however, Phake::when() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75
            new KickBoxApiException('expectedMessage', new Request('GET', 'error'), $this->getErrorResponse())
76
        );
77
78
        $this->setExpectedException(
79
            KickBoxApiException::class,
80
            'An error occurred during the api call : exception message'
81
        );
82
83
        $client = new Client($this->httpClient, $this->responseFactory, '', '');
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Phake_IMock>; however, Andi\KickBoxBundle\Http\Client::__construct() does only seem to accept object<GuzzleHttp\Client>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
        $client->verify('');
85
    }
86
87
    public function testEmptyContentException()
88
    {
89
        Phake::when($this->httpClient)->get(Phake::anyParameters())->thenReturn(new HttpResponse(400));
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<GuzzleHttp\Client>; however, Phake::when() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
91
        $this->setExpectedException(
92
            EmptyContentException::class
93
        );
94
95
        $client = new Client($this->httpClient, $this->responseFactory, '', '');
0 ignored issues
show
Bug introduced by
It seems like $this->httpClient can also be of type object<Phake_IMock>; however, Andi\KickBoxBundle\Http\Client::__construct() does only seem to accept object<GuzzleHttp\Client>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
        $client->verify('');
97
    }
98
99
    /**
100
     * @return HttpResponse
101
     */
102
    protected function getResponse()
103
    {
104
        return new HttpResponse(
105
            200,
106
            ResponseFactoryTest::$goodHeaders,
107
            json_encode(ResponseFactoryTest::$goodParameters)
108
        );
109
    }
110
111
    /**
112
     * @return HttpResponse
113
     */
114
    protected function getErrorResponse()
115
    {
116
        return new HttpResponse(
117
            500,
118
            [],
119
            json_encode(['message' => 'exception message'])
120
        );
121
    }
122
}
123