Skip to content

Commit

Permalink
fix transformed, cast, mutaded values by using attributesToArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Mads Møller committed Sep 5, 2018
1 parent 17dfde3 commit cf5c1ff
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Transformers/ApiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function transformOutput($data): array
if (true === $data instanceof Collection) {
$output = $this->transformCollection($output, $data);
} else if (true === $data instanceof Model) {
$output = $this->transformAttributes($output, $data->getAttributes());
$output = $this->transformAttributes($output, $data->attributesToArray());
$output = $this->transformRelationships($output, $data);
} else {
$data = (true === \is_array($data)) ? $data : $data->toArray();
Expand Down
29 changes: 24 additions & 5 deletions tests/ApiTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Napp\Core\Api\Tests\Unit;

use Napp\Core\Api\Tests\Models\Category;
use Napp\Core\Api\Tests\Models\Post;
use Napp\Core\Api\Tests\Models\Product;
use Napp\Core\Api\Tests\Transformers\PostTransformer;
use Napp\Core\Api\Tests\Transformers\ProductTransformer;
use Napp\Core\Api\Transformers\ApiTransformer;
use Napp\Core\Api\Tests\TestCase;
Expand Down Expand Up @@ -42,7 +44,7 @@ public function test_input_transforming()
'price' => '1000'
];

$expectedInput = [
$expected = [
'id' => 1,
'name' => 'Wayne Industries',
'has_access' => 0,
Expand All @@ -52,7 +54,7 @@ public function test_input_transforming()
];
$transformedInput = $this->transformer->transformInput($input);

$this->assertEquals($expectedInput, $transformedInput);
$this->assertEquals($expected, $transformedInput);
}

public function test_strict_output_transforming()
Expand Down Expand Up @@ -181,15 +183,15 @@ public function test_output_transforming_with_collection_strict_mode()
public function test_the_datatype_is_nullable()
{
$this->transformer->setApiMapping([
'price' => ['newName' => 'price', 'dataType' => 'nullable|int']
'price' => ['newName' => 'price_new', 'dataType' => 'nullable|int']
]);

$input = [
'price' => '100'
];

$expectedOutput = [
'price' => 100
'price_new' => 100
];

$this->assertSame($expectedOutput, $this->transformer->transformOutput($input));
Expand All @@ -199,7 +201,7 @@ public function test_the_datatype_is_nullable()
];

$expectedOutput = [
'price' => 0
'price_new' => 0
];

$this->assertSame($expectedOutput, $this->transformer->transformOutput($input));
Expand Down Expand Up @@ -296,4 +298,21 @@ public function test_transform_deeply_nested_relationships()

$this->assertEquals('iPhone 8', $result['products'][0]['variants'][0]['title']);
}

public function test_transform_not_strict_model()
{
$post = Post::create([
'title' => 'My First post',
'desc' => 'body text',
'tags' => [2, '222', 'wow'],
'other_tags' => null,
'owner' => 34,
'uuid' => '66220588-c944-3425-a3ea-0fc80f8c32fe'
]);
$result = app(PostTransformer::class)->transformOutput($post);

$this->assertArrayNotHasKey('updated_at', $result);
$this->assertEquals(3, count($result['tags']));
$this->assertNull($result['otherTags']);
}
}
49 changes: 49 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Napp\Core\Api\Tests\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

protected $guarded = [
'uuid'
];

/**
* @var array
*/
public $dates = [
'created_at',
];

/**
* @var array
*/
public $casts = [
'tags' => 'json'
];

/**
* @var array
*/
public $hidden = [
'updated_at',
];

/**
* @var array
*/
public $apiMapping = [
'id' => ['newName' => 'id', 'dataType' => 'int'],
'title' => ['newName' => 'name', 'dataType' => 'string'],
'desc' => ['newName' => 'desc', 'dataType' => 'string'],
'tags' => ['newName' => 'tags', 'dataType' => 'array'],
'other_tags' => ['newName' => 'otherTags', 'dataType' => 'nullable[array'],
'owner' => ['newName' => 'owner_id', 'dataType' => 'nullable[int'],
'created_at' => ['newName' => 'postedAt', 'dataType' => 'datetime'],
];


}
14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ public function migrateTables()
$table->timestamps();
});
}

if ( ! Schema::hasTable('posts')) {
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('desc');
$table->json('tags');
$table->text('other_tags')->nullable();
$table->integer('owner')->nullable();
$table->uuid('uuid')->nullable();
$table->timestamps();
});

}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Transformers/PostTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Napp\Core\Api\Tests\Transformers;

use Napp\Core\Api\Tests\Models\Post;
use Napp\Core\Api\Transformers\ApiTransformer;

/**
* Class PostTransformer
* @package Napp\Core\Api\Tests\Transformers
*/
class PostTransformer extends ApiTransformer
{
protected $strict = false;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->setApiMapping($post);
}
}

0 comments on commit cf5c1ff

Please sign in to comment.