dongou1970 2017-11-02 07:38
浏览 297
已采纳

Laravel系列。 有某种断言结构方法吗?

I'm writing tests and I want to assert, that a returned collection has some specific structure.

For asserting jsons I'm using assertJsonStructure() method on the Responce object.

I have not found similar for the \Illuminate\Support\Collection. Did I miss some package/framework method.

An example of what do I want

$collection = collect([
    'name'      => 'John',
    'surname'   => 'Smith',
    'birthoday' => [
        'day'   => 23,
        'month' => 5,
        'year'  => 1970,
    ],
]);

$collection->assertStructure([          //true
    'name',
    'surname',
    'birthday' => ['day', 'month', 'year'],
]);

I will accept

no

as an answer too, but if it is with an example of how to validate such a nested collection.

  • 写回答

2条回答 默认 最新

  • dongpao1873 2017-11-02 08:25
    关注

    There is no such function on Collection instance, the closest you can do are:

    • check if it has a key with has()
    • Check if it contains some value with contains()
    • There are other methods to check if something exist but

    If you need inspiration, you can get it with the way Laravel implements assertJsonStructure() in /Illuminate/Foundation/Testing/TestResponse.php:

    /**
     * Assert that the response has a given JSON structure.
     *
     * @param  array|null  $structure
     * @param  array|null  $responseData
     * @return $this
     */
    public function assertJsonStructure(array $structure = null, $responseData = null)
    {
        if (is_null($structure)) {
            return $this->assertJson($this->json());
        }
    
        if (is_null($responseData)) {
            $responseData = $this->decodeResponseJson();
        }
    
        foreach ($structure as $key => $value) {
            if (is_array($value) && $key === '*') {
                PHPUnit::assertInternalType('array', $responseData);
    
                foreach ($responseData as $responseDataItem) {
                    $this->assertJsonStructure($structure['*'], $responseDataItem);
                }
            } elseif (is_array($value)) {
                PHPUnit::assertArrayHasKey($key, $responseData);
    
                $this->assertJsonStructure($structure[$key], $responseData[$key]);
            } else {
                PHPUnit::assertArrayHasKey($value, $responseData);
            }
        }
    
        return $this;
    }
    

    As you can see there is a recursive calls to check the structure in case there is sub-structure.

    UPDATE:

    As a basic test to solve your question, I modified the assertJsonStructure() to have assertArrayStructure() and this working test:

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $collect = collect(['name' => '1', 'detail' => ['age' => 1,'class' => 'abc']]);
    
        $this->assertArrayStructure(['name', 'detail' => ['class', 'age']], $collect->toArray());
    }
    
    
    /**
     * Assert the array has a given structure.
     *
     * @param  array  $structure
     * @param  array  $arrayData
     * @return $this
     */
    public function assertArrayStructure(array $structure, array $arrayData)
    {
        foreach ($structure as $key => $value) {
            if (is_array($value) && $key === '*') {
                $this->assertInternalType('array', $arrayData);
    
                foreach ($arrayData as $arrayDataItem) {
                    $this->assertArrayStructure($structure['*'], $arrayDataItem);
                }
            } elseif (is_array($value)) {
                $this->assertArrayHasKey($key, $arrayData);
    
                $this->assertArrayStructure($structure[$key], $arrayData[$key]);
            } else {
                $this->assertArrayHasKey($value, $arrayData);
            }
        }
    
        return $this;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?