forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphResponseTest.php
More file actions
152 lines (122 loc) · 5.35 KB
/
GraphResponseTest.php
File metadata and controls
152 lines (122 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
use PHPUnit\Framework\TestCase;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Http\GraphRequest;
use Microsoft\Graph\Http\GraphResponse;
use Microsoft\Graph\Exception\GraphException;
use Microsoft\Graph\Model;
class GraphResponseTest extends TestCase
{
public $client;
public $request;
public $response;
public $responseBody;
public function setUp(): void
{
$this->responseBody = array('body' => 'content', 'displayName' => 'Bob Barker');
$body = json_encode($this->responseBody);
$multiBody = json_encode(array('value' => array('1' => array('givenName' => 'Bob'), '2' => array('givenName' => 'Drew'))));
$valueBody = json_encode(array('value' => 'Bob Barker'));
$emptyMultiBody = json_encode(array('value' => array()));
$mock = new GuzzleHttp\Handler\MockHandler([
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $multiBody),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $valueBody),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $emptyMultiBody),
]);
$handler = GuzzleHttp\HandlerStack::create($mock);
$this->client = new GuzzleHttp\Client(['handler' => $handler]);
$this->request = new GraphRequest("GET", "/endpoint", "token", "baseUrl", "version");
$this->response = new GraphResponse($this->request, "{response}", "200", ["foo" => "bar"]);
}
public function testGetResponseAsObject()
{
$this->request->setReturnType(Model\User::class);
$response = $this->request->execute($this->client);
$this->assertInstanceOf(Model\User::class, $response);
$this->assertEquals($this->responseBody['displayName'], $response->getDisplayName());
}
public function testGetResponseHeaders()
{
$response = $this->request->execute($this->client);
$headers = $response->getHeaders();
$this->assertEquals(["foo" => ["bar"]], $headers);
}
public function testGetNextLink()
{
$body = json_encode(array('@odata.nextLink' => 'https://url.com/resource?$top=4&skip=4'));
$response = new GraphResponse($this->request, $body);
$nextLink = $response->getNextLink();
$this->assertEquals('https://url.com/resource?$top=4&skip=4', $nextLink);
}
public function testDecodeBody()
{
//Temporarily make decodeBody() public
$reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphResponse', '_decodeBody');
$reflectionMethod->setAccessible(true);
$response = new GraphResponse($this->request, json_encode($this->responseBody));
$decodedBody = $reflectionMethod->invokeArgs($response, array());
$this->assertEquals($this->responseBody, $decodedBody);
}
public function testDecodeEmptyBody()
{
//Temporarily make decodeBody() public
$reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphResponse', '_decodeBody');
$reflectionMethod->setAccessible(true);
$response = new GraphResponse($this->request);
$decodedBody = $reflectionMethod->invokeArgs($response, array());
$this->assertEquals(array(), $decodedBody);
}
public function testGetHeaders()
{
$headers = $this->response->getHeaders();
$this->assertEquals(["foo" => "bar"], $headers);
}
public function testGetBody()
{
$response = $this->request->execute($this->client);
$this->assertInstanceOf(GraphResponse::class, $response);
$body = $response->getBody();
$this->assertEquals($this->responseBody, $body);
}
public function testGetRawBody()
{
$response = $this->request->execute($this->client);
$body = $response->getRawBody();
$this->assertEquals(json_encode($this->responseBody), $body);
}
public function testGetStatus()
{
$response = $this->request->execute($this->client);
$this->assertEquals('200', $response->getStatus());
}
public function testGetMultipleObjects()
{
$this->request->execute($this->client);
$this->request->execute($this->client);
$hosts = $this->request->setReturnType(Model\User::class)->execute($this->client);
$this->assertIsArray($hosts);
$this->assertContainsOnlyInstancesOf(Model\User::class, $hosts);
$this->assertSame(array_values($hosts), $hosts);
$this->assertEquals(2, count($hosts));
$this->assertEquals("Bob", $hosts[0]->getGivenName());
}
public function testGetValueObject()
{
$this->request->execute($this->client);
$this->request->execute($this->client);
$this->request->execute($this->client);
$response = $this->request->setReturnType(Model\User::class)->execute($this->client);
$this->assertInstanceOf(Model\User::class, $response);
}
public function testGetZeroMultipleObjects()
{
$this->request->execute($this->client);
$this->request->execute($this->client);
$this->request->execute($this->client);
$this->request->execute($this->client);
$response = $this->request->setReturnType(Model\User::class)->execute($this->client);
$this->assertSame(array(), $response);
}
}