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