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')); } }