|
| 1 | +<?php |
| 2 | +namespace Relay\Middleware; |
| 3 | + |
| 4 | +use Zend\Diactoros\Response; |
| 5 | +use Zend\Diactoros\ServerRequestFactory; |
| 6 | +use Zend\Diactoros\Stream; |
| 7 | + |
| 8 | + |
| 9 | +class JsonDecoderTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + |
| 12 | + protected $data = ['foo', 'bar']; |
| 13 | + |
| 14 | + |
| 15 | + public function requestProvider() |
| 16 | + { |
| 17 | + return [ |
| 18 | + ['GET' , 'application/json', []], |
| 19 | + ['GET' , null, []], |
| 20 | + ['POST' , 'application/json', $this->data], |
| 21 | + ['POST' , null, []], |
| 22 | + ['PUT' , 'application/json', $this->data], |
| 23 | + ['PUT' , null, []], |
| 24 | + ['PATCH' , 'application/json', $this->data], |
| 25 | + ['PATCH' , null, []], |
| 26 | + ['other' , 'application/json', $this->data], |
| 27 | + ['other' , null, []] |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @dataProvider requestProvider |
| 33 | + */ |
| 34 | + public function testParser($method, $contentType, $expected) |
| 35 | + { |
| 36 | + $json = json_encode($this->data); |
| 37 | + |
| 38 | + $stream = new Stream('php://temp', 'wb+'); |
| 39 | + $stream->write($json); |
| 40 | + |
| 41 | + $jsonDecoder = new JsonDecoder(); |
| 42 | + |
| 43 | + $response = new Response(); |
| 44 | + $request = ServerRequestFactory::fromGlobals() |
| 45 | + ->withMethod($method) |
| 46 | + ->withBody($stream); |
| 47 | + |
| 48 | + if ($contentType) { |
| 49 | + $request = $request->withHeader('Content-Type', $contentType); |
| 50 | + } |
| 51 | + |
| 52 | + $parsedRequest = $jsonDecoder( |
| 53 | + $request, |
| 54 | + $response, |
| 55 | + function ($request, $response) { |
| 56 | + return $request; |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + $this->assertEquals($expected, $parsedRequest->getParsedBody()); |
| 61 | + } |
| 62 | +} |
0 commit comments