diff --git a/tests/fixtures/plugins/database/tester/models/Author.php b/tests/fixtures/plugins/database/tester/models/Author.php index 05e9a039f..2d9656fee 100644 --- a/tests/fixtures/plugins/database/tester/models/Author.php +++ b/tests/fixtures/plugins/database/tester/models/Author.php @@ -20,6 +20,7 @@ class Author extends Model */ public $belongsTo = [ 'user' => ['Database\Tester\Models\User', 'delete' => true], + 'country' => ['Database\Tester\Models\Country'], 'user_soft' => ['Database\Tester\Models\SoftDeleteUser', 'key' => 'user_id', 'softDelete' => true], ]; diff --git a/tests/fixtures/plugins/database/tester/models/Country.php b/tests/fixtures/plugins/database/tester/models/Country.php new file mode 100644 index 000000000..e7bb583c5 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/models/Country.php @@ -0,0 +1,35 @@ + [ + 'Database\Tester\Models\User', + ], + ]; + + public $hasManyThrough = [ + 'posts' => [ + 'Database\Tester\Models\Post', + 'through' => 'Database\Tester\Models\Author', + ] + ]; +} + +class SoftDeleteCountry extends Country +{ + use \October\Rain\Database\Traits\SoftDelete; +} diff --git a/tests/fixtures/plugins/database/tester/models/User.php b/tests/fixtures/plugins/database/tester/models/User.php index 867992c2c..130120e9c 100644 --- a/tests/fixtures/plugins/database/tester/models/User.php +++ b/tests/fixtures/plugins/database/tester/models/User.php @@ -17,6 +17,19 @@ class User extends Model /** * @var array Relations */ + public $hasOne = [ + 'author' => [ + 'Database\Tester\Models\Author', + ] + ]; + + public $hasOneThrough = [ + 'phone' => [ + 'Database\Tester\Models\Phone', + 'through' => 'Database\Tester\Models\Author', + ], + ]; + public $attachOne = [ 'avatar' => 'System\Models\File' ]; diff --git a/tests/fixtures/plugins/database/tester/updates/create_authors_table.php b/tests/fixtures/plugins/database/tester/updates/create_authors_table.php index 6466e4e13..d6277e525 100644 --- a/tests/fixtures/plugins/database/tester/updates/create_authors_table.php +++ b/tests/fixtures/plugins/database/tester/updates/create_authors_table.php @@ -11,6 +11,7 @@ class CreateAuthorsTable extends Migration $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('user_id')->unsigned()->index()->nullable(); + $table->integer('country_id')->unsigned()->index()->nullable(); $table->string('name')->nullable(); $table->string('email')->nullable(); $table->softDeletes(); diff --git a/tests/fixtures/plugins/database/tester/updates/create_countries_table.php b/tests/fixtures/plugins/database/tester/updates/create_countries_table.php new file mode 100644 index 000000000..7fc85ae54 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/updates/create_countries_table.php @@ -0,0 +1,23 @@ +engine = 'InnoDB'; + $table->increments('id'); + $table->string('name')->nullable(); + $table->softDeletes(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('database_tester_countries'); + } +} diff --git a/tests/fixtures/plugins/database/tester/updates/version.yaml b/tests/fixtures/plugins/database/tester/updates/version.yaml index 613fc5337..590bb7892 100644 --- a/tests/fixtures/plugins/database/tester/updates/version.yaml +++ b/tests/fixtures/plugins/database/tester/updates/version.yaml @@ -9,3 +9,4 @@ - create_users_table.php - create_event_log_table.php - create_meta_table.php + - create_countries_table.php diff --git a/tests/unit/plugins/database/HasManyThroughModelTest.php b/tests/unit/plugins/database/HasManyThroughModelTest.php new file mode 100644 index 000000000..85ac947f2 --- /dev/null +++ b/tests/unit/plugins/database/HasManyThroughModelTest.php @@ -0,0 +1,54 @@ +runPluginRefreshCommand('Database.Tester'); + } + + public function testGet() + { + Model::unguard(); + $country = Country::create(['name' => 'Australia']); + $author1 = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']); + $author2 = Author::create(['name' => 'Louie', 'email' => 'louie@email.tld']); + $post1 = Post::create(['title' => "First post", 'description' => "Yay!!"]); + $post2 = Post::create(['title' => "Second post", 'description' => "Woohoo!!"]); + $post3 = Post::create(['title' => "Third post", 'description' => "Yipiee!!"]); + $post4 = Post::make(['title' => "Fourth post", 'description' => "Hooray!!"]); + Model::reguard(); + + // Set data + $author1->country = $country; + $author2->country = $country; + + $author1->posts = new Collection([$post1, $post2]); + $author2->posts = new Collection([$post3, $post4]); + + $author1->save(); + $author2->save(); + + $country = Country::with([ + 'posts' + ])->find($country->id); + + $this->assertEquals([ + $post1->id, + $post2->id, + $post3->id, + $post4->id + ], $country->posts->pluck('id')->toArray()); + } +} diff --git a/tests/unit/plugins/database/HasOneThroughModelTest.php b/tests/unit/plugins/database/HasOneThroughModelTest.php new file mode 100644 index 000000000..91be83b60 --- /dev/null +++ b/tests/unit/plugins/database/HasOneThroughModelTest.php @@ -0,0 +1,39 @@ +runPluginRefreshCommand('Database.Tester'); + } + + public function testGet() + { + Model::unguard(); + $phone = Phone::create(['number' => '08 1234 5678']); + $author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']); + $user = User::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']); + Model::reguard(); + + // Set data + $author->phone = $phone; + $author->user = $user; + $author->save(); + + $user = User::with([ + 'phone' + ])->find($user->id); + + $this->assertEquals($phone->id, $user->phone->id); + } +}