Fixes tests

main
Yasen Pramatarov 2026-01-20 23:38:49 +02:00
parent 138ea70185
commit a030562071
4 changed files with 38 additions and 4 deletions

View File

@ -1,11 +1,13 @@
<?php <?php
require_once dirname(__DIR__, 3) . '/app/core/App.php';
require_once dirname(__DIR__, 3) . '/app/classes/database.php'; require_once dirname(__DIR__, 3) . '/app/classes/database.php';
require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php'; require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php';
require_once dirname(__DIR__, 3) . '/app/classes/log.php'; require_once dirname(__DIR__, 3) . '/app/classes/log.php';
require_once dirname(__DIR__, 3) . '/app/includes/rate_limit_middleware.php'; require_once dirname(__DIR__, 3) . '/app/includes/rate_limit_middleware.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\App;
class RateLimitMiddlewareTest extends TestCase class RateLimitMiddlewareTest extends TestCase
{ {
@ -34,8 +36,11 @@ class RateLimitMiddlewareTest extends TestCase
'password' => $password 'password' => $password
]); ]);
// Set up App::db() for RateLimiter
App::set('db', $this->db->getConnection());
// Create rate limiter instance // Create rate limiter instance
$this->rateLimiter = new RateLimiter($this->db); $this->rateLimiter = new RateLimiter();
// Drop tables if they exist // Drop tables if they exist
$this->db->getConnection()->exec("DROP TABLE IF EXISTS security_rate_auth"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS security_rate_auth");
@ -119,6 +124,10 @@ class RateLimitMiddlewareTest extends TestCase
$this->db->getConnection()->exec("TRUNCATE TABLE security_ip_whitelist"); $this->db->getConnection()->exec("TRUNCATE TABLE security_ip_whitelist");
$this->db->getConnection()->exec("TRUNCATE TABLE security_rate_auth"); $this->db->getConnection()->exec("TRUNCATE TABLE security_rate_auth");
$this->db->getConnection()->exec("TRUNCATE TABLE log"); $this->db->getConnection()->exec("TRUNCATE TABLE log");
// Clean up App state
App::reset('db');
parent::tearDown(); parent::tearDown();
} }

View File

@ -1,10 +1,12 @@
<?php <?php
require_once dirname(__DIR__, 3) . '/app/core/App.php';
require_once dirname(__DIR__, 3) . '/app/classes/database.php'; require_once dirname(__DIR__, 3) . '/app/classes/database.php';
require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php'; require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php';
require_once dirname(__DIR__, 3) . '/app/classes/log.php'; require_once dirname(__DIR__, 3) . '/app/classes/log.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\App;
class RateLimiterTest extends TestCase class RateLimiterTest extends TestCase
{ {
@ -29,8 +31,11 @@ class RateLimiterTest extends TestCase
'password' => $password 'password' => $password
]); ]);
// Set up App::db() for RateLimiter
App::set('db', $this->db->getConnection());
// The RateLimiter constructor will create all necessary tables // The RateLimiter constructor will create all necessary tables
$this->rateLimiter = new RateLimiter($this->db); $this->rateLimiter = new RateLimiter();
} }
protected function tearDown(): void protected function tearDown(): void
@ -40,6 +45,10 @@ class RateLimiterTest extends TestCase
$this->db->getConnection()->exec("DROP TABLE IF EXISTS {$this->rateLimiter->pagesRatelimitTable}"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS {$this->rateLimiter->pagesRatelimitTable}");
$this->db->getConnection()->exec("DROP TABLE IF EXISTS {$this->rateLimiter->blacklistTable}"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS {$this->rateLimiter->blacklistTable}");
$this->db->getConnection()->exec("DROP TABLE IF EXISTS {$this->rateLimiter->whitelistTable}"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS {$this->rateLimiter->whitelistTable}");
// Clean up App state
App::reset('db');
parent::tearDown(); parent::tearDown();
} }

View File

@ -1,11 +1,13 @@
<?php <?php
require_once dirname(__DIR__, 3) . '/app/core/App.php';
require_once dirname(__DIR__, 3) . '/app/classes/database.php'; require_once dirname(__DIR__, 3) . '/app/classes/database.php';
require_once dirname(__DIR__, 3) . '/app/classes/user.php'; require_once dirname(__DIR__, 3) . '/app/classes/user.php';
require_once dirname(__DIR__, 3) . '/plugins/register/models/register.php'; require_once dirname(__DIR__, 3) . '/plugins/register/models/register.php';
require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php'; require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\App;
class UserRegisterTest extends TestCase class UserRegisterTest extends TestCase
{ {
@ -30,6 +32,9 @@ class UserRegisterTest extends TestCase
'password' => $password 'password' => $password
]); ]);
// Set up App::db() for Register class to use
App::set('db', $this->db->getConnection());
// Create user table with MariaDB syntax // Create user table with MariaDB syntax
$this->db->getConnection()->exec(" $this->db->getConnection()->exec("
CREATE TABLE IF NOT EXISTS user ( CREATE TABLE IF NOT EXISTS user (
@ -78,12 +83,15 @@ class UserRegisterTest extends TestCase
) )
"); ");
$this->register = new Register($this->db); $this->register = new Register();
$this->user = new User($this->db); $this->user = new User($this->db);
} }
protected function tearDown(): void protected function tearDown(): void
{ {
// Clean up App state
App::reset('db');
// Drop tables in correct order // Drop tables in correct order
$this->db->getConnection()->exec("DROP TABLE IF EXISTS user_2fa"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS user_2fa");
$this->db->getConnection()->exec("DROP TABLE IF EXISTS security_rate_auth"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS security_rate_auth");

View File

@ -1,11 +1,13 @@
<?php <?php
require_once dirname(__DIR__, 3) . '/app/core/App.php';
require_once dirname(__DIR__, 3) . '/app/classes/database.php'; require_once dirname(__DIR__, 3) . '/app/classes/database.php';
require_once dirname(__DIR__, 3) . '/app/classes/user.php'; require_once dirname(__DIR__, 3) . '/app/classes/user.php';
require_once dirname(__DIR__, 3) . '/plugins/register/models/register.php'; require_once dirname(__DIR__, 3) . '/plugins/register/models/register.php';
require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php'; require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\App;
class UserTest extends TestCase class UserTest extends TestCase
{ {
@ -30,6 +32,9 @@ class UserTest extends TestCase
'password' => $password 'password' => $password
]); ]);
// Set up App::db() for Register class to use
App::set('db', $this->db->getConnection());
// Create user table with MariaDB syntax // Create user table with MariaDB syntax
$this->db->getConnection()->exec(" $this->db->getConnection()->exec("
CREATE TABLE IF NOT EXISTS user ( CREATE TABLE IF NOT EXISTS user (
@ -79,11 +84,14 @@ class UserTest extends TestCase
"); ");
$this->user = new User($this->db); $this->user = new User($this->db);
$this->register = new Register($this->db); $this->register = new Register();
} }
protected function tearDown(): void protected function tearDown(): void
{ {
// Clean up App state
App::reset('db');
// Drop tables in correct order // Drop tables in correct order
$this->db->getConnection()->exec("DROP TABLE IF EXISTS user_2fa"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS user_2fa");
$this->db->getConnection()->exec("DROP TABLE IF EXISTS security_rate_auth"); $this->db->getConnection()->exec("DROP TABLE IF EXISTS security_rate_auth");