Core MVC, HTTP client, Session, Cookie, Config, HTTPException — 111 PHPUnit tests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
213 lines
6.1 KiB
PHP
213 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use System\Classes\Request;
|
|
|
|
class RequestTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Request::$current = null;
|
|
Request::$initial = null;
|
|
unset($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'], $_SERVER['HTTP_X_REQUESTED_WITH']);
|
|
$_GET = [];
|
|
$_POST = [];
|
|
}
|
|
|
|
public function testMethodGet(): void
|
|
{
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$req = Request::factory();
|
|
$this->assertSame('GET', $req->method());
|
|
}
|
|
|
|
public function testMethodPost(): void
|
|
{
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$req = Request::factory();
|
|
$this->assertSame('POST', $req->method());
|
|
}
|
|
|
|
public function testMethodDefaultsToGet(): void
|
|
{
|
|
unset($_SERVER['REQUEST_METHOD']);
|
|
$req = Request::factory();
|
|
$this->assertSame('GET', $req->method());
|
|
}
|
|
|
|
public function testMethodNormalizesToUppercase(): void
|
|
{
|
|
$_SERVER['REQUEST_METHOD'] = 'delete';
|
|
$req = Request::factory();
|
|
$this->assertSame('DELETE', $req->method());
|
|
}
|
|
|
|
public function testFactoryParsesRootUri(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$this->assertSame('Index', $req->controller());
|
|
$this->assertSame('index', $req->action());
|
|
$this->assertSame([], $req->params());
|
|
}
|
|
|
|
public function testFactoryParsesControllerAndAction(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/index/about';
|
|
$req = Request::factory();
|
|
$this->assertSame('Index', $req->controller());
|
|
$this->assertSame('about', $req->action());
|
|
}
|
|
|
|
public function testFactoryParsesParams(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/index/show/hello/world';
|
|
$req = Request::factory();
|
|
$this->assertSame(['hello', 'world'], $req->params());
|
|
}
|
|
|
|
public function testParamByIndex(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/index/show/42/extra';
|
|
$req = Request::factory();
|
|
$this->assertSame('42', $req->param(0));
|
|
$this->assertSame('extra', $req->param(1));
|
|
}
|
|
|
|
public function testParamDefaultValue(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$this->assertNull($req->param(0));
|
|
$this->assertSame('fallback', $req->param(0, 'fallback'));
|
|
}
|
|
|
|
public function testFactorySetsCurrentRequest(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$this->assertSame($req, Request::$current);
|
|
}
|
|
|
|
public function testUriGetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/index/test';
|
|
$req = Request::factory();
|
|
$this->assertSame('index/test', $req->uri());
|
|
}
|
|
|
|
public function testUriQueryStringIsStripped(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/index/test?foo=bar&baz=1';
|
|
$req = Request::factory();
|
|
$this->assertSame('index/test', $req->uri());
|
|
}
|
|
|
|
public function testInitialIsSetOnFirstCall(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$this->assertSame($req, Request::$initial);
|
|
}
|
|
|
|
public function testPostArraySetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$req->post(['name' => 'Ivan', 'age' => '30']);
|
|
$this->assertSame(['name' => 'Ivan', 'age' => '30'], $req->post());
|
|
}
|
|
|
|
public function testPostKeyGetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$req->post(['name' => 'Ivan']);
|
|
$this->assertSame('Ivan', $req->post('name'));
|
|
$this->assertNull($req->post('missing'));
|
|
}
|
|
|
|
public function testPostKeySetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$req->post('key', 'value');
|
|
$this->assertSame('value', $req->post('key'));
|
|
}
|
|
|
|
public function testQueryArraySetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$req->query(['page' => '2', 'sort' => 'name']);
|
|
$this->assertSame(['page' => '2', 'sort' => 'name'], $req->query());
|
|
}
|
|
|
|
public function testQueryKeyGetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$req->query(['page' => '3']);
|
|
$this->assertSame('3', $req->query('page'));
|
|
$this->assertNull($req->query('missing'));
|
|
}
|
|
|
|
public function testBodyGetterSetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$this->assertSame('', $req->body());
|
|
$req->body('{"key":"val"}');
|
|
$this->assertSame('{"key":"val"}', $req->body());
|
|
}
|
|
|
|
public function testIsAjaxFalseByDefault(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$this->assertFalse($req->is_ajax());
|
|
}
|
|
|
|
public function testIsAjaxTrueWhenHeaderSet(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
|
|
$req = Request::factory();
|
|
$this->assertTrue($req->is_ajax());
|
|
}
|
|
|
|
public function testMethodSetter(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$req = Request::factory();
|
|
$same = $req->method('patch');
|
|
$this->assertSame($req, $same);
|
|
$this->assertSame('PATCH', $req->method());
|
|
}
|
|
|
|
public function testFactoryPopulatesGetFromGlobals(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$_GET = ['foo' => 'bar'];
|
|
$req = Request::factory();
|
|
$this->assertSame('bar', $req->query('foo'));
|
|
}
|
|
|
|
public function testFactoryPopulatesPostFromGlobals(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_POST = ['field' => 'hello'];
|
|
$req = Request::factory();
|
|
$this->assertSame('hello', $req->post('field'));
|
|
}
|
|
}
|