Add simple unit tests for HasOneThrough and HasManyThrough relations
This commit is contained in:
parent
e33032441b
commit
26944a5f68
|
|
@ -20,6 +20,7 @@ class Author extends Model
|
||||||
*/
|
*/
|
||||||
public $belongsTo = [
|
public $belongsTo = [
|
||||||
'user' => ['Database\Tester\Models\User', 'delete' => true],
|
'user' => ['Database\Tester\Models\User', 'delete' => true],
|
||||||
|
'country' => ['Database\Tester\Models\Country'],
|
||||||
'user_soft' => ['Database\Tester\Models\SoftDeleteUser', 'key' => 'user_id', 'softDelete' => true],
|
'user_soft' => ['Database\Tester\Models\SoftDeleteUser', 'key' => 'user_id', 'softDelete' => true],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php namespace Database\Tester\Models;
|
||||||
|
|
||||||
|
use Model;
|
||||||
|
|
||||||
|
class Country extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string The database table used by the model.
|
||||||
|
*/
|
||||||
|
public $table = 'database_tester_countries';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array Guarded fields
|
||||||
|
*/
|
||||||
|
protected $guarded = [];
|
||||||
|
|
||||||
|
public $hasMany = [
|
||||||
|
'users' => [
|
||||||
|
'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;
|
||||||
|
}
|
||||||
|
|
@ -17,6 +17,19 @@ class User extends Model
|
||||||
/**
|
/**
|
||||||
* @var array Relations
|
* @var array Relations
|
||||||
*/
|
*/
|
||||||
|
public $hasOne = [
|
||||||
|
'author' => [
|
||||||
|
'Database\Tester\Models\Author',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
public $hasOneThrough = [
|
||||||
|
'phone' => [
|
||||||
|
'Database\Tester\Models\Phone',
|
||||||
|
'through' => 'Database\Tester\Models\Author',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
public $attachOne = [
|
public $attachOne = [
|
||||||
'avatar' => 'System\Models\File'
|
'avatar' => 'System\Models\File'
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ class CreateAuthorsTable extends Migration
|
||||||
$table->engine = 'InnoDB';
|
$table->engine = 'InnoDB';
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->integer('user_id')->unsigned()->index()->nullable();
|
$table->integer('user_id')->unsigned()->index()->nullable();
|
||||||
|
$table->integer('country_id')->unsigned()->index()->nullable();
|
||||||
$table->string('name')->nullable();
|
$table->string('name')->nullable();
|
||||||
$table->string('email')->nullable();
|
$table->string('email')->nullable();
|
||||||
$table->softDeletes();
|
$table->softDeletes();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php namespace Database\Tester\Updates;
|
||||||
|
|
||||||
|
use Schema;
|
||||||
|
use October\Rain\Database\Updates\Migration;
|
||||||
|
|
||||||
|
class CreateCountriesTable extends Migration
|
||||||
|
{
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('database_tester_countries', function ($table) {
|
||||||
|
$table->engine = 'InnoDB';
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name')->nullable();
|
||||||
|
$table->softDeletes();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('database_tester_countries');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,3 +9,4 @@
|
||||||
- create_users_table.php
|
- create_users_table.php
|
||||||
- create_event_log_table.php
|
- create_event_log_table.php
|
||||||
- create_meta_table.php
|
- create_meta_table.php
|
||||||
|
- create_countries_table.php
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Database\Tester\Models\Author;
|
||||||
|
use Database\Tester\Models\Country;
|
||||||
|
use Database\Tester\Models\Post;
|
||||||
|
use October\Rain\Database\Collection;
|
||||||
|
|
||||||
|
class HasManyThroughModelTest extends PluginTestCase
|
||||||
|
{
|
||||||
|
public function setUp() : void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
|
||||||
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
|
||||||
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Country.php';
|
||||||
|
|
||||||
|
$this->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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Database\Tester\Models\Author;
|
||||||
|
use Database\Tester\Models\Phone;
|
||||||
|
use Database\Tester\Models\User;
|
||||||
|
|
||||||
|
class HasOneThroughModelTest extends PluginTestCase
|
||||||
|
{
|
||||||
|
public function setUp() : void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/User.php';
|
||||||
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
|
||||||
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Phone.php';
|
||||||
|
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue