-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathConnectorTest.php
More file actions
58 lines (42 loc) · 2.11 KB
/
ConnectorTest.php
File metadata and controls
58 lines (42 loc) · 2.11 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
<?php
declare(strict_types=1);
use Saloon\Http\Response;
use GuzzleHttp\Promise\Promise;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Connectors\TestConnector;
use Saloon\Tests\Fixtures\Requests\HasConnectorUserRequest;
use Saloon\Tests\Fixtures\Connectors\RequestSelectionConnector;
test('a connector class can be instantiated using the make method', function () {
$connectorA = TestConnector::make();
expect($connectorA)->toBeInstanceOf(TestConnector::class);
$connectorB = RequestSelectionConnector::make('yee-haw-1-2-3');
expect($connectorB)->toBeInstanceOf(RequestSelectionConnector::class);
expect($connectorB)->apiKey->toEqual('yee-haw-1-2-3');
});
test('the same connector instance is kept if you instantiate it on the request with HasConnector', function () {
$request = new HasConnectorUserRequest;
$connector = $request->connector();
expect($connector)->toBe($request->connector());
});
test('you can send a request through the connector', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']),
]);
$connector = new TestConnector();
$response = $connector->send(new UserRequest, $mockClient);
expect($response)->toBeInstanceOf(Response::class);
expect($response->json())->toEqual(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']);
});
test('you can send an asynchronous request through the connector', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']),
]);
$connector = new TestConnector();
$promise = $connector->sendAsync(new UserRequest, $mockClient);
expect($promise)->toBeInstanceOf(Promise::class);
$response = $promise->wait();
expect($response)->toBeInstanceOf(Response::class);
expect($response->json())->toEqual(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']);
});