-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDispatchBaseUrlRequestTest.php
100 lines (78 loc) · 2.97 KB
/
DispatchBaseUrlRequestTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Tests\Feature;
use Database\Factories\UserFactory;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Request;
use Illuminate\Testing\TestResponse;
use Inertia\Testing\AssertableInertia;
use InertiaUI\Modal\DispatchBaseUrlRequest;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class DispatchBaseUrlRequestTest extends TestCase
{
protected DispatchBaseUrlRequest $dispatcher;
protected function setUp(): void
{
parent::setUp();
$this->dispatcher = new DispatchBaseUrlRequest($this->app['router']);
}
#[Test]
public function it_creates_new_request_with_original_properties()
{
UserFactory::new()->count(3)->create();
$originalRequest = Request::create('/users', 'GET', ['query' => 'param']);
$originalRequest->headers->set('X-Test-Header', 'test-header-value');
$originalRequest->setLocale('en');
$baseUrl = '/roles/create';
$response = ($this->dispatcher)($originalRequest, $baseUrl);
$testResponse = new TestResponse($response);
$testResponse->assertInertia(fn (AssertableInertia $page) => $page
->component('CreateRole')
->url(route('roles.create', ['query' => 'param'], false))
->where('headerValue', 'test-header-value')
);
}
#[Test]
public function it_preserves_request_data()
{
$originalRequest = Request::create(
'/users', 'POST', [], [], [], [], json_encode(['name' => 'new-role'])
);
$originalRequest->headers->set('Content-Type', 'application/json');
$baseUrl = '/roles';
($this->dispatcher)($originalRequest, $baseUrl);
$this->assertEquals('Role created successfully!', session('message'));
}
#[Test]
public function it_handles_responsable_objects()
{
$originalRequest = Request::create('/create/users', 'GET');
$baseUrl = '/test/test';
$this->app['router']->get('/test/test', function () {
return new class implements Responsable
{
public function toResponse($request)
{
return response('Responsable Response');
}
};
});
$response = ($this->dispatcher)($originalRequest, $baseUrl);
$this->assertEquals('Responsable Response', $response->getContent());
}
#[Test]
public function it_disables_cookie_encryption_on_base_url()
{
$originalRequest = Request::create('/users', 'GET');
$baseUrl = '/roles';
$response = ($this->dispatcher)($originalRequest, $baseUrl);
$cookies = $response->headers->getCookies();
$this->assertNotEmpty($cookies);
foreach ($cookies as $cookie) {
$value = $cookie->getValue();
$this->assertEquals(40, strlen($value));
$decrypted = rescue(fn () => decrypt($value, false), report: false);
$this->assertNull($decrypted);
}
}
}