Files
zemailnator/tests/Feature/Controllers/AppControllerTest.php
idevakk 3706072ce5 fix: resolve PSR-4 autoloading and test failures
- Add proper Tests\ namespace to all test classes in tests/Feature and tests/Unit
  - Split RemainingModelsTest.php into separate files (PSR-4 compliance)
  - Create missing factories: MetaFactory, RemoteEmailFactory
  - Add HasFactory trait to RemoteEmail model
  - Add missing ReflectionClass imports to test files
  - Fix mass assignment issues in Meta and RemoteEmail models
  - Override database connection for RemoteEmail in testing environment
  - Fix DateTime comparison precision issues in tests
2025-11-13 09:49:21 -08:00

167 lines
4.2 KiB
PHP

<?php
namespace Tests\Feature\Controllers;
use App\Http\Controllers\AppController;
use Illuminate\Support\Facades\Config;
use ReflectionClass;
use Tests\TestCase;
class AppControllerTest extends TestCase
{
/** @test */
public function it_redirects_to_home_when_no_email_exists_in_mailbox()
{
$response = $this->get('/mailbox');
$response->assertRedirect('/');
}
/** @test */
public function it_creates_custom_email_from_url_when_enabled()
{
$email = 'custom@example.com';
$response = $this->get("/mailbox/{$email}");
$response->assertRedirect('/mailbox');
}
/** @test */
public function it_validates_email_parameter_in_mailbox_route()
{
$response = $this->get('/mailbox/invalid-email');
$response->assertStatus(302); // Validation redirects back
$response->assertSessionHasErrors();
}
/** @test */
public function it_redirects_to_home_when_mailbox_slug_is_disabled()
{
Config::set('app.settings.configuration_settings', json_encode([
'disable_mailbox_slug' => true,
]));
$response = $this->get('/mailbox');
$response->assertRedirect('/');
}
/** @test */
public function it_switches_email_successfully()
{
$email = 'newemail@example.com';
$response = $this->get("/switch/{$email}");
$response->assertRedirect('/mailbox');
}
/** @test */
public function it_redirects_to_home_when_switching_email_with_disabled_mailbox_slug()
{
Config::set('app.settings.configuration_settings', json_encode([
'disable_mailbox_slug' => true,
]));
$email = 'newemail@example.com';
$response = $this->get("/switch/{$email}");
$response->assertRedirect('/');
}
/** @test */
public function it_deletes_email_successfully()
{
$email = 'delete@example.com';
$response = $this->get("/delete/{$email}");
$response->assertRedirect('/mailbox');
}
/** @test */
public function it_redirects_to_home_when_deleting_email_without_parameter()
{
$response = $this->get('/delete');
$response->assertRedirect('/');
}
/** @test */
public function it_switches_locale_successfully()
{
$locale = 'es';
$response = $this->get("/locale/{$locale}");
$response->assertRedirect();
$this->assertEquals($locale, session('locale'));
}
/** @test */
public function it_aborts_with_400_for_invalid_locale()
{
$invalidLocale = 'invalid';
$response = $this->get("/locale/{$invalidLocale}");
$response->assertStatus(400);
}
/** @test */
public function it_redirects_back_after_locale_switch()
{
$locale = 'fr';
$response = $this->get("/locale/{$locale}");
$response->assertRedirect();
$this->assertEquals($locale, session('locale'));
}
/** @test */
public function it_handles_get_string_between_method_correctly()
{
$controller = new AppController;
$reflection = new ReflectionClass($controller);
$method = $reflection->getMethod('getStringBetween');
$method->setAccessible(true);
$string = 'Hello [world] test';
$result = $method->invoke($controller, $string, '[', ']');
$this->assertEquals('world', $result);
}
/** @test */
public function it_handles_get_string_between_with_missing_end()
{
$controller = new AppController;
$reflection = new ReflectionClass($controller);
$method = $reflection->getMethod('getStringBetween');
$method->setAccessible(true);
$string = 'Hello [world test';
$result = $method->invoke($controller, $string, '[', ']');
$this->assertEquals('wo', $result);
}
/** @test */
public function it_handles_get_string_between_with_no_match()
{
$controller = new AppController;
$reflection = new ReflectionClass($controller);
$method = $reflection->getMethod('getStringBetween');
$method->setAccessible(true);
$string = 'Hello world test';
$result = $method->invoke($controller, $string, '[', ']');
$this->assertEquals('', $result);
}
}