202 lines
6.5 KiB
PHP
202 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace CheeseCake\Test\TestCase\Controller\Component;
|
|
|
|
use Cake\Controller\ComponentRegistry;
|
|
use Cake\Controller\Controller;
|
|
use Cake\Core\Configure;
|
|
use Cake\Event\Event;
|
|
use Cake\Http\ServerRequest;
|
|
use Cake\Routing\Route\DashedRoute;
|
|
use Cake\Routing\RouteBuilder;
|
|
use Cake\Routing\Router;
|
|
use Cake\TestSuite\TestCase;
|
|
use CheeseCake\Controller\Component\HtmxComponent;
|
|
|
|
class HtmxComponentTest extends TestCase {
|
|
|
|
/**
|
|
* @var RouteBuilder
|
|
*/
|
|
protected RouteBuilder $routeBuilder;
|
|
|
|
/**
|
|
* @var Controller
|
|
*/
|
|
protected $controller;
|
|
|
|
/**
|
|
* @var \Cake\Http\ServerRequest
|
|
*/
|
|
protected $request;
|
|
|
|
/**
|
|
* @var HtmxComponent
|
|
*/
|
|
protected $component;
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function setUp(): void {
|
|
parent::setUp();
|
|
|
|
parent::setUp();
|
|
// Setup our component and provide it a basic controller.
|
|
// If your component relies on Application features, use AppController.
|
|
$request = new ServerRequest();
|
|
$this->controller = new Controller($request);
|
|
$registry = new ComponentRegistry($this->controller);
|
|
|
|
$this->component = new HtmxComponent($registry);
|
|
|
|
$this->routeBuilder = Router::createRouteBuilder('/');
|
|
$this->routeBuilder->scope('/', function (RouteBuilder $routes) {
|
|
$routes->setRouteClass(DashedRoute::class);
|
|
$routes->get(
|
|
'/tests',
|
|
['controller' => 'Tests', 'action' => 'index']
|
|
);
|
|
// ...
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function tearDown(): void {
|
|
parent::tearDown();
|
|
|
|
unset($this->Controller);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testIsHtmx() {
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('HX-Request', 'true'));
|
|
$this->assertTrue($this->component->isHtmx());
|
|
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('hx-request', 'true'));
|
|
$this->assertTrue($this->component->isHtmx());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testIsBoosted() {
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('HX-Boosted', 'true'));
|
|
$this->assertTrue($this->component->isBoosted());
|
|
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('hx-boosted', 'true'));
|
|
$this->assertTrue($this->component->isBoosted());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testGetHtmxTarget() {
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('HX-Target', 'test'));
|
|
$this->assertEquals('test', $this->component->getHtmxTarget());
|
|
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('hx-target', 'TEST'));
|
|
$this->assertEquals('TEST', $this->component->getHtmxTarget());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testGetHtmxTrigger() {
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('HX-Trigger', 'test'));
|
|
$this->assertEquals('test', $this->component->getHtmxTrigger());
|
|
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('hx-trigger', 'TEST'));
|
|
$this->assertEquals('TEST', $this->component->getHtmxTrigger());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testGetHtmxTriggerName() {
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('HX-Trigger-Name', 'test'));
|
|
$this->assertEquals('test', $this->component->getHtmxTriggerName());
|
|
|
|
$this->controller->setRequest($this->controller->getRequest()->withHeader('hx-trigger-name', 'TEST'));
|
|
$this->assertEquals('TEST', $this->component->getHtmxTriggerName());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testClientSideRedirectString()
|
|
{
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Location'));
|
|
$this->component->clientSideRedirect('/');
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('/', $response->getHeaderLine('HX-Location'));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testClientSideRedirectStringFull()
|
|
{
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Location'));
|
|
$this->component->clientSideRedirect('/', true);
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('/', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Location'));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testClientSideRedirectArray()
|
|
{
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Location'));
|
|
$this->component->clientSideRedirect([
|
|
'controller' => 'Tests',
|
|
'action' => 'index',
|
|
]);
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('/tests', $response->getHeaderLine('HX-Location'));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testClientSideRedirectArrayFull()
|
|
{
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Location'));
|
|
$this->component->clientSideRedirect([
|
|
'controller' => 'Tests',
|
|
'action' => 'index',
|
|
], true);
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('/tests', $response->getHeaderLine('HX-Redirect'));
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Location'));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testClientSideRefresh()
|
|
{
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('', $response->getHeaderLine('HX-Refresh'));
|
|
$this->component->clientSideRefresh();
|
|
$response = $this->controller->getResponse();
|
|
$this->assertEquals('true', $response->getHeaderLine('HX-Refresh'));
|
|
}
|
|
}
|