Skip to content

Commit 103f253

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #7 from microsoftgraph/improved-unit-testing
Add unit tests for uncovered code in HTTP
2 parents ec0bd46 + 2d788f6 commit 103f253

6 files changed

Lines changed: 146 additions & 35 deletions

File tree

phpunit.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<filter>
99
<whitelist addUncoveredFilesFromWhitelist="true">
1010
<directory suffix=".php">src</directory>
11-
<exclude><directory suffix=".php">src/Model</directory></exclude>
11+
<exclude>
12+
<directory suffix=".php">src/Model</directory>
13+
<directory suffix=".php">src/Core</directory>
14+
</exclude>
1215
</whitelist>
1316
</filter>
1417
<logging>

src/Http/GraphRequest.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,12 @@ public function execute($client = null)
252252
}
253253

254254
// Wrap response in GraphResponse layer
255-
try {
256-
$response = new GraphResponse(
257-
$this,
258-
$result->getBody()->getContents(),
259-
$result->getStatusCode(),
260-
$result->getHeaders()
261-
);
262-
} catch (GraphException $e) {
263-
throw new GraphException(GraphConstants::UNABLE_TO_PARSE_RESPONSE);
264-
}
255+
$response = new GraphResponse(
256+
$this,
257+
$result->getBody()->getContents(),
258+
$result->getStatusCode(),
259+
$result->getHeaders()
260+
);
265261

266262
// If no return type is specified, return GraphResponse
267263
$returnObj = $response;
@@ -336,17 +332,22 @@ public function download($path, $client = null)
336332
$client = $this->createGuzzleClient();
337333
}
338334
try {
339-
$file = fopen($path, 'w');
335+
if (file_exists($path) && is_writeable($path)) {
336+
$file = fopen($path, 'w');
340337

341-
$client->request(
342-
$this->requestType,
343-
$this->_getRequestUrl(),
344-
[
345-
'body' => $this->requestBody,
346-
'sink' => $file
347-
]
348-
);
349-
fclose($file);
338+
$client->request(
339+
$this->requestType,
340+
$this->_getRequestUrl(),
341+
[
342+
'body' => $this->requestBody,
343+
'sink' => $file
344+
]
345+
);
346+
fclose($file);
347+
} else {
348+
throw new GraphException(GraphConstants::INVALID_FILE);
349+
}
350+
350351
} catch(GraphException $e) {
351352
throw new GraphException(GraphConstants::INVALID_FILE);
352353
}
@@ -369,10 +370,14 @@ public function upload($path, $client = null)
369370
$client = $this->createGuzzleClient();
370371
}
371372
try {
372-
$file = fopen($path, 'r');
373-
$stream = \GuzzleHttp\Psr7\stream_for($file);
374-
$this->requestBody = $stream;
375-
return $this->execute($client);
373+
if (file_exists($path) && is_readable($path)) {
374+
$file = fopen($path, 'r');
375+
$stream = \GuzzleHttp\Psr7\stream_for($file);
376+
$this->requestBody = $stream;
377+
return $this->execute($client);
378+
} else {
379+
throw new GraphException(GraphConstants::INVALID_FILE);
380+
}
376381
} catch(GraphException $e) {
377382
throw new GraphException(GraphConstants::INVALID_FILE);
378383
}

src/Http/GraphResponse.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,13 @@ public function getSkipToken()
157157
{
158158
if (array_key_exists("@odata.nextLink", $this->getBody())) {
159159
$nextLink = $this->getBody()['@odata.nextLink'];
160-
$url = explode("?", $nextLink)[1];
161-
$url = explode("skiptoken=", $url);
162-
if (count($url) > 1) {
163-
return $url[1];
160+
if (stripos($nextLink, "?") !== FALSE) {
161+
$url = explode("?", $nextLink)[1];
162+
if (stripos($url, "skiptoken=") !== FALSE) {
163+
$url = explode("skiptoken=", $url);
164+
return $url[1];
165+
}
164166
}
165-
return null;
166167
}
167168
return null;
168169
}

tests/Http/GraphRequestTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public function setUp()
2929
$mock = new GuzzleHttp\Handler\MockHandler([
3030
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
3131
new GuzzleHttp\Psr7\Response(201, ['foo' => 'bar']),
32-
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
33-
new GuzzleHttp\Psr7\Response(201, ['foo' => 'bar'])
32+
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body)
3433
]);
3534
$handler = GuzzleHttp\HandlerStack::create($mock);
3635
$this->client = new GuzzleHttp\Client(['handler' => $handler]);
@@ -96,12 +95,61 @@ public function testDefaultHeaders()
9695
$this->assertEquals($this->defaultHeaders, $headers);
9796
}
9897

