phplaravelphpunitlaravel-5.7

Laravel resources wrap twice when unit testing


When I run a test suit, my tests that return a ResourceCollection fails because the data is wrap twice but success when I run them individually.

I already know the cause
ResourceCollection use a static variable called wrap and between the tests, this variable keep his state.

Now I need to fix it

Take theses two methods as starting point in the OrganizationsController

/**
 * @param Request $request
 * @return OrganizationsResource
 */
public function index(Request $request): OrganizationsResource
{
    return new OrganizationsResource(
        Organization::paginate()
    );
}

/**
 * @param Request $request
 * @param Organization $organization
 * @return OrganizationResource
 */
public function show(Request $request, Organization $organization): OrganizationResource
{
    OrganizationResource::withoutWrapping();
    return new OrganizationResource($organization);
}

The OrganizationsResource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class OrganizationsResource extends ResourceCollection
{

    /**
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'data' => OrganizationResource::collection($this->collection),
        ];
    }
}

And the OrganizationResource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class OrganizationResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {

        return [
            'id'         => $this->id,
            'type'       => 'organization',
            'attributes' => [
                'name' => $this->resource->name,
            ],
        ];
    }
}

This works perfectly fine the response given for the index will be build like this:

{
 "data": [
    {"id": 1},{"id": 2}, ....
 ],
 "links": {
    "next": "http://...",
    ....
 },
 "meta": {
    "current_page": 1,
    ....
 }
}

And as expected, the show will return

{
    "id": 1,
    "type" "organization",
    ....
}

However, when I run all my unit tests, my index(es) returns the the following json (notice {"data":{"data"...

{
 "data": {
     "data": [
        {"id": 1},{"id": 2}, ....
     ],
     "links": {
        "next": "http://...",
        ....
     },
     "meta": {
        "current_page": 1,
        ....
     }
    }
}

Here's one of the test that succeed when running alone but fail after withoutWrapping as been called once in a test suit

/** @test */
public function organizations_api_index_returns_valid_schema(): void
{
    factory(Organization::class, 2)->create(['name' => 'My-Company']);
    $this->json('get', '/organizations')->assertJsonSchema(base_path("{$this->schema}/organizations.json"));
}

In case you wondering about assertJsonSchema this is from sixlive/laravel-json-schema-assertions

Temporary workaround

I'm running my tests with processIsolation set to true in my phpunit.xml But it's extremely slow


Solution

  • I'm not satisfied with my answer, but it fix the error if I set $wrap manually in my ResourceCollection.

    class OrganizationsResource extends ResourceCollection
    {
        public static $wrap = 'data';
    
        /**
         * @param Request $request
         * @return array
         */
        public function toArray($request): array
        {
            return [
                'data' => OrganizationResource::collection($this->collection),
            ];
        }
    }