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
HttpTest.php
160 lines
| 1 | <?php |
| 2 | use PHPUnit\Framework\TestCase; |
| 3 | use Microsoft\Graph\Graph; |
| 4 | use Microsoft\Graph\Http\GraphRequest; |
| 5 | use Microsoft\Graph\Exception\GraphException; |
| 6 | use AmeliaGuzzleHttp\Handler\MockHandler; |
| 7 | use AmeliaGuzzleHttp\Psr7\Response; |
| 8 | use AmeliaGuzzleHttp\HandlerStack; |
| 9 | use AmeliaGuzzleHttp\Client; |
| 10 | |
| 11 | class HttpTest extends TestCase |
| 12 | { |
| 13 | public $client; |
| 14 | public $getRequest; |
| 15 | public $container; |
| 16 | |
| 17 | public function setUp(): void |
| 18 | { |
| 19 | $mock = new MockHandler([ |
| 20 | new Response(200, ['foo' => 'bar']), |
| 21 | new Response(200, ['foo' => 'bar']) |
| 22 | ]); |
| 23 | $this->container = []; |
| 24 | $history = AmeliaGuzzleHttp\Middleware::history($this->container); |
| 25 | $handler = HandlerStack::create($mock); |
| 26 | $handler->push($history); |
| 27 | $this->client = new Client(['handler' => $handler]); |
| 28 | |
| 29 | $this->getRequest = new GraphRequest("GET", "/endpoint", "token", "baseUrl", "version"); |
| 30 | } |
| 31 | |
| 32 | public function testGet() |
| 33 | { |
| 34 | $response = $this->getRequest->execute($this->client); |
| 35 | $code = $response->getStatus(); |
| 36 | |
| 37 | $this->assertEquals("200", $code); |
| 38 | } |
| 39 | |
| 40 | public function testPost() |
| 41 | { |
| 42 | $request = new GraphRequest("POST", "/endpoint", "token", "baseUrl", "version"); |
| 43 | $response = $request->execute($this->client); |
| 44 | $code = $response->getStatus(); |
| 45 | |
| 46 | $this->assertEquals("200", $code); |
| 47 | } |
| 48 | |
| 49 | public function testPut() |
| 50 | { |
| 51 | $request = new GraphRequest("PUT", "/endpoint", "token", "baseUrl", "version"); |
| 52 | $response = $request->execute($this->client); |
| 53 | $code = $response->getStatus(); |
| 54 | |
| 55 | $this->assertEquals("200", $code); |
| 56 | } |
| 57 | |
| 58 | public function testPatch() |
| 59 | { |
| 60 | $request = new GraphRequest("PATCH", "/endpoint", "token", "baseUrl", "version"); |
| 61 | $response = $request->execute($this->client); |
| 62 | $code = $response->getStatus(); |
| 63 | |
| 64 | $this->assertEquals("200", $code); |
| 65 | } |
| 66 | |
| 67 | public function testUpdate() |
| 68 | { |
| 69 | $request = new GraphRequest("UPDATE", "/endpoint", "token", "baseUrl", "version"); |
| 70 | $response = $request->execute($this->client); |
| 71 | $code = $response->getStatus(); |
| 72 | |
| 73 | $this->assertEquals("200", $code); |
| 74 | } |
| 75 | |
| 76 | public function testDelete() |
| 77 | { |
| 78 | $request = new GraphRequest("DELETE", "/endpoint", "token", "baseUrl", "version"); |
| 79 | $response = $request->execute($this->client); |
| 80 | $code = $response->getStatus(); |
| 81 | |
| 82 | $this->assertEquals("200", $code); |
| 83 | } |
| 84 | |
| 85 | public function testInvalidVerb() |
| 86 | { |
| 87 | $this->expectException(AmeliaGuzzleHttp\Exception\ClientException::class); |
| 88 | |
| 89 | $mock = new MockHandler([ |
| 90 | new Response(400, ['foo' => 'bar']) |
| 91 | ]); |
| 92 | |
| 93 | $handler = HandlerStack::create($mock); |
| 94 | $client = new Client(['handler' => $handler]); |
| 95 | |
| 96 | $request = new GraphRequest("OBLITERATE", "/endpoint", "token", "baseUrl", "version"); |
| 97 | $response = $request->execute($client); |
| 98 | $code = $response->getStatus(); |
| 99 | |
| 100 | $this->assertEquals("400", $code); |
| 101 | } |
| 102 | |
| 103 | public function testSendJson() |
| 104 | { |
| 105 | $body = json_encode(array('1' => 'a', '2' => 'b')); |
| 106 | |
| 107 | $request = $this->getRequest->attachBody($body); |
| 108 | $this->assertInstanceOf(GraphRequest::class, $request); |
| 109 | |
| 110 | $response = $request->execute($this->client); |
| 111 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 112 | $this->assertEquals($body, $this->container[0]['request']->getBody()->getContents()); |
| 113 | } |
| 114 | |
| 115 | public function testSendArray() |
| 116 | { |
| 117 | $body = array('1' => 'a', '2' => 'b'); |
| 118 | $request = $this->getRequest->attachBody($body); |
| 119 | $this->assertInstanceOf(GraphRequest::class, $request); |
| 120 | |
| 121 | $response = $request->execute($this->client); |
| 122 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 123 | $this->assertEquals(json_encode($body), $this->container[0]['request']->getBody()->getContents()); |
| 124 | } |
| 125 | |
| 126 | public function testSendObject() |
| 127 | { |
| 128 | $user = new Microsoft\Graph\Model\User(); |
| 129 | $user->setDisplayName('Bob Barker'); |
| 130 | $request = $this->getRequest->attachBody($user); |
| 131 | $this->assertInstanceOf(GraphRequest::class, $request); |
| 132 | |
| 133 | $response = $request->execute($this->client); |
| 134 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 135 | $this->assertEquals(json_encode($user->getProperties()), $this->container[0]['request']->getBody()->getContents()); |
| 136 | } |
| 137 | |
| 138 | public function testSendString() |
| 139 | { |
| 140 | $body = '{"1":"a","2":"b"}'; |
| 141 | $request = $this->getRequest->attachBody($body); |
| 142 | $this->assertInstanceOf(GraphRequest::class, $request); |
| 143 | |
| 144 | $response = $request->execute($this->client); |
| 145 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 146 | $this->assertEquals($body, $this->container[0]['request']->getBody()->getContents()); |
| 147 | } |
| 148 | |
| 149 | public function testSendStream() |
| 150 | { |
| 151 | $body = AmeliaGuzzleHttp\Psr7\Utils::streamFor('stream'); |
| 152 | $request = $this->getRequest->attachBody($body); |
| 153 | $this->assertInstanceOf(GraphRequest::class, $request); |
| 154 | |
| 155 | $response = $request->execute($this->client); |
| 156 | $this->assertInstanceOf(Microsoft\Graph\Http\GraphResponse::class, $response); |
| 157 | $this->assertEquals($body, $this->container[0]['request']->getBody()->getContents()); |
| 158 | } |
| 159 | } |
| 160 |