Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
language: php

php:
- 5.5
- 5.5
- 5.6
- hhvm

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev

# script:
# - vendor/bin/phpunit --configuration phpunit.xml

install:
- composer require satooshi/php-coveralls:~0.6@stable

before_script:
- mkdir -p build/logs

script:
- vendor/bin/phpunit --configuration phpunit.xml
- phpunit --coverage-clover build/logs/clover.xml

after_success:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'

notifications:
on_success: always
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) 2016 :author_name <:author_email>
Copyright (c) 2016 Le Duc <leeduc55@gmail.com>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# json-api-builder
# Json Api Builder

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Total Downloads][ico-downloads]][link-downloads]
[![Coverage Status][ico-coverall]][link-coverrall]
<!-- [![Coverage Status][ico-scrutinizer]][link-scrutinizer] -->
<!-- [![Quality Score][ico-code-quality]][link-code-quality] -->

Expand All @@ -14,7 +15,7 @@ This package is auto generate data follow jsonapi.org.
Via Composer

``` bash
$ composer require php-soft/json-api-builder
$ composer require leeduc/json-api-builder
```

Once this has finished, you will need to add the service provider to the providers array in your app.php config as follows:
Expand Down Expand Up @@ -288,6 +289,7 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/leeduc/json-api-builder.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/leeduc/json-api-builder.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/leeduc/json-api-builder.svg?style=flat-square
[ico-coverall]: https://img.shields.io/coveralls/leeduc/json-api-builder/badge.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/leeduc/json-api-builder
[link-travis]: https://travis-ci.org/leeduc/json-api-builder
Expand All @@ -296,3 +298,4 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[link-downloads]: https://packagist.org/packages/leeduc/json-api-builder
[link-author]: https://github.com/leeduc
[link-contributors]: ../../contributors
[link-coverrall]: https://coveralls.io/github/leeduc/json-api-builder?branch=master
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/report" lowUpperBound="35"
highLowerBound="70"/>
<log type="coverage-clover" target="build/coverage.xml"/>
<log type="coverage-php" target="build/coverage.serialized"/>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
<log type="json" target="build/logfile.json"/>
<log type="tap" target="build/logfile.tap"/>
<log type="junit" target="build/logfile.xml" logIncompleteSkipped="false"/>
<log type="testdox-html" target="build/testdox.html"/>
<log type="testdox-text" target="build/testdox.txt"/>
</logging>
</phpunit>
2 changes: 0 additions & 2 deletions src/JsonApiBuilder/JsonApiBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

