WebTestCase::runApp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Tests;
4
5
use App\Application;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ResponseInterface;
8
use Slim\Http\Environment;
9
use Slim\Http\Request;
10
use Slim\Http\Response;
11
use Symfony\Component\Dotenv\Dotenv;
12
13
class WebTestCase extends TestCase
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function __construct($name = null, array $data = [], $dataName = '')
19
    {
20
        session_start();
21
22
        parent::__construct($name, $data, $dataName);
23
    }
24
25
    /**
26
     * Processes the application given a request method and URI.
27
     *
28
     * @param string            $requestMethod
29
     * @param string            $requestUri
30
     * @param array|object|null $requestData
31
     *
32
     * @return ResponseInterface
33
     */
34
    public function runApp(string $requestMethod, string $requestUri, $requestData = null)
35
    {
36
        $environment = Environment::mock([
37
            'REQUEST_METHOD' => $requestMethod,
38
            'REQUEST_URI' => $requestUri
39
        ]);
40
41
        $request = Request::createFromEnvironment($environment);
42
43
        if (isset($requestData)) {
44
            $request = $request->withParsedBody($requestData);
45
        }
46
47
        $response = new Response();
48
49
        (new Dotenv())->loadEnv(__DIR__.'/../.env');
50
51
        $app = new Application('test');
52
53
        return $app->process($request, $response);
54
    }
55
}
56