WebTestCase::runApp()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 3
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
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
        parent::__construct($name, $data, $dataName);
21
    }
22
23
    /**
24
     * Processes the application given a request method and URI.
25
     *
26
     * @param string $requestMethod
27
     * @param string $requestUri
28
     * @param array|object|null $requestData
29
     *
30
     * @return ResponseInterface
31
     */
32
    public function runApp($requestMethod, $requestUri, $requestData = null)
33
    {
34
        $environment = Environment::mock([
35
            'REQUEST_METHOD' => $requestMethod,
36
            'REQUEST_URI' => $requestUri
37
        ]);
38
39
        $request = Request::createFromEnvironment($environment);
40
41
        if (isset($requestData)) {
42
            $request = $request->withParsedBody($requestData);
43
        }
44
45
        if (!isset($_SERVER['APP_TEST_ENV'])) {
46
            if (!class_exists(Dotenv::class)) {
47
                throw new \RuntimeException('APP_TEST_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
48
            }
49
            (new Dotenv())->load(__DIR__.'/../.env');
50
        }
51
52
        $app = new Application($_SERVER['APP_TEST_ENV'] ?? 'test');
53
54
        return $app->process($request, new Response());
55
    }
56
}
57