2024-05-23 00:39:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Slovocast\Tests\Controller;
|
|
|
|
|
|
|
|
use Slovocast\Tests\TestCase;
|
|
|
|
use Slovocast\Controller\Controller;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
|
|
|
class ControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testHtmlInlineResponse(): void
|
|
|
|
{
|
|
|
|
$testController = new class extends Controller {
|
|
|
|
public function respond(): Response
|
|
|
|
{
|
|
|
|
return $this->renderInline(
|
|
|
|
'<p>{{ name }}</p>',
|
|
|
|
[ 'name' => 'Dave' ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$app = $this->getAppInstance();
|
|
|
|
$app->get('/test', $testController);
|
|
|
|
|
|
|
|
$request = $this->createRequest('GET', '/test');
|
|
|
|
$response = $app->handle($request);
|
|
|
|
|
|
|
|
$this->assertEquals('<p>Dave</p>', $response->getBody());
|
|
|
|
}
|
2024-05-23 01:13:05 +00:00
|
|
|
|
|
|
|
public function testHtmlInlineResponseCodes(): void
|
|
|
|
{
|
|
|
|
$testController = new class extends Controller {
|
|
|
|
public function respond(): Response
|
|
|
|
{
|
|
|
|
$response = $this->renderInline(
|
|
|
|
'<p>{{ name }}</p>',
|
|
|
|
[ 'name' => 'Oh no!' ]
|
|
|
|
);
|
|
|
|
|
|
|
|
return $response->withStatus(400);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$app = $this->getAppInstance();
|
|
|
|
$app->get('/test', $testController);
|
|
|
|
|
|
|
|
$request = $this->createRequest('GET', '/test');
|
|
|
|
$response = $app->handle($request);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$this->assertEquals('<p>Oh no!</p>', $response->getBody());
|
|
|
|
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
|
|
|
|
}
|
2024-05-23 01:16:47 +00:00
|
|
|
|
|
|
|
public function testJsonResponse(): void
|
|
|
|
{
|
|
|
|
$testController = new class extends Controller {
|
|
|
|
public function respond(): Response
|
|
|
|
{
|
|
|
|
return $this->json([ 'data' => 'hello' ]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$app = $this->getAppInstance();
|
|
|
|
$app->get('/test', $testController);
|
|
|
|
|
|
|
|
$request = $this->createRequest('GET', '/test');
|
|
|
|
$response = $app->handle($request);
|
|
|
|
|
|
|
|
$this->assertEquals('{"data":"hello"}', $response->getBody());
|
|
|
|
$this->assertEquals(
|
|
|
|
'application/json',
|
|
|
|
$response->getHeaderLine('Content-Type')
|
|
|
|
);
|
|
|
|
}
|
2024-05-23 00:39:33 +00:00
|
|
|
}
|