vonbraunlabs /
slimx
| 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, $body); |
||
| 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, ?string $message = null) |
||
| 51 | { |
||
| 52 | $max = null === $max ? $min : $max; |
||
| 53 | $httpCode = $this->client->getResponse()->getStatusCode(); |
||
| 54 | $body = $this->client->getResponse()->getBody(); |
||
| 55 | |||
| 56 | if ($message !== null) { |
||
| 57 | $message = ". Custom message: $message"; |
||
| 58 | } |
||
| 59 | $this->assertTrue( |
||
| 60 | $min <= $httpCode && $httpCode <= $max, |
||
| 61 | "Returned code $httpCode is not between $min and $max. Body: " . |
||
| 62 | $body . $message |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | abstract public function getSlimInstance(); |
||
| 67 | |||
| 68 | protected function setUp() |
||
| 69 | { |
||
| 70 | $this->app = $this->getSlimInstance(); |
||
| 71 | $this->client = new WebTestClient($this->app); |
||
| 72 | parent::setUp(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Valid requests should produce a 2xx http response code. |
||
| 77 | */ |
||
| 78 | public function testValidRequest() |
||
| 79 | { |
||
| 80 | $this->client->{$this->method}( |
||
| 81 | $this->endpoint, |
||
| 82 | $this->getValidData(), |
||
| 83 | $this->requestHeaders |
||
| 84 | ); |
||
| 85 | $this->assertResponseCode(200, 299); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testCorsOptions() |
||
| 89 | { |
||
| 90 | $this->client->options($this->endpoint, []); |
||
| 91 | |||
| 92 | $this->assertResponseCode(200, 299); |
||
| 93 | $response = $this->client->getResponse(); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get full path of given file. Look for file on current dir, and |
||
| 98 | * recursively on its parent dir, until file is found. |
||
| 99 | * |
||
| 100 | * @param string $file File name. |
||
| 101 | * |
||
| 102 | * @return string Full name of the file. |
||
| 103 | */ |
||
| 104 | protected function getFullPath(string $file): string |
||
| 105 | { |
||
| 106 | for ($i = 0; |
||
| 107 | !file_exists( |
||
| 108 | $fullPath = __DIR__ . '/' . str_repeat('../', $i) . $file |
||
| 109 | ) && $i < 100; |
||
| 110 | $i++) { |
||
| 111 | } |
||
| 112 | return $fullPath; |
||
| 113 | } |
||
| 114 | } |
||
| 115 |