Skip to content

Commit 1011e40

Browse files
bwoodhigidi
authored andcommitted
Add tests ensuring that both legacy (non-indexed) cassettes and indexed cassettes playback multiple identical requests correctly.
1 parent 074fb85 commit 1011e40

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

tests/Unit/CassetteTest.php

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,85 @@ public function testPlaybackLegacyCassette(): void
7777
'response' => $response->toArray(),
7878
];
7979

80+
$cassette = $this->createCassetteWithRecordings([$recording]);
81+
82+
$this->assertEquals($response->toArray(), $cassette->playback($request)->toArray());
83+
}
84+
85+
/**
86+
* Ensure that if a second identical request is played back from a legacy
87+
* cassette, the first response will be returned.
88+
*/
89+
public function testPlaybackOfIdenticalRequestsFromLegacyCassette(): void
90+
{
91+
$request1 = new Request('GET', 'https://example.com');
92+
$response1 = new Response(200, [], 'response1');
93+
94+
$request2 = new Request('GET', 'https://example.com');
95+
$response2 = new Response(200, [], 'response2');
96+
97+
// These are legacy recordings with no index keys.
98+
$recordings = [
99+
[
100+
'request' => $request1->toArray(),
101+
'response' => $response1->toArray(),
102+
],
103+
[
104+
'request' => $request2->toArray(),
105+
'response' => $response2->toArray(),
106+
],
107+
];
108+
109+
$cassette = $this->createCassetteWithRecordings($recordings);
110+
111+
$this->assertEquals($response1->toArray(), $cassette->playback($request1)->toArray());
112+
$this->assertEquals($response1->toArray(), $cassette->playback($request2)->toArray());
113+
}
114+
115+
/**
116+
* Ensure that if a second identical request is played back from an cassette
117+
* with indexed recordings, the response corresponding to the recording
118+
* index will be returned.
119+
*/
120+
public function testPlaybackOfIdenticalRequests(): void
121+
{
122+
$request1 = new Request('GET', 'https://example.com');
123+
$response1 = new Response(200, [], 'response1');
124+
125+
$request2 = new Request('GET', 'https://example.com');
126+
$response2 = new Response(200, [], 'response2');
127+
128+
// These are recordings with index keys which support playback of
129+
// multiple identical requests.
130+
$recordings = [
131+
[
132+
'request' => $request1->toArray(),
133+
'response' => $response1->toArray(),
134+
'index' => 0,
135+
],
136+
[
137+
'request' => $request2->toArray(),
138+
'response' => $response2->toArray(),
139+
'index' => 1,
140+
],
141+
];
142+
143+
$cassette = $this->createCassetteWithRecordings($recordings);
144+
145+
$this->assertEquals($response1->toArray(), $cassette->playback($request1, 0)->toArray());
146+
$this->assertNotEquals($response1->toArray(), $cassette->playback($request2, 1)->toArray());
147+
$this->assertEquals($response2->toArray(), $cassette->playback($request2, 1)->toArray());
148+
}
149+
150+
protected function createCassetteWithRecordings(array $recordings)
151+
{
80152
$storage = new Storage\Yaml(vfsStream::url('test/'), 'json_test');
81-
$storage->storeRecording($recording);
153+
154+
foreach ($recordings as $recording) {
155+
$storage->storeRecording($recording);
156+
}
82157
$configuration = new Configuration();
83-
$cassette = new Cassette('cassette_name', $configuration, $storage);
84158

85-
$this->assertEquals($response->toArray(), $cassette->playback($request)->toArray());
159+
return new Cassette('cassette_name', $configuration, $storage);
86160
}
87161
}

0 commit comments

Comments
 (0)