Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add wherehas soft deleting scopes #42100

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add Test
  • Loading branch information
tiagof committed Apr 22, 2022
commit 468a02a5606475a637b99f3ecda526c438aa7e0b
24 changes: 23 additions & 1 deletion tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->integer('user_id')->nullable(); // circular reference to parent User
$table->integer('group_id')->nullable();
$table->string('email')->unique();
$table->timestamps();
Expand Down Expand Up @@ -878,14 +879,30 @@ public function testMorphToNonSoftDeletingModel()
$this->assertNull($comment->owner);
}

public function testSelfReferencingRelationshipWithSoftDeletes()
{
/*
* https://github.com/laravel/framework/issues/42075
*/
[$taylor, $abigail] = $this->createUsers();

$versionsA = $abigail->versions;
$versionsT = $taylor->versions;

self::assertCount(1, $versionsA);
self::assertTrue($versionsA->first()->is($taylor));

self::assertCount(0, $versionsT);
}
Copy link
Contributor

@BrandonSurowiec BrandonSurowiec Apr 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would match the code style of the rest of the tests (by using $this over self:: and inline your variables $versionsA and $versionsT. More like this:

    public function testSelfReferencingRelationshipWithSoftDeletes()
    {
        [$taylor, $abigail] = $this->createUsers();

        $this->assertCount(1, $abigail->versions);
        $this->assertTrue($abigail->versions->first()->is($taylor));

        $this->assertCount(0, $taylor->versions);
        $this->assertEquals(1, SoftDeletesTestUser::whereHas('versions')->count());
    }

There also may be a better name than version for this "test" relationship. Maybe self_referencing() to make the test relation match the test case, making it more clear?

    public function testSelfReferencingRelationshipWithSoftDeletes()
    {
        [$taylor, $abigail] = $this->createUsers();

        $this->assertCount(1, $abigail->self_referencing);
        $this->assertTrue($abigail->self_referencing->first()->is($taylor));

        $this->assertCount(0, $taylor->self_referencing);
        $this->assertEquals(1, SoftDeletesTestUser::whereHas('self_referencing')->count());
    }

    public function self_referencing()
    {
        return $this->hasMany(SoftDeletesTestUser::class, 'user_id')->onlyTrashed();
    }


/**
* Helpers...
*
* @return \Illuminate\Tests\Database\SoftDeletesTestUser[]
*/
protected function createUsers()
{
$taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
$taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'user_id' => 2]);
$abigail = SoftDeletesTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);

$taylor->delete();
Expand Down Expand Up @@ -938,6 +955,11 @@ class SoftDeletesTestUser extends Eloquent
protected $table = 'users';
protected $guarded = [];

public function versions()
{
return $this->hasMany(SoftDeletesTestUser::class, 'user_id')->onlyTrashed();
}

public function posts()
{
return $this->hasMany(SoftDeletesTestPost::class, 'user_id');
Expand Down