Skip to content

Commit 3316011

Browse files
committed
Use Guzzle 6 MockHandler in ArchiveTest
1 parent 1951f2f commit 3316011

File tree

1 file changed

+86
-46
lines changed

1 file changed

+86
-46
lines changed

tests/OpenTok/ArchiveTest.php

Lines changed: 86 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
use Guzzle\Plugin\Mock\MockPlugin;
3+
use GuzzleHttp\Handler\MockHandler;
4+
use GuzzleHttp\HandlerStack;
5+
use GuzzleHttp\Middleware;
46

57
use OpenTok\Archive;
68
use OpenTok\OpenTokTestCase;
@@ -25,7 +27,7 @@ public static function setUpBeforeClass()
2527
self::$mockBasePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'mock' . DIRECTORY_SEPARATOR;
2628
}
2729

28-
public function setUp()
30+
public function setupArchives()
2931
{
3032
// Set up fixtures
3133
$this->archiveData = array(
@@ -43,27 +45,66 @@ public function setUp()
4345
'hasAudio' => true,
4446
'outputMode' => 'composed'
4547
);
46-
$this->API_KEY = defined('API_KEY') ? API_KEY : '12345678';
47-
$this->API_SECRET = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
4848

49-
$this->client = new Client();
5049
$this->archive = new Archive($this->archiveData, array(
5150
'apiKey' => $this->API_KEY,
5251
'apiSecret' => $this->API_SECRET,
5352
'client' => $this->client
5453
));
5554
}
5655

56+
private function setupOTWithMocks($mocks)
57+
{
58+
$this->API_KEY = defined('API_KEY') ? API_KEY : '12345678';
59+
$this->API_SECRET = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
60+
61+
if (is_array($mocks)) {
62+
$responses = TestHelpers::mocksToResponses($mocks, self::$mockBasePath);
63+
} else {
64+
$responses = [];
65+
}
66+
67+
$mock = new MockHandler($responses);
68+
$handlerStack = HandlerStack::create($mock);
69+
$clientOptions = [
70+
'handler' => $handlerStack
71+
];
72+
73+
$this->client = new Client();
74+
$this->client->configure(
75+
$this->API_KEY,
76+
$this->API_SECRET,
77+
'https://api.opentok.com',
78+
$clientOptions
79+
);
80+
81+
// Push history onto handler stack *after* configuring client to
82+
// ensure auth header is added before history handler is invoked
83+
$this->historyContainer = [];
84+
$history = Middleware::history($this->historyContainer);
85+
$handlerStack->push($history);
86+
}
87+
88+
private function setupOT()
89+
{
90+
return $this->setupOTWithMocks([]);
91+
}
92+
5793
public function testInitializes()
5894
{
5995
// Arrange
96+
$this->setupOT();
97+
$this->setupArchives();
6098
// Act
6199
// Assert
62100
$this->assertInstanceOf('OpenTok\Archive', $this->archive);
63101
}
64102

