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

HttpTestCase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A request() 0 13 2
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