wishthis/tests/LogInTest.php

62 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2023-09-25 10:15:51 +00:00
<?php
declare(strict_types=1);
namespace wishthis\Tests;
use PHPUnit\Framework\TestCase;
use wishthis\{Database, User};
final class LogInTest extends TestCase
{
2023-11-24 14:37:50 +00:00
public static function setUpBeforeClass(): void
2023-09-25 10:15:51 +00:00
{
\define('DEFAULT_LOCALE', 'en_GB');
\define('ROOT', \dirname(__DIR__));
require __DIR__ . '/../src/classes/wishthis/User.php';
require __DIR__ . '/../src/classes/wishthis/Database.php';
require __DIR__ . '/../src/functions/gettext.php';
require __DIR__ . '/../src/config/config.php';
2023-11-24 14:37:50 +00:00
}
2023-09-25 10:15:51 +00:00
2023-11-24 14:37:50 +00:00
public function testLogInExistingUser(): void
{
2023-09-25 10:15:51 +00:00
$user = User::getCurrent();
$userEmpty = new User();
$userEmail = 'email@domain.tld';
$userPassword = '1234isnotarealpassword';
$this->assertEquals($user, $userEmpty);
$this->assertFalse($user->isLoggedIn());
$database = new Database(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$database->connect();
$database->query(
'REPLACE INTO `users`
(`email`, `password`) VALUES
(:userEmail, :userPassword)',
2024-05-29 15:08:43 +00:00
[
2023-09-25 10:15:51 +00:00
'userEmail' => $userEmail,
'userPassword' => $userPassword,
2024-05-29 15:08:43 +00:00
]
2023-09-25 10:15:51 +00:00
);
$userLoginSuccessful = $user->logIn($userEmail, $userPassword);
$this->assertTrue($userLoginSuccessful);
$this->assertEquals($userEmail, $_SESSION['user']->getEmail());
}
2023-11-24 14:37:50 +00:00
public function testLogInNonExistingUser(): void
{
$user = new User();
$userEmail = 'thisemail@shouldnt.exist';
$userPassword = '1234isnotarealpassword';
$userLoginSuccessful = $user->logIn($userEmail, $userPassword);
$this->assertFalse($userLoginSuccessful);
}
2023-09-25 10:15:51 +00:00
}