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

tempestphp / tempest-framework / 14049246919

24 Mar 2025 09:42PM UTC coverage: 79.353% (-0.04%) from 79.391%
14049246919

push

github

web-flow
feat(support): support array parameters in string manipulations (#1073)

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

735 existing lines in 126 files now uncovered.

10492 of 13222 relevant lines covered (79.35%)

90.78 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
    ) {}
54✔
26

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

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

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

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

48
        return $this;
10✔
49
    }
50

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

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

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

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

65
        return $this;
7✔
66
    }
67

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

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

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

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

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

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

107
        return $this;
29✔
108
    }
109

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

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

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

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

UNCOV
122
        return $this;
×
123
    }
124

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

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

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

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

145
        return $this;
1✔
146
    }
147

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

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

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

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

169
        return $this;
2✔
170
    }
171

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

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

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

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

193
        return $this;
2✔
194
    }
195

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

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

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

206
        return $this;
1✔
207
    }
208

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

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

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

UNCOV
219
        return $this;
×
220
    }
221
}
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