BaseTestCase::createRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace App\Tests;
4
5
use App\Application;
6
use Middlewares\Utils\Factory\ServerRequestFactory;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
abstract class BaseTestCase extends TestCase
12
{
13
    /**
14
     * @var Application
15
     */
16
    protected $app;
17
18
    protected function setUp()
19
    {
20
        $app = include __DIR__ . '/../bootstrap/app.php';
21
        $this->assertInstanceOf(Application::class, $app);
22
23
        $this->app = $app;
24
    }
25
26
    /**
27
     * @param string $method
28
     * @param string $uri
29
     * @return ServerRequestInterface
30
     */
31
    protected function createRequest($method, $uri)
32
    {
33
        if (!preg_match('/^http/', $uri)) {
34
            $uri = 'http://localhost/' . ltrim($uri, '/');
35
        }
36
37
        $request = (new ServerRequestFactory())->createServerRequest($method, $uri);
38
        $this->assertInstanceOf(ServerRequestInterface::class, $request);
39
40
        return $request;
41
    }
42
43
    /**
44
     * @param ServerRequestInterface $request
45
     * @return ResponseInterface
46
     */
47
    protected function call(ServerRequestInterface $request)
48
    {
49
        $response = $this->app->dispatch($request);
50
        $this->assertInstanceOf(ResponseInterface::class, $response);
51
52
        return $response;
53
    }
54
}
55