-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BelongsToMany, HasMany and HasOne tests
- Loading branch information
1 parent
8412881
commit f2c68ce
Showing
15 changed files
with
1,332 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Winter\Storm\Tests\Database\Fixtures; | ||
|
||
use Illuminate\Database\Schema\Builder; | ||
use Winter\Storm\Database\Model; | ||
|
||
class EventLog extends Model | ||
{ | ||
use MigratesForTest; | ||
use \Winter\Storm\Database\Traits\SoftDelete; | ||
|
||
/** | ||
* @var string The database table used by the model. | ||
*/ | ||
public $table = 'database_tester_event_log'; | ||
|
||
/** | ||
* @var array Guarded fields | ||
*/ | ||
protected $guarded = ['*']; | ||
|
||
/** | ||
* @var array Fillable fields | ||
*/ | ||
protected $fillable = []; | ||
|
||
/** | ||
* @var array Relations | ||
*/ | ||
public $morphTo = [ | ||
'related' => [] | ||
]; | ||
|
||
public static function migrateUp(Builder $builder): void | ||
{ | ||
if ($builder->hasTable('database_tester_event_log')) { | ||
return; | ||
} | ||
|
||
$builder->create('database_tester_event_log', function ($table) { | ||
$table->engine = 'InnoDB'; | ||
$table->increments('id'); | ||
$table->string('action', 30)->nullable(); | ||
$table->string('related_id')->index()->nullable(); | ||
$table->string('related_type')->index()->nullable(); | ||
$table->softDeletes(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public static function migrateDown(Builder $builder): void | ||
{ | ||
if (!$builder->hasTable('database_tester_event_log')) { | ||
return; | ||
} | ||
|
||
$builder->dropIfExists('database_tester_event_log'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Winter\Storm\Tests\Database\Fixtures; | ||
|
||
class NullablePost extends Post | ||
{ | ||
use \Winter\Storm\Database\Traits\Nullable; | ||
|
||
/** | ||
* @var array Guarded fields | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* @var array List of attributes to nullify | ||
*/ | ||
protected $nullable = [ | ||
'author_nickname', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Winter\Storm\Tests\Database\Fixtures; | ||
|
||
use Winter\Storm\Database\Model; | ||
use Illuminate\Database\Schema\Builder; | ||
|
||
class Phone extends Model | ||
{ | ||
use MigratesForTest; | ||
|
||
/** | ||
* @var string The database table used by the model. | ||
*/ | ||
public $table = 'database_tester_phones'; | ||
|
||
/** | ||
* @var array Guarded fields | ||
*/ | ||
protected $guarded = ['*']; | ||
|
||
/** | ||
* @var array Fillable fields | ||
*/ | ||
protected $fillable = []; | ||
|
||
/** | ||
* @var array Relations | ||
*/ | ||
public $belongsTo = [ | ||
'author' => Author::class, | ||
]; | ||
|
||
public static function migrateUp(Builder $builder): void | ||
{ | ||
if ($builder->hasTable('database_tester_phones')) { | ||
return; | ||
} | ||
|
||
$builder->create('database_tester_phones', function ($table) { | ||
$table->engine = 'InnoDB'; | ||
$table->increments('id'); | ||
$table->string('number')->nullable(); | ||
$table->integer('author_id')->unsigned()->index()->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public static function migrateDown(Builder $builder): void | ||
{ | ||
if (!$builder->hasTable('database_tester_phones')) { | ||
return; | ||
} | ||
|
||
$builder->dropIfExists('database_tester_phones'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Winter\Storm\Tests\Database\Fixtures; | ||
|
||
use Winter\Storm\Database\Models\Revision; | ||
|
||
class RevisionablePost extends Post | ||
{ | ||
use \Winter\Storm\Database\Traits\Revisionable; | ||
use \Winter\Storm\Database\Traits\SoftDelete; | ||
|
||
/** | ||
* @var array Guarded fields | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* @var array Dates | ||
*/ | ||
protected $dates = ['published_at', 'deleted_at']; | ||
|
||
/** | ||
* @var array Monitor these attributes for changes. | ||
*/ | ||
protected $revisionable = [ | ||
'title', | ||
'slug', | ||
'description', | ||
'is_published', | ||
'published_at', | ||
'deleted_at' | ||
]; | ||
|
||
/** | ||
* @var int Maximum number of revision records to keep. | ||
*/ | ||
public $revisionableLimit = 8; | ||
|
||
/** | ||
* @var array Relations | ||
*/ | ||
public $morphMany = [ | ||
'revision_history' => [Revision::class, 'name' => 'revisionable'] | ||
]; | ||
|
||
/** | ||
* The user who made the revision. | ||
*/ | ||
public function getRevisionableUser() | ||
{ | ||
return 7; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Winter\Storm\Tests\Database\Fixtures; | ||
|
||
use Illuminate\Database\Schema\Builder; | ||
use Winter\Storm\Database\Model; | ||
use Winter\Storm\Database\Relations\BelongsToMany; | ||
|
||
/** | ||
* Role Model | ||
*/ | ||
class Role extends Model | ||
{ | ||
use MigratesForTest; | ||
|
||
/** | ||
* @var string The database table used by the model. | ||
*/ | ||
public $table = 'database_tester_roles'; | ||
|
||
/** | ||
* @var array Guarded fields | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* @var array Fillable fields | ||
*/ | ||
protected $fillable = []; | ||
|
||
/** | ||
* @var array Relations | ||
*/ | ||
public $belongsToMany = [ | ||
'authors' => [ | ||
User::class, | ||
'table' => 'database_tester_authors_roles' | ||
], | ||
]; | ||
|
||
public function users(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(User::class, 'database_tester_authors_roles'); | ||
} | ||
|
||
public static function migrateUp(Builder $builder): void | ||
{ | ||
if ($builder->hasTable('database_tester_roles')) { | ||
return; | ||
} | ||
|
||
$builder->create('database_tester_roles', function ($table) { | ||
$table->engine = 'InnoDB'; | ||
$table->increments('id'); | ||
$table->string('name')->nullable(); | ||
$table->text('description')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
|
||
$builder->create('database_tester_authors_roles', function ($table) { | ||
$table->engine = 'InnoDB'; | ||
$table->integer('author_id')->unsigned(); | ||
$table->integer('role_id')->unsigned(); | ||
$table->primary(['author_id', 'role_id']); | ||
$table->string('clearance_level')->nullable(); | ||
$table->boolean('is_executive')->default(false); | ||
}); | ||
} | ||
|
||
public static function migrateDown(Builder $builder): void | ||
{ | ||
if (!$builder->hasTable('database_tester_roles')) { | ||
return; | ||
} | ||
|
||
$builder->dropIfExists('database_tester_roles'); | ||
$builder->dropIfExists('database_tester_authors_roles'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Winter\Storm\Tests\Database\Fixtures; | ||
|
||
class SluggablePost extends Post | ||
{ | ||
use \Winter\Storm\Database\Traits\Sluggable; | ||
|
||
/** | ||
* @var array Guarded fields | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* @var array List of attributes to automatically generate unique URL names (slugs) for. | ||
*/ | ||
protected $slugs = [ | ||
'slug' => 'title', | ||
'long_slug' => ['title', 'description'] | ||
]; | ||
} |
Oops, something went wrong.