123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- <?php
- namespace Illuminate\Tests\Database;
- use BadMethodCallException;
- use Illuminate\Database\Capsule\Manager as DB;
- use Illuminate\Database\Eloquent\Model as Eloquent;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Database\Eloquent\SoftDeletingScope;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Pagination\CursorPaginator;
- use Illuminate\Pagination\Paginator;
- use Illuminate\Support\Carbon;
- use Mockery;
- use PHPUnit\Framework\TestCase;
- class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase
- {
- protected function setUp(): void
- {
- Carbon::setTestNow(Carbon::now());
- $db = new DB;
- $db->addConnection([
- 'driver' => 'sqlite',
- 'database' => ':memory:',
- ]);
- $db->bootEloquent();
- $db->setAsGlobal();
- $this->createSchema();
- }
- /**
- * Setup the database schema.
- *
- * @return void
- */
- public function createSchema()
- {
- $this->schema()->create('users', function ($table) {
- $table->increments('id');
- $table->integer('group_id')->nullable();
- $table->string('email')->unique();
- $table->timestamps();
- $table->softDeletes();
- });
- $this->schema()->create('posts', function ($table) {
- $table->increments('id');
- $table->integer('user_id');
- $table->string('title');
- $table->timestamps();
- $table->softDeletes();
- });
- $this->schema()->create('comments', function ($table) {
- $table->increments('id');
- $table->integer('owner_id')->nullable();
- $table->string('owner_type')->nullable();
- $table->integer('post_id');
- $table->string('body');
- $table->timestamps();
- $table->softDeletes();
- });
- $this->schema()->create('addresses', function ($table) {
- $table->increments('id');
- $table->integer('user_id');
- $table->string('address');
- $table->timestamps();
- $table->softDeletes();
- });
- $this->schema()->create('groups', function ($table) {
- $table->increments('id');
- $table->string('name');
- $table->timestamps();
- $table->softDeletes();
- });
- }
- /**
- * Tear down the database schema.
- *
- * @return void
- */
- protected function tearDown(): void
- {
- Carbon::setTestNow(null);
- $this->schema()->drop('users');
- $this->schema()->drop('posts');
- $this->schema()->drop('comments');
- }
- /**
- * Tests...
- */
- public function testSoftDeletesAreNotRetrieved()
- {
- $this->createUsers();
- $users = SoftDeletesTestUser::all();
- $this->assertCount(1, $users);
- $this->assertEquals(2, $users->first()->id);
- $this->assertNull(SoftDeletesTestUser::find(1));
- }
- public function testSoftDeletesAreNotRetrievedFromBaseQuery()
- {
- $this->createUsers();
- $query = SoftDeletesTestUser::query()->toBase();
- $this->assertInstanceOf(Builder::class, $query);
- $this->assertCount(1, $query->get());
- }
- public function testSoftDeletesAreNotRetrievedFromBuilderHelpers()
- {
- $this->createUsers();
- $count = 0;
- $query = SoftDeletesTestUser::query();
- $query->chunk(2, function ($user) use (&$count) {
- $count += count($user);
- });
- $this->assertEquals(1, $count);
- $query = SoftDeletesTestUser::query();
- $this->assertCount(1, $query->pluck('email')->all());
- Paginator::currentPageResolver(function () {
- return 1;
- });
- CursorPaginator::currentCursorResolver(function () {
- return null;
- });
- $query = SoftDeletesTestUser::query();
- $this->assertCount(1, $query->paginate(2)->all());
- $query = SoftDeletesTestUser::query();
- $this->assertCount(1, $query->simplePaginate(2)->all());
- $query = SoftDeletesTestUser::query();
- $this->assertCount(1, $query->cursorPaginate(2)->all());
- $this->assertEquals(0, SoftDeletesTestUser::where('email', 'taylorotwell@gmail.com')->increment('id'));
- $this->assertEquals(0, SoftDeletesTestUser::where('email', 'taylorotwell@gmail.com')->decrement('id'));
- }
- public function testWithTrashedReturnsAllRecords()
- {
- $this->createUsers();
- $this->assertCount(2, SoftDeletesTestUser::withTrashed()->get());
- $this->assertInstanceOf(Eloquent::class, SoftDeletesTestUser::withTrashed()->find(1));
- }
- public function testWithTrashedAcceptsAnArgument()
- {
- $this->createUsers();
- $this->assertCount(1, SoftDeletesTestUser::withTrashed(false)->get());
- $this->assertCount(2, SoftDeletesTestUser::withTrashed(true)->get());
- }
- public function testDeleteSetsDeletedColumn()
- {
- $this->createUsers();
- $this->assertInstanceOf(Carbon::class, SoftDeletesTestUser::withTrashed()->find(1)->deleted_at);
- $this->assertNull(SoftDeletesTestUser::find(2)->deleted_at);
- }
- public function testForceDeleteActuallyDeletesRecords()
- {
- $this->createUsers();
- SoftDeletesTestUser::find(2)->forceDelete();
- $users = SoftDeletesTestUser::withTrashed()->get();
- $this->assertCount(1, $users);
- $this->assertEquals(1, $users->first()->id);
- }
- public function testForceDeleteUpdateExistsProperty()
- {
- $this->createUsers();
- $user = SoftDeletesTestUser::find(2);
- $this->assertTrue($user->exists);
- $user->forceDelete();
- $this->assertFalse($user->exists);
- }
- public function testForceDeleteDoesntUpdateExistsPropertyIfFailed()
- {
- $user = new class() extends SoftDeletesTestUser
- {
- public $exists = true;
- public function newModelQuery()
- {
- return Mockery::spy(parent::newModelQuery(), function (Mockery\MockInterface $mock) {
- $mock->shouldReceive('forceDelete')->andThrow(new \Exception());
- });
- }
- };
- $this->assertTrue($user->exists);
- try {
- $user->forceDelete();
- } catch (\Exception $exception) {
- }
- $this->assertTrue($user->exists);
- }
- public function testRestoreRestoresRecords()
- {
- $this->createUsers();
- $taylor = SoftDeletesTestUser::withTrashed()->find(1);
- $this->assertTrue($taylor->trashed());
- $taylor->restore();
- $users = SoftDeletesTestUser::all();
- $this->assertCount(2, $users);
- $this->assertNull($users->find(1)->deleted_at);
- $this->assertNull($users->find(2)->deleted_at);
- }
- public function testOnlyTrashedOnlyReturnsTrashedRecords()
- {
- $this->createUsers();
- $users = SoftDeletesTestUser::onlyTrashed()->get();
- $this->assertCount(1, $users);
- $this->assertEquals(1, $users->first()->id);
- }
- public function testOnlyWithoutTrashedOnlyReturnsTrashedRecords()
- {
- $this->createUsers();
- $users = SoftDeletesTestUser::withoutTrashed()->get();
- $this->assertCount(1, $users);
- $this->assertEquals(2, $users->first()->id);
- $users = SoftDeletesTestUser::withTrashed()->withoutTrashed()->get();
- $this->assertCount(1, $users);
- $this->assertEquals(2, $users->first()->id);
- }
- public function testFirstOrNew()
- {
- $this->createUsers();
- $result = SoftDeletesTestUser::firstOrNew(['email' => 'taylorotwell@gmail.com']);
- $this->assertNull($result->id);
- $result = SoftDeletesTestUser::withTrashed()->firstOrNew(['email' => 'taylorotwell@gmail.com']);
- $this->assertEquals(1, $result->id);
- }
- public function testFindOrNew()
- {
- $this->createUsers();
- $result = SoftDeletesTestUser::findOrNew(1);
- $this->assertNull($result->id);
- $result = SoftDeletesTestUser::withTrashed()->findOrNew(1);
- $this->assertEquals(1, $result->id);
- }
- public function testFirstOrCreate()
- {
- $this->createUsers();
- $result = SoftDeletesTestUser::withTrashed()->firstOrCreate(['email' => 'taylorotwell@gmail.com']);
- $this->assertSame('taylorotwell@gmail.com', $result->email);
- $this->assertCount(1, SoftDeletesTestUser::all());
- $result = SoftDeletesTestUser::firstOrCreate(['email' => 'foo@bar.com']);
- $this->assertSame('foo@bar.com', $result->email);
- $this->assertCount(2, SoftDeletesTestUser::all());
- $this->assertCount(3, SoftDeletesTestUser::withTrashed()->get());
- }
- /**
- * @throws \Exception
- */
- public function testUpdateModelAfterSoftDeleting()
- {
- $now = Carbon::now();
- $this->createUsers();
- /** @var \Illuminate\Tests\Database\SoftDeletesTestUser $userModel */
- $userModel = SoftDeletesTestUser::find(2);
- $userModel->delete();
- $this->assertEquals($now->toDateTimeString(), $userModel->getOriginal('deleted_at'));
- $this->assertNull(SoftDeletesTestUser::find(2));
- $this->assertEquals($userModel, SoftDeletesTestUser::withTrashed()->find(2));
- }
- /**
- * @throws \Exception
- */
- public function testRestoreAfterSoftDelete()
- {
- $this->createUsers();
- /** @var \Illuminate\Tests\Database\SoftDeletesTestUser $userModel */
- $userModel = SoftDeletesTestUser::find(2);
- $userModel->delete();
- $userModel->restore();
- $this->assertEquals($userModel->id, SoftDeletesTestUser::find(2)->id);
- }
- /**
- * @throws \Exception
- */
- public function testSoftDeleteAfterRestoring()
- {
- $this->createUsers();
- /** @var \Illuminate\Tests\Database\SoftDeletesTestUser $userModel */
- $userModel = SoftDeletesTestUser::withTrashed()->find(1);
- $userModel->restore();
- $this->assertEquals($userModel->deleted_at, SoftDeletesTestUser::find(1)->deleted_at);
- $this->assertEquals($userModel->getOriginal('deleted_at'), SoftDeletesTestUser::find(1)->deleted_at);
- $userModel->delete();
- $this->assertNull(SoftDeletesTestUser::find(1));
- $this->assertEquals($userModel->deleted_at, SoftDeletesTestUser::withTrashed()->find(1)->deleted_at);
- $this->assertEquals($userModel->getOriginal('deleted_at'), SoftDeletesTestUser::withTrashed()->find(1)->deleted_at);
- }
- public function testModifyingBeforeSoftDeletingAndRestoring()
- {
- $this->createUsers();
- /** @var \Illuminate\Tests\Database\SoftDeletesTestUser $userModel */
- $userModel = SoftDeletesTestUser::find(2);
- $userModel->email = 'foo@bar.com';
- $userModel->delete();
- $userModel->restore();
- $this->assertEquals($userModel->id, SoftDeletesTestUser::find(2)->id);
- $this->assertSame('foo@bar.com', SoftDeletesTestUser::find(2)->email);
- }
- public function testUpdateOrCreate()
- {
- $this->createUsers();
- $result = SoftDeletesTestUser::updateOrCreate(['email' => 'foo@bar.com'], ['email' => 'bar@baz.com']);
- $this->assertSame('bar@baz.com', $result->email);
- $this->assertCount(2, SoftDeletesTestUser::all());
- $result = SoftDeletesTestUser::withTrashed()->updateOrCreate(['email' => 'taylorotwell@gmail.com'], ['email' => 'foo@bar.com']);
- $this->assertSame('foo@bar.com', $result->email);
- $this->assertCount(2, SoftDeletesTestUser::all());
- $this->assertCount(3, SoftDeletesTestUser::withTrashed()->get());
- }
- public function testHasOneRelationshipCanBeSoftDeleted()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $abigail->address()->create(['address' => 'Laravel avenue 43']);
- // delete on builder
- $abigail->address()->delete();
- $abigail = $abigail->fresh();
- $this->assertNull($abigail->address);
- $this->assertSame('Laravel avenue 43', $abigail->address()->withTrashed()->first()->address);
- // restore
- $abigail->address()->withTrashed()->restore();
- $abigail = $abigail->fresh();
- $this->assertSame('Laravel avenue 43', $abigail->address->address);
- // delete on model
- $abigail->address->delete();
- $abigail = $abigail->fresh();
- $this->assertNull($abigail->address);
- $this->assertSame('Laravel avenue 43', $abigail->address()->withTrashed()->first()->address);
- // force delete
- $abigail->address()->withTrashed()->forceDelete();
- $abigail = $abigail->fresh();
- $this->assertNull($abigail->address);
- }
- public function testBelongsToRelationshipCanBeSoftDeleted()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $group = SoftDeletesTestGroup::create(['name' => 'admin']);
- $abigail->group()->associate($group);
- $abigail->save();
- // delete on builder
- $abigail->group()->delete();
- $abigail = $abigail->fresh();
- $this->assertNull($abigail->group);
- $this->assertSame('admin', $abigail->group()->withTrashed()->first()->name);
- // restore
- $abigail->group()->withTrashed()->restore();
- $abigail = $abigail->fresh();
- $this->assertSame('admin', $abigail->group->name);
- // delete on model
- $abigail->group->delete();
- $abigail = $abigail->fresh();
- $this->assertNull($abigail->group);
- $this->assertSame('admin', $abigail->group()->withTrashed()->first()->name);
- // force delete
- $abigail->group()->withTrashed()->forceDelete();
- $abigail = $abigail->fresh();
- $this->assertNull($abigail->group()->withTrashed()->first());
- }
- public function testHasManyRelationshipCanBeSoftDeleted()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $abigail->posts()->create(['title' => 'First Title']);
- $abigail->posts()->create(['title' => 'Second Title']);
- // delete on builder
- $abigail->posts()->where('title', 'Second Title')->delete();
- $abigail = $abigail->fresh();
- $this->assertCount(1, $abigail->posts);
- $this->assertSame('First Title', $abigail->posts->first()->title);
- $this->assertCount(2, $abigail->posts()->withTrashed()->get());
- // restore
- $abigail->posts()->withTrashed()->restore();
- $abigail = $abigail->fresh();
- $this->assertCount(2, $abigail->posts);
- // force delete
- $abigail->posts()->where('title', 'Second Title')->forceDelete();
- $abigail = $abigail->fresh();
- $this->assertCount(1, $abigail->posts);
- $this->assertCount(1, $abigail->posts()->withTrashed()->get());
- }
- public function testSecondLevelRelationshipCanBeSoftDeleted()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post = $abigail->posts()->create(['title' => 'First Title']);
- $post->comments()->create(['body' => 'Comment Body']);
- $abigail->posts()->first()->comments()->delete();
- $abigail = $abigail->fresh();
- $this->assertCount(0, $abigail->posts()->first()->comments);
- $this->assertCount(1, $abigail->posts()->first()->comments()->withTrashed()->get());
- }
- public function testWhereHasWithDeletedRelationship()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post = $abigail->posts()->create(['title' => 'First Title']);
- $users = SoftDeletesTestUser::where('email', 'taylorotwell@gmail.com')->has('posts')->get();
- $this->assertCount(0, $users);
- $users = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->has('posts')->get();
- $this->assertCount(1, $users);
- $users = SoftDeletesTestUser::where('email', 'doesnt@exist.com')->orHas('posts')->get();
- $this->assertCount(1, $users);
- $users = SoftDeletesTestUser::whereHas('posts', function ($query) {
- $query->where('title', 'First Title');
- })->get();
- $this->assertCount(1, $users);
- $users = SoftDeletesTestUser::whereHas('posts', function ($query) {
- $query->where('title', 'Another Title');
- })->get();
- $this->assertCount(0, $users);
- $users = SoftDeletesTestUser::where('email', 'doesnt@exist.com')->orWhereHas('posts', function ($query) {
- $query->where('title', 'First Title');
- })->get();
- $this->assertCount(1, $users);
- // With Post Deleted...
- $post->delete();
- $users = SoftDeletesTestUser::has('posts')->get();
- $this->assertCount(0, $users);
- }
- public function testWhereHasWithNestedDeletedRelationshipAndOnlyTrashedCondition()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post = $abigail->posts()->create(['title' => 'First Title']);
- $post->delete();
- $users = SoftDeletesTestUser::has('posts')->get();
- $this->assertCount(0, $users);
- $users = SoftDeletesTestUser::whereHas('posts', function ($q) {
- $q->onlyTrashed();
- })->get();
- $this->assertCount(1, $users);
- $users = SoftDeletesTestUser::whereHas('posts', function ($q) {
- $q->withTrashed();
- })->get();
- $this->assertCount(1, $users);
- }
- public function testWhereHasWithNestedDeletedRelationship()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post = $abigail->posts()->create(['title' => 'First Title']);
- $comment = $post->comments()->create(['body' => 'Comment Body']);
- $comment->delete();
- $users = SoftDeletesTestUser::has('posts.comments')->get();
- $this->assertCount(0, $users);
- $users = SoftDeletesTestUser::doesntHave('posts.comments')->get();
- $this->assertCount(1, $users);
- }
- public function testWhereDoesntHaveWithNestedDeletedRelationship()
- {
- $this->createUsers();
- $users = SoftDeletesTestUser::doesntHave('posts.comments')->get();
- $this->assertCount(1, $users);
- }
- public function testWhereHasWithNestedDeletedRelationshipAndWithTrashedCondition()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUserWithTrashedPosts::where('email', 'abigailotwell@gmail.com')->first();
- $post = $abigail->posts()->create(['title' => 'First Title']);
- $post->delete();
- $users = SoftDeletesTestUserWithTrashedPosts::has('posts')->get();
- $this->assertCount(1, $users);
- }
- public function testWithCountWithNestedDeletedRelationshipAndOnlyTrashedCondition()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post1 = $abigail->posts()->create(['title' => 'First Title']);
- $post1->delete();
- $abigail->posts()->create(['title' => 'Second Title']);
- $abigail->posts()->create(['title' => 'Third Title']);
- $user = SoftDeletesTestUser::withCount('posts')->orderBy('postsCount', 'desc')->first();
- $this->assertEquals(2, $user->posts_count);
- $user = SoftDeletesTestUser::withCount(['posts' => function ($q) {
- $q->onlyTrashed();
- }])->orderBy('postsCount', 'desc')->first();
- $this->assertEquals(1, $user->posts_count);
- $user = SoftDeletesTestUser::withCount(['posts' => function ($q) {
- $q->withTrashed();
- }])->orderBy('postsCount', 'desc')->first();
- $this->assertEquals(3, $user->posts_count);
- $user = SoftDeletesTestUser::withCount(['posts' => function ($q) {
- $q->withTrashed()->where('title', 'First Title');
- }])->orderBy('postsCount', 'desc')->first();
- $this->assertEquals(1, $user->posts_count);
- $user = SoftDeletesTestUser::withCount(['posts' => function ($q) {
- $q->where('title', 'First Title');
- }])->orderBy('postsCount', 'desc')->first();
- $this->assertEquals(0, $user->posts_count);
- }
- public function testOrWhereWithSoftDeleteConstraint()
- {
- $this->createUsers();
- $users = SoftDeletesTestUser::where('email', 'taylorotwell@gmail.com')->orWhere('email', 'abigailotwell@gmail.com');
- $this->assertEquals(['abigailotwell@gmail.com'], $users->pluck('email')->all());
- }
- public function testMorphToWithTrashed()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post1 = $abigail->posts()->create(['title' => 'First Title']);
- $post1->comments()->create([
- 'body' => 'Comment Body',
- 'owner_type' => SoftDeletesTestUser::class,
- 'owner_id' => $abigail->id,
- ]);
- $abigail->delete();
- $comment = SoftDeletesTestCommentWithTrashed::with(['owner' => function ($q) {
- $q->withoutGlobalScope(SoftDeletingScope::class);
- }])->first();
- $this->assertEquals($abigail->email, $comment->owner->email);
- $comment = SoftDeletesTestCommentWithTrashed::with(['owner' => function ($q) {
- $q->withTrashed();
- }])->first();
- $this->assertEquals($abigail->email, $comment->owner->email);
- $comment = TestCommentWithoutSoftDelete::with(['owner' => function ($q) {
- $q->withTrashed();
- }])->first();
- $this->assertEquals($abigail->email, $comment->owner->email);
- }
- public function testMorphToWithBadMethodCall()
- {
- $this->expectException(BadMethodCallException::class);
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post1 = $abigail->posts()->create(['title' => 'First Title']);
- $post1->comments()->create([
- 'body' => 'Comment Body',
- 'owner_type' => SoftDeletesTestUser::class,
- 'owner_id' => $abigail->id,
- ]);
- TestCommentWithoutSoftDelete::with(['owner' => function ($q) {
- $q->thisMethodDoesNotExist();
- }])->first();
- }
- public function testMorphToWithConstraints()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post1 = $abigail->posts()->create(['title' => 'First Title']);
- $post1->comments()->create([
- 'body' => 'Comment Body',
- 'owner_type' => SoftDeletesTestUser::class,
- 'owner_id' => $abigail->id,
- ]);
- $comment = SoftDeletesTestCommentWithTrashed::with(['owner' => function ($q) {
- $q->where('email', 'taylorotwell@gmail.com');
- }])->first();
- $this->assertNull($comment->owner);
- }
- public function testMorphToWithoutConstraints()
- {
- $this->createUsers();
- $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first();
- $post1 = $abigail->posts()->create(['title' => 'First Title']);
- $post1->comments()->create([
- 'body' => 'Comment Body',
- 'owner_type' => SoftDeletesTestUser::class,
- 'owner_id' => $abigail->id,
- ]);
- $comment = SoftDeletesTestCommentWithTrashed::with('owner')->first();
- $this->assertEquals($abigail->email, $comment->owner->email);
- $abigail->delete();
- $comment = SoftDeletesTestCommentWithTrashed::with('owner')->first();
- $this->assertNull($comment->owner);
- }
- public function testMorphToNonSoftDeletingModel()
- {
- $taylor = TestUserWithoutSoftDelete::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
- $post1 = $taylor->posts()->create(['title' => 'First Title']);
- $post1->comments()->create([
- 'body' => 'Comment Body',
- 'owner_type' => TestUserWithoutSoftDelete::class,
- 'owner_id' => $taylor->id,
- ]);
- $comment = SoftDeletesTestCommentWithTrashed::with('owner')->first();
- $this->assertEquals($taylor->email, $comment->owner->email);
- $taylor->delete();
- $comment = SoftDeletesTestCommentWithTrashed::with('owner')->first();
- $this->assertNull($comment->owner);
- }
- /**
- * Helpers...
- */
- protected function createUsers()
- {
- $taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
- SoftDeletesTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
- $taylor->delete();
- }
- /**
- * Get a database connection instance.
- *
- * @return \Illuminate\Database\Connection
- */
- protected function connection()
- {
- return Eloquent::getConnectionResolver()->connection();
- }
- /**
- * Get a schema builder instance.
- *
- * @return \Illuminate\Database\Schema\Builder
- */
- protected function schema()
- {
- return $this->connection()->getSchemaBuilder();
- }
- }
- /**
- * Eloquent Models...
- */
- class TestUserWithoutSoftDelete extends Eloquent
- {
- protected $table = 'users';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(SoftDeletesTestPost::class, 'user_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class SoftDeletesTestUser extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'users';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(SoftDeletesTestPost::class, 'user_id');
- }
- public function address()
- {
- return $this->hasOne(SoftDeletesTestAddress::class, 'user_id');
- }
- public function group()
- {
- return $this->belongsTo(SoftDeletesTestGroup::class, 'group_id');
- }
- }
- class SoftDeletesTestUserWithTrashedPosts extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'users';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(SoftDeletesTestPost::class, 'user_id')->withTrashed();
- }
- }
- /**
- * Eloquent Models...
- */
- class SoftDeletesTestPost extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'posts';
- protected $guarded = [];
- public function comments()
- {
- return $this->hasMany(SoftDeletesTestComment::class, 'post_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class TestCommentWithoutSoftDelete extends Eloquent
- {
- protected $table = 'comments';
- protected $guarded = [];
- public function owner()
- {
- return $this->morphTo();
- }
- }
- /**
- * Eloquent Models...
- */
- class SoftDeletesTestComment extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'comments';
- protected $guarded = [];
- public function owner()
- {
- return $this->morphTo();
- }
- }
- class SoftDeletesTestCommentWithTrashed extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'comments';
- protected $guarded = [];
- public function owner()
- {
- return $this->morphTo();
- }
- }
- /**
- * Eloquent Models...
- */
- class SoftDeletesTestAddress extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'addresses';
- protected $guarded = [];
- }
- /**
- * Eloquent Models...
- */
- class SoftDeletesTestGroup extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'groups';
- protected $guarded = [];
- public function users()
- {
- $this->hasMany(SoftDeletesTestUser::class);
- }
- }
|