Skip to content

Commit 89ec565

Browse files
committed
feat: add back session flashing
1 parent 6a6fc88 commit 89ec565

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

packages/http/src/IsResponse.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use JsonSerializable;
99
use Tempest\Http\Cookie\Cookie;
1010
use Tempest\Http\Cookie\CookieManager;
11+
use Tempest\Http\Session\Session;
1112
use Tempest\View\View;
13+
use UnitEnum;
1214

1315
use function Tempest\get;
1416

@@ -26,6 +28,10 @@ trait IsResponse
2628
get => get(CookieManager::class);
2729
}
2830

31+
public Session $session {
32+
get => get(Session::class);
33+
}
34+
2935
private(set) ?View $view = null;
3036

3137
public function getHeader(string $name): ?Header
@@ -102,4 +108,11 @@ public function setBody(View|string|array|Generator|null $body): self
102108

103109
return $this;
104110
}
111+
112+
public function flash(string|UnitEnum $key, mixed $value): self
113+
{
114+
$this->session->flash($key, $value);
115+
116+
return $this;
117+
}
105118
}

packages/http/src/Response.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,70 @@
88
use JsonSerializable;
99
use Tempest\Http\Cookie\Cookie;
1010
use Tempest\View\View;
11+
use UnitEnum;
1112

1213
interface Response
1314
{
15+
/**
16+
* Gets the status code of the response.
17+
*/
1418
public Status $status {
1519
get;
1620
}
1721

18-
/** @var \Tempest\Http\Header[] $headers */
22+
/**
23+
* Gets the headers of the response.
24+
*
25+
* @var \Tempest\Http\Header[] $headers
26+
*/
1927
public array $headers {
2028
get;
2129
}
2230

31+
/**
32+
* Gets the body of the response.
33+
*/
2334
public View|string|array|Generator|JsonSerializable|null $body {
2435
get;
2536
}
2637

38+
/**
39+
* Gets a header by its name, case insensitive.
40+
*/
2741
public function getHeader(string $name): ?Header;
2842

43+
/**
44+
* Adds a header to the response.
45+
*/
2946
public function addHeader(string $key, string $value): self;
3047

48+
/**
49+
* Removes a header from the response.
50+
*/
3151
public function removeHeader(string $key): self;
3252

53+
/**
54+
* Adds a cookie to the response.
55+
*/
3356
public function addCookie(Cookie $cookie): self;
3457

58+
/**
59+
* Removes a cookie from the response.
60+
*/
3561
public function removeCookie(string $key): self;
3662

63+
/**
64+
* Sets the status code of the response.
65+
*/
3766
public function setStatus(Status $status): self;
3867

68+
/**
69+
* Sets the body of the response.
70+
*/
3971
public function setBody(View|string|array|Generator|null $body): self;
72+
73+
/**
74+
* Flash a value to the session for the next request.
75+
*/
76+
public function flash(string|UnitEnum $key, mixed $value): self;
4077
}

tests/Integration/Http/Responses/GenericResponseTest.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Tests\Tempest\Integration\Http\Responses;
66

7+
use PHPUnit\Framework\Attributes\Test;
78
use Tempest\Http\Cookie\Cookie;
89
use Tempest\Http\Cookie\CookieManager;
910
use Tempest\Http\Responses\Ok;
11+
use Tempest\Http\Session\Session;
1012
use Tempest\Http\Status;
1113
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1214

@@ -15,26 +17,18 @@
1517
*/
1618
final class GenericResponseTest extends FrameworkIntegrationTestCase
1719
{
18-
// public function test_sessions(): void
19-
// {
20-
// $response = new Ok()
21-
// ->addSession('test', 'test')
22-
// ->addSession('original', 'original');
23-
24-
// $session = $this->container->get(Session::class);
25-
26-
// $this->assertSame('test', $session->get('test'));
27-
28-
// $response->removeSession('test');
29-
30-
// $this->assertNull($session->get('test'));
20+
#[Test]
21+
public function sessions(): void
22+
{
23+
new Ok()->flash('success', 'Operation successful');
3124

32-
// $response->destroySession();
25+
$session = $this->container->get(Session::class);
3326

34-
// $this->assertNull($session->get('original'));
35-
// }
27+
$this->assertSame('Operation successful', $session->get('success'));
28+
}
3629

37-
public function test_cookies(): void
30+
#[Test]
31+
public function cookies(): void
3832
{
3933
$response = new Ok()->addCookie(new Cookie('test'));
4034

@@ -47,7 +41,8 @@ public function test_cookies(): void
4741
$this->assertSame(-1, $cookieManager->get('test')->expiresAt);
4842
}
4943

50-
public function test_set_status(): void
44+
#[Test]
45+
public function set_status(): void
5146
{
5247
$response = new Ok()->setStatus(Status::ACCEPTED);
5348

0 commit comments

Comments
 (0)