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;
|
2024-06-05 01:16:59 +00:00
|
|
|
use Odan\Session\SessionInterface;
|
2024-05-23 00:39:33 +00:00
|
|
|
|
|
|
|
class ControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testHtmlInlineResponse(): void
|
|
|
|
{
|
|
|
|
$testController = new class extends Controller {
|
2024-06-17 01:25:22 +00:00
|
|
|
public function handle(): Response
|
2024-05-23 00:39:33 +00:00
|
|
|
{
|
|
|
|
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 {
|
2024-06-17 01:25:22 +00:00
|
|
|
public function handle(): Response
|
2024-05-23 01:13:05 +00:00
|
|
|
{
|
|
|
|
$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 {
|
2024-06-17 01:25:22 +00:00
|
|
|
public function handle(): Response
|
2024-05-23 01:16:47 +00:00
|
|
|
{
|
|
|
|
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 02:47:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We have some Global variables set in the Configuration, for now its
|
|
|
|
* hardcoded in the Bootstrap class.
|
|
|
|
*/
|
|
|
|
public function testRenderSiteName(): void
|
|
|
|
{
|
|
|
|
$testController = new class extends Controller {
|
2024-06-17 01:25:22 +00:00
|
|
|
public function handle(): Response
|
2024-05-23 02:47:20 +00:00
|
|
|
{
|
|
|
|
return $this->renderInline("<h1>{{ site_name }}</h1>");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$app = $this->getAppInstance();
|
|
|
|
$app->get('/test', $testController);
|
|
|
|
|
|
|
|
$request = $this->createRequest('GET', '/test');
|
|
|
|
$response = $app->handle($request);
|
|
|
|
|
|
|
|
$this->assertEquals('<h1>Slovocast</h1>', $response->getBody());
|
|
|
|
}
|
2024-06-05 01:16:59 +00:00
|
|
|
|
|
|
|
public function testFlashMessages(): void
|
|
|
|
{
|
|
|
|
$app = $this->getAppInstance();
|
|
|
|
$session = $app->getContainer()->get(SessionInterface::class);
|
|
|
|
|
|
|
|
$testController = new class($session) extends Controller {
|
|
|
|
protected SessionInterface $session;
|
|
|
|
|
|
|
|
public function __construct(SessionInterface $session)
|
|
|
|
{
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
2024-06-17 01:25:22 +00:00
|
|
|
public function handle(): Response
|
2024-06-05 01:16:59 +00:00
|
|
|
{
|
|
|
|
$this->session->getFlash()->add("error", "Error message");
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$app->get('/test', $testController);
|
|
|
|
$request = $this->createRequest('GET', '/test');
|
|
|
|
|
|
|
|
$response = $app->handle($request);
|
|
|
|
$flash = $app->getContainer()->get(SessionInterface::class)->getFlash();
|
|
|
|
$errorMessages = $flash->get('error');
|
|
|
|
$this->assertEquals('Error message', $errorMessages[0]);
|
|
|
|
$this->assertEquals(1, count($errorMessages));
|
|
|
|
}
|
2024-05-23 00:39:33 +00:00
|
|
|
}
|