phpunit测试资源laravel 5.5返回集合而不是json

问题描述 投票:1回答:3

当我通过邮递员访问我的api时返回json。但是,当我尝试使用phpunit返回集合进行测试时。

我正在使用laravel 5.5和资源来转换数据。

邮递员回复:

{
    "data": [
        {
            "id": 1,
            "name": "Admin",
            "email": "[email protected]",
            "role": [
                {
                    "id": 1,
                    "name": "admin"
                }
            ]
        }
    ],
    "links": {
        "first": "http://localhost:8000/api/admin/users?page=1",
        "last": "http://localhost:8000/api/admin/users?page=2",
        "prev": null,
        "next": "http://localhost:8000/api/admin/users?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 2,
        "path": "http://localhost:8000/api/admin/users",
        "per_page": "1",
        "to": 1,
        "total": 2
    }
}

我的phpunit函数:

$this->get('api/admin/users')
            ->assertStatus(200)
            ->assertJsonCount(count($users));

响应来自uri $ this-> get('api / admin / users')使用php单元:

Illuminate\Foundation\Testing\TestResponse {#39
  +baseResponse: Illuminate\Http\JsonResponse {#1347
    #data: "{"data":[{"id":1,"name":"Watson Cole","email":"[email protected]","role":[{"id":1,"name":"admin"}]}],"links":{"first":"http:\/\/localhost\/api\/admin\/users?page=1","last":"http:\/\/localhost\/api\/admin\/users?page=1","prev":null,"next":null},"meta":{"current_page":1,"from":1,"last_page":1,"path":"http:\/\/localhost\/api\/admin\/users","per_page":15,"to":1,"total":1}}"
    #callback: null
    #encodingOptions: 0
    +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#1220
      #computedCacheControl: array:2 [
        "no-cache" => true
        "private" => true
      ]
      #cookies: []
      #headerNames: array:5 [
        "cache-control" => "Cache-Control"
        "date" => "Date"
        "content-type" => "Content-Type"
        "x-ratelimit-limit" => "X-RateLimit-Limit"
        "x-ratelimit-remaining" => "X-RateLimit-Remaining"
      ]
      #headers: array:5 [
        "cache-control" => array:1 [
          0 => "no-cache, private"
        ]
        "date" => array:1 [
          0 => "Mon, 22 Jan 2018 13:54:31 GMT"
        ]
        "content-type" => array:1 [
          0 => "application/json"
        ]
        "x-ratelimit-limit" => array:1 [
          0 => 60
        ]
        "x-ratelimit-remaining" => array:1 [
          0 => 59
        ]
      ]
      #cacheControl: []
    }
    #content: "{"data":[{"id":1,"name":"Watson Cole","email":"[email protected]","role":[{"id":1,"name":"admin"}]}],"links":{"first":"http:\/\/localhost\/api\/admin\/users?page=1","last":"http:\/\/localhost\/api\/admin\/users?page=1","prev":null,"next":null},"meta":{"current_page":1,"from":1,"last_page":1,"path":"http:\/\/localhost\/api\/admin\/users","per_page":15,"to":1,"total":1}}"
    #version: "1.1"
    #statusCode: 200
    #statusText: "OK"
    #charset: null
    +original: array:3 [
      "data" => Illuminate\Support\Collection {#1218
        #items: array:1 [
          0 => App\Http\Resources\User {#1585
            +resource: App\User {#1345
              #fillable: array:3 [
                0 => "name"
                1 => "email"
                2 => "password"
              ]
              #hidden: array:2 [
                0 => "password"
                1 => "remember_token"
              ]
              #connection: "sqlite"
              #table: null
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:7 [
                "id" => "1"
                "name" => "Watson Cole"
                "email" => "[email protected]"
                "password" => "$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm"
                "remember_token" => "dIKErDUxJU"
                "created_at" => "2018-01-22 13:54:31"
                "updated_at" => "2018-01-22 13:54:31"
              ]
              #original: array:7 [
                "id" => "1"
                "name" => "Watson Cole"
                "email" => "[email protected]"
                "password" => "$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm"
                "remember_token" => "dIKErDUxJU"
                "created_at" => "2018-01-22 13:54:31"
                "updated_at" => "2018-01-22 13:54:31"
              ]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [
                "role" => Illuminate\Database\Eloquent\Collection {#1214
                  #items: array:1 [
                    0 => App\Models\Role {#1456
                      #fillable: array:4 [
                        0 => "id"
                        1 => "name"
                        2 => "display_name"
                        3 => "description"
                      ]
                      #table: "roles"
                      #connection: "sqlite"
                      #primaryKey: "id"
                      #keyType: "int"
                      +incrementing: true
                      #with: []
                      #withCount: []
                      #perPage: 15
                      +exists: true
                      +wasRecentlyCreated: false
                      #attributes: array:6 [
                        "id" => "1"
                        "name" => "admin"
                        "display_name" => "Raphael Schuster V"
                        "description" => "Myles Wehner"
                        "created_at" => "2018-01-22 13:54:31"
                        "updated_at" => "2018-01-22 13:54:31"
                      ]
                      #original: array:8 [
                        "id" => "1"
                        "name" => "admin"
                        "display_name" => "Raphael Schuster V"
                        "description" => "Myles Wehner"
                        "created_at" => "2018-01-22 13:54:31"
                        "updated_at" => "2018-01-22 13:54:31"
                        "pivot_user_id" => "1"
                        "pivot_role_id" => "1"
                      ]
                      #changes: []
                      #casts: []
                      #dates: []
                      #dateFormat: null
                      #appends: []
                      #dispatchesEvents: []
                      #observables: []
                      #relations: array:1 [
                        "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#1344
                          +pivotParent: App\User {#1216
                            #fillable: array:3 [
                              0 => "name"
                              1 => "email"
                              2 => "password"
                            ]
                            #hidden: array:2 [
                              0 => "password"
                              1 => "remember_token"
                            ]
                            #connection: null
                            #table: null
                            #primaryKey: "id"
                            #keyType: "int"
                            +incrementing: true
                            #with: []
                            #withCount: []
                            #perPage: 15
                            +exists: false
                            +wasRecentlyCreated: false
                            #attributes: []
                            #original: []
                            #changes: []
                            #casts: []
                            #dates: []
                            #dateFormat: null
                            #appends: []
                            #dispatchesEvents: []
                            #observables: []
                            #relations: []
                            #touches: []
                            +timestamps: true
                            #visible: []
                            #guarded: array:1 [
                              0 => "*"
                            ]
                            #rememberTokenName: "remember_token"
                            #accessToken: null
                          }
                          #foreignKey: "user_id"
                          #relatedKey: "role_id"
                          #guarded: []
                          #connection: null
                          #table: "role_user"
                          #primaryKey: "id"
                          #keyType: "int"
                          +incrementing: true
                          #with: []
                          #withCount: []
                          #perPage: 15
                          +exists: true
                          +wasRecentlyCreated: false
                          #attributes: array:2 [
                            "user_id" => "1"
                            "role_id" => "1"
                          ]
                          #original: array:2 [
                            "user_id" => "1"
                            "role_id" => "1"
                          ]
                          #changes: []
                          #casts: []
                          #dates: []
                          #dateFormat: null
                          #appends: []
                          #dispatchesEvents: []
                          #observables: []
                          #relations: []
                          #touches: []
                          +timestamps: false
                          #hidden: []
                          #visible: []
                          #fillable: []
                        }
                      ]
                      #touches: []
                      +timestamps: true
                      #hidden: []
                      #visible: []
                      #guarded: array:1 [
                        0 => "*"
                      ]
                    }
                  ]
                }
              ]
              #touches: []
              +timestamps: true
              #visible: []
              #guarded: array:1 [
                0 => "*"
              ]
              #rememberTokenName: "remember_token"
              #accessToken: null
            }
            +with: []
            +additional: []
          }
        ]
      }
      "links" => array:4 [
        "first" => "http://localhost/api/admin/users?page=1"
        "last" => "http://localhost/api/admin/users?page=1"
        "prev" => null
        "next" => null
      ]
      "meta" => array:7 [
        "current_page" => 1
        "from" => 1
        "last_page" => 1
        "path" => "http://localhost/api/admin/users"
        "per_page" => 15
        "to" => 1
        "total" => 1
      ]
    ]
    +exception: null
  }
}

如何获得json响应而不是集合?

php json laravel phpunit laravel-5.5
3个回答
0
投票

要获得JSON,您可以这样做:

->baseResponse->getData()

或者你可以这样做:

->original->toJson()

0
投票

这适用于我的测试:

$users = createUserAs($this->adminRole , 3);

$result = $this->get('api/admin/users')
    ->assertStatus(200)
    ->baseResponse->getData()->data;

$this->assertEquals(count($users), count($result));

0
投票

资源基类具有在测试中将资源转换为数组的解析方法

/** @test */
    public function it_will_ensures_resource_return_response()
    {
        factory(Model::class, 2)->create();

        $collect = ModelResource::collection(Model::all())->resolve();

        $this->assertCount(2, $collect);
    }
© www.soinside.com 2019 - 2024. All rights reserved.