105 lines
3.2 KiB
PHP
105 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use ReflectionClass;
|
|
use Services\Auth;
|
|
use Services\DataBase\Classes\Profiler;
|
|
use System\Classes\Config;
|
|
use System\Classes\Controller;
|
|
use System\Classes\Session;
|
|
|
|
class AfterTestController extends Controller
|
|
{
|
|
protected bool $_csrf_protection = false;
|
|
|
|
public function pingAction(): string
|
|
{
|
|
return 'pong';
|
|
}
|
|
}
|
|
|
|
class ControllerAfterTest extends TestCase
|
|
{
|
|
private string $users_file;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
if (!function_exists('xdebug_get_headers')) {
|
|
$this->markTestSkipped('Нужен xdebug_get_headers() для проверки заголовков ответа');
|
|
}
|
|
|
|
header_remove();
|
|
|
|
$this->users_file = sys_get_temp_dir() . '/bicycle_after_users_' . uniqid() . '.php';
|
|
file_put_contents($this->users_file, '<?php return ' . var_export([
|
|
'admin' => ['password' => password_hash('x', PASSWORD_DEFAULT), 'role' => 'admin'],
|
|
'bob' => ['password' => password_hash('x', PASSWORD_DEFAULT), 'role' => 'manager'],
|
|
], true) . ';');
|
|
|
|
Config::set('auth', ['driver' => 'file', 'users_file' => $this->users_file, 'session_key' => 'auth_user']);
|
|
Profiler::reset();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
header_remove();
|
|
unlink($this->users_file);
|
|
Session::instance()->destroy();
|
|
Profiler::reset();
|
|
|
|
foreach ([Session::class, Auth::class] as $class) {
|
|
$ref = new ReflectionClass($class);
|
|
$prop = $ref->getProperty($class === Session::class ? 'instances' : '_instances');
|
|
$prop->setAccessible(true);
|
|
$prop->setValue(null, []);
|
|
}
|
|
|
|
$ref = new ReflectionClass(Config::class);
|
|
$prop = $ref->getProperty('config');
|
|
$prop->setAccessible(true);
|
|
$prop->setValue(null, null);
|
|
}
|
|
|
|
public function testNoXProfilerHeaderForGuest(): void
|
|
{
|
|
(new AfterTestController())->executeAction('pingAction');
|
|
|
|
$headers = implode("\n", xdebug_get_headers());
|
|
$this->assertStringNotContainsString('X-Profiler:', $headers);
|
|
}
|
|
|
|
public function testNoXProfilerHeaderForNonAdmin(): void
|
|
{
|
|
Session::instance()->set('auth_user', 'bob');
|
|
|
|
(new AfterTestController())->executeAction('pingAction');
|
|
|
|
$headers = implode("\n", xdebug_get_headers());
|
|
$this->assertStringNotContainsString('X-Profiler:', $headers);
|
|
}
|
|
|
|
public function testXProfilerHeaderForAdminContainsSqlSummary(): void
|
|
{
|
|
Session::instance()->set('auth_user', 'admin');
|
|
Profiler::log('SELECT * FROM budget', [], 12.34);
|
|
|
|
(new AfterTestController())->executeAction('pingAction');
|
|
|
|
$header = null;
|
|
foreach (xdebug_get_headers() as $h) {
|
|
if (str_starts_with($h, 'X-Profiler:')) {
|
|
$header = $h;
|
|
}
|
|
}
|
|
|
|
$this->assertNotNull($header);
|
|
|
|
$data = json_decode(substr($header, strlen('X-Profiler: ')), true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
$this->assertSame(1, $data['sql_count']);
|
|
$this->assertSame('SELECT * FROM budget', $data['sql'][0]['sql']);
|
|
}
|
|
}
|