Skip to content

Commit

Permalink
Add BelongsToMany, HasMany and HasOne tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Feb 19, 2024
1 parent 8412881 commit f2c68ce
Show file tree
Hide file tree
Showing 15 changed files with 1,332 additions and 9 deletions.
36 changes: 27 additions & 9 deletions tests/Database/Fixtures/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Database\Schema\Builder;
use Winter\Storm\Database\Model;
use Winter\Storm\Database\Relations\BelongsToMany;
use Winter\Storm\Database\Relations\HasMany;
use Winter\Storm\Database\Relations\HasOne;

class Author extends Model
{
Expand Down Expand Up @@ -33,23 +36,18 @@ class Author extends Model
];

public $hasOne = [
'phone' => 'Database\Tester\Models\Phone',
'phone' => Phone::class,
];

public $belongsToMany = [
'roles' => [
'Database\Tester\Models\Role',
'Winter\Storm\Tests\Database\Fixtures\Role',
'table' => 'database_tester_authors_roles'
],
'executive_authors' => [
'Database\Tester\Models\Role',
'table' => 'database_tester_authors_roles',
'conditions' => 'is_executive = 1'
],
];

public $morphMany = [
'event_log' => ['Database\Tester\Models\EventLog', 'name' => 'related', 'delete' => true, 'softDelete' => true],
'event_log' => [EventLog::class, 'name' => 'related', 'delete' => true, 'softDelete' => true],
];

public $morphOne = [
Expand All @@ -58,13 +56,33 @@ class Author extends Model

public $morphToMany = [
'tags' => [
'Database\Tester\Models\Tag',
Tag::class,
'name' => 'taggable',
'table' => 'database_tester_taggables',
'pivot' => ['added_by']
],
];

public function contactNumber(): HasOne
{
return $this->hasOne(Phone::class);
}

public function messages(): HasMany
{
return $this->hasMany(Post::class);
}

public function scopes(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'database_tester_authors_roles');
}

public function executiveAuthors(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'database_tester_authors_roles')->wherePivot('is_executive', 1);
}

public static function migrateUp(Builder $builder): void
{
if ($builder->hasTable('database_tester_authors')) {
Expand Down
60 changes: 60 additions & 0 deletions tests/Database/Fixtures/EventLog.php
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');
}
}
20 changes: 20 additions & 0 deletions tests/Database/Fixtures/NullablePost.php
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',
];
}
57 changes: 57 additions & 0 deletions tests/Database/Fixtures/Phone.php
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');
}
}
10 changes: 10 additions & 0 deletions tests/Database/Fixtures/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public static function migrateUp(Builder $builder): void
$table->softDeletes();
$table->timestamps();
});

$builder->create('database_tester_categories_posts', function ($table) {
$table->engine = 'InnoDB';
$table->integer('category_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->primary(['category_id', 'post_id']);
$table->string('category_name')->nullable();
$table->string('post_name')->nullable();
});
}

public static function migrateDown(Builder $builder): void
Expand All @@ -93,6 +102,7 @@ public static function migrateDown(Builder $builder): void
return;
}

$builder->dropIfExists('database_tester_categories_posts');
$builder->dropIfExists('database_tester_posts');
}
}
53 changes: 53 additions & 0 deletions tests/Database/Fixtures/RevisionablePost.php
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;
}
}
79 changes: 79 additions & 0 deletions tests/Database/Fixtures/Role.php
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');
}
}
21 changes: 21 additions & 0 deletions tests/Database/Fixtures/SluggablePost.php
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']
];
}
Loading

0 comments on commit f2c68ce

Please sign in to comment.