Completed
Push — master ( 39d3db...e82fd4 )
by Alexis
02:42
created

WebTestCase::runApp()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 4
nop 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Tests;
4
5
session_start();
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ResponseInterface;
9
use Slim\App;
10
use Slim\Http\Environment;
11
use Slim\Http\Request;
12
use Slim\Http\Response;
13
14
class WebTestCase extends TestCase
15
{
16
    /**
17
     * @var bool
18
     */
19
    protected $withMiddleware = true;
20
21
    /**
22
     * Processes the application given a request method and URI.
23
     *
24
     * @param string $requestMethod
25
     * @param string $requestUri
26
     * @param array|object|null $requestData
27
     *
28
     * @return ResponseInterface
29
     */
30
    public function runApp($requestMethod, $requestUri, $requestData = null)
31
    {
32
        $environment = Environment::mock(
33
            [
34
                'REQUEST_METHOD' => $requestMethod,
35
                'REQUEST_URI' => $requestUri
36
            ]
37
        );
38
39
        $request = Request::createFromEnvironment($environment);
40
41
        if (isset($requestData)) {
42
            $request = $request->withParsedBody($requestData);
43
        }
44
45
        $response = new Response();
46
47
        $app = new App([
48
            'env' => 'test',
49
            'root_dir' => dirname(__DIR__)
50
        ]);
51
        $container = $app->getContainer();
52
53
        $container['config'] = require __DIR__ . '/../app/config/config.php';
54
55
        require __DIR__ . '/../app/dependencies.php';
56
        require __DIR__ . '/../app/handlers.php';
57
58
        if ($this->withMiddleware) {
59
            require __DIR__ . '/../app/middleware.php';
60
        }
61
62
        require __DIR__ . '/../app/controllers.php';
63
        require __DIR__ . '/../app/routing.php';
64
65
        return $app->process($request, $response);
66
    }
67
}
68