PHP code example of sofa / eloquent-testsuite
1. Go to this page and download the library: Download sofa/eloquent-testsuite 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' );
sofa / eloquent-testsuite example snippets
class SomeModelTest extends \PHPUnit \Framework \TestCase
{
use EloquentSuite ;
public function user_belongs_to_organization ()
{
$user = $this ->createRelationMock(User::class, 'belongsTo' , Organization::class);
$this ->assertRelation('belongsTo' , $user->organization());
}
public function user_has_many_customers ()
{
[$user, $relation] = $this ->createRelationChainMock(User::class, 'hasMany' , Customer::class);
$relation->shouldReceive('active' )->once()->andReturnSelf();
$relation->shouldReceive('latest' )->once()->andReturnSelf();
$this ->assertRelation('hasMany' , $user->customers());
}
}
assertScopeFilters(Model $model, string $scope, string $column, string $value)
assertScopeFiltersNull(Model $model, string $scope, string $column)
assertScopeFiltersNotNull(Model $model, string $scope, string $column)
assertScopeFiltersIn(Model $model, string $scope, string $column, array $values)
assertScopeFiltersNotIn(Model $model, string $scope, string $column, array $values)
class SomeModelTest extends \PHPUnit \Framework \TestCase
{
use EloquentSuite ;
public function testScopePublished ()
{
$article = new Article();
$this ->assertScopeFilters($article, 'published' , 'status' , 1 );
}
public function testScopeAvailable ()
{
$article = new Article();
$this ->assertScopeFiltersNull($article, 'available' , 'deleted_at' );
}
public function testScopeDeleted ()
{
$article = new Article();
$this ->assertScopeFiltersNotNull($article, 'deleted' , 'deleted_at' );
}
}
public function scopeValidOnDate ($query, $date)
{
return $query->whereRaw('? between valid_from and valid_to' , [$date]);
}
public function testScopeValidOnDate ()
{
$assertion = $this ->stringContains('valid_from' );
$params = ['2020-10-10' ];
$query = $this ->createQueryMock('whereRaw' , $assertion, $params);
$ticket = new Ticket();
$ticket->scopeValidOnDate($query, '2020-10-10' );
}