-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathConfigTest.php
More file actions
102 lines (68 loc) · 2.79 KB
/
ConfigTest.php
File metadata and controls
102 lines (68 loc) · 2.79 KB
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
101
102
<?php
declare(strict_types=1);
use Saloon\Config;
use Saloon\Http\Response;
use Saloon\Http\PendingRequest;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Http\Senders\GuzzleSender;
use Saloon\Exceptions\StrayRequestException;
use Saloon\Tests\Fixtures\Senders\ArraySender;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Connectors\TestConnector;
afterEach(function () {
Config::clearGlobalMiddleware();
Config::$defaultSender = GuzzleSender::class;
});
test('the config can specify global middleware', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Jake Owen - Beachin']),
]);
$count = 0;
Config::globalMiddleware()->onRequest(function (PendingRequest $pendingRequest) use (&$count) {
$count++;
});
Config::globalMiddleware()->onResponse(function (Response $response) use (&$count) {
$count++;
});
TestConnector::make()->send(new UserRequest, $mockClient);
expect($count)->toBe(2);
});
test('you can change the global default sender used', function () {
Config::$defaultSender = ArraySender::class;
$connector = new TestConnector;
$connector->send(new UserRequest);
expect($connector->sender())->toBeInstanceOf(ArraySender::class);
Config::$defaultSender = GuzzleSender::class;
$connector = new TestConnector;
$response = $connector->send(new UserRequest);
expect($response->getPendingRequest()->getConnector()->sender())->toBeInstanceOf(GuzzleSender::class);
});
test('you can change how the global default sender is resolved', function () {
$sender = TestConnector::make()->sender();
expect($sender)->toBeInstanceOf(GuzzleSender::class);
Config::setSenderResolver(static fn () => new ArraySender);
$sender = TestConnector::make()->sender();
expect($sender)->toBeInstanceOf(ArraySender::class);
Config::setSenderResolver(null);
$sender = TestConnector::make()->sender();
expect($sender)->toBeInstanceOf(GuzzleSender::class);
});
test('you can prevent stray api requests', function () {
Config::preventStrayRequests();
$this->expectException(StrayRequestException::class);
$this->expectExceptionMessage('Attempted to make a real API request! Make sure to use a mock response or fixture.');
TestConnector::make()->send(new UserRequest);
Config::clearGlobalMiddleware();
});
test('you can prevent and then allow stray api requests', function () {
Config::preventStrayRequests();
try {
TestConnector::make()->send(new UserRequest);
} catch (StrayRequestException $e) {
expect($e)->toBeInstanceOf(StrayRequestException::class);
}
Config::allowStrayRequests();
TestConnector::make()->send(new UserRequest);
Config::clearGlobalMiddleware();
});