forked from michiruf/laravel-http-automock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleUsageTest.php
More file actions
105 lines (86 loc) · 4.69 KB
/
Copy pathExampleUsageTest.php
File metadata and controls
105 lines (86 loc) · 4.69 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
103
104
105
<?php
use HttpAutomock\Exceptions\PreventedRequestException;
use HttpAutomock\Resolver\RequestUrlResolver;
use Illuminate\Http\Client\Request;
/**
* In all these example tests, please view the files that are specified in the File::exists assertion.
*/
it('can do stuff with the api', function () {
Http::automock();
$response = Http::get('http://localhost:9337/coffee/hot')->json();
expect($response)->toHaveCount(20, 'There are not 20 hot coffees in the api service')
->and(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_do_stuff_with_the_api/1_GET_4c147242.mock'))->toBeTrue();
});
it('can specify file names via file resolver', function () {
Http::automock()->resolveFileNameUsing(new RequestUrlResolver(port: false, removeSlashes: false));
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_specify_file_names_via_file_resolver/localhost/coffee/hot.mock'))->toBeTrue();
});
it('can specify file names via closure', function () {
Http::automock()->resolveFileNameUsing(fn (Request $request, bool $forWriting) => "TEST-{$request->method()}");
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_specify_file_names_via_closure/TEST-GET.mock'))->toBeTrue();
});
it('can put mocks in subdirectories', function () {
Http::automock()->resolveFileNameUsing('url_subdirectory');
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_put_mocks_in_subdirectories/localhost-9337/coffee/hot.mock'))->toBeTrue();
});
it('can persist some headers', function () {
Http::automock()->withHeaders(['Server']);
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_persist_some_headers/1_GET_4c147242.mock'))->toBeTrue();
});
it('can persist default headers', function () {
config()->set('http-automock.use_default_headers', true);
Http::automock();
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_persist_default_headers/1_GET_4c147242.mock'))->toBeTrue();
});
it('can persist all headers', function () {
Http::automock()->withHeaders();
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_can_persist_all_headers/1_GET_4c147242.mock'))->toBeTrue();
});
it('can renew responses', function () {
$now = now();
Http::automock()->renew();
Http::get('http://localhost:9337/coffee/hot');
$path = 'tests/.pest/automock/Feature/ExampleUsageTest/it_can_renew_responses/1_GET_4c147242.mock';
expect(File::exists($path))->toBeTrue()
->and(File::lastModified($path))->toBeGreaterThanOrEqual($now->getTimestamp());
});
it('can prevent real requests', function () {
Http::automock()->preventRealRequests();
Http::get('http://localhost:9337/coffee/hot');
})->throws(PreventedRequestException::class, 'Tried to send a real request that was prevented', 1);
it('can prevent unknown real requests', function () {
Http::automock()->preventUnknownRealRequests();
Http::get('http://localhost:9337/coffee/hot');
})->throws(PreventedRequestException::class, 'Tried to send an unknown real request that was prevented', 2);
it('can renew known requests', function () {
$path = 'tests/.pest/automock/Feature/ExampleUsageTest/it_can_renew_known_requests/1_GET_4c147242.mock';
File::ensureDirectoryExists(dirname($path));
File::put($path, '');
Http::automock()
->preventUnknownRealRequests()
->renew();
Http::get('http://localhost:9337/coffee/hot');
expect(File::get($path))->not->toBeEmpty();
});
it('can disable automock entirely', function () {
Http::automock()->disable();
Http::get('http://localhost:9337/coffee/hot');
expect(File::isDirectory('tests/.pest/automock/Feature/ExampleUsageTest/it_can_disable_automock_entirely'))->toBeFalse();
});
it('can skip automock for all requests', function () {
Http::automock()->skip(fn (Request $request) => true, 'skip all');
Http::get('http://localhost:9337/coffee/hot');
expect(File::isDirectory('tests/.pest/automock/Feature/ExampleUsageTest/it_can_skip_all_requests'))->toBeFalse();
});
it('is possible to set the extension via config', function () {
config()->set('http-automock.extension', '.json');
Http::automock();
Http::get('http://localhost:9337/coffee/hot');
expect(File::exists('tests/.pest/automock/Feature/ExampleUsageTest/it_is_possible_to_set_the_extension_via_config/1_GET_4c147242.json'))->toBeTrue();
});