diff --git a/src/Transformers/ApiTransformer.php b/src/Transformers/ApiTransformer.php index 0be5202..b90e8b9 100644 --- a/src/Transformers/ApiTransformer.php +++ b/src/Transformers/ApiTransformer.php @@ -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(); diff --git a/tests/ApiTransformerTest.php b/tests/ApiTransformerTest.php index 4c49fde..b61df76 100644 --- a/tests/ApiTransformerTest.php +++ b/tests/ApiTransformerTest.php @@ -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; @@ -42,7 +44,7 @@ public function test_input_transforming() 'price' => '1000' ]; - $expectedInput = [ + $expected = [ 'id' => 1, 'name' => 'Wayne Industries', 'has_access' => 0, @@ -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() @@ -181,7 +183,7 @@ 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 = [ @@ -189,7 +191,7 @@ public function test_the_datatype_is_nullable() ]; $expectedOutput = [ - 'price' => 100 + 'price_new' => 100 ]; $this->assertSame($expectedOutput, $this->transformer->transformOutput($input)); @@ -199,7 +201,7 @@ public function test_the_datatype_is_nullable() ]; $expectedOutput = [ - 'price' => 0 + 'price_new' => 0 ]; $this->assertSame($expectedOutput, $this->transformer->transformOutput($input)); @@ -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']); + } } diff --git a/tests/Models/Post.php b/tests/Models/Post.php new file mode 100644 index 0000000..0454226 --- /dev/null +++ b/tests/Models/Post.php @@ -0,0 +1,49 @@ + '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'], + ]; + + +} diff --git a/tests/TestCase.php b/tests/TestCase.php index d1643c0..a87af57 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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(); + }); + + } } /** diff --git a/tests/Transformers/PostTransformer.php b/tests/Transformers/PostTransformer.php new file mode 100644 index 0000000..f8dbdc2 --- /dev/null +++ b/tests/Transformers/PostTransformer.php @@ -0,0 +1,22 @@ +setApiMapping($post); + } +} \ No newline at end of file