Completed
Branch erdiko2 (40e193)
by John
04:03
created

BaseTestCase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 62
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B runApp() 0 44 3
1
<?php
2
3
namespace Tests\Functional;
4
5
use Slim\App;
6
use Slim\Http\Request;
7
use Slim\Http\Response;
8
use Slim\Http\Environment;
9
10
/**
11
 * This is an example class that shows how you could set up a method that
12
 * runs the application. Note that it doesn't cover all use-cases and is
13
 * tuned to the specifics of this skeleton app, so if your needs are
14
 * different, you'll need to change it.
15
 */
16
class BaseTestCase extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * Use middleware when running application?
20
     *
21
     * @var bool
22
     */
23
    protected $withMiddleware = true;
24
25
    /**
26
     * Process the application given a request method and URI
27
     *
28
     * @param string $requestMethod the request method (e.g. GET, POST, etc.)
29
     * @param string $requestUri the request URI
30
     * @param array|object|null $requestData the request data
31
     * @return \Slim\Http\Response
32
     */
33
    public function runApp($requestMethod, $requestUri, $requestData = null)
34
    {
35
        // Create a mock environment for testing with
36
        $environment = Environment::mock(
37
            [
38
                'REQUEST_METHOD' => $requestMethod,
39
                'REQUEST_URI' => $requestUri
40
            ]
41
        );
42
43
        // Set up a request object based on the environment
44
        $request = Request::createFromEnvironment($environment);
45
46
        // Add request data, if it exists
47
        if (isset($requestData)) {
48
            $request = $request->withParsedBody($requestData);
49
        }
50
51
        // Set up a response object
52
        $response = new Response();
53
54
        // Use the application settings
55
        $settings = require __DIR__ . '/../../src/settings.php';
56
57
        // Instantiate the application
58
        $app = new App($settings);
59
60
        // Set up dependencies
61
        require __DIR__ . '/../../src/dependencies.php';
62
63
        // Register middleware
64
        if ($this->withMiddleware) {
65
            require __DIR__ . '/../../src/middleware.php';
66
        }
67
68
        // Register routes
69
        require __DIR__ . '/../../src/routes.php';
70
71
        // Process the application
72
        $response = $app->process($request, $response);
73
74
        // Return the response
75
        return $response;
76
    }
77
}
78