Completed
Push — master ( 035400...ab4b15 )
by Diogo Oliveira de
03:38
created

AbstractSlimXTest::getFullPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SlimX\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\Http\Message\ResponseInterface;
7
8
abstract class AbstractSlimXTest extends TestCase
9
{
10
    protected $app;
11
    protected $client;
12
    protected $method;
13
    protected $endpoint;
14
    protected $requestHeaders = ['HTTP_ACCEPT' => 'application/vnd.v1+json'];
15
16
    abstract protected function getValidData() : array;
17
18
    /**
19
     * Asserts if given response respects what is expected from the respective Error obj.
20
     *
21
     * @param ResponseInterface $response Request's response
22
     * @param int $code Expected error code
23
     * @param ?int $codeMax If specified, $code and $codeMax will act as minimum
24
     * expected error code and maximum expected error code, respectively.
25
     * @return void
26
     */
27
    protected function assertError(ResponseInterface $response, int $code, ?int $codeMax = null)
28
    {
29
        $body = (string) $response->getBody();
30
        $json = json_decode($body);
31
        $this->assertNotNull($json, "Returned body is not valid json: " . $body);
32
        $this->assertInstanceOf('stdClass', $json);
33
        $this->assertNotEmpty($json);
34
        $this->assertTrue(isset($json->code), "Code not present: " . $body);
35
        $this->assertTrue(isset($json->message), "Message not present: " . $body);
36
        $error = $this->app->getContainer()->get('error');
37
        if (null !== $codeMax) {
38
            $this->assertTrue(
39
                $code <= $json->code && $json->code <= $codeMax,
40
                "Code {$json->code} is not within boundaries min $code, max $codeMax"
41
            );
42
        } else {
43
            $this->assertEquals($code, $json->code, "Expecting code $code but received JSON $body");
44
        }
45
        $node = $error->getNode($json->code);
46
        $this->assertEquals($node['status'], $response->getStatusCode());
47
        $this->assertEquals($node['message'], $json->message);
48
    }
49
50
    protected function assertResponseCode(int $min, ?int $max = null)
51
    {
52
        $max = null === $max ? $min : $max;
53
        $httpCode = $this->client->getResponse()->getStatusCode();
54
        $body = $this->client->getResponse()->getBody();
55
        $this->assertTrue(
56
            $min <= $httpCode && $httpCode <= $max,
57
            "Returned code $httpCode is not between $min and $max. Body: " . $body
58
        );
59
    }
60
61
    abstract public function getSlimInstance();
62
63
    protected function setUp()
64
    {
65
        $this->app = $this->getSlimInstance();
66
        $this->client = new WebTestClient($this->app);
67
        parent::setUp();
68
    }
69
70
    /**
71
     * Valid requests should produce a 2xx http response code.
72
     */
73
    public function testValidRequest()
74
    {
75
        $this->client->{$this->method}(
76
            $this->endpoint,
77
            $this->getValidData(),
78
            $this->requestHeaders
79
        );
80
        $this->assertResponseCode(200, 299);
81
    }
82
83
    public function testCorsOptions()
84
    {
85
        $this->client->options($this->endpoint, []);
86
87
        $this->assertResponseCode(200, 299);
88
        $response = $this->client->getResponse();
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
89
    }
90
91
    /**
92
     * Get full path of given file. Look for file on current dir, and
93
     * recursively on its parent dir, until file is found.
94
     *
95
     * @param string $file File name.
96
     *
97
     * @return string Full name of the file.
98
     */
99
    protected function getFullPath(string $file): string
100
    {
101
        for ($i = 0;
102
            !file_exists(
103
                $fullPath = __DIR__ . '/' . str_repeat('../', $i) . $file
104
            ) && $i < 100;
105
            $i++) {
106
        }
107
        return $fullPath;
108
    }
109
}
110