54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Tests\Http;
|
||
|
|
|
||
|
|
use MintyPHP\Http\RequestContext;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
|
||
|
|
class RequestContextTest extends TestCase
|
||
|
|
{
|
||
|
|
private array $serverBackup = [];
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
$this->serverBackup = $_SERVER;
|
||
|
|
RequestContext::resetForTests();
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function tearDown(): void
|
||
|
|
{
|
||
|
|
$_SERVER = $this->serverBackup;
|
||
|
|
RequestContext::resetForTests();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testItGeneratesStableRequestIdPerRequest(): void
|
||
|
|
{
|
||
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||
|
|
$_SERVER['REQUEST_URI'] = '/admin/users?x=1';
|
||
|
|
|
||
|
|
RequestContext::start();
|
||
|
|
$first = RequestContext::id();
|
||
|
|
$second = RequestContext::id();
|
||
|
|
|
||
|
|
$this->assertSame($first, $second);
|
||
|
|
$this->assertMatchesRegularExpression('/^[a-f0-9-]{36}$/', $first);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testItAppliesOverridesForChannelAndPath(): void
|
||
|
|
{
|
||
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||
|
|
$_SERVER['REQUEST_URI'] = '/admin/settings';
|
||
|
|
|
||
|
|
RequestContext::start([
|
||
|
|
'channel' => 'scheduler',
|
||
|
|
'path' => '/bin/scheduler-run.php',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$context = RequestContext::context();
|
||
|
|
|
||
|
|
$this->assertSame('scheduler', $context['channel']);
|
||
|
|
$this->assertSame('/bin/scheduler-run.php', $context['path']);
|
||
|
|
$this->assertSame('POST', $context['method']);
|
||
|
|
}
|
||
|
|
}
|