98+
public function testGetBody()
99+
{
100+
$testBody = json_encode(array('body' => 'content'));
101+
$this->requests[0]->attachBody($testBody);
102+
$body = $this->requests[0]->getBody();
103+
$this->assertEquals($testBody, $body);
104+
}
105+
106+
public function testAttachPropertyDictionary()
107+
{
108+
$model = new Microsoft\Graph\Model\User(array("id" => 1, "manager" => new Microsoft\Graph\Model\User(array("id" => 2))));
109+
$this->requests[0]->attachBody($model);
110+
$body = $this->requests[0]->getBody();
111+
$this->assertEquals('{user:{"id":1,"manager":{"id":2}}}', $body);
112+
}
113+
114+
public function testAttachDoubleNestedDictionary()
115+
{
116+
$testBody = json_encode(array("data"=> array("key" => array("key2" => "val"))));
117+
$this->requests[0]->attachBody(array("data"=> array("key" => array("key2" => "val"))));
118+
$body = $this->requests[0]->getBody();
119+
$this->assertEquals($testBody, $body);
120+
}
121+
122+
public function testSetTimeout()
123+
{
124+
$this->requests[0]->setTimeout('200');
125+
$this->assertAttributeEquals('200', 'timeout', $this->requests[0]);
126+
}
127+
128+
public function testCreateGuzzleClient()
129+
{
130+
$reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphRequest', 'createGuzzleClient');
131+
$reflectionMethod->setAccessible(true);
132+
133+
$request = $this->requests[0];
134+
$client = $reflectionMethod->invokeArgs($request, array());
135+
136+
$this->assertInstanceOf(GuzzleHttp\Client::class, $client);
137+
}
138+
99139
public function testExecute()
100140
{
101141
$response = $this->requests[0]->execute($this->client);
102142

103143
$this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response);
104144
}
145+
146+
public function testReturnStream()
147+
{
148+
$this->requests[0]->setReturnType('stream');
149+
$response = $this->requests[0]->execute($this->client);
150+
151+
$this->assertInstanceOf(GuzzleHttp\Psr7\Response::class, $response);
152+
}
105153

106154
public function testExecuteAsync()
107155
{

tests/Http/GraphResponseTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@ class GraphResponseTest extends TestCase
1010
{
1111
public $client;
1212
public $request;
13+
public $response;
1314
public $responseBody;
1415

1516
public function setUp()
1617
{
1718
$this->responseBody = array('body' => 'content', 'displayName' => 'Bob Barker');
18-
$body = json_encode($this->responseBody);
19+
$this->collectionBody = array("value" => array(array('displayName' => 'Bob Barker'), array('displayName' => 'Drew Carey')));
20+
1921
$mock = new GuzzleHttp\Handler\MockHandler([
20-
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
21-
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body)
22+
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], json_encode($this->responseBody)),
23+
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], json_encode($this->collectionBody))
2224
]);
2325
$handler = GuzzleHttp\HandlerStack::create($mock);
2426
$this->client = new GuzzleHttp\Client(['handler' => $handler]);
2527

2628
$this->request = new GraphRequest("GET", "/endpoint", "token", "baseUrl", "/version");
29+
$this->response = new GraphResponse($this->request, "{response}", "200", ["foo" => "bar"]);
2730
}
2831

2932
public function testGetResponseAsObject()
@@ -33,7 +36,17 @@ public function testGetResponseAsObject()
3336

3437
$this->assertInstanceOf(Model\User::class, $response);
3538
$this->assertEquals($this->responseBody['displayName'], $response->getDisplayName());
39+
}
40+
41+
public function testGetResponseAsListOfObjects()
42+
{
43+
$this->request->setReturnType(Model\User::class);
44+
$response = $this->request->execute($this->client);
45+
$response = $this->request->execute($this->client);
3646

47+
$this->assertContainsOnlyInstancesOf(Model\User::class, $response);
48+
$this->assertEquals('Drew Carey', $response[1]->getDisplayName());
49+
$this->assertEquals(2, count($response));
3750
}
3851

3952
public function testGetSkipToken()
@@ -49,6 +62,19 @@ public function testGetSkipToken()
4962
$this->assertEquals('10', $token);
5063
}
5164

65+
public function testNoSkipToken()
66+
{
67+
//Temporarily make getSkipToken() public
68+
$reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphResponse', 'getSkipToken');
69+
$reflectionMethod->setAccessible(true);
70+
71+
$body = json_encode(array('@odata.nextLink' => 'https://url.com/resource'));
72+
$response = new GraphResponse($this->request, $body);
73+
74+
$token = $reflectionMethod->invokeArgs($response, array());
75+
$this->assertNull($token);
76+
}
77+
5278
public function testDecodeBody()
5379
{
5480
//Temporarily make decodeBody() public
@@ -73,6 +99,12 @@ public function testDecodeEmptyBody()
7399
$this->assertEquals(array(), $decodedBody);
74100
}
75101

102+
public function testGetHeaders()
103+
{
104+
$headers = $this->response->getHeaders();
105+
$this->assertEquals(["foo" => "bar"], $headers);
106+
}
107+
76108
public function testGetBody()
77109
{
78110
$response = $this->request->execute($this->client);

tests/Http/StreamTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public function testUpload()
4444
$this->assertEquals($this->container[0]['request']->getBody()->getContents(), $file->getContent());
4545
}
4646

47+
public function testInvalidUpload()
48+
{
49+
$this->expectException(Microsoft\Graph\Exception\GraphException::class);
50+
51+
$file = new VfsStreamFile('foo.txt', 0000);
52+
$this->root->addChild($file);
53+
54+
$request = new GraphRequest("GET", "/me", "token", "url", "/v1.0");
55+
$request->upload($file->url(), $this->client);
56+
}
57+
4758
public function testDownload()
4859
{
4960
$request = new GraphRequest("GET", "/me", "token", "url", "/v1.0");
@@ -54,6 +65,17 @@ public function testDownload()
5465
$this->assertEquals($this->body, $file->getContent());
5566
}
5667

68+
public function testInvalidDownload()
69+
{
70+
$this->expectException(Microsoft\Graph\Exception\GraphException::class);
71+
72+
$file = new VfsStreamFile('foo.txt', 0000);
73+
$this->root->addChild($file);
74+
75+
$request = new GraphRequest("GET", "/me", "token", "url", "/v1.0");
76+
$request->download($file->url(), $this->client);
77+
}
78+
5779
public function testSetReturnStream()
5880
{
5981
$request = new GraphRequest("GET", "/me", "token", "url", "/v1.0");

0 commit comments

Comments
 (0)