|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace App\Tests\Service\Decoder; |
| 5 | + |
| 6 | +use App\Service\Cache; |
| 7 | +use App\Service\Decoder\ResponseDecoder; |
| 8 | +use Http\Client\Exception\NetworkException; |
| 9 | +use Http\Client\HttpClient; |
| 10 | +use Nyholm\Psr7\Request; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use Symfony\Component\Cache\Simple\FilesystemCache; |
| 14 | + |
| 15 | +class ResponseDecoderTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var HttpClient|\Prophecy\Prophecy\ObjectProphecy |
| 19 | + */ |
| 20 | + private $client; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Cache|\Prophecy\Prophecy\ObjectProphecy |
| 24 | + */ |
| 25 | + private $cache; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \Prophecy\Prophecy\ObjectProphecy|FilesystemCache |
| 29 | + */ |
| 30 | + private $simpleCache; |
| 31 | + |
| 32 | + protected function setUp() |
| 33 | + { |
| 34 | + $this->client = $this->prophesize(HttpClient::class); |
| 35 | + $this->cache = $this->prophesize(Cache::class); |
| 36 | + $this->simpleCache = $this->prophesize(FilesystemCache::class); |
| 37 | + $this->cache->__invoke()->willReturn($this->simpleCache->reveal()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testGetDecodedResponse() |
| 41 | + { |
| 42 | + $decoder = new ResponseDecoder(false, $this->client->reveal(), $this->cache->reveal()); |
| 43 | + |
| 44 | + $request = new Request('GET', 'endpoint/data.json'); |
| 45 | + $response = $this->prophesize(ResponseInterface::class); |
| 46 | + $response->getStatusCode()->willReturn(200); |
| 47 | + $response->getBody()->willReturn(json_encode(['json' => 'data'])); |
| 48 | + |
| 49 | + $this->client->sendRequest($request)->willReturn($response->reveal()); |
| 50 | + |
| 51 | + $this->assertSame( |
| 52 | + ['json' => 'data'], |
| 53 | + $decoder->getDecodedResponse($request) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function testGetDecodedResponseEmptyOnRequestError() |
| 58 | + { |
| 59 | + $decoder = new ResponseDecoder(false, $this->client->reveal(), $this->cache->reveal()); |
| 60 | + |
| 61 | + $request = new Request('GET', 'endpoint/data.json'); |
| 62 | + $response = $this->prophesize(ResponseInterface::class); |
| 63 | + $response->getStatusCode()->willReturn(500); |
| 64 | + $response->getBody()->willReturn(''); |
| 65 | + |
| 66 | + $this->client->sendRequest($request)->willReturn($response->reveal()); |
| 67 | + |
| 68 | + $this->assertSame( |
| 69 | + [], |
| 70 | + $decoder->getDecodedResponse($request) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testGetDecodedResponseFromCacheOnRequestError() |
| 75 | + { |
| 76 | + $decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal()); |
| 77 | + |
| 78 | + $request = new Request('GET', 'endpoint/data.json'); |
| 79 | + $response = $this->prophesize(ResponseInterface::class); |
| 80 | + $response->getStatusCode()->willReturn(500); |
| 81 | + $response->getBody()->willReturn(''); |
| 82 | + |
| 83 | + $this->simpleCache->has('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(true); |
| 84 | + $this->simpleCache->get('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(['json' => 'cache']); |
| 85 | + |
| 86 | + $this->client->sendRequest($request)->willReturn($response->reveal()); |
| 87 | + |
| 88 | + $this->assertSame( |
| 89 | + ['json' => 'cache'], |
| 90 | + $decoder->getDecodedResponse($request) |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public function testGetDecodedResponseCachesDataIfEnabled() |
| 95 | + { |
| 96 | + $decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal()); |
| 97 | + |
| 98 | + $request = new Request('GET', 'endpoint/data.json'); |
| 99 | + $response = $this->prophesize(ResponseInterface::class); |
| 100 | + $response->getStatusCode()->willReturn(200); |
| 101 | + $response->getBody()->willReturn(json_encode(['json' => 'cache'])); |
| 102 | + |
| 103 | + $this->simpleCache->has('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(false); |
| 104 | + $this->simpleCache->set('4429b090fd82239e188859ae626162e5e790b4db', ['json' => 'cache'])->shouldBeCalledOnce(); |
| 105 | + |
| 106 | + $this->client->sendRequest($request)->willReturn($response->reveal()); |
| 107 | + |
| 108 | + $this->assertSame( |
| 109 | + ['json' => 'cache'], |
| 110 | + $decoder->getDecodedResponse($request) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + public function testGetDecodedResponseFromCacheWhenNetworkFails() |
| 115 | + { |
| 116 | + $decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal()); |
| 117 | + |
| 118 | + $request = new Request('GET', 'endpoint/data.json'); |
| 119 | + |
| 120 | + $this->simpleCache->has('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(true); |
| 121 | + $this->simpleCache->get('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(['json' => 'cache']); |
| 122 | + |
| 123 | + $this->client->sendRequest($request)->willThrow(NetworkException::class); |
| 124 | + |
| 125 | + $this->assertSame( |
| 126 | + ['json' => 'cache'], |
| 127 | + $decoder->getDecodedResponse($request) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @throws \Http\Client\Exception |
| 133 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 134 | + * @expectedException \Http\Client\Exception\NetworkException |
| 135 | + */ |
| 136 | + public function testGetDecodedResponseThrowsNetworkExceptionWhenClientFailsAndNoCachedVersion() |
| 137 | + { |
| 138 | + $decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal()); |
| 139 | + $request = new Request('GET', 'endpoint/data.json'); |
| 140 | + |
| 141 | + $this->simpleCache->has('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(false); |
| 142 | + $this->client->sendRequest($request)->willThrow(NetworkException::class); |
| 143 | + |
| 144 | + $decoder->getDecodedResponse($request); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @todo this seems like a bug, we should not store in the cache invalid json? should we throw an error? |
| 149 | + * @throws \Http\Client\Exception |
| 150 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 151 | + */ |
| 152 | + public function testGetDecodedResponseReturnsBodyWhenJsonDecodingFails() |
| 153 | + { |
| 154 | + $decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal()); |
| 155 | + |
| 156 | + $request = new Request('GET', 'endpoint/data.json'); |
| 157 | + $response = $this->prophesize(ResponseInterface::class); |
| 158 | + $response->getStatusCode()->willReturn(200); |
| 159 | + $response->getBody()->willReturn('{invalid_json'); |
| 160 | + |
| 161 | + $this->client->sendRequest($request)->willReturn($response->reveal()); |
| 162 | + $this->simpleCache->set('4429b090fd82239e188859ae626162e5e790b4db', '{invalid_json')->shouldBeCalledOnce(); |
| 163 | + |
| 164 | + $this->assertSame( |
| 165 | + '{invalid_json', |
| 166 | + $decoder->getDecodedResponse($request) |
| 167 | + ); |
| 168 | + |
| 169 | + $this->markAsRisky(); |
| 170 | + } |
| 171 | +} |
0 commit comments