-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathResponseCacheMockFixtureTest.php
More file actions
74 lines (53 loc) · 2.18 KB
/
ResponseCacheMockFixtureTest.php
File metadata and controls
74 lines (53 loc) · 2.18 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
<?php
declare(strict_types=1);
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Saloon\CachePlugin\Tests\Fixtures\Connectors\TestConnector;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequest;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
$filesystem = new Filesystem(new LocalFilesystemAdapter(cachePath()));
beforeEach(function () use ($filesystem): void {
$filesystem->deleteDirectory('/');
});
function cachedUserRequestFixtureAbsolutePath(): string
{
$dir = getcwd().'/tests/Fixtures/Saloon';
if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}
return $dir.'/cached-user-request.json';
}
function writeCachedUserRequestFixture(int $version): void
{
$payload = [
'statusCode' => 200,
'headers' => ['Content-Type' => ['application/json']],
'data' => json_encode(['version' => $version]),
'context' => [],
];
file_put_contents(
cachedUserRequestFixtureAbsolutePath(),
json_encode($payload, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
);
}
test('response cache can return a stale body when mocks use fixtures and withoutCache is not used', function (): void {
writeCachedUserRequestFixture(1);
$mockClient = new MockClient([
CachedUserRequest::class => MockResponse::fixture('cached-user-request'),
]);
$connector = TestConnector::make();
expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(1);
writeCachedUserRequestFixture(2);
expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(1);
});
test('withoutCache on the mock client skips response cache so updated fixture files are used', function (): void {
writeCachedUserRequestFixture(1);
$mockClient = (new MockClient([
CachedUserRequest::class => MockResponse::fixture('cached-user-request'),
]))->withoutCache();
$connector = TestConnector::make();
expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(1);
writeCachedUserRequestFixture(2);
expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(2);
});