class JsonApiBuilder
{
public $layout = 'JsonApiBuilder::default';

private $data = [];
private $source = [];

Expand Down
26 changes: 0 additions & 26 deletions src/SkeletonClass.php

This file was deleted.

12 changes: 12 additions & 0 deletions tests/JsonApiBuilder/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ protected function getPackageAliases($app)
public function setUp()
{
parent::setUp();

Route::get('user/{id}', ['as' => 'stdclass', function ($id) {
return $id;
}]);

Route::get('comment/{id}', ['as' => 'comments', function ($id) {
return $id;
}]);

Route::get('comment/{id}', ['as' => 'posts', function ($id) {
return $id;
}]);
}

public function testExample()
Expand Down
193 changes: 188 additions & 5 deletions tests/JsonApiBuilder/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function setUpBeforeClass()

$posts = [];
for ($i=0; $i < 10; $i++) {
$posts[$i] = new stdClass();
$posts[$i] = new Posts();
$posts[$i]->id = $i;
$posts[$i]->user_id = 1;
$posts[$i]->title = $faker->sentence($nbWords = 6, $variableNbWords = true);
Expand All @@ -47,19 +47,109 @@ public function testSetData()
$this->assertArrayStructure((array) [$data['user']], (array) $response);
}

public function testEntity()
public function testErrorNotObjectOrArray()
{
try {
\JsonApiBuilder::setData('String');
} catch (\Exception $e) {
$this->assertEquals($e->getCode(), 400);
$this->assertEquals($e->getMessage(), 'Resource must be array or object.');
}
}

public function testGetData()
{
$data = self::$objects;
$a = \JsonApiBuilder::setData($data['user'])
->entity(['email', 'name', 'gender'])
->entity(['email', 'name', 'gender'])
->relationship(['comments', 'posts'])
->included(['posts', 'comments' => ['post_id', 'content']]);

foreach ($a->data as $resource) {
foreach ($resource as $key => $value) {
switch ($key) {
case 'id':
$this->assertEquals($value, $data['user']->id);
break;
case 'type':
$this->assertEquals($value, strtolower(class_basename($data['user'])));
break;
case 'attributes':
foreach ($value as $k => $v) {
$this->assertEquals($v, $data['user']->$k);
}
break;
case 'relationships':
foreach ($value as $k => $v) {
foreach ($v['data'] as $k1 => $v1) {
$rel = $data['user']->$k;
foreach ($rel as $k2 => $v2) {
$rel[$k2] = (array) $v2;
}

$this->assertTrue(array_search($v1['id'], array_column($rel, 'id')) !== false);
}
}
break;
}
}
}


foreach ($a->included as $k => $v) {
$type = $v['type'];
$resources = [];
if (isset($data['user']->$type)) {
foreach ($data['user']->$type as $key => $value) {
$resources[] = (array) $value;
}
}

$list_ids = array_column($resources, 'id');

foreach ($v as $key => $value) {
$resource = $resources[array_search($v['id'], $list_ids)];

switch ($key) {
case 'id':
$this->assertEquals($value, $resource['id']);
break;
case 'type':
$this->assertEquals($value, $type);
break;
case 'attributes':
foreach ($value as $k1 => $v1) {
$this->assertEquals($v1, $resource[$k1]);
}
break;
}
}
}
}

public function testEntityObject()
{
$data = self::$objects;
$a = \JsonApiBuilder::setData($data['user'])
->entity(['email', 'name', 'gender'], function($data) {
$data[0]['id'] = 100;
return $data;
})
->relationship(['comments', 'posts'])
->included(['posts', 'comments' => ['post_id', 'content']]);
->included(['posts', 'comments'], function($data) {
foreach ($data as $key => $value) {
if($value['type'] == 'comments') {
$data[$key]['attributes']['id'] = 100;
}
}
return $data;
});
$response = $a->parse();

foreach ($response['data'] as $key => $value) {
switch ($key) {
case 'id':
$this->assertEquals($value, $data['user']->id);
$this->assertEquals($value, 100);
break;
case 'type':
$this->assertEquals($value, strtolower(class_basename($data['user'])));
Expand All @@ -84,6 +174,78 @@ public function testEntity()
}
}

foreach ($response['included'] as $k => $v) {
$type = $v['type'];
$resources = [];
if (isset($data['user']->$type)) {
foreach ($data['user']->$type as $key => $value) {
$resources[] = (array) $value;
}
}

$list_ids = array_column($resources, 'id');
foreach ($v as $key => $value) {
$resource = $resources[array_search($v['id'], $list_ids)];
switch ($key) {
case 'id':
$this->assertEquals($value, $resource['id']);
break;
case 'type':
$this->assertEquals($value, $type);
break;
case 'attributes':
foreach ($value as $k1 => $v1) {
if($v['type'] == 'comments' && $k1 == 'id') {
$this->assertEquals($v1, 100);
} else {
$this->assertEquals($v1, $resource[$k1]);
}
}
break;
}
}
}
}

public function testEntityArray()
{
$data = self::$objects;
$a = \JsonApiBuilder::setData([$data['user']])
->entity(['email', 'name', 'gender'])
->relationship(['comments', 'posts'])
->included(['posts', 'comments' => ['post_id', 'content']]);
$response = $a->parse();

foreach ($response['data'] as $key => $resource) {
foreach ($resource as $key => $value) {
switch ($key) {
case 'id':
$this->assertEquals($value, $data['user']->id);
break;
case 'type':
$this->assertEquals($value, strtolower(class_basename($data['user'])));
break;
case 'attributes':
foreach ($value as $k => $v) {
$this->assertEquals($v, $data['user']->$k);
}
break;
case 'relationships':
foreach ($value as $k => $v) {
foreach ($v['data'] as $k1 => $v1) {
$rel = $data['user']->$k;
foreach ($rel as $k2 => $v2) {
$rel[$k2] = (array) $v2;
}

$this->assertTrue(array_search($v1['id'], array_column($rel, 'id')) !== false);
}
}
break;
}
}
}

foreach ($response['included'] as $k => $v) {
$type = $v['type'];
$resources = [];
Expand Down Expand Up @@ -115,3 +277,24 @@ public function testEntity()
}
}
}

class Posts
{
public function __call($method, $args)
{
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}

public function toArray()
{
return [
'id' => $this->id,
'user_id' => $this->user_id,
'title' => $this->title,
'content' => $this->content
];
}
}