123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Database\Capsule\Manager as DB;
- use Illuminate\Database\Eloquent\Model as Eloquent;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use PHPUnit\Framework\TestCase;
- class DatabaseEloquentHasOneThroughIntegrationTest extends TestCase
- {
- protected function setUp(): void
- {
- $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->string('email')->unique();
- $table->unsignedInteger('position_id')->unique()->nullable();
- $table->string('position_short');
- $table->timestamps();
- $table->softDeletes();
- });
- $this->schema()->create('contracts', function ($table) {
- $table->increments('id');
- $table->integer('user_id')->unique();
- $table->string('title');
- $table->text('body');
- $table->string('email');
- $table->timestamps();
- });
- $this->schema()->create('positions', function ($table) {
- $table->increments('id');
- $table->string('name');
- $table->string('shortname');
- $table->timestamps();
- });
- }
- /**
- * Tear down the database schema.
- *
- * @return void
- */
- protected function tearDown(): void
- {
- $this->schema()->drop('users');
- $this->schema()->drop('contracts');
- $this->schema()->drop('positions');
- }
- public function testItLoadsAHasOneThroughRelationWithCustomKeys()
- {
- $this->seedData();
- $contract = HasOneThroughTestPosition::first()->contract;
- $this->assertSame('A title', $contract->title);
- }
- public function testItLoadsADefaultHasOneThroughRelation()
- {
- $this->migrateDefault();
- $this->seedDefaultData();
- $contract = HasOneThroughDefaultTestPosition::first()->contract;
- $this->assertSame('A title', $contract->title);
- $this->assertArrayNotHasKey('email', $contract->getAttributes());
- $this->resetDefault();
- }
- public function testItLoadsARelationWithCustomIntermediateAndLocalKey()
- {
- $this->seedData();
- $contract = HasOneThroughIntermediateTestPosition::first()->contract;
- $this->assertSame('A title', $contract->title);
- }
- public function testEagerLoadingARelationWithCustomIntermediateAndLocalKey()
- {
- $this->seedData();
- $contract = HasOneThroughIntermediateTestPosition::with('contract')->first()->contract;
- $this->assertSame('A title', $contract->title);
- }
- public function testWhereHasOnARelationWithCustomIntermediateAndLocalKey()
- {
- $this->seedData();
- $position = HasOneThroughIntermediateTestPosition::whereHas('contract', function ($query) {
- $query->where('title', 'A title');
- })->get();
- $this->assertCount(1, $position);
- }
- public function testFirstOrFailThrowsAnException()
- {
- $this->expectException(ModelNotFoundException::class);
- $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\HasOneThroughTestContract].');
- HasOneThroughTestPosition::create(['id' => 1, 'name' => 'President', 'shortname' => 'ps'])
- ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'position_short' => 'ps']);
- HasOneThroughTestPosition::first()->contract()->firstOrFail();
- }
- public function testFindOrFailThrowsAnException()
- {
- $this->expectException(ModelNotFoundException::class);
- HasOneThroughTestPosition::create(['id' => 1, 'name' => 'President', 'shortname' => 'ps'])
- ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'position_short' => 'ps']);
- HasOneThroughTestPosition::first()->contract()->findOrFail(1);
- }
- public function testFirstRetrievesFirstRecord()
- {
- $this->seedData();
- $contract = HasOneThroughTestPosition::first()->contract()->first();
- $this->assertNotNull($contract);
- $this->assertSame('A title', $contract->title);
- }
- public function testAllColumnsAreRetrievedByDefault()
- {
- $this->seedData();
- $contract = HasOneThroughTestPosition::first()->contract()->first();
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($contract->getAttributes()));
- }
- public function testOnlyProperColumnsAreSelectedIfProvided()
- {
- $this->seedData();
- $contract = HasOneThroughTestPosition::first()->contract()->first(['title', 'body']);
- $this->assertEquals([
- 'title',
- 'body',
- 'laravel_through_key',
- ], array_keys($contract->getAttributes()));
- }
- public function testChunkReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $position = HasOneThroughTestPosition::find(1);
- $position->contract()->chunk(10, function ($contractsChunk) {
- $contract = $contractsChunk->first();
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key', ], array_keys($contract->getAttributes()));
- });
- }
- public function testCursorReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $position = HasOneThroughTestPosition::find(1);
- $contracts = $position->contract()->cursor();
- foreach ($contracts as $contract) {
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key', ], array_keys($contract->getAttributes()));
- }
- }
- public function testEachReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $position = HasOneThroughTestPosition::find(1);
- $position->contract()->each(function ($contract) {
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key', ], array_keys($contract->getAttributes()));
- });
- }
- public function testLazyReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $position = HasOneThroughTestPosition::find(1);
- $position->contract()->lazy()->each(function ($contract) {
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key', ], array_keys($contract->getAttributes()));
- });
- }
- public function testIntermediateSoftDeletesAreIgnored()
- {
- $this->seedData();
- HasOneThroughSoftDeletesTestUser::first()->delete();
- $contract = HasOneThroughSoftDeletesTestPosition::first()->contract;
- $this->assertSame('A title', $contract->title);
- }
- public function testEagerLoadingLoadsRelatedModelsCorrectly()
- {
- $this->seedData();
- $position = HasOneThroughSoftDeletesTestPosition::with('contract')->first();
- $this->assertSame('ps', $position->shortname);
- $this->assertSame('A title', $position->contract->title);
- }
- /**
- * Helpers...
- */
- protected function seedData()
- {
- HasOneThroughTestPosition::create(['id' => 1, 'name' => 'President', 'shortname' => 'ps'])
- ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'position_short' => 'ps'])
- ->contract()->create(['title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com']);
- }
- protected function seedDataExtended()
- {
- $position = HasOneThroughTestPosition::create(['id' => 2, 'name' => 'Vice President', 'shortname' => 'vp']);
- $position->user()->create(['id' => 2, 'email' => 'example1@gmail.com', 'position_short' => 'vp'])
- ->contract()->create(
- ['title' => 'Example1 title1', 'body' => 'Example1 body1', 'email' => 'example1contract1@gmail.com']
- );
- }
- /**
- * Seed data for a default HasOneThrough setup.
- */
- protected function seedDefaultData()
- {
- HasOneThroughDefaultTestPosition::create(['id' => 1, 'name' => 'President'])
- ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com'])
- ->contract()->create(['title' => 'A title', 'body' => 'A body']);
- }
- /**
- * Drop the default tables.
- */
- protected function resetDefault()
- {
- $this->schema()->drop('users_default');
- $this->schema()->drop('contracts_default');
- $this->schema()->drop('positions_default');
- }
- /**
- * Migrate tables for classes with a Laravel "default" HasOneThrough setup.
- */
- protected function migrateDefault()
- {
- $this->schema()->create('users_default', function ($table) {
- $table->increments('id');
- $table->string('email')->unique();
- $table->unsignedInteger('has_one_through_default_test_position_id')->unique()->nullable();
- $table->timestamps();
- });
- $this->schema()->create('contracts_default', function ($table) {
- $table->increments('id');
- $table->integer('has_one_through_default_test_user_id')->unique();
- $table->string('title');
- $table->text('body');
- $table->timestamps();
- });
- $this->schema()->create('positions_default', function ($table) {
- $table->increments('id');
- $table->string('name');
- $table->timestamps();
- });
- }
- /**
- * 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 HasOneThroughTestUser extends Eloquent
- {
- protected $table = 'users';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOne(HasOneThroughTestContract::class, 'user_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class HasOneThroughTestContract extends Eloquent
- {
- protected $table = 'contracts';
- protected $guarded = [];
- public function owner()
- {
- return $this->belongsTo(HasOneThroughTestUser::class, 'user_id');
- }
- }
- class HasOneThroughTestPosition extends Eloquent
- {
- protected $table = 'positions';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOneThrough(HasOneThroughTestContract::class, HasOneThroughTestUser::class, 'position_id', 'user_id');
- }
- public function user()
- {
- return $this->hasOne(HasOneThroughTestUser::class, 'position_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class HasOneThroughDefaultTestUser extends Eloquent
- {
- protected $table = 'users_default';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOne(HasOneThroughDefaultTestContract::class);
- }
- }
- /**
- * Eloquent Models...
- */
- class HasOneThroughDefaultTestContract extends Eloquent
- {
- protected $table = 'contracts_default';
- protected $guarded = [];
- public function owner()
- {
- return $this->belongsTo(HasOneThroughDefaultTestUser::class);
- }
- }
- class HasOneThroughDefaultTestPosition extends Eloquent
- {
- protected $table = 'positions_default';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOneThrough(HasOneThroughDefaultTestContract::class, HasOneThroughDefaultTestUser::class);
- }
- public function user()
- {
- return $this->hasOne(HasOneThroughDefaultTestUser::class);
- }
- }
- class HasOneThroughIntermediateTestPosition extends Eloquent
- {
- protected $table = 'positions';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOneThrough(HasOneThroughTestContract::class, HasOneThroughTestUser::class, 'position_short', 'email', 'shortname', 'email');
- }
- public function user()
- {
- return $this->hasOne(HasOneThroughTestUser::class, 'position_id');
- }
- }
- class HasOneThroughSoftDeletesTestUser extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'users';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOne(HasOneThroughSoftDeletesTestContract::class, 'user_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class HasOneThroughSoftDeletesTestContract extends Eloquent
- {
- protected $table = 'contracts';
- protected $guarded = [];
- public function owner()
- {
- return $this->belongsTo(HasOneThroughSoftDeletesTestUser::class, 'user_id');
- }
- }
- class HasOneThroughSoftDeletesTestPosition extends Eloquent
- {
- protected $table = 'positions';
- protected $guarded = [];
- public function contract()
- {
- return $this->hasOneThrough(HasOneThroughSoftDeletesTestContract::class, HasOneThroughTestUser::class, 'position_id', 'user_id');
- }
- public function user()
- {
- return $this->hasOne(HasOneThroughSoftDeletesTestUser::class, 'position_id');
- }
- }
|