|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the xAPI package. |
| 5 | + * |
| 6 | + * (c) Christian Flothmann <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Xabbuh\XApi\Client\Tests\Request; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Http\Client\ClientInterface; |
| 16 | +use Psr\Http\Message\RequestFactoryInterface; |
| 17 | +use Psr\Http\Message\RequestInterface; |
| 18 | +use Xabbuh\XApi\Client\Request\Handler; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Christian Flothmann <[email protected]> |
| 22 | + */ |
| 23 | +class HandlerTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ClientInterface|\PHPUnit\Framework\MockObject\MockObject |
| 27 | + */ |
| 28 | + private $httpClient; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var RequestFactoryInterface|\PHPUnit\Framework\MockObject\MockObject |
| 32 | + */ |
| 33 | + private $requestFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject |
| 37 | + */ |
| 38 | + private $request; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Handler |
| 42 | + */ |
| 43 | + private $handler; |
| 44 | + |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $this->httpClient = $this->createMock(ClientInterface::class); |
| 48 | + $this->requestFactory = $this->createMock(RequestFactoryInterface::class); |
| 49 | + $this->request = $this->createMock(RequestInterface::class); |
| 50 | + $this->handler = new Handler($this->httpClient, $this->requestFactory, 'http://example.com/xapi', '1.0.3'); |
| 51 | + } |
| 52 | + |
| 53 | + public function testCreateRequestWithGetMethod() |
| 54 | + { |
| 55 | + $this->requestFactory->expects($this->once()) |
| 56 | + ->method('createRequest') |
| 57 | + ->with( |
| 58 | + 'GET', |
| 59 | + 'http://example.com/xapi/statements' |
| 60 | + ) |
| 61 | + ->willReturn($this->request); |
| 62 | + |
| 63 | + $this->request->expects($this->exactly(2)) |
| 64 | + ->method('withHeader') |
| 65 | + ->withConsecutive( |
| 66 | + ['X-Experience-API-Version', '1.0.3'], |
| 67 | + ['Content-Type', 'application/json'] |
| 68 | + ) |
| 69 | + ->willReturnOnConsecutiveCalls( |
| 70 | + $this->request, |
| 71 | + $this->request |
| 72 | + ); |
| 73 | + |
| 74 | + $request = $this->handler->createRequest('GET', '/statements'); |
| 75 | + |
| 76 | + $this->assertSame($this->request, $request); |
| 77 | + } |
| 78 | + |
| 79 | + public function testCreateRequestWithPostMethod() |
| 80 | + { |
| 81 | + $body = '{"id":"12345678-1234-5678-1234-567812345678"}'; |
| 82 | + $stream = $this->createMock(\Psr\Http\Message\StreamInterface::class); |
| 83 | + |
| 84 | + $this->requestFactory->expects($this->once()) |
| 85 | + ->method('createRequest') |
| 86 | + ->with( |
| 87 | + 'POST', |
| 88 | + 'http://example.com/xapi/statements' |
| 89 | + ) |
| 90 | + ->willReturn($this->request); |
| 91 | + |
| 92 | + $this->request->expects($this->exactly(2)) |
| 93 | + ->method('withHeader') |
| 94 | + ->withConsecutive( |
| 95 | + ['X-Experience-API-Version', '1.0.3'], |
| 96 | + ['Content-Type', 'application/json'] |
| 97 | + ) |
| 98 | + ->willReturnOnConsecutiveCalls( |
| 99 | + $this->request, |
| 100 | + $this->request |
| 101 | + ); |
| 102 | + |
| 103 | + $this->request->expects($this->once()) |
| 104 | + ->method('getBody') |
| 105 | + ->willReturn($stream); |
| 106 | + |
| 107 | + $stream->expects($this->once()) |
| 108 | + ->method('write') |
| 109 | + ->with($body) |
| 110 | + ->willReturn(strlen($body)); |
| 111 | + |
| 112 | + $stream->expects($this->once()) |
| 113 | + ->method('rewind'); |
| 114 | + |
| 115 | + $this->request->expects($this->once()) |
| 116 | + ->method('withBody') |
| 117 | + ->with($stream) |
| 118 | + ->willReturn($this->request); |
| 119 | + |
| 120 | + $request = $this->handler->createRequest('POST', '/statements', [], $body); |
| 121 | + |
| 122 | + $this->assertSame($this->request, $request); |
| 123 | + } |
| 124 | + |
| 125 | + public function testCreateRequestWithPutMethod() |
| 126 | + { |
| 127 | + $body = '{"id":"12345678-1234-5678-1234-567812345678"}'; |
| 128 | + $stream = $this->createMock(\Psr\Http\Message\StreamInterface::class); |
| 129 | + |
| 130 | + $this->requestFactory->expects($this->once()) |
| 131 | + ->method('createRequest') |
| 132 | + ->with( |
| 133 | + 'PUT', |
| 134 | + 'http://example.com/xapi/statements' |
| 135 | + ) |
| 136 | + ->willReturn($this->request); |
| 137 | + |
| 138 | + $this->request->expects($this->exactly(2)) |
| 139 | + ->method('withHeader') |
| 140 | + ->withConsecutive( |
| 141 | + ['X-Experience-API-Version', '1.0.3'], |
| 142 | + ['Content-Type', 'application/json'] |
| 143 | + ) |
| 144 | + ->willReturnOnConsecutiveCalls( |
| 145 | + $this->request, |
| 146 | + $this->request |
| 147 | + ); |
| 148 | + |
| 149 | + $this->request->expects($this->once()) |
| 150 | + ->method('getBody') |
| 151 | + ->willReturn($stream); |
| 152 | + |
| 153 | + $stream->expects($this->once()) |
| 154 | + ->method('write') |
| 155 | + ->with($body) |
| 156 | + ->willReturn(strlen($body)); |
| 157 | + |
| 158 | + $stream->expects($this->once()) |
| 159 | + ->method('rewind'); |
| 160 | + |
| 161 | + $this->request->expects($this->once()) |
| 162 | + ->method('withBody') |
| 163 | + ->with($stream) |
| 164 | + ->willReturn($this->request); |
| 165 | + |
| 166 | + $request = $this->handler->createRequest('PUT', '/statements', [], $body); |
| 167 | + |
| 168 | + $this->assertSame($this->request, $request); |
| 169 | + } |
| 170 | + |
| 171 | + public function testCreateRequestWithDeleteMethod() |
| 172 | + { |
| 173 | + $this->requestFactory->expects($this->once()) |
| 174 | + ->method('createRequest') |
| 175 | + ->with( |
| 176 | + 'DELETE', |
| 177 | + 'http://example.com/xapi/statements' |
| 178 | + ) |
| 179 | + ->willReturn($this->request); |
| 180 | + |
| 181 | + $this->request->expects($this->exactly(2)) |
| 182 | + ->method('withHeader') |
| 183 | + ->withConsecutive( |
| 184 | + ['X-Experience-API-Version', '1.0.3'], |
| 185 | + ['Content-Type', 'application/json'] |
| 186 | + ) |
| 187 | + ->willReturnOnConsecutiveCalls( |
| 188 | + $this->request, |
| 189 | + $this->request |
| 190 | + ); |
| 191 | + |
| 192 | + $request = $this->handler->createRequest('DELETE', '/statements'); |
| 193 | + |
| 194 | + $this->assertSame($this->request, $request); |
| 195 | + } |
| 196 | + |
| 197 | + public function testCreateRequestWithUrlParameters() |
| 198 | + { |
| 199 | + $this->requestFactory->expects($this->once()) |
| 200 | + ->method('createRequest') |
| 201 | + ->with( |
| 202 | + 'GET', |
| 203 | + 'http://example.com/xapi/statements?statementId=12345678-1234-5678-1234-567812345678&limit=10' |
| 204 | + ) |
| 205 | + ->willReturn($this->request); |
| 206 | + |
| 207 | + $this->request->expects($this->exactly(2)) |
| 208 | + ->method('withHeader') |
| 209 | + ->withConsecutive( |
| 210 | + ['X-Experience-API-Version', '1.0.3'], |
| 211 | + ['Content-Type', 'application/json'] |
| 212 | + ) |
| 213 | + ->willReturnOnConsecutiveCalls( |
| 214 | + $this->request, |
| 215 | + $this->request |
| 216 | + ); |
| 217 | + |
| 218 | + $request = $this->handler->createRequest( |
| 219 | + 'GET', |
| 220 | + '/statements', |
| 221 | + [ |
| 222 | + 'statementId' => '12345678-1234-5678-1234-567812345678', |
| 223 | + 'limit' => 10, |
| 224 | + ] |
| 225 | + ); |
| 226 | + |
| 227 | + $this->assertSame($this->request, $request); |
| 228 | + } |
| 229 | + |
| 230 | + public function testCreateRequestWithCustomHeaders() |
| 231 | + { |
| 232 | + $this->requestFactory->expects($this->once()) |
| 233 | + ->method('createRequest') |
| 234 | + ->with( |
| 235 | + 'GET', |
| 236 | + 'http://example.com/xapi/statements' |
| 237 | + ) |
| 238 | + ->willReturn($this->request); |
| 239 | + |
| 240 | + $this->request->expects($this->exactly(3)) |
| 241 | + ->method('withHeader') |
| 242 | + ->withConsecutive( |
| 243 | + ['Authorization', 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='], |
| 244 | + ['X-Experience-API-Version', '1.0.3'], |
| 245 | + ['Content-Type', 'application/json'] |
| 246 | + ) |
| 247 | + ->willReturnOnConsecutiveCalls( |
| 248 | + $this->request, |
| 249 | + $this->request, |
| 250 | + $this->request |
| 251 | + ); |
| 252 | + |
| 253 | + $request = $this->handler->createRequest( |
| 254 | + 'GET', |
| 255 | + '/statements', |
| 256 | + [], |
| 257 | + null, |
| 258 | + ['Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='] |
| 259 | + ); |
| 260 | + |
| 261 | + $this->assertSame($this->request, $request); |
| 262 | + } |
| 263 | + |
| 264 | + public function testCreateRequestWithCustomApiVersion() |
| 265 | + { |
| 266 | + $this->requestFactory->expects($this->once()) |
| 267 | + ->method('createRequest') |
| 268 | + ->with( |
| 269 | + 'GET', |
| 270 | + 'http://example.com/xapi/statements' |
| 271 | + ) |
| 272 | + ->willReturn($this->request); |
| 273 | + |
| 274 | + $this->request->expects($this->exactly(2)) |
| 275 | + ->method('withHeader') |
| 276 | + ->withConsecutive( |
| 277 | + ['X-Experience-API-Version', '1.0.2'], |
| 278 | + ['Content-Type', 'application/json'] |
| 279 | + ) |
| 280 | + ->willReturnOnConsecutiveCalls( |
| 281 | + $this->request, |
| 282 | + $this->request |
| 283 | + ); |
| 284 | + |
| 285 | + $request = $this->handler->createRequest( |
| 286 | + 'GET', |
| 287 | + '/statements', |
| 288 | + [], |
| 289 | + null, |
| 290 | + ['X-Experience-API-Version' => '1.0.2'] |
| 291 | + ); |
| 292 | + |
| 293 | + $this->assertSame($this->request, $request); |
| 294 | + } |
| 295 | + |
| 296 | + public function testCreateRequestWithCustomContentType() |
| 297 | + { |
| 298 | + $this->requestFactory->expects($this->once()) |
| 299 | + ->method('createRequest') |
| 300 | + ->with( |
| 301 | + 'GET', |
| 302 | + 'http://example.com/xapi/statements' |
| 303 | + ) |
| 304 | + ->willReturn($this->request); |
| 305 | + |
| 306 | + $this->request->expects($this->exactly(2)) |
| 307 | + ->method('withHeader') |
| 308 | + ->withConsecutive( |
| 309 | + ['Content-Type', 'application/xml'], |
| 310 | + ['X-Experience-API-Version', '1.0.3'] |
| 311 | + ) |
| 312 | + ->willReturnOnConsecutiveCalls( |
| 313 | + $this->request, |
| 314 | + $this->request |
| 315 | + ); |
| 316 | + |
| 317 | + $request = $this->handler->createRequest( |
| 318 | + 'GET', |
| 319 | + '/statements', |
| 320 | + [], |
| 321 | + null, |
| 322 | + ['Content-Type' => 'application/xml'] |
| 323 | + ); |
| 324 | + |
| 325 | + $this->assertSame($this->request, $request); |
| 326 | + } |
| 327 | + |
| 328 | + public function testCreateRequestWithInvalidMethod() |
| 329 | + { |
| 330 | + $this->expectException(\InvalidArgumentException::class); |
| 331 | + $this->expectExceptionMessage( |
| 332 | + '"PATCH" is no valid HTTP method (expected one of [GET, POST, PUT, DELETE]) in an xAPI context.' |
| 333 | + ); |
| 334 | + |
| 335 | + $this->handler->createRequest('PATCH', '/statements'); |
| 336 | + } |
| 337 | +} |
0 commit comments