Completed
Push — master ( 80f228...46ce65 )
by Matthias
03:00
created

HttpTestCase::request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 4
1
<?php
2
3
namespace MatthiasMullie\Api\TestHelpers;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use PHPUnit_Framework_TestCase;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * API end-to-end test that performs API requests to an actual (local) web
12
 * server, and verifies the results.
13
 * This is slower than RequestHandlerTestCase, but I suggest doing at least one
14
 * of these just to verify the bootstrapping. RequestHandlerTestCase can then
15
 * be used to test the controller logic.
16
 */
17
abstract class HttpTestCase extends PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    public function setUp()
25
    {
26
        $this->client = new Client([
27
            'base_uri' => 'http://localhost',
28
        ]);
29
    }
30
31
    /**
32
     * @param string $method
33
     * @param string $uri
34
     * @param array  $get
35
     * @param array  $post
36
     *
37
     * @return ResponseInterface
38
     */
39
    public function request($method, $uri, array $get = [], array $post = [])
40
    {
41
        $options = [
42
            'query' => $get,
43
            'form_params' => $post,
44
        ];
45
46
        try {
47
            return $this->client->request($method, $uri, $options);
48
        } catch (RequestException $e) {
49
            return $e->getResponse();
50
        }
51
    }
52
}
53