laravel-websockets/tests/Unit/ControllerResolverTest.php

101 lines
3.0 KiB
PHP
Raw Normal View History

2026-01-24 13:42:35 +00:00
<?php
namespace BlaxSoftware\LaravelWebSockets\Tests\Unit;
use BlaxSoftware\LaravelWebSockets\Websocket\ControllerResolver;
use PHPUnit\Framework\TestCase;
class ControllerResolverTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
ControllerResolver::clearCache();
}
/** @test */
public function it_resolves_simple_controller_names()
{
// The package has a PusherController in vendor namespace
$result = ControllerResolver::resolve('pusher');
$this->assertNotNull($result);
$this->assertStringContainsString('PusherController', $result);
}
/** @test */
public function it_caches_resolved_controllers()
{
2026-01-24 13:49:51 +00:00
// First call resolves and caches
2026-01-24 13:42:35 +00:00
ControllerResolver::resolve('pusher');
$stats = ControllerResolver::getStats();
2026-01-24 13:49:51 +00:00
// scanned may or may not be true (we no longer auto-scan on resolve)
// but cached should be > 0
2026-01-24 13:42:35 +00:00
$this->assertGreaterThan(0, $stats['cached']);
}
/** @test */
public function it_caches_null_for_nonexistent_controllers()
{
// First call - not found
$result1 = ControllerResolver::resolve('nonexistent-controller-xyz');
$this->assertNull($result1);
// Second call - should still be null (cached)
$result2 = ControllerResolver::resolve('nonexistent-controller-xyz');
$this->assertNull($result2);
// Check it's in cache
$stats = ControllerResolver::getStats();
$this->assertGreaterThan(0, $stats['cached']);
}
/** @test */
public function it_converts_kebab_case_to_pascal_case()
{
// admin-user should try AdminUserController
// This tests the internal conversion (we can't directly test private method,
// but we can test the resolve behavior)
$stats = ControllerResolver::getStats();
$this->assertIsArray($stats);
}
/** @test */
public function it_clears_cache_correctly()
{
2026-01-24 13:49:51 +00:00
// Preload to ensure scanned is true
ControllerResolver::preload();
2026-01-24 13:42:35 +00:00
$statsBefore = ControllerResolver::getStats();
$this->assertTrue($statsBefore['scanned']);
// Clear cache
ControllerResolver::clearCache();
$statsAfter = ControllerResolver::getStats();
$this->assertFalse($statsAfter['scanned']);
$this->assertEquals(0, $statsAfter['cached']);
$this->assertEquals(0, $statsAfter['available']);
}
/** @test */
public function it_preloads_controllers()
{
ControllerResolver::preload();
$stats = ControllerResolver::getStats();
$this->assertTrue($stats['scanned']);
$this->assertGreaterThan(0, $stats['available']);
}
/** @test */
public function it_returns_same_result_on_repeated_calls()
{
$result1 = ControllerResolver::resolve('pusher');
$result2 = ControllerResolver::resolve('pusher');
$this->assertSame($result1, $result2);
}
}