Skip to content

Commit

Permalink
#111. Add coverage on Json helper
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed May 30, 2018
1 parent 28dc57a commit 215583e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/helpers/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ public static function getData(array $jsonApiArr) : array
}

/**
* @param $relation \Illuminate\Database\Eloquent\Collection | Model
* @param $relations \Illuminate\Database\Eloquent\Collection
* @param string $entity
* @return array JSON API rels compatible array
*/
public static function getRelations($relation, string $entity)
public static function getRelations($relations, string $entity) : array
{
$jsonArr = [];
if ($relation instanceof \Illuminate\Database\Eloquent\Collection) {
$cnt = count($relation);
if ($relations instanceof \Illuminate\Database\Eloquent\Collection) {
$cnt = count($relations);
if ($cnt > 1) {
foreach ($relation as $v) {
foreach ($relations as $v) {
$attrs = $v->getAttributes();
$jsonArr[] = [RamlInterface::RAML_TYPE => $entity,
RamlInterface::RAML_ID => $attrs[RamlInterface::RAML_ID]];
}
} else {
foreach ($relation as $v) {
foreach ($relations as $v) {
$attrs = $v->getAttributes();
$jsonArr = [RamlInterface::RAML_TYPE => $entity,
RamlInterface::RAML_ID => $attrs[RamlInterface::RAML_ID]];
Expand Down
106 changes: 104 additions & 2 deletions tests/unit/helpers/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use PHPUnit_Framework_MockObject_MockObject;
use rjapi\extension\BaseFormRequest;
use rjapi\extension\BaseModel;
use rjapi\extension\JSONApiInterface;
use rjapi\helpers\Json;
use rjapi\types\RamlInterface;
use rjapitest\unit\TestCase;

/**
Expand All @@ -26,7 +28,7 @@ class JsonTest extends TestCase
public function setUp()
{
parent::setUp();
$this->baseModel = new Article();
$this->baseModel = new Article();
$this->baseFormRequest = new ArticleMiddleware();
}

Expand All @@ -44,7 +46,107 @@ public function it_gets_resource_collection()
*/
public function it_gets_resource_item()
{
$resource = Json::getResource($this->baseFormRequest, $this->baseModel, Article::class, false);
$resource = Json::getResource($this->baseFormRequest, $this->baseModel, Article::class);
$this->assertInstanceOf(Item::class, $resource);
}

/**
* @test
*/
public function it_gets_attributes()
{
$attrsData = Json::getAttributes([
RamlInterface::RAML_DATA => [
RamlInterface::RAML_ATTRS => [
'title' => 'Foo Bar Baz',
'description' => 'Foo Bar Baz Foo Bar Baz Foo Bar Baz',
]
]
]);
$this->assertArraySubset([
'title' => 'Foo Bar Baz',
'description' => 'Foo Bar Baz Foo Bar Baz Foo Bar Baz',
], $attrsData);
}

/**
* @test
*/
public function it_gets_data()
{
$data = Json::getData([
RamlInterface::RAML_DATA => [
RamlInterface::RAML_ATTRS => [
'title' => 'Foo Bar Baz',
'description' => 'Foo Bar Baz Foo Bar Baz Foo Bar Baz',
]
]
]);
$this->assertArraySubset([
RamlInterface::RAML_ATTRS => [
'title' => 'Foo Bar Baz',
'description' => 'Foo Bar Baz Foo Bar Baz Foo Bar Baz',
]
], $data);
}

/**
* @test
*/
public function it_gets_relationships()
{
$relations = Json::getRelationships([
RamlInterface::RAML_DATA => [
RamlInterface::RAML_RELATIONSHIPS => [
'type' => 'TagRelationships[]'
]
]
]);
$this->assertArraySubset([
'type' => 'TagRelationships[]'
], $relations);
}

/**
* @test
*/
public function it_returns_json_error()
{
$encodedJson = Json::outputErrors(
[
[
JSONApiInterface::ERROR_TITLE => 'JSON API support disabled',
JSONApiInterface::ERROR_DETAIL => 'JSON API method myMethod'
.
' was called. You can`t call this method while JSON API support is disabled.',
],
], true
);
$this->assertArraySubset(['errors' => [
[
JSONApiInterface::ERROR_TITLE => 'JSON API support disabled',
JSONApiInterface::ERROR_DETAIL => 'JSON API method myMethod'
.
' was called. You can`t call this method while JSON API support is disabled.',
]]],
json_decode($encodedJson, true)
);
}

// /**
// * @test
// */
// public function it_gets_relations()
// {
// $collection = new Collection();
//// $items = new class extends \ArrayAccessible {
////
//// };
//// $items->offsetSet(0, 'title')
// $collection->add([
// 'title' => 'Foo Bar Baz'
// ]);
// $rels = Json::getRelations($collection, 'article');
// print_r($rels);
// }
}

0 comments on commit 215583e

Please sign in to comment.