65103
public function testReadsProperties()
66104
{
105+
$this->setupOT();
106+
$this->setupArchives();
107+
67108
$this->assertEquals($this->archiveData['createdAt'], $this->archive->createdAt);
68109
$this->assertEquals($this->archiveData['duration'], $this->archive->duration);
69110
$this->assertEquals($this->archiveData['id'], $this->archive->id);
@@ -82,37 +123,38 @@ public function testReadsProperties()
82123
public function testStopsArchive()
83124
{
84125
// Arrange
85-
$mock = new MockPlugin();
86-
$response = MockPlugin::getMockFile(
87-
self::$mockBasePath . 'v2/project/APIKEY/archive/ARCHIVEID/stop'
88-
);
89-
$mock->addResponse($response);
90-
$this->client->addSubscriber($mock);
126+
$this->setupOTWithMocks([[
127+
'code' => 200,
128+
'headers' => [
129+
'Content-Type' => 'application/json'
130+
],
131+
'path' => 'v2/project/APIKEY/archive/ARCHIVEID/stop'
132+
]]);
133+
$this->setupArchives();
91134

92135
// Act
93136
$this->archive->stop();
94137

95138
// Assert
96-
$requests = $mock->getReceivedRequests();
97-
$this->assertCount(1, $requests);
139+
$this->assertCount(1, $this->historyContainer);
98140

99-
$request = $requests[0];
141+
$request = $this->historyContainer[0]['request'];
100142
$this->assertEquals('POST', strtoupper($request->getMethod()));
101-
$this->assertEquals('/v2/project/'.$this->API_KEY.'/archive/'.$this->archiveData['id'].'/stop', $request->getPath());
102-
$this->assertEquals('api.opentok.com', $request->getHost());
103-
$this->assertEquals('https', $request->getScheme());
143+
$this->assertEquals('/v2/project/'.$this->API_KEY.'/archive/'.$this->archiveData['id'].'/stop', $request->getUri()->getPath());
144+
$this->assertEquals('api.opentok.com', $request->getUri()->getHost());
145+
$this->assertEquals('https', $request->getUri()->getScheme());
104146

105-
$contentType = $request->getHeader('Content-Type');
147+
$contentType = $request->getHeaderLine('Content-Type');
106148
$this->assertNotEmpty($contentType);
107149
$this->assertEquals('application/json', $contentType);
108150

109-
$authString = $request->getHeader('X-OPENTOK-AUTH');
151+
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
110152
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
111153

112154
// TODO: test the dynamically built User Agent string
113-
$userAgent = $request->getHeader('User-Agent');
155+
$userAgent = $request->getHeaderLine('User-Agent');
114156
$this->assertNotEmpty($userAgent);
115-
$this->assertStringStartsWith('OpenTok-PHP-SDK/3.1.1', $userAgent->__toString());
157+
$this->assertStringStartsWith('OpenTok-PHP-SDK/3.1.1', $userAgent);
116158

117159
// TODO: test the properties of the actual archive object
118160
$this->assertEquals('stopped', $this->archive->status);
@@ -122,39 +164,36 @@ public function testStopsArchive()
122164
public function testDeletesArchive()
123165
{
124166
// Arrange
125-
$mock = new MockPlugin();
126-
$response = MockPlugin::getMockFile(
127-
self::$mockBasePath . 'v2/project/APIKEY/archive/ARCHIVEID/delete'
128-
);
129-
$mock->addResponse($response);
130-
$this->client->addSubscriber($mock);
167+
$this->setupOTWithMocks([[
168+
'code' => 204
169+
]]);
170+
$this->setupArchives();
131171

132172
// Act
133173
// TODO: should this test be run on an archive object whose fixture has status 'available'
134174
// instead of 'started'?
135175
$success = $this->archive->delete();
136176

137177
// Assert
138-
$requests = $mock->getReceivedRequests();
139-
$this->assertCount(1, $requests);
178+
$this->assertCount(1, $this->historyContainer);
140179

141-
$request = $requests[0];
180+
$request = $this->historyContainer[0]['request'];
142181
$this->assertEquals('DELETE', strtoupper($request->getMethod()));
143-
$this->assertEquals('/v2/project/'.$this->API_KEY.'/archive/'.$this->archiveData['id'], $request->getPath());
144-
$this->assertEquals('api.opentok.com', $request->getHost());
145-
$this->assertEquals('https', $request->getScheme());
182+
$this->assertEquals('/v2/project/'.$this->API_KEY.'/archive/'.$this->archiveData['id'], $request->getUri()->getPath());
183+
$this->assertEquals('api.opentok.com', $request->getUri()->getHost());
184+
$this->assertEquals('https', $request->getUri()->getScheme());
146185

147-
$contentType = $request->getHeader('Content-Type');
186+
$contentType = $request->getHeaderLine('Content-Type');
148187
$this->assertNotEmpty($contentType);
149188
$this->assertEquals('application/json', $contentType);
150189

151-
$authString = $request->getHeader('X-OPENTOK-AUTH');
190+
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
152191
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
153192

154193
// TODO: test the dynamically built User Agent string
155-
$userAgent = $request->getHeader('User-Agent');
194+
$userAgent = $request->getHeaderLine('User-Agent');
156195
$this->assertNotEmpty($userAgent);
157-
$this->assertStringStartsWith('OpenTok-PHP-SDK/3.1.1', $userAgent->__toString());
196+
$this->assertStringStartsWith('OpenTok-PHP-SDK/3.1.1', $userAgent);
158197

159198
$this->assertTrue($success);
160199
// TODO: assert that all properties of the archive object were cleared
@@ -163,6 +202,8 @@ public function testDeletesArchive()
163202

164203
public function testAllowsUnknownProperties()
165204
{
205+
$this->setupOT();
206+
166207
// Set up fixtures
167208
$archiveData = array(
168209
'createdAt' => 1394394801000,
@@ -177,10 +218,7 @@ public function testAllowsUnknownProperties()
177218
'url' => null,
178219
'notarealproperty' => 'not a real value'
179220
);
180-
$apiKey = defined('API_KEY') ? API_KEY : '12345678';
181-
$apiSecret = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
182221

183-
$client = new Client();
184222
$archive = new Archive($archiveData, array(
185223
'apiKey' => $this->API_KEY,
186224
'apiSecret' => $this->API_SECRET,
@@ -195,6 +233,8 @@ public function testAllowsUnknownProperties()
195233
*/
196234
public function testRejectsBadArchiveData()
197235
{
236+
$this->setupOT();
237+
198238
// Set up fixtures
199239
$badArchiveData = array(
200240
'createdAt' => 'imnotanumber',
@@ -208,10 +248,7 @@ public function testRejectsBadArchiveData()
208248
'status' => 'started',
209249
'url' => null
210250
);
211-
$apiKey = defined('API_KEY') ? API_KEY : '12345678';
212-
$apiSecret = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
213251

214-
$client = new Client();
215252
$archive = new Archive($badArchiveData, array(
216253
'apiKey' => $this->API_KEY,
217254
'apiSecret' => $this->API_SECRET,
@@ -221,6 +258,8 @@ public function testRejectsBadArchiveData()
221258

222259
public function testAllowsPausedStatus()
223260
{
261+
$this->setupOT();
262+
224263
// Set up fixtures
225264
$archiveData = array(
226265
'createdAt' => 1394394801000,
@@ -234,10 +273,7 @@ public function testAllowsPausedStatus()
234273
'status' => 'paused',
235274
'url' => null,
236275
);
237-
$apiKey = defined('API_KEY') ? API_KEY : '12345678';
238-
$apiSecret = defined('API_SECRET') ? API_SECRET : '0123456789abcdef0123456789abcdef0123456789';
239276

240-
$client = new Client();
241277
$archive = new Archive($archiveData, array(
242278
'apiKey' => $this->API_KEY,
243279
'apiSecret' => $this->API_SECRET,
@@ -251,6 +287,8 @@ public function testAllowsPausedStatus()
251287
public function testSerializesToJson()
252288
{
253289
// Arrange
290+
$this->setupOT();
291+
$this->setupArchives();
254292

255293
// Act
256294
$archiveJson = $this->archive->toJson();
@@ -263,6 +301,8 @@ public function testSerializesToJson()
263301
public function testSerializedToArray()
264302
{
265303
// Arrange
304+
$this->setupOT();
305+
$this->setupArchives();
266306

267307
// Act
268308
$archiveArray = $this->archive->toArray();

0 commit comments

Comments
 (0)