Laravel 带有关系单元测试的用户层次结构出错
Laravel user hierarchy w/ relationship unit testing gone wrong
场景:
因此,我有一个用户 table,其中包含一个名为 parent_id
的 ForeignKey,它引用了用户 table 的 ID。这允许一个用户属于另一个用户,并且一个用户拥有许多“children”用户(one-to-many)。
现在,问题本身是由于单元测试引起的。当我使用数据库中的记录时,它按预期工作,但模拟关系值似乎不起作用。另请注意,针对数据库进行此测试 运行 没有意义,因为该结构具有很多依赖性。
目标:在不访问数据库的情况下测试规则
规则:
<?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();
}
}
我尝试了很多组合,但我似乎无法让它发挥作用。我什至尝试过一直模拟用户模型,但结果相同:用户的 children 被转换为数组,但 grandchildren 仍保留为项目 objects一个集合。
这是此测试的示例输出:
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() 将所有内容都转换为具有真实数据库的数组 objects 而不是在设置预期结果时?
我认为您可能正在寻找 $parentModel->setRelation($name, $modelOrEloquentCollection)
。如果您实际上没有将记录保存到数据库,Eloquent 将不会为您连接它们,即使 ID 值匹配也是如此。
场景:
因此,我有一个用户 table,其中包含一个名为 parent_id
的 ForeignKey,它引用了用户 table 的 ID。这允许一个用户属于另一个用户,并且一个用户拥有许多“children”用户(one-to-many)。
现在,问题本身是由于单元测试引起的。当我使用数据库中的记录时,它按预期工作,但模拟关系值似乎不起作用。另请注意,针对数据库进行此测试 运行 没有意义,因为该结构具有很多依赖性。
目标:在不访问数据库的情况下测试规则
规则:
<?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();
}
}
我尝试了很多组合,但我似乎无法让它发挥作用。我什至尝试过一直模拟用户模型,但结果相同:用户的 children 被转换为数组,但 grandchildren 仍保留为项目 objects一个集合。
这是此测试的示例输出:
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() 将所有内容都转换为具有真实数据库的数组 objects 而不是在设置预期结果时?
我认为您可能正在寻找 $parentModel->setRelation($name, $modelOrEloquentCollection)
。如果您实际上没有将记录保存到数据库,Eloquent 将不会为您连接它们,即使 ID 值匹配也是如此。