WebTestClient   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 39
c 4
b 0
f 0
dl 0
loc 119
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getResponse() 0 3 1
A __call() 0 8 2
A getRequest() 0 3 1
B request() 0 46 8
1
<?php
2
3
namespace SlimX\Tests;
4
5
/**
6
 * Based on WebTestClient from
7
 * [email protected]:there4/slim-unit-testing-example.git
8
 */
9
10
use \Slim\Http\Body;
11
use \Slim\Http\Environment;
12
13
/**
14
 * Mocks the client request and response.
15
 */
16
class WebTestClient
17
{
18
    private $app;
19
    private $request;
20
    private $response;
21
22
    /**
23
     * Abstract way to make a request to SlimPHP, this allows us to mock the
24
     * slim environment.
25
     *
26
     * @param  string $method          Method name.
27
     * @param  string $path            URI.
28
     * @param  array  $data            Data request.
29
     * @param  array  $optionalHeaders Optional headers.
30
     * @return mixed.
0 ignored issues
show
Documentation Bug introduced by
The doc comment mixed. at position 0 could not be parsed: Unknown type name 'mixed.' at position 0 in mixed..
Loading history...
31
     */
32
    private function request(
33
        string $method,
34
        string $path,
35
        $data = array(),
36
        array $optionalHeaders = array()
37
    ) {
38
        // Capture STDOUT
39
        ob_start();
40
41
        $options = array(
42
            'REQUEST_METHOD' => strtoupper($method),
43
            'REQUEST_URI'      => $path,
44
        );
45
46
        if ($method === 'get') {
47
            $options['QUERY_STRING'] = http_build_query($data);
48
        } elseif (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
49
            $options['slim.input']   = http_build_query($data);
50
        } else {
51
            $options['slim.input']   = $data;
52
        }
53
54
        // Prepare a mock environment
55
        $env = Environment::mock($options);
56
        $request = \Slim\Http\Request::createFromEnvironment($env);
57
58
        foreach ($optionalHeaders as $key => $value) {
59
            $request = $request->withHeader($key, $value);
60
        }
61
62
        if ('get' !== $method && is_array($data)) {
63
            $request = $request->withParsedBody($data);
64
        } elseif ('get' !== $method && is_string($data)) {
65
            $body = new Body(fopen('php://temp', 'r+'));
0 ignored issues
show
Bug introduced by
It seems like fopen('php://temp', 'r+') can also be of type false; however, parameter $stream of Slim\Http\Body::__construct() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $body = new Body(/** @scrutinizer ignore-type */ fopen('php://temp', 'r+'));
Loading history...
66
            $body->write($data);
67
            $body->rewind();
68
            $request = $request->withBody($body);
69
        }
70
71
        // Execute our app
72
        $response = new \Slim\Http\Response();
73
        $this->response = $this->app->process($request, $response);
74
        $this->request = $request;
75
76
        // Return the application output. Also available in `response->body()`
77
        return ob_get_clean();
78
    }
79
80
81
    // We support these methods for testing. These are available via
82
    // `this->get()` and `$this->post()`. This is accomplished with the
83
    // `__call()` magic method below.
84
    private $testingMethods = array(
85
        'get', 'post', 'patch', 'put', 'delete', 'head', 'options'
86
    );
87
88
    /**
89
     * Set the Slim app.
90
     *
91
     * @param \Slim\App $app Slim app instance.
92
     */
93
    public function __construct(\Slim\App $app)
94
    {
95
        $this->app = $app;
96
    }
97
98
    /**
99
     * Implement our `get`, `post`, and other http operations, as defined on
100
     * $testingMethods.
101
     *
102
     * @param  string $method    Method being called.
103
     * @param  array  $arguments List of arguments.
104
     * @return mixed.
0 ignored issues
show
Documentation Bug introduced by
The doc comment mixed. at position 0 could not be parsed: Unknown type name 'mixed.' at position 0 in mixed..
Loading history...
105
     */
106
    public function __call(string $method, array $arguments)
107
    {
108
        if (in_array(strtolower($method), $this->testingMethods)) {
109
            list($path, $data, $headers) = array_pad($arguments, 3, array());
110
            return $this->request($method, $path, $data, $headers);
111
        }
112
        throw new \BadMethodCallException(
113
            strtoupper($method) . ' is not supported'
114
        );
115
    }
116
117
    /**
118
     * Get the response objects.
119
     *
120
     * @return \Slim\Http\Response Response object.
121
     */
122
    public function getResponse()
123
    {
124
        return $this->response;
125
    }
126
127
    /**
128
     * Get the request object.
129
     *
130
     * @return \Slim\Http\Request Request object.
131
     */
132
    public function getRequest()
133
    {
134
        return $this->request;
135
    }
136
}
137