Skip to content

Commit

Permalink
Improve ResponseException title #9 (#10)
Browse files Browse the repository at this point in the history
* Improve ResponseException title #9

Fix slim exception response
Add coveralls
Fix lint errors
  • Loading branch information
nohponex authored Jan 29, 2017
1 parent c9fadbd commit febfe05
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 59 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 2.0.0
- Add AbstractEndpoint, to allow extending Endpoint class
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions src/AbstractEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Phramework\JSONAPI\Client;
declare(strict_types=1);
/*
* Copyright 2016-2017 Xenofon Spafaridis
*
Expand All @@ -15,6 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\JSONAPI\Client;

use Phramework\JSONAPI\Client\Directive\Directive;
use Phramework\JSONAPI\Client\Exceptions\ResponseException;
use Phramework\JSONAPI\Client\Response\Collection;
Expand All @@ -30,7 +32,7 @@ abstract class AbstractEndpoint
* @return Collection
* @throws ResponseException
*/
public abstract function get(
abstract public function get(
Directive ...$directives
) : Collection;

Expand All @@ -40,7 +42,7 @@ public abstract function get(
* @return JSONAPIResource
* @throws ResponseException
*/
public abstract function getById(
abstract public function getById(
string $id,
Directive ...$directives
) : JSONAPIResource;
Expand All @@ -52,7 +54,7 @@ public abstract function getById(
* @return JSONAPIResource
* @throws ResponseException
*/
public abstract function post(
abstract public function post(
\stdClass $attributes = null,
RelationshipsData $relationships = null,
Directive ...$directives
Expand All @@ -66,7 +68,7 @@ public abstract function post(
* @return JSONAPIResource
* @throws ResponseException
*/
public abstract function patch(
abstract public function patch(
string $id,
\stdClass $attributes = null,
RelationshipsData $relationships = null,
Expand All @@ -81,7 +83,7 @@ public abstract function patch(
* @return JSONAPIResource
* @throws ResponseException
*/
public abstract function delete(
abstract public function delete(
string $id,
\stdClass $attributes = null,
RelationshipsData $relationships = null,
Expand All @@ -96,7 +98,7 @@ public abstract function delete(
* @return JSONAPIResource
* @throws ResponseException
*/
public abstract function put(
abstract public function put(
string $id,
\stdClass $attributes = null,
RelationshipsData $relationships = null,
Expand Down
3 changes: 2 additions & 1 deletion src/Directive/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ abstract class Directive
/**
* @return string
*/
public function getURL(): string {
public function getURL(): string
{
return '';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Directive/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ public function getLimit()
{
return $this->limit;
}
}
}
2 changes: 1 addition & 1 deletion src/Endpoint/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ public function get(

return (new Collection($response));
}
}
}
13 changes: 11 additions & 2 deletions src/Exceptions/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ public function offsetGet($offset)
{
return isset($this->data[$offset]) ? $this->data[$offset] : null;
}
}
}
8 changes: 8 additions & 0 deletions src/Response/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Phramework\JSONAPI\Client\Response;

use Phramework\JSONAPI\Client\Error;
use Psr\Http\Message\ResponseInterface;

/**
* @author Xenofon Spafaridis <nohponex@gmail.com>
Expand All @@ -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[]
*/
Expand Down
1 change: 0 additions & 1 deletion src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
11 changes: 6 additions & 5 deletions tests/APP/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) [
Expand All @@ -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) [
Expand All @@ -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) [
Expand All @@ -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) [
Expand All @@ -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);
Expand Down
14 changes: 2 additions & 12 deletions tests/src/Endpoint/EndpointPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
*/
namespace Phramework\JSONAPI\Client;

require_once __DIR__ . '/BaseEndpoint.php';

use Phramework\JSONAPI\APP\BaseEndpoint;
use Phramework\JSONAPI\Client\Response\JSONAPIResource;

/**
Expand All @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions tests/src/Endpoint/GetByIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +33,6 @@ class GetByIdTest extends \PHPUnit_Framework_TestCase
use BaseEndpoint;

/**
* @param string $id
* @covers ::getById
*/
public function testGeyById()
Expand Down
3 changes: 1 addition & 2 deletions tests/src/Endpoint/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 3 additions & 16 deletions tests/src/Endpoint/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions tests/src/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit febfe05

Please sign in to comment.