Laravel资源在单元测试时包装两次

时间:2019-03-27 14:45:57

标签: php laravel phpunit laravel-5.7

我运行测试服时,返回ResourceCollection的测试失败,因为数据被包装两次,但是当我单独运行它们时成功。

我已经知道了原因
ResourceCollection使用称为 wrap static 变量,在测试之间,该变量保持其状态。

现在我需要修复

将这两种方法作为 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);
}

组织资源

<?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),
        ];
    }
}

组织资源

<?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,
            ],
        ];
    }
}

这很好用,为 index 给出的响应将如下所示:

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

并且按预期,显示将返回

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

但是,当我运行所有单元测试时,我的索引会返回以下json(注意{"data":{"data"...

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

这是单独运行时成功但在withoutWrapping之后失败的测试之一,如一次测试中所称。

/** @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"));
}

如果您想知道assertJsonSchema来自sixlive/laravel-json-schema-assertions

临时解决方法

我在 phpunit.xml 中将processIsolation设置为true的情况下运行测试,但这非常慢

1 个答案:

答案 0 :(得分:0)

我对我的回答不满意,但是如果我在ResourceCollection中手动设置$wrap,它可以解决该错误。

class OrganizationsResource extends ResourceCollection
{
    public static $wrap = 'data';

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