<?php
namespace Jaxon\Tests\TestResponse;
use Jaxon\Jaxon;
use Jaxon\Exception\RequestException;
use Jaxon\Exception\SetupException;
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ServerRequestInterface;
use PHPUnit\Framework\TestCase;
class PageDomTest extends TestCase
{
/**
* @throws SetupException
*/
public function setUp(): void
{
jaxon()->setOption('core.prefix.class', '');
jaxon()->register(Jaxon::CALLABLE_DIR, __DIR__ . '/../src/response');
}
/**
* @throws SetupException
*/
public function tearDown(): void
{
jaxon()->reset();
parent::tearDown();
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandAssign()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'assign',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(2, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandHtml()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'html',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandAppend()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'append',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandPrepend()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'prepend',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandReplace()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'replace',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandClear()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'clear',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandRemove()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'remove',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
/**
* @throws SetupException
* @throws RequestException
*/
public function testCommandInsertBefore()
{
// Send a request to the registered class
jaxon()->di()->set(ServerRequestInterface::class, function($c) {
return $c->g(ServerRequestCreator::class)
->fromGlobals()
->withParsedBody([
'jxncall' => json_encode([
'type' => 'class',
'name' => 'TestDom',
'method' => 'insertBefore',
'args' => [],
]),
])
->withMethod('POST');
});
// Process the request and get the response
$this->assertTrue(jaxon()->canProcessRequest());
jaxon()->di()->getRequestHandler()->processRequest();
$xResponse = jaxon()->getResponse();
$this->assertEquals(1, $xResponse->getCommandCount());
}
}
|