Source of file APIClientTest.php
Size: 3,862 Bytes - Last Modified: 2021-12-23T10:28:15+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Service/APIClientTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | <?php namespace SilverStripe\CKANRegistry\Tests\Service; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; use Psr\Http\Message\StreamInterface; use RuntimeException; use SilverStripe\CKANRegistry\Model\Resource; use SilverStripe\CKANRegistry\Service\APIClient; use SilverStripe\Dev\SapphireTest; class APIClientTest extends SapphireTest { /** * @var Client */ protected $guzzleClient; /** * @var Response */ protected $response; /** * @var StreamInterface */ protected $mockBody; /** * @var Resource */ protected $resource; protected function setUp(): void { parent::setUp(); $this->guzzleClient = $this->createMock(Client::class); $this->response = $this->createMock(Response::class); $this->mockBody = $this->createMock(StreamInterface::class); $this->resource = new Resource(); } public function testExceptionThrownOnInvalidHttpStatusCode() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('CKAN API is not available. Error code 123'); $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response); $this->response->expects($this->once())->method('getStatusCode')->willReturn(123); $client = new APIClient(); $client->setGuzzleClient($this->guzzleClient); $client->getData($this->resource); } public function testExceptionThrownOnNonJsonResponse() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('CKAN API returns an invalid response: Content-Type is not JSON'); $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response); $this->response->expects($this->once())->method('getStatusCode')->willReturn(200); $this->response->expects($this->once())->method('getHeader')->with('Content-Type')->willReturn(['junk']); $client = new APIClient(); $client->setGuzzleClient($this->guzzleClient); $client->getData($this->resource); } public function testExceptionThrownOnUnsuccessfulResponse() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('CKAN API returns an invalid response: Responded as invalid'); $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response); $this->response->expects($this->once())->method('getStatusCode')->willReturn(200); $this->response->expects($this->once())->method('getHeader')->willReturn(['application/json']); $this->response->expects($this->once())->method('getBody')->willReturn($this->mockBody); $this->mockBody->expects($this->once())->method('getContents')->willReturn('{ "success": false }'); $client = new APIClient(); $client->setGuzzleClient($this->guzzleClient); $client->getData($this->resource); } public function testReturnsResponseData() { $this->guzzleClient->expects($this->once())->method('send')->willReturn($this->response); $this->response->expects($this->once())->method('getStatusCode')->willReturn(200); $this->response->expects($this->once())->method('getHeader')->willReturn(['application/json']); $this->response->expects($this->once())->method('getBody')->willReturn($this->mockBody); $this->mockBody->expects($this->once())->method('getContents')->willReturn('{ "success": true, "data": "test" }'); $client = new APIClient(); $client->setGuzzleClient($this->guzzleClient); $result = $client->getData($this->resource); $this->assertSame('test', $result['data'], 'Raw response body should be returned'); } } |