Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from protonemedia/transformers
Browse files Browse the repository at this point in the history
Ported Resource Transformers from Splade v1
  • Loading branch information
pascalbaljet authored Oct 25, 2023
2 parents 912353a + 033ac14 commit 0c7c354
Show file tree
Hide file tree
Showing 9 changed files with 438 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"phpunit/phpunit": "^10.4",
"spatie/invade": "^2.0",
"spatie/laravel-ignition": "^2.0",
"spatie/phpunit-snapshot-assertions": "^5.1"
"spatie/phpunit-snapshot-assertions": "^5.1",
"spatie/fractalistic": "^2.9"
},
"autoload": {
"psr-4": {
Expand Down
224 changes: 224 additions & 0 deletions app/tests/Unit/TransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

namespace Tests\Unit;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use ProtoneMedia\SpladeCore\Data\InvalidTransformerException;
use ProtoneMedia\SpladeCore\Data\TransformerRepository;
use ProtoneMedia\SpladeCore\Facades\Transformer;
use Tests\TestCase;
use Tests\Unit\Transformers\UserResource;
use Tests\Unit\Transformers\UserTransformer;

class TransformerTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

User::factory(2)->create();
}

/** @test */
public function it_returns_the_instance_when_no_transformer_has_been_found()
{
$user = User::firstOrFail();

$this->assertTrue(Transformer::handle($user)->is($user));
}

/** @test */
public function it_throws_an_exception_when_the_transformer_is_invalid()
{
$user = User::firstOrFail();

Transformer::register(User::class, []);

try {
Transformer::handle($user);
} catch (InvalidTransformerException $e) {
return $this->assertTrue(true);
}

$this->fail('Should have thrown an exception');
}

/** @test */
public function it_throws_an_exception_when_a_transformer_is_missing()
{
$user = User::firstOrFail();

Transformer::enforce();

try {
Transformer::handle($user);
} catch (InvalidTransformerException $e) {
return $this->assertTrue(true);
}

$this->fail('Should have thrown an exception');
}

/** @test */
public function it_doesnt_throw_an_exception_when_the_transformer_is_missing_when_the_instance_cant_be_transformed()
{
$user = User::firstOrFail()->toArray();

Transformer::enforce();

$this->assertEquals($user, Transformer::handle($user));
$this->assertEquals([$user, $user], Transformer::handle([$user, $user]));
$this->assertEquals([$user, $user], Transformer::handle(collect([$user, $user]))->all());
}

/** @test */
public function it_can_transform_using_a_closure()
{
$user = User::firstOrFail();

Transformer::enforce()->register(User::class, function ($user) {
return [
'name' => $user->name,
'email' => $user->email,
];
});

$this->assertEquals([
'name' => $user->name,
'email' => $user->email,
], Transformer::handle($user));
}

/** @test */
public function it_can_transform_a_collection_using_a_closure()
{
$users = User::take(2)->get();

$this->assertCount(2, $users);

Transformer::register(User::class, function ($user) {
return [
'name' => $user->name,
'email' => $user->email,
];
});

$this->assertEquals([
[
'name' => $users->get(0)->name,
'email' => $users->get(0)->email,
],
[
'name' => $users->get(1)->name,
'email' => $users->get(1)->email,
],
], Transformer::handle($users));
}

/** @test */
public function it_can_transform_using_an_api_resource()
{
$user = User::firstOrFail();

Transformer::register(User::class, UserResource::class);

$this->assertEquals([
'name' => $user->name,
'email' => $user->email,
], Transformer::handle($user));
}

/** @test */
public function it_can_transform_a_collection_using_an_api_resource()
{
$users = User::take(2)->get();

$this->assertCount(2, $users);

Transformer::register(User::class, UserResource::class);

$this->assertEquals([
[
'name' => $users->get(0)->name,
'email' => $users->get(0)->email,
],
[
'name' => $users->get(1)->name,
'email' => $users->get(1)->email,
],
], Transformer::handle($users));
}

/** @test */
public function it_can_transform_an_array_using_an_api_resource()
{
$users = User::take(2)->get();

$this->assertCount(2, $users);

Transformer::register(User::class, UserResource::class);

$this->assertEquals([
[
'name' => $users->get(0)->name,
'email' => $users->get(0)->email,
],
[
'name' => $users->get(1)->name,
'email' => $users->get(1)->email,
],
], Transformer::handle($users->all()));
}

/** @test */
public function it_can_transform_using_a_fractal_transformer()
{
$user = User::firstOrFail();

Transformer::register(User::class, UserTransformer::class);

$this->assertEquals([
'name' => $user->name,
'email' => $user->email,
], Transformer::handle($user));
}

/** @test */
public function it_can_transform_a_collection_using_a_fractal_transformer()
{
$users = User::take(2)->get();

$this->assertCount(2, $users);

Transformer::register(User::class, UserTransformer::class);

$this->assertEquals([
[
'name' => $users->get(0)->name,
'email' => $users->get(0)->email,
],
[
'name' => $users->get(1)->name,
'email' => $users->get(1)->email,
],
], Transformer::handle($users->all()));
}

/** @test */
public function it_can_transform_using_an_instance_that_has_a_transform_method()
{
$user = User::firstOrFail();

Transformer::register(User::class, new UserTransformer);

$transformer = $this->app->make(TransformerRepository::class);

$this->assertEquals([
'name' => $user->name,
'email' => $user->email,
], Transformer::handle($user));
}
}
22 changes: 22 additions & 0 deletions app/tests/Unit/Transformers/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Unit\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'name' => $this->name,
'email' => $this->email,
];
}
}
22 changes: 22 additions & 0 deletions app/tests/Unit/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Unit\Transformers;

use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
/**
* A Fractal transformer.
*
* @return array
*/
public function transform(User $user)
{
return [
'name' => $user->name,
'email' => $user->email,
];
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"ProtoneMedia\\SpladeCore\\SpladeCoreServiceProvider"
],
"aliases": {
"SpladeCore": "ProtoneMedia\\SpladeCore\\Facades\\SpladeCore"
"SpladeCore": "ProtoneMedia\\SpladeCore\\Facades\\SpladeCore",
"Transformer": "ProtoneMedia\\SpladeCore\\Facades\\Transformer"
}
}
},
Expand Down
9 changes: 9 additions & 0 deletions src/Data/InvalidTransformerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ProtoneMedia\SpladeCore\Data;

use Exception;

class InvalidTransformerException extends Exception
{
}
Loading

0 comments on commit 0c7c354

Please sign in to comment.