diff --git a/src/helpers/Json.php b/src/helpers/Json.php index 4017175..0c35d1b 100644 --- a/src/helpers/Json.php +++ b/src/helpers/Json.php @@ -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]]; diff --git a/tests/unit/helpers/JsonTest.php b/tests/unit/helpers/JsonTest.php index ae27ed5..2d0afd8 100644 --- a/tests/unit/helpers/JsonTest.php +++ b/tests/unit/helpers/JsonTest.php @@ -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; /** @@ -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(); } @@ -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); +// } }