Completed
Pull Request — master (#21)
by Sullivan
04:01
created

Client::setDefaultException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Mock;
4
5
use Http\Client\Common\HttpAsyncClientEmulator;
6
use Http\Client\Exception;
7
use Http\Client\HttpAsyncClient;
8
use Http\Client\HttpClient;
9
use Http\Discovery\MessageFactoryDiscovery;
10
use Http\Message\ResponseFactory;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * HTTP client mock.
16
 *
17
 * This mock is most useful in tests. It does not send requests but stores them
18
 * for later retrieval. Additionally, you can set an exception to test
19
 * exception handling.
20
 *
21
 * @author David de Boer <[email protected]>
22
 */
23
class Client implements HttpClient, HttpAsyncClient
24
{
25
    use HttpAsyncClientEmulator;
26
27
    /**
28
     * @var ResponseFactory
29
     */
30
    private $responseFactory;
31
32
    /**
33
     * @var RequestInterface[]
34
     */
35
    private $requests = [];
36
37
    /**
38
     * @var ResponseInterface[]
39
     */
40
    private $responses = [];
41
42
    /**
43
     * @var ResponseInterface|null
44
     */
45
    private $defaultResponse;
46
47
    /**
48
     * @var Exception[]
49
     */
50
    private $exceptions = [];
51
52
    /**
53
     * @var Exception|null
54
     */
55
    private $defaultException;
56
57
    /**
58
     * @param ResponseFactory|null $responseFactory
59
     */
60 8
    public function __construct(ResponseFactory $responseFactory = null)
61
    {
62 8
        $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
63 8
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 5
    public function sendRequest(RequestInterface $request)
69
    {
70 5
        $this->requests[] = $request;
71
72 5
        if (count($this->exceptions) > 0) {
73 1
            throw array_shift($this->exceptions);
74
        }
75
76 4
        if (count($this->responses) > 0) {
77 1
            return array_shift($this->responses);
78
        }
79
80 3
        if ($this->defaultException) {
81 1
            throw $this->defaultException;
82
        }
83
84 2
        if ($this->defaultResponse) {
85 1
            return $this->defaultResponse;
86
        }
87
88
        // Return success response by default
89 1
        return $this->responseFactory->createResponse();
90
    }
91
92
    /**
93
     * Adds an exception that will be thrown.
94
     *
95
     * @param \Exception $exception
96
     */
97 1
    public function addException(\Exception $exception)
98
    {
99 1
        $this->exceptions[] = $exception;
100 1
    }
101
102
    /**
103
     * Sets the default exception to throw if none added.
104
     *
105
     * @param \Exception|null $defaultException
106
     */
107 1
    public function setDefaultException(\Exception $defaultException = null)
108
    {
109 1
        $this->defaultException = $defaultException;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaultException can also be of type object<Exception>. However, the property $defaultException is declared as type object<Http\Client\Exception>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
110 1
    }
111
112
    /**
113
     * Adds a response that will be returned.
114
     *
115
     * @param ResponseInterface $response
116
     */
117 1
    public function addResponse(ResponseInterface $response)
118
    {
119 1
        $this->responses[] = $response;
120 1
    }
121
122
    /**
123
     * Sets the default response to be returned if none added.
124
     *
125
     * @param ResponseInterface|null $defaultResponse
126
     */
127 1
    public function setDefaultResponse(ResponseInterface $defaultResponse = null)
128
    {
129 1
        $this->defaultResponse = $defaultResponse;
130 1
    }
131
132
    /**
133
     * Returns requests that were sent.
134
     *
135
     * @return RequestInterface[]
136
     */
137
    public function getRequests()
138
    {
139
        return $this->requests;
140
    }
141
}
142