83 lines
2.6 KiB
PHP
83 lines
2.6 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\ProfilerToolbar;
|
|
use System\Classes\Session;
|
|
|
|
class ProfilerToolbarTest extends TestCase
|
|
{
|
|
private string $users_file;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->users_file = sys_get_temp_dir() . '/bicycle_toolbar_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
|
|
{
|
|
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 testReturnsEmptyStringForGuest(): void
|
|
{
|
|
$this->assertSame('', ProfilerToolbar::render());
|
|
}
|
|
|
|
public function testReturnsEmptyStringForNonAdminRole(): void
|
|
{
|
|
Session::instance()->set('auth_user', 'bob');
|
|
|
|
$this->assertSame('', ProfilerToolbar::render());
|
|
}
|
|
|
|
public function testRendersPanelForAdmin(): void
|
|
{
|
|
Session::instance()->set('auth_user', 'admin');
|
|
|
|
$html = ProfilerToolbar::render();
|
|
|
|
$this->assertStringContainsString('Profiler', $html);
|
|
$this->assertStringContainsString('SQL', $html);
|
|
}
|
|
|
|
public function testIncludesProfilerEntries(): void
|
|
{
|
|
Session::instance()->set('auth_user', 'admin');
|
|
Profiler::log('SELECT * FROM users WHERE id = ?', [42], 1.23);
|
|
|
|
$html = ProfilerToolbar::render();
|
|
|
|
$this->assertStringContainsString('SELECT * FROM users WHERE id = ?', $html);
|
|
$this->assertStringContainsString('42', $html);
|
|
}
|
|
}
|