forked from saloonphp/saloon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataObjectWrapperTest.php
More file actions
66 lines (48 loc) · 2.49 KB
/
DataObjectWrapperTest.php
File metadata and controls
66 lines (48 loc) · 2.49 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
<?php
declare(strict_types=1);
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Tests\Fixtures\Data\User;
use Saloon\Contracts\DataObjects\WithResponse;
use Saloon\Tests\Fixtures\Requests\DTORequest;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Data\UserWithResponse;
use Saloon\Tests\Fixtures\Requests\DTOWithResponseRequest;
test('if a dto does not implement the WithResponse interface and HasResponse trait Saloon will not add the original response', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam', 'twitter' => '@carre_sam']),
]);
$response = connector()->send(new DTORequest, $mockClient);
$dto = $response->dto(User::class);
expect($dto)->toBeInstanceOf(User::class);
expect($dto)->not->toBeInstanceOf(WithResponse::class);
});
test('if a dto implements the WithResponse interface and HasResponse trait Saloon will add the original response', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam', 'twitter' => '@carre_sam']),
]);
$request = new DTOWithResponseRequest();
$response = connector()->send($request, $mockClient);
/** @var UserWithResponse $dto */
$dto = $response->dto();
expect($dto)->toBeInstanceOf(UserWithResponse::class);
expect($dto)->toBeInstanceOf(WithResponse::class);
expect($dto->getResponse())->toBe($response);
});
test('if a dto type is provided and the class does not support a dto null is still returned', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam', 'twitter' => '@carre_sam']),
]);
$response = connector()->send(new UserRequest, $mockClient);
$dto = $response->dto(User::class);
expect($dto)->toBeNull();
});
test('if a dto type is provided and the class returned doesnt match an exception is thrown', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam', 'twitter' => '@carre_sam']),
]);
$response = connector()->send(new DTORequest, $mockClient);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The class-string provided [Saloon\Tests\Fixtures\Data\UserWithResponse] must match the class-string returned by the connector/request [Saloon\Tests\Fixtures\Data\User].');
$response->dto(UserWithResponse::class);
});