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

jomweb / billplz / 22912138050

10 Mar 2026 04:09PM UTC coverage: 73.745% (-23.8%) from 97.537%
22912138050

Pull #59

github

web-flow
Merge f68ad896b into 7e55fdd4e
Pull Request #59: Release Billplz 6.0 with Money objects, bundled Codex internals, and updated CI

263 of 443 new or added lines in 24 files covered. (59.37%)

3 existing lines in 2 files now uncovered.

514 of 697 relevant lines covered (73.74%)

24.87 hits per line

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

65.85
/src/Internal/Codex/Testing/Faker.php
1
<?php
2

3
namespace Laravie\Codex\Testing;
4

5
use GuzzleHttp\Psr7\HttpFactory;
6
use GuzzleHttp\Psr7\Utils;
7
use Http\Client\Common\HttpMethodsClient;
8
use Http\Client\HttpClient;
9
use Mockery as m;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\StreamInterface;
13

14
class Faker
15
{
16
    /**
17
     * HTTP methods client.
18
     *
19
     * @var \Http\Client\Common\HttpMethodsClient
20
     */
21
    protected $http;
22

23
    /**
24
     * Mock for "Http\Client\HttpClient".
25
     *
26
     * @var \Mockery\MockeryInterface
27
     */
28
    protected $client;
29

30
    /**
31
     * Mock for "Psr\Http\Message\ResponseInterface".
32
     *
33
     * @var \Mockery\MockeryInterface
34
     */
35
    protected $message;
36

37
    /**
38
     * Expected URL endpoint.
39
     *
40
     * @var string
41
     */
42
    protected $expectedRequestEndpoint;
43

44
    /**
45
     * Expected HTTP Request headers.
46
     *
47
     * @var array
48
     */
49
    protected $expectedRequestHeaders = [];
50

51
    /**
52
     * Expected HTTP Response status code.
53
     *
54
     * @var int|null
55
     */
56
    protected $expectedResponseStatusCode;
57

58
    /**
59
     * Expected HTTP Response reason phrase.
60
     *
61
     * @var string|null
62
     */
63
    protected $expectedResponseReasonPhrase;
64

65
    /**
66
     * Expected HTTP Response body.
67
     *
68
     * @var string|null
69
     */
70
    protected $expectedResponseBody;
71

72
    /**
73
     * Expected HTTP Response headers.
74
     *
75
     * @var array
76
     */
77
    protected $expectedResponseHeaders = [];
78

79
    /**
80
     * Construct a fake request.
81
     */
82
    public function __construct()
83
    {
84
        $this->client = m::mock(HttpClient::class);
142✔
85
        $this->message = m::mock(ResponseInterface::class);
142✔
86
        $factory = new HttpFactory;
142✔
87

88
        $this->http = new HttpMethodsClient(
142✔
89
            $this->client, $factory, $factory
142✔
90
        );
142✔
91
    }
92

93
    /**
94
     * Create a fake request.
95
     *
96
     * @return static
97
     */
98
    public static function create()
99
    {
100
        return new static;
142✔
101
    }
102

103
    /**
104
     * Set expected URL.
105
     *
106
     * @return $this
107
     */
108
    public function expectEndpointIs(string $endpoint)
109
    {
110
        $this->expectedRequestEndpoint = $endpoint;
67✔
111

112
        return $this;
67✔
113
    }
114

115
    /**
116
     * Make expected HTTP request.
117
     *
118
     * @param  \Mockery\Matcher\Type|array  $headers
119
     * @param  \Mockery\Matcher\Type|mixed  $body
120
     * @return $this
121
     */
122
    public function call(string $method, $headers = [], $body = '')
123
    {
124
        if ($method === 'GET') {
67✔
125
            $body = m::any();
38✔
126
        }
127

128
        $this->client->shouldReceive('sendRequest')
67✔
129
            ->with(m::on(function (RequestInterface $request) use ($method, $headers, $body): bool {
67✔
130
                Assert::assertSame($method, $request->getMethod());
67✔
131
                Assert::assertSame($this->expectedRequestEndpoint, (string) $request->getUri());
67✔
132

133
                $expectedHeaders = ! empty($this->expectedRequestHeaders) ? $this->expectedRequestHeaders : $headers;
67✔
134

135
                if (\is_array($expectedHeaders) && ! empty($expectedHeaders)) {
67✔
NEW
136
                    foreach ($expectedHeaders as $headerKey => $headerValue) {
×
NEW
137
                        Assert::assertTrue($request->hasHeader($headerKey));
×
NEW
138
                        Assert::assertSame(
×
NEW
139
                            \is_array($headerValue) ? $headerValue : ["{$headerValue}"],
×
NEW
140
                            $request->getHeader($headerKey)
×
NEW
141
                        );
×
142
                    }
143
                }
144

145
                if (! $body instanceof \Mockery\Matcher\MatcherAbstract && ! $body instanceof StreamInterface) {
67✔
146
                    Assert::assertSame((string) $body, (string) $request->getBody());
20✔
147
                }
148

149
                if ($body instanceof StreamInterface) {
67✔
NEW
150
                    Assert::assertSame((string) $body, (string) $request->getBody());
×
151
                }
152

153
                return true;
67✔
154
            }))
67✔
155
            ->andReturn($this->message());
67✔
156

157
        return $this;
67✔
158
    }
159

160
    /**
161
     * Make expected HTTP request.
162
     *
163
     * @param  \Mockery\Matcher\Type|array  $headers
164
     * @param  \Mockery\Matcher\Type|mixed  $body
165
     * @return $this
166
     */
167
    public function send(string $method, $headers = [], $body = '')
168
    {
NEW
169
        return $this->call($method, $headers, $body);
×
170
    }
171

172
    /**
173
     * Make expected HTTP JSON request.
174
     *
175
     * @param  \Mockery\Matcher\Type|array  $headers
176
     * @param  \Mockery\Matcher\Type|array|string  $body
177
     * @return $this
178
     */
179
    public function sendJson(string $method, $headers = [], $body = '')
180
    {
NEW
181
        if (\is_array($headers)) {
×
NEW
182
            $headers['Content-Type'] = 'application/json';
×
NEW
183
            $this->expectedRequestHeaders = $headers;
×
184
        }
185

NEW
186
        if (\is_array($body)) {
×
NEW
187
            $body = json_encode($body);
×
188
        }
189

NEW
190
        return $this->call($method, $headers, $body);
×
191
    }
192

193
    /**
194
     * Make expected HTTP JSON request.
195
     *
196
     * @param  \Mockery\Matcher\Type|array  $headers
197
     * @return $this
198
     */
199
    public function stream(string $method, $headers = [])
200
    {
201
        if (\is_array($headers)) {
9✔
202
            $this->expectedRequestHeaders = $headers;
9✔
203
        }
204

205
        return $this->call($method, m::type('Array'), m::type(StreamInterface::class));
9✔
206
    }
207

208
    /**
209
     * Request should response with.
210
     *
211
     * @return $this
212
     */
213
    public function shouldResponseWith(int $code = 200, string $body = '', array $headers = [])
214
    {
215
        $this->expectedResponseStatusCode = $code;
67✔
216
        $this->expectedResponseBody = $body;
67✔
217

218
        $this->message->shouldReceive('getStatusCode')->andReturn($code)
67✔
219
            ->shouldReceive('getBody')->andReturn(Utils::streamFor($body));
67✔
220

221
        $this->expectResponseHeaders($headers);
67✔
222

223
        return $this;
67✔
224
    }
225

226
    /**
227
     * Request should response with.
228
     *
229
     * @return $this
230
     */
231
    public function shouldResponseWithJson(int $code = 200, string $body = '', array $headers = [])
232
    {
233
        $headers['Content-Type'] = 'application/json';
63✔
234

235
        return $this->shouldResponseWith($code, $body, $headers);
63✔
236
    }
237

238
    /**
239
     * Response should have reason phrase as.
240
     *
241
     * @return $this
242
     */
243
    public function expectResponseHeaders(array $headers)
244
    {
245
        foreach ($headers as $headerKey => $headerValue) {
67✔
246
            if (! \is_string($headerKey)) {
63✔
NEW
247
                continue;
×
248
            }
249

250
            if (\is_array($headerValue)) {
63✔
NEW
251
                $this->expectedResponseHeaders[$headerKey] = array_merge(
×
NEW
252
                    $this->expectedResponseHeaders[$headerKey] ?? [], $headerValue
×
NEW
253
                );
×
254
            } else {
255
                $this->expectedResponseHeaders[$headerKey][] = "{$headerValue}";
63✔
256
            }
257
        }
258

259
        $this->message->shouldReceive('hasHeader')
67✔
260
            ->andReturnUsing(function ($key) {
67✔
NEW
261
                return \array_key_exists($key, $this->expectedResponseHeaders);
×
262
            });
67✔
263

264
        $this->message->shouldReceive('getHeader')
67✔
265
            ->andReturnUsing(function ($key) {
67✔
NEW
266
                return \array_key_exists($key, $this->expectedResponseHeaders)
×
NEW
267
                    ? $this->expectedResponseHeaders[$key]
×
NEW
268
                    : [];
×
269
            });
67✔
270

271
        $this->message->shouldReceive('getHeaderLine')
67✔
272
            ->andReturnUsing(function ($key) {
67✔
273
                return \array_key_exists($key, $this->expectedResponseHeaders)
63✔
274
                    ? implode(', ', $this->expectedResponseHeaders[$key])
6✔
275
                    : '';
63✔
276
            });
67✔
277

278
        return $this;
67✔
279
    }
280

281
    /**
282
     * Response should have reason phrase as.
283
     *
284
     * @return $this
285
     */
286
    public function expectReasonPhraseIs(string $reason)
287
    {
NEW
288
        $this->expectedResponseReasonPhrase = $reason;
×
289

NEW
290
        $this->message->shouldReceive('getReasonPhrase')->andReturn($reason);
×
291

NEW
292
        return $this;
×
293
    }
294

295
    /**
296
     * Response should have reason phrase as.
297
     *
298
     * @return $this
299
     */
300
    public function expectContentTypeIs(string $contentType)
301
    {
NEW
302
        return $this->expectResponseHeaders([
×
NEW
303
            'Content-Type' => $contentType,
×
NEW
304
        ]);
×
305
    }
306

307
    /**
308
     * Get HTTP mock.
309
     *
310
     * @return \Http\Client\Common\HttpMethodsClient
311
     */
312
    public function http()
313
    {
314
        return $this->http;
142✔
315
    }
316

317
    /**
318
     * Get message mock.
319
     *
320
     * @return \Mockery\MockeryInterface
321
     */
322
    public function message()
323
    {
324
        return $this->message;
67✔
325
    }
326
}
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