• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 14024978163

23 Mar 2025 05:55PM UTC coverage: 79.391% (-0.05%) from 79.441%
14024978163

push

github

web-flow
feat(view): cache Blade and Twig templates in internal storage (#1061)

2 of 2 new or added lines in 2 files covered. (100.0%)

912 existing lines in 110 files now uncovered.

10478 of 13198 relevant lines covered (79.39%)

91.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/src/Tempest/Framework/Testing/Http/TestResponseHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Framework\Testing\Http;
6

7
use Closure;
8
use Generator;
9
use PHPUnit\Framework\Assert;
10
use Tempest\Http\Status;
11
use Tempest\Router\Cookie\CookieManager;
12
use Tempest\Router\Response;
13
use Tempest\Router\Session\Session;
14
use Tempest\Validation\Rule;
15
use Tempest\View\View;
16
use Tempest\View\ViewRenderer;
17

18
use function Tempest\get;
19
use function Tempest\Support\arr;
20

21
final class TestResponseHelper
22
{
23
    public function __construct(
54✔
24
        private(set) Response $response,
25
    ) {
26
    }
54✔
27

28
    public Status $status {
29
        get => $this->response->status;
30
    }
31

32
    /** @var \Tempest\Router\Header[] */
33
    public array $headers {
34
        get => $this->response->headers;
35
    }
36

37
    public View|string|array|Generator|null $body {
38
        get => $this->response->body;
39
    }
40

41
    public function assertHasHeader(string $name): self
13✔
42
    {
43
        Assert::assertArrayHasKey(
13✔
44
            $name,
13✔
45
            $this->response->headers,
13✔
46
            sprintf('Failed to assert that response contains header [%s].', $name),
13✔
47
        );
13✔
48

49
        return $this;
10✔
50
    }
51

52
    public function assertHeaderContains(string $name, mixed $value): self
8✔
53
    {
54
        $this->assertHasHeader($name);
8✔
55

56
        $header = $this->response->getHeader($name);
7✔
57

58
        $headerString = var_export($header, true); // @mago-expect best-practices/no-debug-symbols
7✔
59

60
        Assert::assertContains(
7✔
61
            $value,
7✔
62
            $header->values,
7✔
63
            sprintf('Failed to assert that response header [%s] value contains %s. These header values were found: %s', $name, $value, $headerString),
7✔
64
        );
7✔
65

66
        return $this;
7✔
67
    }
68

69
    public function assertRedirect(?string $to = null): self
6✔
70
    {
71
        Assert::assertTrue(
6✔
72
            $this->status->isRedirect(),
6✔
73
            sprintf('Failed asserting that status [%s] is a redirect.', $this->status->value),
6✔
74
        );
6✔
75

76
        return $to === null
5✔
77
            ? $this->assertHasHeader('Location')
2✔
78
            : $this->assertHeaderContains('Location', $to);
4✔
79
    }
80

81
    public function assertOk(): self
32✔
82
    {
83
        return $this->assertStatus(Status::OK);
32✔
84
    }
85

86
    public function assertForbidden(): self
1✔
87
    {
88
        return $this->assertStatus(Status::FORBIDDEN);
1✔
89
    }
90

91
    public function assertNotFound(): self
3✔
92
    {
93
        return $this->assertStatus(Status::NOT_FOUND);
3✔
94
    }
95

96
    public function assertStatus(Status $expected): self
43✔
97
    {
98
        Assert::assertSame(
43✔
99
            expected: $expected,
43✔
100
            actual: $this->status,
43✔
101
            message: sprintf(
43✔
102
                'Failed asserting status [%s] matched expected status of [%s].',
43✔
103
                $expected->value,
43✔
104
                $this->status->value,
43✔
105
            ),
43✔
106
        );
43✔
107

108
        return $this;
29✔
109
    }
110

UNCOV
111
    public function assertHasCookie(string $key, ?Closure $test = null): self
×
112
    {
UNCOV
113
        $cookies = get(CookieManager::class);
×
114

UNCOV
115
        $cookie = $cookies->get($key);
×
116

UNCOV
117
        Assert::assertNotNull($cookie);
×
118

UNCOV
119
        if ($test !== null) {
×
120
            $test($cookie);
×
121
        }
122

123
        return $this;
×
124
    }
125

126
    public function assertHasSession(string $key, ?Closure $test = null): self
1✔
127
    {
128
        /** @var Session $session */
129
        $session = get(Session::class);
1✔
130

131
        $data = $session->get($key);
1✔
132

133
        Assert::assertNotNull(
1✔
134
            $data,
1✔
135
            sprintf(
1✔
136
                'No session value was set for %s, available session keys: %s',
1✔
137
                $key,
1✔
138
                implode(', ', array_keys($session->data)),
1✔
139
            ),
1✔
140
        );
1✔
141

142
        if ($test !== null) {
1✔
143
            $test($session, $data);
1✔
144
        }
145

146
        return $this;
1✔
147
    }
148

149
    public function assertHasValidationError(string $key, ?Closure $test = null): self
2✔
150
    {
151
        /** @var Session $session */
152
        $session = get(Session::class);
2✔
153

154
        $validationErrors = $session->get(Session::VALIDATION_ERRORS) ?? [];
2✔
155

156
        Assert::assertArrayHasKey(
2✔
157
            $key,
2✔
158
            $validationErrors,
2✔
159
            sprintf(
2✔
160
                'No validation error was set for %s, available validation errors: %s',
2✔
161
                $key,
2✔
162
                implode(', ', array_keys($validationErrors)),
2✔
163
            ),
2✔
164
        );
2✔
165

166
        if ($test !== null) {
2✔
UNCOV
167
            $test($validationErrors);
×
168
        }
169

170
        return $this;
2✔
171
    }
172

173
    public function assertHasNoValidationsErrors(): self
2✔
174
    {
175
        /** @var Session $session */
176
        $session = get(Session::class);
2✔
177

178
        $validationErrors = $session->get(Session::VALIDATION_ERRORS) ?? [];
2✔
179

180
        Assert::assertEmpty(
2✔
181
            $validationErrors,
2✔
182
            sprintf(
2✔
183
                'There should be no validation errors, but there were: %s',
2✔
184
                arr($validationErrors)
2✔
185
                    ->map(function (array $failingRules, $key) {
2✔
186
                        $failingRules = arr($failingRules)->map(fn (Rule $rule) => $rule->message())->implode(', ');
×
187

188
                        return $key . ': ' . $failingRules;
×
189
                    })
2✔
190
                    ->implode(', '),
2✔
191
            ),
2✔
192
        );
2✔
193

194
        return $this;
2✔
195
    }
196

197
    public function assertSee(string $search): self
1✔
198
    {
199
        $body = $this->body;
1✔
200

201
        if ($body instanceof View) {
1✔
202
            $body = get(ViewRenderer::class)->render($body);
1✔
203
        }
204

205
        Assert::assertStringContainsString($search, $body);
1✔
206

207
        return $this;
1✔
208
    }
209

UNCOV
210
    public function assertNotSee(string $search): self
×
211
    {
212
        $body = $this->body;
×
213

UNCOV
214
        if ($body instanceof View) {
×
215
            $body = get(ViewRenderer::class)->render($body);
×
216
        }
217

UNCOV
218
        Assert::assertStringNotContainsString($search, $body);
×
219

UNCOV
220
        return $this;
×
221
    }
222
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc