5
5
6
6
require __DIR__ . '/vendor/autoload.php ' ;
7
7
8
- echo "====== Integration Test for Cookies AND Caching ====== \n" ;
8
+ echo "====== Integration Test for Cookies AND Caching (FIXED) ====== \n" ;
9
9
10
10
Task::run (function () {
11
- // 1. Arrange: Enable testing with a managed CookieJar.
12
- $ handler = Http::testing ()->withGlobalCookieJar ();
13
- $ handler ->reset ();
11
+ // 1. Arrange: Get testing handler and reset FIRST
12
+ $ handler = Http::testing ();
13
+ $ handler ->reset (); // Reset first
14
+
15
+ // THEN enable cookie jar (after reset)
16
+ $ handler ->withGlobalCookieJar ();
17
+
18
+ // Set as global instance
19
+ Http::setInstance ($ handler );
20
+
21
+ echo "DEBUG: Testing handler created, reset, and configured with cookie jar \n" ;
14
22
15
23
$ sessionId = 'session-for-caching-test ' ;
16
24
$ domain = 'api.example.com ' ;
17
25
$ profileUrl = "https:// {$ domain }/api/profile " ;
18
26
$ loginUrl = "https:// {$ domain }/login " ;
19
27
20
- Http::mock ('POST ' )->url ($ loginUrl )
28
+ // Set up mocks
29
+ $ handler ->mock ('POST ' )->url ($ loginUrl )
21
30
->setCookie ('session_id ' , $ sessionId )
22
31
->json (['status ' => 'logged_in ' ])
23
32
->register ();
24
33
25
- // Mock the /profile endpoint. It MUST expect the cookie.
26
- // It is NOT persistent. If it's called more than once, the test will fail.
27
- Http::mock ('GET ' )->url ($ profileUrl )
34
+ $ handler ->mock ('GET ' )->url ($ profileUrl )
28
35
->withHeader ('Cookie ' , "session_id= {$ sessionId }" )
29
- ->delay (0.5 ) // Add a delay to make the cache hit obvious
36
+ ->delay (0.5 )
30
37
->json (['user ' => 'John Doe ' , 'timestamp ' => microtime (true )])
31
38
->register ();
32
39
33
- echo "--- Step 1: Logging in to establish a session --- \n" ;
34
- await (Http::post ($ loginUrl ));
40
+ echo "\n--- Step 1: Logging in to establish a session --- \n" ;
41
+ $ loginResponse = await (Http::post ($ loginUrl ));
42
+
43
+ // Now we can safely assert cookie exists
35
44
$ handler ->assertCookieExists ('session_id ' );
36
45
echo " ✓ SUCCESS: Logged in and session cookie was stored. \n" ;
37
46
38
47
// -----------------------------------------------------------------
39
48
40
49
echo "\n--- Step 2: First profile fetch (expecting CACHE MISS) --- \n" ;
41
50
$ start1 = microtime (true );
42
- // This request uses BOTH the cookie jar (implicitly) and caching.
43
51
$ response1 = await (
44
52
Http::request ()
45
53
->cache (60 )
66
74
67
75
echo "\n--- Step 4: Verifying Results --- \n" ;
68
76
69
- // Assertion 1: Verify the second call was a cache hit (instant and same data).
77
+ // Assertion 1: Verify the second call was a cache hit
70
78
if ($ elapsed2 < 0.01 && $ data1 ['timestamp ' ] === $ data2 ['timestamp ' ]) {
71
79
echo " ✓ SUCCESS: Second request was an instant cache hit with the correct data. \n" ;
72
80
} else {
73
81
echo " ✗ FAILED: Second request was not a cache hit. \n" ;
74
82
}
75
83
76
- // Assertion 2: Verify the total number of requests made.
84
+ // Assertion 2: Verify the total number of requests made
77
85
try {
78
- // We expect 3 total recorded requests:
79
- // 1. POST /login (consumes a mock)
80
- // 2. GET /profile (cache miss, consumes a mock)
81
- // 3. GET /profile (cache hit, does NOT consume a mock)
82
86
$ handler ->assertRequestCount (3 );
83
- echo " ✓ SUCCESS: Correct number of requests recorded (login, cache miss, cache hit) . \n" ;
87
+ echo " ✓ SUCCESS: Correct number of requests recorded. \n" ;
84
88
} catch (Exception $ e ) {
85
89
echo " ✗ FAILED: " . $ e ->getMessage () . "\n" ;
86
90
}
87
91
88
- // Final check: Let's see the history to be sure.
92
+ // Final check: Request history
89
93
$ history = $ handler ->getRequestHistory ();
90
94
$ profile_miss_found = false ;
91
95
$ profile_hit_found = false ;
95
99
}
96
100
97
101
if ($ profile_miss_found && $ profile_hit_found ) {
98
- echo " ✓ SUCCESS: Request history correctly shows one cache miss and one cache hit for the profile . \n" ;
102
+ echo " ✓ SUCCESS: Request history shows cache miss and cache hit. \n" ;
99
103
} else {
100
104
echo " ✗ FAILED: Request history is incorrect. \n" ;
105
+ echo " DEBUG: History: \n" ;
106
+ foreach ($ history as $ req ) {
107
+ echo " - {$ req ->method } {$ req ->url }\n" ;
108
+ }
101
109
}
102
-
103
110
});
104
111
105
112
echo "\n====== Cookie and Cache Integration Test Complete ====== \n" ;
0 commit comments