lara30453's avatar

Testing Fractal Transformer in Lumen

This is Lumen specific.

I am currently creating an API with spatie's package fractalistic.

I am building it using TDD, but I am finding it hard to find the best method of testing them.

My first approach was to create a feature test for getting a collection of articles from the index endpoint.

ArticleTransformer.php

<?php

namespace App\Transformers;

use App\Models\Article;

class ArticleTransformer extends Transformer
{
    /**
     * Transform the given article collection into JSON.
     *
     * @param  Article $article
     * @return array
     */
    public function transform(Article $article)
    {
        return [
            'title' => $article->title,
            'slug' => $article->slug,
            'teaser' => $article->teaser,
            'body' => $article->body,
            'created_at' => $article->created_at,
            'updated_at' => $article->updated_at,
        ];
    }
}

ArticleTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use App\Models\Article;

class ArticleTest extends TestCase
{
    /**
     * @test
     */
    public function a_collection_of_articles_are_returned_as_json()
    {
        factory(User::class)->create();
        $articles = factory(Article::class, 4)->create();

        $this->json('GET', '/articles')
            ->seeJsonStructure([
                'data' => [[
                    'title',
                    'slug',
                    'teaser',
                    'body',
                    'created_at',
                    'updated_at'
                ]]
            ])
            ->assertResponseStatus(200);
    }
}

In ArticleTest.php, I use "seeJsonStructure" to test that the structure is correct, I use this because Lumen's "seeJson" requires an array to be passed - fractal returns json wrapped in 'data'. However, I unsure if theres a better approach like testing a Transformer directly?

Anybody have any experience testing transformers?

0 likes
1 reply
bobbybouwmann's avatar

You can unit test a transformer completely. For example

public function testTransformer()
{
    // Note the make here, we don't store it in the database, we simply want a model object
    $article = factory(Article::class)->make([
        'title' => 'Something',
        'slug' => 'something
    ]); 

    $transformer = new ArticleTransformer();
    $transformedArticle = $transformer->transform($article):
    
    $this->assertEquals('Something', transformedArticle->title);
    $this->assertEquals('something', transformedArticle->slug);
}

This is just a basic example, but it should do the trick for you.

Testing the json structure is fine here since you only want to know if the correct fields are returned. You can use unit tests (like above) to test if the transformer is giving you the correct data, for example calculations or data transformations.

Let me know if you have any more questions ;)

Please or to participate in this conversation.