Files
alrahma_sunday_school/tests/app/Controllers/View/RegisterControllerTest.php
T
root f83ebe66b9
Tests / PHPUnit (push) Failing after 43s
fix tests issues
2026-07-12 20:36:40 -04:00

102 lines
3.2 KiB
PHP

<?php
namespace Tests\App\Controllers\View {
use App\Controllers\View\RegisterController;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;
class TestableRegisterController extends RegisterController
{
public function __construct()
{
// Skip the heavy constructor that connects to the real database.
}
}
class RegisterControllerTest extends CIUnitTestCase
{
private TestableRegisterController $controller;
protected function setUp(): void
{
parent::setUp();
Services::resetSingle('session');
session()->start();
$this->controller = new TestableRegisterController();
}
protected function tearDown(): void
{
session()->remove('captcha_answer');
session()->remove('user_email');
parent::tearDown();
}
public function testIndexGeneratesCaptchaWhenMissing()
{
session()->remove('captcha_answer');
$response = $this->controller->index();
if (is_array($response)) {
$this->assertSame('user/register', $response['view']);
} else {
$this->assertIsString($response);
}
$captcha = session()->get('captcha_answer');
$this->assertNotEmpty($captcha);
$this->assertGreaterThanOrEqual(4, strlen($captcha));
$this->assertLessThanOrEqual(8, strlen($captcha));
if (is_array($response)) {
$this->assertSame($captcha, $response['data']['captchaQuestion']);
}
}
public function testIndexReusesExistingCaptcha()
{
session()->set('captcha_answer', 'EXISTING');
$response = $this->controller->index();
if (is_array($response)) {
$this->assertSame('user/register', $response['view']);
} else {
$this->assertIsString($response);
}
$this->assertSame('EXISTING', session()->get('captcha_answer'));
if (is_array($response)) {
$this->assertSame('EXISTING', $response['data']['captchaQuestion']);
}
}
public function testSuccessRedirectsWhenEmailMissing()
{
session()->remove('user_email');
$response = $this->controller->success();
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertStringContainsString('/register', $response->getHeaderLine('Location') ?: '');
}
public function testSuccessReturnsViewWhenEmailPresent()
{
session()->set('user_email', 'parent@example.com');
$response = $this->controller->success();
if (is_array($response)) {
$this->assertSame('success', $response['view']);
$this->assertSame('parent@example.com', $response['data']['email']);
} else {
$this->assertIsString($response);
}
$this->assertSame('parent@example.com', session()->get('user_email'));
}
}
}