From 94ad49396a314c9dcea58016221ac77fff6c506e Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Tue, 5 Nov 2024 22:05:25 -0800 Subject: [PATCH] removed htmx component & component test, added id to index table --- src/Controller/Component/HtmxComponent.php | 130 ----------- templates/bake/Template/index.twig | 2 +- .../Component/HtmxComponentTest.php | 201 ------------------ 3 files changed, 1 insertion(+), 332 deletions(-) delete mode 100644 src/Controller/Component/HtmxComponent.php delete mode 100644 tests/TestCase/Controller/Component/HtmxComponentTest.php diff --git a/src/Controller/Component/HtmxComponent.php b/src/Controller/Component/HtmxComponent.php deleted file mode 100644 index 6bd0074..0000000 --- a/src/Controller/Component/HtmxComponent.php +++ /dev/null @@ -1,130 +0,0 @@ - - */ - protected array $_defaultConfig = []; - - /** - * @var \Cake\Controller\Controller - */ - protected $controller; - - /** - * @param array $config - * @return void - */ - public function initialize(array $config): void { - parent::initialize($config); - - $this->controller = $this->getController(); - } - - /** - * Convenience method to check if request is from HTMX - * - * @return bool - */ - public function isHtmx() - { - return $this->controller->getRequest()->getHeaderLine('HX-Request') === 'true'; - } - - /** - * Convenience method to check if request is Boosted - * - * @return bool - */ - public function isBoosted() - { - return $this->controller->getRequest()->getHeaderLine('HX-Boosted') === 'true'; - } - - /** - * Get HTMX target id - * - * @return string|null - */ - public function getHtmxTarget() - { - $target = $this->controller->getRequest()->getHeaderLine('HX-Target'); - - return $target ?: null; - } - - /** - * Get HTMX trigger id - * - * @return string|null - */ - public function getHtmxTrigger() - { - $trigger = $this->controller->getRequest()->getHeaderLine('HX-Trigger'); - - return $trigger ?: null; - } - - /** - * Get HTMX trigger name - * - * @return string|null - */ - public function getHtmxTriggerName() - { - $trigger = $this->controller->getRequest()->getHeaderLine('HX-Trigger-Name'); - - return $trigger ?: null; - } - - /** - * Set headers to cache this request. - * Opposite of Controller::disableCache() - * - * @param array|string $redirectTo - * @param bool $full if client side redirect should be a full page reload or not - * - * @return void - */ - public function clientSideRedirect(array|string $redirectTo, bool $full = false): void - { - $response = $this->controller->getResponse(); - if (is_array($redirectTo)) { - $redirectTo = Router::url($redirectTo); - } - $header = $full ? 'HX-Redirect' : 'HX-Location'; - $response = $response->withHeader($header, $redirectTo); - - $this->controller->setResponse($response); - } - - /** - * Set headers to cache this request. - * Opposite of Controller::disableCache() - * - * @param array|string $redirectTo - * @param bool $full if client side redirect should be a full page reload or not - * - * @return void - */ - public function clientSideRefresh(): void - { - $response = $this->controller->getResponse(); - $response = $response->withHeader('HX-Refresh', 'true'); - - $this->controller->setResponse($response); - } -} diff --git a/templates/bake/Template/index.twig b/templates/bake/Template/index.twig index 8651c8f..86defd3 100644 --- a/templates/bake/Template/index.twig +++ b/templates/bake/Template/index.twig @@ -24,7 +24,7 @@ Html->link(__('New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?> {% set done = [] %}

-
+
diff --git a/tests/TestCase/Controller/Component/HtmxComponentTest.php b/tests/TestCase/Controller/Component/HtmxComponentTest.php deleted file mode 100644 index c121450..0000000 --- a/tests/TestCase/Controller/Component/HtmxComponentTest.php +++ /dev/null @@ -1,201 +0,0 @@ -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')); - } -}