fedi-feed-router/backend/tests/Unit/Http/Middleware/HandleInertiaRequestsTest.php

116 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2025-08-15 22:09:55 +02:00
<?php
namespace Tests\Unit\Http\Middleware;
use App\Http\Middleware\HandleInertiaRequests;
use Illuminate\Http\Request;
use Tests\TestCase;
class HandleInertiaRequestsTest extends TestCase
{
protected HandleInertiaRequests $middleware;
protected function setUp(): void
{
parent::setUp();
$this->middleware = new HandleInertiaRequests();
}
public function test_root_view_is_set_to_app(): void
{
$reflection = new \ReflectionClass($this->middleware);
$property = $reflection->getProperty('rootView');
$property->setAccessible(true);
$this->assertEquals('app', $property->getValue($this->middleware));
}
public function test_version_calls_parent_version(): void
{
$request = Request::create('/test');
// Since this method calls parent::version(), we're testing that it doesn't throw an exception
// and returns the expected type (string or null)
$version = $this->middleware->version($request);
$this->assertTrue(is_string($version) || is_null($version));
}
public function test_share_returns_array_with_parent_data(): void
{
$request = Request::create('/test');
$shared = $this->middleware->share($request);
$this->assertIsArray($shared);
// Since we're merging with parent::share(), we expect at least some basic Inertia data
// The exact keys depend on the parent implementation, but it should be an array
}
public function test_share_merges_parent_data_correctly(): void
{
$request = Request::create('/test');
$shared = $this->middleware->share($request);
// Test that the method returns an array and doesn't throw exceptions
$this->assertIsArray($shared);
// Since the implementation currently only returns parent data merged with empty array,
// we can test that the structure is maintained
$this->assertNotNull($shared);
}
public function test_middleware_can_be_instantiated(): void
{
$this->assertInstanceOf(HandleInertiaRequests::class, $this->middleware);
$this->assertInstanceOf(\Inertia\Middleware::class, $this->middleware);
}
public function test_share_method_is_callable(): void
{
$request = Request::create('/test');
$this->assertTrue(method_exists($this->middleware, 'share'));
$this->assertTrue(is_callable([$this->middleware, 'share']));
// Ensure the method can be called without throwing exceptions
$result = $this->middleware->share($request);
$this->assertIsArray($result);
}
public function test_version_method_is_callable(): void
{
$request = Request::create('/test');
$this->assertTrue(method_exists($this->middleware, 'version'));
$this->assertTrue(is_callable([$this->middleware, 'version']));
// Ensure the method can be called without throwing exceptions
$result = $this->middleware->version($request);
$this->assertTrue(is_string($result) || is_null($result));
}
public function test_share_with_different_request_methods(): void
{
$getMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
foreach ($getMethods as $method) {
$request = Request::create('/test', $method);
$shared = $this->middleware->share($request);
$this->assertIsArray($shared, "Failed for {$method} method");
}
}
public function test_share_with_request_containing_data(): void
{
$request = Request::create('/test', 'POST', ['key' => 'value']);
$request->headers->set('X-Custom-Header', 'test-value');
$shared = $this->middleware->share($request);
$this->assertIsArray($shared);
// The middleware should handle requests with data without issues
}
}