Php 拉维收藏。是否存在某种资产结构方法?
我正在编写测试,我想断言,返回的集合具有某种特定的结构 对于断言Php 拉维收藏。是否存在某种资产结构方法?,php,laravel,phpunit,laravel-5.5,laravel-collection,Php,Laravel,Phpunit,Laravel 5.5,Laravel Collection,我正在编写测试,我想断言,返回的集合具有某种特定的结构 对于断言jsons,我在response对象上使用assertJsonStructure()方法 我还没有找到类似的\illumb\Support\Collection。我是否错过了一些包/框架方法 我想要什么的一个例子 我会接受的 没有 这也是一个答案,但如果它是一个如何验证这样一个嵌套集合的示例 答案是否,您只需在上查找断言的laravel方法,在命名空间illighted\Support\Collection中没有方法可以满足您的需要
json
s,我在response
对象上使用assertJsonStructure()
方法
我还没有找到类似的\illumb\Support\Collection
。我是否错过了一些包/框架方法
我想要什么的一个例子
我会接受的
没有
这也是一个答案,但如果它是一个如何验证这样一个嵌套集合的示例 答案是否,您只需在上查找断言的laravel方法,在命名空间
illighted\Support\Collection
中没有方法可以满足您的需要。(您可以找到Laravel断言方法)
作为一个可行的替代方案,为什么不直接序列化集合并使用assertJsonStructure()
方法检查它呢
您可以使用response()
帮助程序填充照明/Foundation/Testing/TestResponse
:
use Illuminate/Foundation/Testing/TestResponse;
$testResponse = new TestResponse(response()->json($collection->toArray());
return $testResponse->assertJsonStructure([
'name',
'surname',
'birthday' => ['day', 'month', 'year'],
]);
我是如何找到这个解决方案的:
- 您需要在测试中返回方法
的完全相同的对象,该方法来自trait$this->json()
右侧MakesHttpRequests
- 正如方法注释所指定的,它返回一个
illumb\Foundation\Testing\TestResponse
- 查找构造函数并查看它需要什么,一个通用响应对象,
李>illighted/Http/response
希望这对您有所帮助。收集实例上没有此类功能,您可以执行的最接近的操作是:
- 检查它是否有一个带有
has()
- 使用
contains()
- 还有其他方法来检查是否存在某些东西,但是
/illumb/Foundation/Testing/TestResponse.php中实现assertJsonStructure()
的方式获得灵感:
/**
* 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;
}
如您所见,在存在子结构的情况下,有一个递归调用来检查结构
更新:
作为解决您问题的一个基本测试,我修改了assertJsonStructure()
使其具有assertarystucture()
,并且此工作测试:
/**
* 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;
}
@D.R.你是对的,对不起,你需要一个响应对象,我的错!是的,我想是的,检查了一下。它调用了undefined method illumize\Http\JsonResponse::assertJsonStructure()
:)@D.R.。我很有头脑,我认为这个更新可以工作:我仍然认为这是一个相当大的开销,但我投票赞成你的问题)谢谢。我相信有人会觉得它有用的。谢谢。是的,看起来这是目前最好也是唯一的选择。看来我有个建议给泰勒)@D.R.那是个好主意。但是,我试图修改assertJsonStructure()
,改为使用数组。我会将答案更新为可能的解决方案-以防您急需,@D.R.我已经包含了解决方案,以防您或任何人发现它有用。哦。非常感谢。我已经完成了phpUnit
assertArraySubset()
方法)我将检查您的选项,并将更新您,如果它有效。
/**
* 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;
}