@@ -31,37 +31,36 @@ class AbstractRequestTest extends BaseTestCase
31
31
{
32
32
use MocksGuzzleInstance;
33
33
34
+ protected function setUp (): void
35
+ {
36
+ parent ::setUp ();
37
+ Storage::fake ('api-logs ' );
38
+ }
39
+
34
40
protected function mockRequestWithLog ()
35
41
{
36
42
return new class extends ConcreteRequest {
37
43
use ParseResponseJSON;
38
44
39
45
protected bool $ shouldLog = true ;
40
46
41
- public $ logged ;
42
- public function log ($ outcome ): void
43
- {
44
- $ this ->logged = $ outcome ;
45
- }
46
-
47
- public function isFromCache (): bool
47
+ public function getLogFolder (): string
48
48
{
49
- return $ this -> responseIsFromCache ;
49
+ return ' one/two ' ;
50
50
}
51
51
};
52
52
}
53
53
54
54
public function testCacheMissUsesGuzzle (): void
55
55
{
56
56
$ this ->mockGuzzleWithTapper ();
57
- $ expectedResponse = new Response (200 , [], '{"awesome":"sauce"} ' );
58
- $ this ->tapper ->addMatch ('POST ' , '/.*?/ ' , $ expectedResponse );
57
+ $ this ->tapper ->addMatchBody ('POST ' , '/.*?/ ' , '{"awesome":"sauce"} ' );
59
58
60
59
$ requestClass = $ this ->mockRequestWithLog ();
61
60
62
61
$ result = $ requestClass ->sync ();
63
62
64
- self ::assertSame ( $ expectedResponse , $ requestClass ->logged );
63
+ self ::assertStringContainsString ( ' "sauce" ' , $ requestClass ->getLastLogContents () );
65
64
$ this ->expectTotalRequestCount (1 );
66
65
$ this ->assertTapperRequestLike ('POST ' , '/.*?/ ' , 1 );
67
66
@@ -103,6 +102,38 @@ public function testCacheHitAfterFirstRequest(): void
103
102
self ::assertTrue ($ secondRequest ->canBeFulfilledByCache ());
104
103
}
105
104
105
+ public function testCacheHitHasAccessToOriginalLog (): void
106
+ {
107
+ Storage::fake ('api-logs ' );
108
+ $ tapper = $ this ->mockGuzzleWithTapper ();
109
+ $ tapper ->addMatchBody ('POST ' , '/awesome/ ' , '{"awesome":"sauce"} ' );
110
+
111
+ $ firstLogTime = '2018-01-01T00:00:00.000000+00:00 ' ;
112
+ Carbon::setTestNow ($ firstLogTime );
113
+ $ firstRequest = $ this ->mockRequestWithLog ();
114
+ $ firstRequest ->sync ();
115
+ self ::assertTrue ($ firstRequest ->canBeFulfilledByCache ());
116
+ self ::assertSame ($ firstRequest ->getLastLogFile (), "one/two/ {$ firstLogTime }" );
117
+ self ::assertSame (1 , $ tapper ->getCountLike ('POST ' , '/awesome/ ' ));
118
+
119
+ // Regenerate the request
120
+ $ secondLogTime = '2018-01-01T00:02:02.000000+00:00 ' ;
121
+ Carbon::setTestNow ($ secondLogTime );
122
+ $ secondRequest = $ this ->mockRequestWithLog ();
123
+ self ::assertTrue ($ secondRequest ->canBeFulfilledByCache ());
124
+ // Both requests have same key
125
+ self ::assertSame ($ firstRequest ->cacheKey (), $ secondRequest ->cacheKey ());
126
+
127
+ $ secondRequest ->sync ();
128
+ self ::assertSame (1 , $ tapper ->getCountLike ('POST ' , '/awesome/ ' ));
129
+ // Result was in cache
130
+ self ::assertTrue ($ secondRequest ->isFromCache ());
131
+ // Second request can retrieve log from original time
132
+ self ::assertSame ($ secondRequest ->getLastLogFile (), "one/two/ {$ firstLogTime }" );
133
+ // Second request did not log to disk, only the first request
134
+ self ::assertSame (["one/two/ {$ firstLogTime }" ], Storage::disk ('api-logs ' )->files ('one/two ' ));
135
+ }
136
+
106
137
public function testDontCacheErrorStatus (): void
107
138
{
108
139
$ firstRequest = $ this ->mockRequestWithPostProcessor ();
@@ -320,6 +351,7 @@ public function testMissingGetLogFolderImplementation(): void
320
351
$ tapper ->addMatchBody ('POST ' , '/awesome/ ' , 'true ' );
321
352
322
353
$ this ->expectException (ToDoException::class);
354
+ $ this ->expectExceptionMessageMatches ('/To enable request logging, .+ will have to implement the method getLogFolder/ ' );
323
355
$ request ->sync ();
324
356
}
325
357
@@ -366,6 +398,12 @@ public function getLogFolder(): string
366
398
} catch (\DomainException $ exception ) {
367
399
self ::assertSame ('No log files have been saved by this instance. ' , $ exception ->getMessage ());
368
400
}
401
+ try {
402
+ $ request ->getLastLogFile ();
403
+ self ::fail ('Should have thrown ' );
404
+ } catch (\DomainException $ exception ) {
405
+ self ::assertSame ('No log files have been saved by this instance. ' , $ exception ->getMessage ());
406
+ }
369
407
370
408
$ tapper = $ this ->mockGuzzleWithTapper ();
371
409
$ tapper ->addMatchBody ('POST ' , '/awesome/ ' , 'true ' );
@@ -374,6 +412,7 @@ public function getLogFolder(): string
374
412
Carbon::setTestNow ($ firstLogTime );
375
413
$ request ->sync ();
376
414
Storage::disk ('api-logs ' )->assertExists ("one/two/ {$ firstLogTime }" );
415
+ self ::assertSame ($ request ->getLastLogFile (), "one/two/ {$ firstLogTime }" );
377
416
self ::assertSame ($ request ->getLastLogContents (), Storage::disk ('api-logs ' )->get ("one/two/ {$ firstLogTime }" ));
378
417
379
418
// Make a second log, see that it's now returned
@@ -383,6 +422,7 @@ public function getLogFolder(): string
383
422
Carbon::setTestNow ($ secondLogTime );
384
423
$ request ->sync ();
385
424
Storage::disk ('api-logs ' )->assertExists ("one/two/ {$ secondLogTime }" );
425
+ self ::assertSame ($ request ->getLastLogFile (), "one/two/ {$ secondLogTime }" );
386
426
self ::assertSame ($ request ->getLastLogContents (), Storage::disk ('api-logs ' )->get ("one/two/ {$ secondLogTime }" ));
387
427
}
388
428
@@ -553,7 +593,7 @@ public function testResponseIsFromCachePreventsWritesToCache(): void
553
593
Cache::shouldReceive ('get ' )
554
594
->once ()
555
595
->with ($ request ->cacheKey ())
556
- ->andReturn ([200 , [], '42 ' ]);
596
+ ->andReturn ([' logs ' => [], ' response ' => [ 200 , [], '42 ' ] ]);
557
597
// It is NOT written back to cache
558
598
Cache::shouldReceive ('put ' )->never ();
559
599
0 commit comments