gpt4 book ai didi

带有关系单元测试的 Laravel 用户层次结构出错了

转载 作者:行者123 更新时间:2023-12-05 04:49:08 26 4
gpt4 key购买 nike

场景:因此,我有一个用户表,其中包含一个名为 parent_id 的 ForeignKey,它引用了用户表的 ID。这允许一个用户属于另一个用户,并且一个用户有许多“子”用户(一对多)。

现在,问题本身是由于单元测试引起的。当我使用数据库中的记录时,它按预期工作,但模拟关系值似乎不起作用。另请注意,针对数据库运行此测试没有意义,因为该结构具有很多依赖性。

目标:在不访问数据库的情况下测试规则

规则:

<?php

namespace App\Rules;

use App\Repositories\UserRepository;
use Illuminate\Contracts\Validation\Rule;

class UserHierarchy implements Rule
{
/**
* User related repository
*
* @var \App\Repositories\UserRepository $userRepository
*/
private $userRepository;

/**
* User to affected
*
* @var null|int $userId
*/
private $userId;

/**
* Automatic dependency injection
*
* @param \App\Repositories\UserRepository $userRepository
* @param integer|null $userId
*/
public function __construct(UserRepository $userRepository, ?int $userId)
{
$this->userRepository = $userRepository;
$this->userId = $userId;
}

/**
* Determine if the validation rule passes.
* Uses recursivity in order to validate if there is it causes an infinite loop
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
if (is_null($value)) {
return true;
}

$childrenOfUserToBeUpdated = $this->userRepository->show($this->userId);
//pluck_key_recursive is a customized function but its not posted because the issue can be traced on the dd below
$notAllowedUserIds = pluck_key_recursive($childrenOfUserToBeUpdated->childrenTree->toArray(), 'children_tree', 'id');
dd($childrenOfUserToBeUpdated->childrenTree->toArray());
return in_array($value, $notAllowedUserIds) ? false : true;
}
}

用户关系如下:

/**
* An User can have multiple children User
*
* @return EloquentRelationship
*/
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id', 'id');
}

/**
* An User can have a hierarchal of children
*
* @return EloquentRelationship
*/
public function childrenTree(): HasMany
{
return $this->children()->with('childrenTree');
}

这是测试:

<?php

namespace Tests\Unit\Rules;

use App\Repositories\UserRepository;
use App\Rules\UserHierarchy;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Mockery;
use Tests\TestCase;

class UserHierarchyTest extends TestCase
{
/**
* Setting up Mockery
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->parent = new User(['id' => 1]);
$this->sonOne = new User(['id' => 2, 'parent_id' => $this->parent->id]);
$this->sonTwo = new User(['id' => 3, 'parent_id' => $this->parent->id]);
$this->sonThree = new User(['id' => 4, 'parent_id' => $this->parent->id]);
$this->grandSonOne = new User(['id' => 5, 'parent_id' => $this->sonOne->id]);
$this->grandSonTwo = new User(['id' => 6, 'parent_id' => $this->sonOne->id]);

//$this->sonOne->children = new Collection([$this->grandSonOne, $this->grandSonTwo]);
//$this->parent->children = new Collection([$this->sonOne, $this->sonTwo, $this->sonThree]);
$this->sonOne->childrenTree = new Collection([$this->grandSonOne, $this->grandSonTwo]);
$this->parent->childrenTree = new Collection([$this->sonOne, $this->sonTwo, $this->sonThree]);


$this->userRepositoryMock = Mockery::mock(UserRepository::class);
$this->app->instance(UserRepository::class, $this->userRepositoryMock);
}

/**
* The rule should pass if the user to be updated will have not a child as a parent (infinite loop)
*
* @return void
*/
public function test_true_if_the_user_id_isnt_in_the_hierarchy()
{
//Arrange
$this->userRepositoryMock->shouldReceive('show')->once()->with($this->parent->id)->andReturn($this->parent);
//Act
$validator = validator(['parent_id' => $this->randomUserSon->id], ['parent_id' => resolve(UserHierarchy::class, ['userId' => $this->parent->id])]);
//Assert
$this->assertTrue($validator->passes());
}

/**
* The rule shouldnt pass if the user to be updated will have a child as a parent (infinite loop)
*
* @return void
*/
public function test_fail_if_the_user_id_is_his_son_or_below()
{
//Arrange
$this->userRepositoryMock->shouldReceive('show')->once()->with($this->parent->id)->andReturn($this->parent);
//Act
$validator = validator(['parent_id' => $this->grandSonOne->id], ['parent_id' => resolve(UserHierarchy::class, ['userId' => $this->parent->id])]);
//Assert
$this->assertFalse($validator->passes());
}

/**
* Tear down Mockery
*
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
Mockery::close();
}
}

我尝试了很多组合,但我似乎无法让它发挥作用。我什至尝试过一直模拟用户模型,但结果相同:用户的 child 被转换为数组,但孙子仍然是集合的项目对象。

这是此测试的示例输出:

array:3 [
0 => array:6 [
"name" => "asd"
"email" => "asdasdasd"
"id" => 2
"parent_id" => 1
"childrenTree" => Illuminate\Database\Eloquent\Collection^ {#898
#items: array:2 [
0 => App\Models\User^ {#915
#fillable: array:8 [...

为什么 ->toArray() 将所有内容都转换为具有真实数据库对象的数组,但在设置预期结果时却没有?

最佳答案

我认为您可能只是在寻找 $parentModel->setRelation($name, $modelOrEloquentCollection)。如果您实际上没有将记录保存到数据库中,即使 ID 值匹配,Eloquent 也不会为您连接它们。

关于带有关系单元测试的 Laravel 用户层次结构出错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67710450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com