-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathThroughToDtoTest.php
More file actions
72 lines (54 loc) · 2.78 KB
/
ThroughToDtoTest.php
File metadata and controls
72 lines (54 loc) · 2.78 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
<?php
declare(strict_types=1);
use Saloon\Http\Response;
use Saloon\Tests\Fixtures\Data\User;
use Saloon\Tests\Fixtures\Data\IntoUser;
use Saloon\Tests\Fixtures\Data\Superhero;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Data\IntoUserWithResponse;
use Saloon\Tests\Fixtures\Data\SuperheroWithResponse;
use Saloon\Tests\Fixtures\Requests\PagedSuperheroRequest;
describe('into', function () {
test('can create a dto using the into method on a request', function () {
$connector = connector();
$request = new UserRequest;
$user = $connector->send($request)->into(IntoUser::class);
expect($user)->toBeInstanceOf(IntoUser::class);
expect($user->name)->toEqual('Sammyjo20');
});
test('if the class does not implement the dto interface it will throw an exception', function () {
$connector = connector();
$request = new UserRequest;
$connector->send($request)->into(User::class);
})->throws(InvalidArgumentException::class, 'The class provided must implement the Saloon\Contracts\DataObjects\IntoObject interface.');
test('if the class implements the with response interface it will populate the response', function () {
$connector = connector();
$request = new UserRequest;
$user = $connector->send($request)->into(IntoUserWithResponse::class);
expect($user)->toBeInstanceOf(IntoUserWithResponse::class);
expect($user->name)->toEqual('Sammyjo20');
expect($user->getResponse())->toBeInstanceOf(Response::class);
});
});
describe('into many', function () {
test('can create many dtos using the intoMany method on a request', function () {
$connector = connector();
$request = new PagedSuperheroRequest;
$superheroes = $connector->send($request)->intoMany(Superhero::class);
expect($superheroes)->toBeArray();
expect($superheroes[0])->toBeInstanceOf(Superhero::class);
expect($superheroes[0]->name)->toEqual('Batman');
});
test('if the class does not implement the dto interface it will throw an exception', function () {
$connector = connector();
$request = new UserRequest;
$connector->send($request)->intoMany(IntoUser::class);
})->throws(InvalidArgumentException::class, 'The class provided must implement the Saloon\Contracts\DataObjects\IntoObjects interface.');
test('if the class implements the with response interface it will populate the response', function () {
$connector = connector();
$request = new PagedSuperheroRequest;
$superheroes = $connector->send($request)->intoMany(SuperheroWithResponse::class);
expect($superheroes)->toBeArray();
expect($superheroes[0]->getResponse())->toBeInstanceOf(Response::class);
});
});