1
|
|
|
<?php |
|
|
|
|
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
|
|
|
|
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.