From febfe05a398aa58aecb33e6e17540a5f199789ff Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Sun, 29 Jan 2017 14:37:16 +0200 Subject: [PATCH] Improve ResponseException title #9 (#10) * Improve ResponseException title #9 Fix slim exception response Add coveralls Fix lint errors --- CHANGELOG.md | 2 ++ README.md | 4 ++++ composer.json | 4 +++- src/AbstractEndpoint.php | 16 +++++++++------- src/Directive/Directive.php | 3 ++- src/Directive/Page.php | 2 +- src/Endpoint/Get.php | 2 +- src/Exceptions/ResponseException.php | 13 +++++++++++-- src/Response/Collection.php | 2 +- src/Response/Errors.php | 8 ++++++++ src/Response/Response.php | 1 - tests/{src/Endpoint => APP}/BaseEndpoint.php | 3 ++- tests/APP/public/index.php | 11 ++++++----- tests/src/Endpoint/EndpointPatchTest.php | 14 ++------------ tests/src/Endpoint/GetByIdTest.php | 4 +--- tests/src/Endpoint/GetTest.php | 3 +-- tests/src/Endpoint/PostTest.php | 19 +++---------------- tests/src/EndpointTest.php | 5 ----- 18 files changed, 57 insertions(+), 59 deletions(-) create mode 100644 CHANGELOG.md rename tests/{src/Endpoint => APP}/BaseEndpoint.php (96%) diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..510000f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +## 2.0.0 +- Add AbstractEndpoint, to allow extending Endpoint class \ No newline at end of file diff --git a/README.md b/README.md index 1332f63..20427d6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # jsonapi-client JSONAPI client, helps creating SDK to consume your JSON RESTful APIs +[![Build Status](https://travis-ci.org/phramework/jsonapi-client.svg?branch=2.x)](https://travis-ci.org/phramework/jsonapi-client) + +[![Coverage Status](https://coveralls.io/repos/github/phramework/jsonapi-client/badge.svg?branch=2.x)](https://coveralls.io/github/phramework/jsonapi-client?branch=2.x) + ## Usage Require package using composer ```bash diff --git a/composer.json b/composer.json index e09f000..4e9489b 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,12 @@ }, "require-dev": { "squizlabs/php_codesniffer": "*", + "satooshi/php-coveralls": "^1.0", "phpunit/phpunit": "5.*", "slim/slim": "^3.0", "ext-pdo_sqlite": "*", - "oscarotero/psr7-middlewares": "^3.16" + "oscarotero/psr7-middlewares": "^3.16", + "codacy/coverage": "^1.0" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/AbstractEndpoint.php b/src/AbstractEndpoint.php index d0b69b3..e092eb5 100644 --- a/src/AbstractEndpoint.php +++ b/src/AbstractEndpoint.php @@ -1,5 +1,5 @@ limit; } -} \ No newline at end of file +} diff --git a/src/Endpoint/Get.php b/src/Endpoint/Get.php index 782281b..55d05f1 100644 --- a/src/Endpoint/Get.php +++ b/src/Endpoint/Get.php @@ -72,4 +72,4 @@ public function get( return (new Collection($response)); } -} \ No newline at end of file +} diff --git a/src/Exceptions/ResponseException.php b/src/Exceptions/ResponseException.php index 274ef45..5350652 100644 --- a/src/Exceptions/ResponseException.php +++ b/src/Exceptions/ResponseException.php @@ -36,9 +36,18 @@ class ResponseException extends \Exception */ public function __construct(Errors $errors) { - $this->errors = $errors; + $title = 'Response exception'; + + if (isset( + $errors->getErrors()[0], + $errors->getErrors()[0]->title + )) { + $title = $errors->getErrors()[0]->title; + } - parent::__construct('Response exception'); + parent::__construct($title); + + $this->errors = $errors; } /** diff --git a/src/Response/Collection.php b/src/Response/Collection.php index 3023476..a83c73b 100644 --- a/src/Response/Collection.php +++ b/src/Response/Collection.php @@ -82,4 +82,4 @@ public function offsetGet($offset) { return isset($this->data[$offset]) ? $this->data[$offset] : null; } -} \ No newline at end of file +} diff --git a/src/Response/Errors.php b/src/Response/Errors.php index 4506886..3066797 100644 --- a/src/Response/Errors.php +++ b/src/Response/Errors.php @@ -18,6 +18,7 @@ namespace Phramework\JSONAPI\Client\Response; use Phramework\JSONAPI\Client\Error; +use Psr\Http\Message\ResponseInterface; /** * @author Xenofon Spafaridis @@ -31,6 +32,13 @@ class Errors extends Response */ protected $errors; + public function __construct(ResponseInterface $response) + { + $this->errors = []; + + parent::__construct($response); + } + /** * @return \Phramework\JSONAPI\Client\Error[] */ diff --git a/src/Response/Response.php b/src/Response/Response.php index 0b0cd18..d361d49 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -51,7 +51,6 @@ public function __construct(ResponseInterface $response) $body = json_decode($response->getBody()->getContents()); if ($body) { - $members = array_keys(get_object_vars($this)); foreach ($members as $member) { diff --git a/tests/src/Endpoint/BaseEndpoint.php b/tests/APP/BaseEndpoint.php similarity index 96% rename from tests/src/Endpoint/BaseEndpoint.php rename to tests/APP/BaseEndpoint.php index f926a29..e68127a 100644 --- a/tests/src/Endpoint/BaseEndpoint.php +++ b/tests/APP/BaseEndpoint.php @@ -15,10 +15,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -namespace Phramework\JSONAPI\Client; +namespace Phramework\JSONAPI\APP; use Phramework\JSONAPI\Client\Directive\IncludeRelationship; use Phramework\JSONAPI\Client\Directive\Page; +use Phramework\JSONAPI\Client\Endpoint; trait BaseEndpoint { diff --git a/tests/APP/public/index.php b/tests/APP/public/index.php index f3d2562..93d316f 100755 --- a/tests/APP/public/index.php +++ b/tests/APP/public/index.php @@ -22,11 +22,12 @@ class Ctrl } $c = new \Slim\Container(); + $c['errorHandler'] = function ($c) { return function ($request, $response, \Exception $exception) use ($c) { switch (get_class($exception)) { case \Phramework\Exceptions\NotFoundException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') ->write(json_encode((object) [ 'errors' => [(object) [ @@ -36,7 +37,7 @@ class Ctrl ]] ])); case \Phramework\Exceptions\MissingParametersException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') //->write($exception->getMessage()) ->write(json_encode((object) [ @@ -51,7 +52,7 @@ class Ctrl ]] ])); case \Phramework\Exceptions\IncorrectParameterException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') //->write($exception->getMessage()) ->write(json_encode((object) [ @@ -64,7 +65,7 @@ class Ctrl ]] ])); case \Phramework\Exceptions\IncorrectParametersException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') //->write($exception->getMessage()) ->write(json_encode((object) [ @@ -73,7 +74,7 @@ class Ctrl ])); case \Exception::class: default: - return $c['errors']->withStatus(400) + return $c['response']->withStatus(400) ->withHeader('Content-Type', 'application') //->write($exception->getMessage()) ->write($exception); diff --git a/tests/src/Endpoint/EndpointPatchTest.php b/tests/src/Endpoint/EndpointPatchTest.php index c7ef7e5..a09ed0c 100644 --- a/tests/src/Endpoint/EndpointPatchTest.php +++ b/tests/src/Endpoint/EndpointPatchTest.php @@ -17,8 +17,7 @@ */ namespace Phramework\JSONAPI\Client; -require_once __DIR__ . '/BaseEndpoint.php'; - +use Phramework\JSONAPI\APP\BaseEndpoint; use Phramework\JSONAPI\Client\Response\JSONAPIResource; /** @@ -38,16 +37,7 @@ public function testPost() (object) [ 'title' => 'do this from phpunit', 'body' => 'do this from phpunit - body', - ]/*, - (new RelationshipsData()) - ->append( - 'tag', - '1' - ) - ->append( - 'tag', - '2' - )*/ + ] ); $this->markTestIncomplete(); diff --git a/tests/src/Endpoint/GetByIdTest.php b/tests/src/Endpoint/GetByIdTest.php index fa8f48b..8f3aa03 100644 --- a/tests/src/Endpoint/GetByIdTest.php +++ b/tests/src/Endpoint/GetByIdTest.php @@ -17,8 +17,7 @@ */ namespace Phramework\JSONAPI\Client; -require_once __DIR__ . '/BaseEndpoint.php'; - +use Phramework\JSONAPI\APP\BaseEndpoint; use Phramework\JSONAPI\Client\Directive\IncludeRelationship; use Phramework\JSONAPI\Client\Directive\Page; use Phramework\JSONAPI\Client\Exceptions\ResponseException; @@ -34,7 +33,6 @@ class GetByIdTest extends \PHPUnit_Framework_TestCase use BaseEndpoint; /** - * @param string $id * @covers ::getById */ public function testGeyById() diff --git a/tests/src/Endpoint/GetTest.php b/tests/src/Endpoint/GetTest.php index aa5997b..cce43e7 100644 --- a/tests/src/Endpoint/GetTest.php +++ b/tests/src/Endpoint/GetTest.php @@ -17,8 +17,7 @@ */ namespace Phramework\JSONAPI\Client; -require_once __DIR__ . '/BaseEndpoint.php'; - +use Phramework\JSONAPI\APP\BaseEndpoint; use Phramework\JSONAPI\Client\Directive\IncludeRelationship; use Phramework\JSONAPI\Client\Directive\Page; use Phramework\JSONAPI\Client\Exceptions\ResponseException; diff --git a/tests/src/Endpoint/PostTest.php b/tests/src/Endpoint/PostTest.php index 880b9e3..1a863f5 100644 --- a/tests/src/Endpoint/PostTest.php +++ b/tests/src/Endpoint/PostTest.php @@ -17,11 +17,7 @@ */ namespace Phramework\JSONAPI\Client; -require_once __DIR__ . '/BaseEndpoint.php'; - -use Phramework\JSONAPI\Client\Directive\IncludeRelationship; -use Phramework\JSONAPI\Client\Directive\Page; -use Phramework\JSONAPI\Client\Exceptions\ResponseException; +use Phramework\JSONAPI\APP\BaseEndpoint; use Phramework\JSONAPI\Client\Response\JSONAPIResource; /** @@ -40,17 +36,8 @@ public function testPost() $post = $this->endpoint->post( (object) [ 'title' => 'do this from phpunit', - 'body' => 'do this from phpunit - body', - ]/*, - (new RelationshipsData()) - ->append( - 'tag', - '1' - ) - ->append( - 'tag', - '2' - )*/ + 'body' => 'do this from phpunit - body', + ] ); $this->markTestIncomplete(); diff --git a/tests/src/EndpointTest.php b/tests/src/EndpointTest.php index c76e38a..774835d 100644 --- a/tests/src/EndpointTest.php +++ b/tests/src/EndpointTest.php @@ -16,8 +16,3 @@ * limitations under the License. */ namespace Phramework\JSONAPI\Client; - -use Phramework\JSONAPI\Client\Directive\IncludeRelationship; -use Phramework\JSONAPI\Client\Directive\Page; -use Phramework\JSONAPI\Client\Exceptions\ResponseException; -use Phramework\JSONAPI\Client\Response\JSONAPIResource;