GraphCollectionRequestTest.php
1 year ago
GraphRequestTest.php
1 year ago
GraphResponseTest.php
1 year ago
HttpTest.php
1 year ago
MockClientFactory.php
1 year ago
StreamTest.php
1 year ago
GraphRequestTest.php
242 lines
| 1 | <?php |
| 2 | use AmeliaGuzzleHttp\Psr7\Response; |
| 3 | use PHPUnit\Framework\TestCase; |
| 4 | use Microsoft\Graph\Core\GraphConstants; |
| 5 | use Microsoft\Graph\Graph; |
| 6 | use Microsoft\Graph\Http\GraphRequest; |
| 7 | use Microsoft\Graph\Http\Test\MockClientFactory; |
| 8 | |
| 9 | class GraphRequestTest extends TestCase |
| 10 | { |
| 11 | protected $requests; |
| 12 | protected $defaultHeaders; |
| 13 | protected $client; |
| 14 | |
| 15 | public function setUp(): void |
| 16 | { |
| 17 | $this->requests = array( |
| 18 | new GraphRequest("GET", "/endpoint", "token", "baseUrl", "version"), |
| 19 | new GraphRequest("PATCH", "/endpoint?query", "token", "baseUrl", "version"), |
| 20 | new GraphRequest("GET", "/endpoint?query&query2", "token", "baseUrl", "version") |
| 21 | ); |
| 22 | |
| 23 | $this->defaultHeaders = array( |
| 24 | "Host" => "baseUrl", |
| 25 | "Content-Type" => "application/json", |
| 26 | "SdkVersion" => "Graph-php-" . GraphConstants::SDK_VERSION, |
| 27 | "Authorization" => "Bearer token" |
| 28 | ); |
| 29 | |
| 30 | $body = json_encode(array('body' => 'content')); |
| 31 | $mock = new AmeliaGuzzleHttp\Handler\MockHandler([ |
| 32 | new AmeliaGuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body), |
| 33 | new AmeliaGuzzleHttp\Psr7\Response(201, ['foo' => 'bar']), |
| 34 | new AmeliaGuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body) |
| 35 | ]); |
| 36 | $handler = AmeliaGuzzleHttp\HandlerStack::create($mock); |
| 37 | $this->client = new AmeliaGuzzleHttp\Client(['handler' => $handler]); |
| 38 | } |
| 39 | |
| 40 | public function testSetReturnType() |
| 41 | { |
| 42 | //Temporarily make getRequestUrl() public |
| 43 | $reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphRequest', '_getRequestUrl'); |
| 44 | $reflectionMethod->setAccessible(true); |
| 45 | |
| 46 | $graph = new Graph(); |
| 47 | $graph->setApiVersion('beta'); |
| 48 | $graph->setAccessToken('token'); |
| 49 | $request = $graph->createRequest("get", "/me"); |
| 50 | $graph->setApiVersion('v1.0'); |
| 51 | |
| 52 | $requestUrl = $reflectionMethod->invokeArgs($request, array()); |
| 53 | $this->assertEquals($requestUrl, "beta/me"); |
| 54 | |
| 55 | $request2 = $graph->createRequest("get", "/me"); |
| 56 | $requestUrl = $reflectionMethod->invokeArgs($request2, array()); |
| 57 | $this->assertEquals("v1.0/me", $requestUrl); |
| 58 | } |
| 59 | |
| 60 | public function testAddHeaders() |
| 61 | { |
| 62 | $testHeader = array("test" => "header"); |
| 63 | $request = $this->requests[0]->addHeaders($testHeader); |
| 64 | $headers = $request->getHeaders(); |
| 65 | |
| 66 | $expectedHeaders = array( |
| 67 | "Host" => "baseUrl", |
| 68 | "Content-Type" => "application/json", |
| 69 | "SdkVersion" => "Graph-php-" . GraphConstants::SDK_VERSION, |
| 70 | "Authorization" => "Bearer token", |
| 71 | "test" => "header" |
| 72 | ); |
| 73 | |
| 74 | $this->assertEquals($expectedHeaders, $headers); |
| 75 | } |
| 76 | |
| 77 | public function testCustomHeadersOverwriteDefaults() |
| 78 | { |
| 79 | $testHeader = array("Content-Type" => "application/x-www-form-urlencoded"); |
| 80 | $request = $this->requests[0]->addHeaders($testHeader); |
| 81 | $headers = $request->getHeaders(); |
| 82 | |
| 83 | $expectedHeaders = array( |
| 84 | "Host" => "baseUrl", |
| 85 | "Content-Type" => "application/x-www-form-urlencoded", |
| 86 | "SdkVersion" => "Graph-php-" . GraphConstants::SDK_VERSION, |
| 87 | "Authorization" => "Bearer token" |
| 88 | ); |
| 89 | |
| 90 | $this->assertEquals($expectedHeaders, $headers); |
| 91 | } |
| 92 | |
| 93 | public function testDefaultHeaders() |
| 94 | { |
| 95 | $headers = $this->requests[0]->getHeaders(); |
| 96 | |
| 97 | $this->assertEquals($this->defaultHeaders, $headers); |
| 98 | } |
| 99 | |
| 100 | public function testGetBody() |
| 101 | { |
| 102 | $testBody = json_encode(array('body' => 'content')); |
| 103 | $this->requests[0]->attachBody($testBody); |
| 104 | $body = $this->requests[0]->getBody(); |
| 105 | $this->assertEquals($testBody, $body); |
| 106 | } |
| 107 | |
| 108 | public function testAttachPropertyDictionary() |
| 109 | { |
| 110 | $model = new Microsoft\Graph\Model\User(array("id" => 1, "manager" => new Microsoft\Graph\Model\User(array("id" => 2)))); |
| 111 | $this->requests[0]->attachBody($model); |
| 112 | $body = $this->requests[0]->getBody(); |
| 113 | $this->assertEquals('{"id":1,"manager":{"id":2}}', $body); |
| 114 | } |
| 115 | |
| 116 | public function testAttachDoubleNestedDictionary() |
| 117 | { |
| 118 | $testBody = json_encode(array("data"=> array("key" => array("key2" => "val")))); |
| 119 | $this->requests[0]->attachBody(array("data"=> array("key" => array("key2" => "val")))); |
| 120 | $body = $this->requests[0]->getBody(); |
| 121 | $this->assertEquals($testBody, $body); |
| 122 | } |
| 123 | |
| 124 | public function testSetTimeout() |
| 125 | { |
| 126 | $this->requests[0]->setTimeout('200'); |
| 127 | $this->assertEquals('200', $this->requests[0]->getTimeout()); |
| 128 | } |
| 129 | |
| 130 | public function testDefaultTimeout() |
| 131 | { |
| 132 | $this->assertEquals('100', $this->requests[0]->getTimeout()); |
| 133 | } |
| 134 | |
| 135 | public function testCreateGuzzleClient() |
| 136 | { |
| 137 | $reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphRequest', 'createGuzzleClient'); |
| 138 | $reflectionMethod->setAccessible(true); |
| 139 | |
| 140 | $request = $this->requests[0]; |
| 141 | $client = $reflectionMethod->invokeArgs($request, array()); |
| 142 | |
| 143 | $this->assertInstanceOf(AmeliaGuzzleHttp\Client::class, $client); |
| 144 | |
| 145 | } |
| 146 | |
| 147 | public function testExecute() |
| 148 | { |
| 149 | $response = $this->requests[0]->execute($this->client); |
| 150 | |
| 151 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 152 | } |
| 153 | |
| 154 | public function testExecuteWithTimeout() |
| 155 | { |
| 156 | $response = $this->requests[0]->setTimeout(300)->execute($this->client); |
| 157 | |
| 158 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 159 | } |
| 160 | |
| 161 | public function testExecuteAsync() |
| 162 | { |
| 163 | $promise = $this->requests[0] |
| 164 | ->executeAsync($this->client); |
| 165 | $this->assertInstanceOf(AmeliaGuzzleHttp\Promise\PromiseInterface::class, $promise); |
| 166 | |
| 167 | $promise = $this->requests[1] |
| 168 | ->executeAsync($this->client); |
| 169 | $this->assertInstanceOf(AmeliaGuzzleHttp\Promise\PromiseInterface::class, $promise); |
| 170 | |
| 171 | $promise = $this->requests[0] |
| 172 | ->executeAsync($this->client); |
| 173 | $promise2 = $this->requests[2] |
| 174 | ->executeAsync($this->client); |
| 175 | |
| 176 | $response = \AmeliaGuzzleHttp\Promise\unwrap(array($promise)); |
| 177 | foreach ($response as $responseItem) { |
| 178 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $responseItem); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | public function testGetRequestUrl() |
| 183 | { |
| 184 | //Temporarily make getRequestUrl() public |
| 185 | $reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphRequest', '_getRequestUrl'); |
| 186 | $reflectionMethod->setAccessible(true); |
| 187 | |
| 188 | $requestUrl = $reflectionMethod->invokeArgs($this->requests[0], array()); |
| 189 | $this->assertEquals($requestUrl, "version/endpoint"); |
| 190 | } |
| 191 | |
| 192 | public function testGetConcatenator() |
| 193 | { |
| 194 | //Temporarily make getConcatenator() public |
| 195 | $reflectionMethod = new ReflectionMethod('Microsoft\Graph\Http\GraphRequest', 'getConcatenator'); |
| 196 | $reflectionMethod->setAccessible(true); |
| 197 | |
| 198 | $concatenator = $reflectionMethod->invokeArgs($this->requests[0], array()); |
| 199 | $this->assertEquals($concatenator, "?"); |
| 200 | |
| 201 | $concatenator = $reflectionMethod->invokeArgs($this->requests[1], array()); |
| 202 | $this->assertEquals($concatenator, "&"); |
| 203 | |
| 204 | $concatenator = $reflectionMethod->invokeArgs($this->requests[2], array()); |
| 205 | $this->assertEquals($concatenator, "&"); |
| 206 | } |
| 207 | |
| 208 | public function testExecuteWith4xxResponse() |
| 209 | { |
| 210 | $this->expectException(AmeliaGuzzleHttp\Exception\ClientException::class); |
| 211 | $mockResponse = array(new Response(400)); |
| 212 | $client = MockClientFactory::create(['http_errors' => true], $mockResponse); |
| 213 | $this->requests[0]->execute($client); |
| 214 | } |
| 215 | |
| 216 | public function testExecuteWith5xxResponse() |
| 217 | { |
| 218 | $this->expectException(AmeliaGuzzleHttp\Exception\ServerException::class); |
| 219 | $mockResponse = array(new Response(500)); |
| 220 | $client = MockClientFactory::create(['http_errors' => true], $mockResponse); |
| 221 | $this->requests[0]->execute($client); |
| 222 | } |
| 223 | |
| 224 | public function testExecuteAsyncWithBadResponseTriggersNotice() |
| 225 | { |
| 226 | $this->expectNotice(); |
| 227 | $mockResponse = array(new Response(400)); |
| 228 | $client = MockClientFactory::create(['http_errors' => true], $mockResponse); |
| 229 | $promise = $this->requests[0]->executeAsync($client); |
| 230 | $promise->wait(); |
| 231 | } |
| 232 | |
| 233 | public function testExecuteAsyncWithBadResponseReturnsNull() |
| 234 | { |
| 235 | $mockResponse = array(new Response(400)); |
| 236 | $client = MockClientFactory::create(['http_errors' => true], $mockResponse); |
| 237 | $promise = $this->requests[0]->executeAsync($client); |
| 238 | $result = @$promise->wait(); |
| 239 | $this->assertNull($result); |
| 240 | } |
| 241 | } |
| 242 |