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