PHP code example of codenco-dev / eloquent-model-tester
1. Go to this page and download the library: Download codenco-dev/eloquent-model-tester library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codenco-dev / eloquent-model-tester example snippets
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasColumns(['id','name','email','password','remember_token'])
->assertHasColumnsInFillable(['name','password'])
->assertHasColumnsInGuarded(['remember_token'])
->assertHasTimestampsColumns();
}
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasOnlyColumns(['id','name','email','password','remember_token', 'created_at', 'updated_at']) // Will fail as missing 'other_field'.
->assertHasOnlyColumnsInFillable(['name','password'])
->assertHasOnlyColumnsInGuarded(['remember_token']);
}
}
class User extends Model
{
$fillable = ['name', 'password', 'email'];
$guarded = ['remember_token'];
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasColumns(['id','name','email','password','remember_token'])
->assertCanOnlyFill(['name','password']); // Will fail as 'email' is in the fillable array.
}
}
class User extends Model
{
$fillable = ['name', 'password', 'email'];
$guarded = ['email'];
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertNoGuardedAndFillableFields(); // Will fail as 'email' is in both the fillable & guarded arrays.
}
}
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasSoftDeleteTimestampColumns();
}
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_category_model()
{
$this->modelTestable(User::class)
->assertHasHasOneRelation(Phone::class);
}
}
class PhoneTest extends TestCase
{
use HasModelTestor;
public function test_have_customer_model()
{
$this->modelTestable(Phone::class)
->assertHasBelongsToRelation(User::class);
}
}
class CategoryTest extends TestCase
{
use HasModelTestor;
public function test_have_category_model()
{
$this->modelTestable(Category::class)
->assertHasHasManyRelation(Customer::class);
}
}
class CustomerTest extends TestCase
{
use HasModelTestor;
public function test_have_customer_model()
{
$this->modelTestable(Customer::class)
->assertHasBelongsToRelation(Category::class);
}
}
class CustomersTest extends TestCase
{
use HasModelTestor;
public function test_have_orders_model()
{
$this->modelTestable(Customer::class)
->assertHasHasManyThroughRelation(Order::class, Location::class);
}
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasManyToManyRelation(Role::class);
}
}
class RoleTest extends TestCase
{
use HasModelTestor;
public function test_have_role_model()
{
$this->modelTestable(User::class)
->assertHasManyToManyRelation(User::class);
}
}
class PostTest extends TestCase
{
use HasModelTestor;
public function test_have_post_model()
{
$this->modelTestable(Post::class)
->assertHasHasManyMorphRelation(Comment::class,'comments');
}
}
class VideoTest extends TestCase
{
use HasModelTestor;
public function test_have_video_model()
{
$this->modelTestable(Video::class)
->assertHasHasManyMorphRelation(Comment::class,'comments');
}
}
class CommentTest extends TestCase
{
use HasModelTestor;
public function test_have_morph_model_model()
{
$this->modelTestable(Comment::class)
->assertHasBelongsToMorphRelation(Post::class,'commentable')
->assertHasBelongsToMorphRelation(Video::class,'commentable');
}
}
class PostTest extends TestCase
{
use HasModelTestor;
public function test_have_post_model()
{
$this->modelTestable(Post::class)
->assertHasMorphOneRelation(Image::class);
}
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasMorphOneRelation(Image::class, 'avatar');
}
}
class ImageTest extends TestCase
{
use HasModelTestor;
public function test_have_image_model()
{
$this->modelTestable(Image::class)
->assertHasBelongsToMorphRelation(Post::class,'imageable')
->assertHasBelongsToMorphRelation(User::class,'imageable');
}
}
class MyPivotTest extends TestCase
{
public function test_have_table_without_model()
{
$this->tableTestable('pivot_table')
->assertHasColumns(['first_model_id','second_model_id','other_property']);
}
}
class User extends Model
{
/**
* Scope a query to only uery
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasScope('popular');
}
}
class User extends Model
{
/**
* Scope a query to only ery
* @param mixed $type
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOfType($query, $type)
{
return $query->where('type', $type);
}
}
class UserTest extends TestCase
{
use HasModelTestor;
public function test_have_user_model()
{
$this->modelTestable(User::class)
->assertHasScope('ofType', 'admin');
}
}