Completed
Push — master ( 0d922b...2a6782 )
by Neomerx
02:06
created

TestCaseTrait::addOnHandleResponseEvent()   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 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Limoncello\Testing;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Closure;
20
use Limoncello\Contracts\Core\ApplicationInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\StreamInterface;
23
24
/**
25
 * @package Limoncello\Testing
26
 */
27
trait TestCaseTrait
28
{
29
    /**
30
     * @var array
31
     */
32
    private $eventHandlers = [];
33
34
    /**
35
     * @param array|null                      $server
36
     * @param array|null                      $queryParams
37
     * @param array|object|null               $parsedBody
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $parsedBody a bit more specific; maybe use null|array.
Loading history...
38
     * @param array|null                      $cookies
39
     * @param array|null                      $files
40
     * @param string|resource|StreamInterface $messageBody
41
     * @param string                          $protocolVersion
42
     *
43
     * @return Sapi
44
     */
45
    abstract protected function createSapi(
46
        array $server = null,
47
        array $queryParams = null,
48
        array $parsedBody = null,
49
        array $cookies = null,
50
        array $files = null,
51
        $messageBody = 'php://input',
52
        string $protocolVersion = '1.1'
53
    ): Sapi;
54
55
    /**
56
     * @param Sapi $sapi
57
     *
58
     * @return ApplicationInterface
59
     */
60
    abstract protected function createApplication(Sapi $sapi): ApplicationInterface;
61
62
    /** @noinspection PhpTooManyParametersInspection
63
     * @param string                          $method
64
     * @param string                          $uri
65
     * @param array                           $queryParams
66
     * @param array                           $parsedBody
67
     * @param array                           $headers
68
     * @param array                           $cookies
69
     * @param array                           $files
70
     * @param array                           $server
71
     * @param string|resource|StreamInterface $content
72
     * @param string                          $host
73
     * @param string                          $protocolVersion
74
     *
75
     * @return ResponseInterface
76
     *
77
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
78
     */
79 8
    protected function call(
80
        string $method,
81
        string $uri,
82
        array $queryParams = [],
83
        array $parsedBody = [],
84
        array $headers = [],
85
        array $cookies = [],
86
        array $files = [],
87
        array $server = [],
88
        $content = 'php://input',
89
        string $host = 'localhost',
90
        string $protocolVersion = '1.1'
91
    ): ResponseInterface {
92 8
        $headers['host'] = $host;
93
94 8
        $prefix = 'HTTP_';
95 8
        foreach ($headers as $name => $value) {
96 8
            $name = strtr(strtoupper($name), '-', '_');
97 8
            if ($name !== 'CONTENT_TYPE' && strpos($name, $prefix) !== 0) {
98 8
                $name = $prefix . $name;
99
            }
100 8
            $server[$name] = $value;
101
        }
102
103 8
        $server['REQUEST_URI']    = $uri;
104 8
        $server['REQUEST_METHOD'] = $method;
105
106 8
        $sapi = $this->createSapi($server, $queryParams, $parsedBody, $cookies, $files, $content, $protocolVersion);
107 8
        $app  = $this->createApplication($sapi);
108
109 8
        $app->run();
110 8
        unset($app);
111
112 8
        $response = $sapi->getResponse();
113 8
        unset($sapi);
114
115 8
        return $response;
116
    }
117
118
    /**
119
     * @return void
120
     */
121 1
    protected function resetEventHandlers()
122
    {
123 1
        $this->eventHandlers = [];
124 1
    }
125
126
    /**
127
     * @param Closure $handler
128
     *
129
     * @return void
130
     */
131 1
    protected function addOnHandleRequestEvent(Closure $handler)
132
    {
133 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST][] = $handler;
134 1
    }
135
136
    /**
137
     * @param Closure $handler
138
     *
139
     * @return void
140
     */
141 1
    protected function addOnHandleResponseEvent(Closure $handler)
142
    {
143 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE][] = $handler;
144 1
    }
145
146
    /**
147
     * @param Closure $handler
148
     *
149
     * @return void
150
     */
151 1
    protected function addOnContainerCreatedEvent(Closure $handler)
152
    {
153 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED][] = $handler;
154 1
    }
155
156
    /**
157
     * @param Closure $handler
158
     *
159
     * @return void
160
     */
161 1
    protected function addOnContainerConfiguredEvent(Closure $handler)
162
    {
163 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR][] = $handler;
164 1
    }
165
166
    /**
167
     * @return array
168
     */
169 1
    protected function getHandleRequestEvents(): array
170
    {
171 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST] ?? [];
172
    }
173
174
    /**
175
     * @return array
176
     */
177 1
    protected function getHandleResponseEvents(): array
178
    {
179 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE] ?? [];
180
    }
181
182
    /**
183
     * @return array
184
     */
185 1
    protected function getContainerCreatedEvents(): array
186
    {
187 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED] ?? [];
188
    }
189
190
    /**
191
     * @return array
192
     */
193 1
    protected function getContainerConfiguredEvents(): array
194
    {
195 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR] ?? [];
196
    }
197
}
198