-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseTestCase.php
More file actions
91 lines (77 loc) · 2.37 KB
/
Copy pathBaseTestCase.php
File metadata and controls
91 lines (77 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace App\Tests;
use PHPUnit\Framework\TestCase;
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Environment;
class BaseTestCase extends TestCase
{
protected $app;
/**
* Setup the test environment.
*
* @return void
*/
public function setUp()
{
$this->createApplication();
}
/**
* Clean up the testing environment before the next test.
*
* @return void
*/
protected function tearDown()
{
$this->app = null;
}
/**
* Creates the application.
*/
public function createApplication()
{
$this->app = require __DIR__ . '/../bootstrap/test.php';
return $this->app;
}
/**
* Process the application given a request method and URI.
*
* @see http://www.slimframework.com/docs/cookbook/environment.html
*
* @param string $requestMethod the request method (e.g. GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.)
* @param string $requestUri the request URI
* @param string $queryString the params (e.g. 'abc=123&foo=bar')
* @param array $envData Array of custom environment keys and values
* @param array|object|null $requestData the request data
* @return \Slim\Http\Response
*/
public function runApp($requestMethod, $requestUri, $queryString, $envData = [], $requestData = null)
{
$mock = array_merge([
'REQUEST_METHOD' => $requestMethod,
'REQUEST_URI' => $requestUri,
'QUERY_STRING' => $queryString,
'CONTENT_TYPE' => 'application/json;charset=utf8'
], $envData);
// Create a mock environment for testing with.
$environment = Environment::mock($mock);
// Set up a request object based on the environment.
$request = Request::createFromEnvironment($environment);
// Add request data, if it exists
if (isset($requestData)) {
$request = $request->withParsedBody($requestData);
}
// Create application.
$app = $this->app;
if (!$app instanceof App) {
$app = $this->createApplication();
}
// Set up a response object.
$response = new Response();
// Process the application
$response = $app->process($request, $response);
// Return the response
return $response;
}
}