Search code examples
laraveleloquentphpunitlaravel-8eloquent-relationship

Laravel user hierarchy w/ relationship unit testing gone wrong


Scenario: So, I've got a users table that contains a ForeignKey named parent_id that references the id of the users table. This allows for one User to belongs to another User, and a User having Many "children" Users (one-to-many).

Now, the question itself is due to the unit testing. When I use records from a database it works as expected but mocking the relationship values doesn't seem it work. Also note that having this test being run against a database doesn't make sense as because the structure has a lot of dependencies.

The Goal: test the rule without hitting the database

The rule:

<?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;
    }
}

The User relationships are as it follows:

/**
     * 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');
    }

This is the test:

<?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();
    }
}

I've tried a lot of combinations but I can't seem to get it to work. I've even tried mocking the user model all the way but it results in the same end: the children of a user are converted to an array but the grandchildren remain as item objects of a collection.

This is the sample output on this test:

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 [...

Why does ->toArray() convert everything to an array with real database objects but not when you set the expected outcome?


Solution

  • I think you might just be looking for $parentModel->setRelation($name, $modelOrEloquentCollection). If you're not actually saving the records to the DB, Eloquent won't connect them for you, even if the ID values match up.