Completed
Pull Request — erdiko2 (#47)
by
unknown
01:35
created

BaseTestCase::getApp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
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
     * @return App
27
     */
28
    protected function getApp()
29
    {
30
        // Use the application settings
31
        $settings = require getenv('ERDIKO_ROOT').'/bootstrap/settings.php';
32
33
        // Instantiate the application
34
        $app = new App($settings);
35
36
        // Set up dependencies
37
        require getenv('ERDIKO_ROOT').'/bootstrap/dependencies.php';
38
39
        // Register middleware
40
        if ($this->withMiddleware) {
41
            require getenv('ERDIKO_ROOT').'/bootstrap/middleware.php';
42
        }
43
        // Register routes
44
        require getenv('ERDIKO_ROOT').'/bootstrap/routes.php';
45
46
        return $app;
47
    }
48
49
    /**
50
     * Process the application given a request method and URI
51
     *
52
     * @param string $requestMethod the request method (e.g. GET, POST, etc.)
53
     * @param string $requestUri the request URI
54
     * @param array|object|null $requestData the request data
55
     * @return \Slim\Http\Response
56
     */
57
    public function runApp($requestMethod, $requestUri, $requestData = null)
58
    {
59
        // Create a mock environment for testing with
60
        $environment = Environment::mock(
61
            [
62
                'REQUEST_METHOD' => $requestMethod,
63
                'REQUEST_URI' => $requestUri
64
            ]
65
        );
66
67
        // Set up a request object based on the environment
68
        $request = Request::createFromEnvironment($environment);
69
70
        // Add request data, if it exists
71
        if (isset($requestData)) {
72
            $request = $request->withParsedBody($requestData);
73
        }
74
75
        // Set up a response object
76
        $response = new Response();
77
78
        // Use the application settings
79
        $settings = require __DIR__ . '/../../src/settings.php';
80
81
        // Instantiate the application
82
        $app = new App($settings);
83
84
        // Set up dependencies
85
        require __DIR__ . '/../../src/dependencies.php';
86
87
        // Register middleware
88
        if ($this->withMiddleware) {
89
            require __DIR__ . '/../../src/middleware.php';
90
        }
91
92
        // Register routes
93
        require __DIR__ . '/../../src/routes.php';
94
95
        // Process the application
96
        $response = $app->process($request, $response);
97
98
        // Return the response
99
        return $response;
100
    }
101
}
102