123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?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 Illuminate\Support\Collection;
- use Illuminate\Support\LazyCollection;
- use PHPUnit\Framework\TestCase;
- class DatabaseEloquentHasManyThroughIntegrationTest 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('country_id');
- $table->string('country_short');
- $table->timestamps();
- $table->softDeletes();
- });
- $this->schema()->create('posts', function ($table) {
- $table->increments('id');
- $table->integer('user_id');
- $table->string('title');
- $table->text('body');
- $table->string('email');
- $table->timestamps();
- });
- $this->schema()->create('countries', 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('posts');
- $this->schema()->drop('countries');
- }
- public function testItLoadsAHasManyThroughRelationWithCustomKeys()
- {
- $this->seedData();
- $posts = HasManyThroughTestCountry::first()->posts;
- $this->assertSame('A title', $posts[0]->title);
- $this->assertCount(2, $posts);
- }
- public function testItLoadsADefaultHasManyThroughRelation()
- {
- $this->migrateDefault();
- $this->seedDefaultData();
- $posts = HasManyThroughDefaultTestCountry::first()->posts;
- $this->assertSame('A title', $posts[0]->title);
- $this->assertCount(2, $posts);
- $this->resetDefault();
- }
- public function testItLoadsARelationWithCustomIntermediateAndLocalKey()
- {
- $this->seedData();
- $posts = HasManyThroughIntermediateTestCountry::first()->posts;
- $this->assertSame('A title', $posts[0]->title);
- $this->assertCount(2, $posts);
- }
- public function testEagerLoadingARelationWithCustomIntermediateAndLocalKey()
- {
- $this->seedData();
- $posts = HasManyThroughIntermediateTestCountry::with('posts')->first()->posts;
- $this->assertSame('A title', $posts[0]->title);
- $this->assertCount(2, $posts);
- }
- public function testWhereHasOnARelationWithCustomIntermediateAndLocalKey()
- {
- $this->seedData();
- $country = HasManyThroughIntermediateTestCountry::whereHas('posts', function ($query) {
- $query->where('title', 'A title');
- })->get();
- $this->assertCount(1, $country);
- }
- public function testFindMethod()
- {
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us'])
- ->posts()->createMany([
- ['id' => 1, 'title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com'],
- ['id' => 2, 'title' => 'Another title', 'body' => 'Another body', 'email' => 'taylorotwell@gmail.com'],
- ]);
- $country = HasManyThroughTestCountry::first();
- $post = $country->posts()->find(1);
- $this->assertNotNull($post);
- $this->assertSame('A title', $post->title);
- $this->assertCount(2, $country->posts()->find([1, 2]));
- $this->assertCount(2, $country->posts()->find(new Collection([1, 2])));
- }
- public function testFindManyMethod()
- {
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us'])
- ->posts()->createMany([
- ['id' => 1, 'title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com'],
- ['id' => 2, 'title' => 'Another title', 'body' => 'Another body', 'email' => 'taylorotwell@gmail.com'],
- ]);
- $country = HasManyThroughTestCountry::first();
- $this->assertCount(2, $country->posts()->findMany([1, 2]));
- $this->assertCount(2, $country->posts()->findMany(new Collection([1, 2])));
- }
- public function testFirstOrFailThrowsAnException()
- {
- $this->expectException(ModelNotFoundException::class);
- $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\HasManyThroughTestPost].');
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us']);
- HasManyThroughTestCountry::first()->posts()->firstOrFail();
- }
- public function testFindOrFailThrowsAnException()
- {
- $this->expectException(ModelNotFoundException::class);
- $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\HasManyThroughTestPost] 1');
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us']);
- HasManyThroughTestCountry::first()->posts()->findOrFail(1);
- }
- public function testFindOrFailWithManyThrowsAnException()
- {
- $this->expectException(ModelNotFoundException::class);
- $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\HasManyThroughTestPost] 1, 2');
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us'])
- ->posts()->create(['id' => 1, 'title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com']);
- HasManyThroughTestCountry::first()->posts()->findOrFail([1, 2]);
- }
- public function testFindOrFailWithManyUsingCollectionThrowsAnException()
- {
- $this->expectException(ModelNotFoundException::class);
- $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\HasManyThroughTestPost] 1, 2');
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us'])
- ->posts()->create(['id' => 1, 'title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com']);
- HasManyThroughTestCountry::first()->posts()->findOrFail(new Collection([1, 2]));
- }
- public function testFirstRetrievesFirstRecord()
- {
- $this->seedData();
- $post = HasManyThroughTestCountry::first()->posts()->first();
- $this->assertNotNull($post);
- $this->assertSame('A title', $post->title);
- }
- public function testAllColumnsAreRetrievedByDefault()
- {
- $this->seedData();
- $post = HasManyThroughTestCountry::first()->posts()->first();
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- }
- public function testOnlyProperColumnsAreSelectedIfProvided()
- {
- $this->seedData();
- $post = HasManyThroughTestCountry::first()->posts()->first(['title', 'body']);
- $this->assertEquals([
- 'title',
- 'body',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- }
- public function testChunkReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $country = HasManyThroughTestCountry::find(2);
- $country->posts()->chunk(10, function ($postsChunk) {
- $post = $postsChunk->first();
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- });
- }
- public function testChunkById()
- {
- $this->seedData();
- $this->seedDataExtended();
- $country = HasManyThroughTestCountry::find(2);
- $i = 0;
- $count = 0;
- $country->posts()->chunkById(2, function ($collection) use (&$i, &$count) {
- $i++;
- $count += $collection->count();
- });
- $this->assertEquals(3, $i);
- $this->assertEquals(6, $count);
- }
- public function testCursorReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $country = HasManyThroughTestCountry::find(2);
- $posts = $country->posts()->cursor();
- $this->assertInstanceOf(LazyCollection::class, $posts);
- foreach ($posts as $post) {
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- }
- }
- public function testEachReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $country = HasManyThroughTestCountry::find(2);
- $country->posts()->each(function ($post) {
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- });
- }
- public function testLazyReturnsCorrectModels()
- {
- $this->seedData();
- $this->seedDataExtended();
- $country = HasManyThroughTestCountry::find(2);
- $country->posts()->lazy(10)->each(function ($post) {
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- });
- }
- public function testLazyById()
- {
- $this->seedData();
- $this->seedDataExtended();
- $country = HasManyThroughTestCountry::find(2);
- $i = 0;
- $country->posts()->lazyById(2)->each(function ($post) use (&$i, &$count) {
- $i++;
- $this->assertEquals([
- 'id',
- 'user_id',
- 'title',
- 'body',
- 'email',
- 'created_at',
- 'updated_at',
- 'laravel_through_key',
- ], array_keys($post->getAttributes()));
- });
- $this->assertEquals(6, $i);
- }
- public function testIntermediateSoftDeletesAreIgnored()
- {
- $this->seedData();
- HasManyThroughSoftDeletesTestUser::first()->delete();
- $posts = HasManyThroughSoftDeletesTestCountry::first()->posts;
- $this->assertSame('A title', $posts[0]->title);
- $this->assertCount(2, $posts);
- }
- public function testEagerLoadingLoadsRelatedModelsCorrectly()
- {
- $this->seedData();
- $country = HasManyThroughSoftDeletesTestCountry::with('posts')->first();
- $this->assertSame('us', $country->shortname);
- $this->assertSame('A title', $country->posts[0]->title);
- $this->assertCount(2, $country->posts);
- }
- /**
- * Helpers...
- */
- protected function seedData()
- {
- HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'country_short' => 'us'])
- ->posts()->createMany([
- ['title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com'],
- ['title' => 'Another title', 'body' => 'Another body', 'email' => 'taylorotwell@gmail.com'],
- ]);
- }
- protected function seedDataExtended()
- {
- $country = HasManyThroughTestCountry::create(['id' => 2, 'name' => 'United Kingdom', 'shortname' => 'uk']);
- $country->users()->create(['id' => 2, 'email' => 'example1@gmail.com', 'country_short' => 'uk'])
- ->posts()->createMany([
- ['title' => 'Example1 title1', 'body' => 'Example1 body1', 'email' => 'example1post1@gmail.com'],
- ['title' => 'Example1 title2', 'body' => 'Example1 body2', 'email' => 'example1post2@gmail.com'],
- ]);
- $country->users()->create(['id' => 3, 'email' => 'example2@gmail.com', 'country_short' => 'uk'])
- ->posts()->createMany([
- ['title' => 'Example2 title1', 'body' => 'Example2 body1', 'email' => 'example2post1@gmail.com'],
- ['title' => 'Example2 title2', 'body' => 'Example2 body2', 'email' => 'example2post2@gmail.com'],
- ]);
- $country->users()->create(['id' => 4, 'email' => 'example3@gmail.com', 'country_short' => 'uk'])
- ->posts()->createMany([
- ['title' => 'Example3 title1', 'body' => 'Example3 body1', 'email' => 'example3post1@gmail.com'],
- ['title' => 'Example3 title2', 'body' => 'Example3 body2', 'email' => 'example3post2@gmail.com'],
- ]);
- }
- /**
- * Seed data for a default HasManyThrough setup.
- */
- protected function seedDefaultData()
- {
- HasManyThroughDefaultTestCountry::create(['id' => 1, 'name' => 'United States of America'])
- ->users()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com'])
- ->posts()->createMany([
- ['title' => 'A title', 'body' => 'A body'],
- ['title' => 'Another title', 'body' => 'Another body'],
- ]);
- }
- /**
- * Drop the default tables.
- */
- protected function resetDefault()
- {
- $this->schema()->drop('users_default');
- $this->schema()->drop('posts_default');
- $this->schema()->drop('countries_default');
- }
- /**
- * Migrate tables for classes with a Laravel "default" HasManyThrough setup.
- */
- protected function migrateDefault()
- {
- $this->schema()->create('users_default', function ($table) {
- $table->increments('id');
- $table->string('email')->unique();
- $table->unsignedInteger('has_many_through_default_test_country_id');
- $table->timestamps();
- });
- $this->schema()->create('posts_default', function ($table) {
- $table->increments('id');
- $table->integer('has_many_through_default_test_user_id');
- $table->string('title');
- $table->text('body');
- $table->timestamps();
- });
- $this->schema()->create('countries_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 HasManyThroughTestUser extends Eloquent
- {
- protected $table = 'users';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(HasManyThroughTestPost::class, 'user_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class HasManyThroughTestPost extends Eloquent
- {
- protected $table = 'posts';
- protected $guarded = [];
- public function owner()
- {
- return $this->belongsTo(HasManyThroughTestUser::class, 'user_id');
- }
- }
- class HasManyThroughTestCountry extends Eloquent
- {
- protected $table = 'countries';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasManyThrough(HasManyThroughTestPost::class, HasManyThroughTestUser::class, 'country_id', 'user_id');
- }
- public function users()
- {
- return $this->hasMany(HasManyThroughTestUser::class, 'country_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class HasManyThroughDefaultTestUser extends Eloquent
- {
- protected $table = 'users_default';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(HasManyThroughDefaultTestPost::class);
- }
- }
- /**
- * Eloquent Models...
- */
- class HasManyThroughDefaultTestPost extends Eloquent
- {
- protected $table = 'posts_default';
- protected $guarded = [];
- public function owner()
- {
- return $this->belongsTo(HasManyThroughDefaultTestUser::class);
- }
- }
- class HasManyThroughDefaultTestCountry extends Eloquent
- {
- protected $table = 'countries_default';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasManyThrough(HasManyThroughDefaultTestPost::class, HasManyThroughDefaultTestUser::class);
- }
- public function users()
- {
- return $this->hasMany(HasManyThroughDefaultTestUser::class);
- }
- }
- class HasManyThroughIntermediateTestCountry extends Eloquent
- {
- protected $table = 'countries';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasManyThrough(HasManyThroughTestPost::class, HasManyThroughTestUser::class, 'country_short', 'email', 'shortname', 'email');
- }
- public function users()
- {
- return $this->hasMany(HasManyThroughTestUser::class, 'country_id');
- }
- }
- class HasManyThroughSoftDeletesTestUser extends Eloquent
- {
- use SoftDeletes;
- protected $table = 'users';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(HasManyThroughSoftDeletesTestPost::class, 'user_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class HasManyThroughSoftDeletesTestPost extends Eloquent
- {
- protected $table = 'posts';
- protected $guarded = [];
- public function owner()
- {
- return $this->belongsTo(HasManyThroughSoftDeletesTestUser::class, 'user_id');
- }
- }
- class HasManyThroughSoftDeletesTestCountry extends Eloquent
- {
- protected $table = 'countries';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasManyThrough(HasManyThroughSoftDeletesTestPost::class, HasManyThroughTestUser::class, 'country_id', 'user_id');
- }
- public function users()
- {
- return $this->hasMany(HasManyThroughSoftDeletesTestUser::class, 'country_id');
- }
- }
|