Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Jan 7, 2023
1 parent 62679dd commit daad50e
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 179 deletions.
5 changes: 0 additions & 5 deletions src/FakeQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ public function crossJoin($table, $first = null, $operator = null, $second = nul
{
return $this;
}

public function addFakeRow(string $table, $val, $key)
{
FakeDB::changeFakeRow($table, $val, $key);
}
}
11 changes: 5 additions & 6 deletions tests/AggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Illuminate\Database\Eloquent\Model;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

class AggregateUser extends Model
{
use MockableModel;
protected $table = 'users';
}

class AggregateTest extends TestCase
Expand All @@ -18,10 +17,10 @@ public function setUp(): void
{
FakeDB::mockQueryBuilder();

AggregateUser::addFakeRow(['id' => 1, 'name' => null, 'age' => 20,]);
AggregateUser::addFakeRow(['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
AggregateUser::addFakeRow(['id' => 3, 'name' => 'Iman 3', 'age' => null,]);
AggregateUser::addFakeRow(['id' => 4, 'name' => 'Iman 4', 'age' => 40,]);
FakeDB::addRow('users', ['id' => 1, 'name' => null, 'age' => 20,]);
FakeDB::addRow('users', ['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
FakeDB::addRow('users', ['id' => 3, 'name' => 'Iman 3', 'age' => null,]);
FakeDB::addRow('users', ['id' => 4, 'name' => 'Iman 4', 'age' => 40,]);
}

public function tearDown(): void
Expand Down
51 changes: 25 additions & 26 deletions tests/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Events\Dispatcher;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

class DeleteUser extends Model
{
use MockableModel;
protected $table = 'users';
}

class DeleteTest extends TestCase
Expand All @@ -30,11 +29,11 @@ public function setUp(): void
*/
public function destroy()
{
DeleteUser::addFakeRow(['id' => 1]);
DeleteUser::addFakeRow(['id' => 2]);
DeleteUser::addFakeRow(['id' => 3]);
DeleteUser::addFakeRow(['id' => 4]);
DeleteUser::fakeSoftDelete();
FakeDB::addRow('users', ['id' => 1]);
FakeDB::addRow('users', ['id' => 2]);
FakeDB::addRow('users', ['id' => 3]);
FakeDB::addRow('users', ['id' => 4]);
//DeleteUser::fakeSoftDelete();
$count = DeleteUser::destroy(1, 2);
$this->assertEquals(2, $count);
//$this->assertNotNull(DeleteUser::getDeletedModel(1));
Expand All @@ -58,11 +57,11 @@ public function destroy()
*/
public function delete()
{
DeleteUser::addFakeRow(['id' => 1]);
FakeDB::addRow('users', ['id' => 1]);
$user = DeleteUser::find(1);
$result = $user->delete();

$user2 = DeleteUser::getDeletedModel();
//$user2 = DeleteUser::getDeletedModel();

//$this->assertEquals(spl_object_hash($user), spl_object_hash($user2));
$this->assertTrue($result);
Expand All @@ -77,7 +76,7 @@ public function delete()
*/
public function delete_non_existent()
{
DeleteUser::addFakeRow(['id' => 1]);
FakeDB::addRow('users', ['id' => 1]);
DeleteUser::setEventDispatcher(new Dispatcher);

$std = new \stdClass();
Expand All @@ -93,9 +92,9 @@ public function delete_non_existent()
$user->exists = false;
$result = $user->delete();

$model = DeleteUser::getDeletedModel();
//$model = DeleteUser::getDeletedModel();

$this->assertNull($model);
//$this->assertNull($model);
$this->assertNull($result);
$this->assertFalse($user->exists);
$this->assertFalse($std->deleting);
Expand All @@ -107,7 +106,7 @@ public function delete_non_existent()
public function delete_on_deleting()
{
DeleteUser::setEventDispatcher(new Dispatcher);
DeleteUser::addFakeRow([
FakeDB::addRow('users', [
'id' => 1,
'name' => 'mocky',
]);
Expand All @@ -128,9 +127,9 @@ public function delete_on_deleting()

$user = DeleteUser::first();
$result = $user->delete();
$model = DeleteUser::getDeletedModel();
//$model = DeleteUser::getDeletedModel();

$this->assertNull($model);
//$this->assertNull($model);
$this->assertFalse($result);
$this->assertTrue($user->exists);
$this->assertTrue($std->deleting);
Expand All @@ -144,7 +143,7 @@ public function delete_on_deleting()
public function delete_events()
{
DeleteUser::setEventDispatcher(new Dispatcher);
DeleteUser::addFakeRow([
FakeDB::addRow('users', [
'id' => 1,
'name' => 'mocky',
]);
Expand All @@ -161,7 +160,7 @@ public function delete_events()

$user = DeleteUser::find(1);
$result = $user->delete();
$model = DeleteUser::getDeletedModel();
//$model = DeleteUser::getDeletedModel();

//$this->assertSame($user, $model);
$this->assertTrue($result);
Expand All @@ -177,11 +176,11 @@ public function force_delete()
{
DeleteUser::setEventDispatcher(new Dispatcher);

DeleteUser::addFakeRow(['id' => 1]);
FakeDB::addRow('users', ['id' => 1]);
$user = DeleteUser::query()->find(1);

$result = $user->forceDelete();
$deletedModel = DeleteUser::getDeletedModel();
//$deletedModel = DeleteUser::getDeletedModel();

//$this->assertEquals(1, $deletedModel->id);
$this->assertTrue($result);
Expand All @@ -194,14 +193,14 @@ public function force_delete()
public function delete_or_fail()
{
DeleteUser::setEventDispatcher(new Dispatcher);
DeleteUser::addFakeRow(['id' => 1]);
FakeDB::addRow('users', ['id' => 1]);
$user = DeleteUser::find(1);

if (! method_exists($user, 'deleteOrFail')) {
$this->markTestSkipped();
}
$result = $user->deleteOrFail();
$deletedModel = DeleteUser::getDeletedModel();
//$deletedModel = DeleteUser::getDeletedModel();

//$this->assertEquals(1, $deletedModel->id);
//$this->assertSame($deletedModel, $user);
Expand All @@ -217,10 +216,10 @@ public function raw_delete_empty_table()
DeleteUser::setEventDispatcher(new Dispatcher);

$count = DeleteUser::where('id', 1)->delete();
$model = DeleteUser::getDeletedModel();
//$model = DeleteUser::getDeletedModel();

$this->assertEquals(0, $count);
$this->assertNull($model);
//$this->assertNull($model);
}

/**
Expand All @@ -229,14 +228,14 @@ public function raw_delete_empty_table()
public function raw_delete2()
{
DeleteUser::setEventDispatcher(new Dispatcher);
DeleteUser::addFakeRow([
FakeDB::addRow('users', [
'id' => 1,
'name' => 'mocky',
]);
$count = DeleteUser::where('id', 2)->delete();
$model = DeleteUser::getDeletedModel();
//$model = DeleteUser::getDeletedModel();

$this->assertEquals(0, $count);
$this->assertNull($model);
//$this->assertNull($model);
}
}
11 changes: 5 additions & 6 deletions tests/ExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Illuminate\Database\Eloquent\Model;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

class ExistsUser extends Model
{
use MockableModel;
protected $table = 'users';
}

class ExistsTest extends TestCase
Expand All @@ -29,10 +28,10 @@ public function setUp(): void
*/
public function basic_exists()
{
ExistsUser::addFakeRow(['id' => 1, 'name' => null, 'age' => 20,]);
ExistsUser::addFakeRow(['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
ExistsUser::addFakeRow(['id' => 3, 'name' => 'Iman 3', 'age' => null,]);
ExistsUser::addFakeRow(['id' => 4, 'name' => 'Iman 4', 'age' => 40,]);
FakeDB::addRow('users',['id' => 1, 'name' => null, 'age' => 20,]);
FakeDB::addRow('users',['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
FakeDB::addRow('users',['id' => 3, 'name' => 'Iman 3', 'age' => null,]);
FakeDB::addRow('users',['id' => 4, 'name' => 'Iman 4', 'age' => 40,]);

$this->assertTrue(ExistsUser::query()->exists());
$this->assertTrue(ExistsUser::query()->exists());
Expand Down
12 changes: 7 additions & 5 deletions tests/FakeDB/FakeDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
namespace Imanghafoori\EloquentMockery\Tests\FakeDB;

use Illuminate\Database\Eloquent\Model;
use Imanghafoori\EloquentMockery\FakeConnection;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

class FakeDbUser extends Model
{
use MockableModel;

public $table = 'users';
protected $table = 'users';
}

class FakeDbTest extends TestCase
{
public function setUp(): void
{
FakeDB::mockQueryBuilder();
}

public function tearDown(): void
{
FakeDB::dontMockQueryBuilder();
FakeDB::truncate();
}

Expand Down
17 changes: 8 additions & 9 deletions tests/IgnoreWheresTest_.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

class IgnoreWheresUser extends Model
{
use MockableModel;
protected $table = 'users';
}

class IgnoreWheresTest_ extends TestCase
Expand All @@ -30,9 +29,9 @@ public function setUp(): void
*/
public function where__whereIn()
{
IgnoreWheresUser::addFakeRow(['id' => 1, 'name' => 'Iman 1', 'age' => 20,]);
IgnoreWheresUser::addFakeRow(['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
IgnoreWheresUser::addFakeRow(['id' => 3, 'name' => 'Iman 3', 'age' => 34,]);
FakeDB::addRow('users', ['id' => 1, 'name' => 'Iman 1', 'age' => 20,]);
FakeDB::addRow('users', ['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
FakeDB::addRow('users', ['id' => 3, 'name' => 'Iman 3', 'age' => 34,]);

IgnoreWheresUser::ignoreWheres();

Expand Down Expand Up @@ -87,10 +86,10 @@ public function where__whereIn()
*/
public function test_get()
{
IgnoreWheresUser::addFakeRow(['id' => 1, 'name' => null, 'age' => 20,]);
IgnoreWheresUser::addFakeRow(['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
IgnoreWheresUser::addFakeRow(['id' => 3, 'name' => 'Iman 3', 'age' => null,]);
IgnoreWheresUser::addFakeRow(['id' => 4, 'name' => 'Iman 4', 'age' => 40,]);
FakeDB::addRow('users', ['id' => 1, 'name' => null, 'age' => 20,]);
FakeDB::addRow('users', ['id' => 2, 'name' => 'Iman 2', 'age' => 30,]);
FakeDB::addRow('users', ['id' => 3, 'name' => 'Iman 3', 'age' => null,]);
FakeDB::addRow('users', ['id' => 4, 'name' => 'Iman 4', 'age' => 40,]);

IgnoreWheresUser::ignoreWheres();

Expand Down
25 changes: 12 additions & 13 deletions tests/Relations/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Imanghafoori\EloquentMockery\MockableModel;
use PHPUnit\Framework\TestCase;

class HasManyUser extends Model
{
public $fillable = ['name'];

use MockableModel;
protected $table = 'users';

public function comments()
{
Expand All @@ -21,7 +20,7 @@ public function comments()

class HasManyComment extends Model
{
use MockableModel;
protected $table = 'users';

public $fillable = ['comment'];

Expand All @@ -38,16 +37,16 @@ class HasManyTest extends TestCase
*/
public function has_many()
{
HasManyUser::addFakeRow(['id' => 1, 'name' => 'Iman 1']);
HasManyUser::addFakeRow(['id' => 2, 'name' => 'Iman 2']);
HasManyUser::addFakeRow(['id' => 3, 'name' => 'Iman 3']);
HasManyUser::addFakeRow(['id' => 4, 'name' => 'Iman 4']);

HasManyComment::addFakeRow(['id' => 1, 'user_id' => 1, 'comment' => 'sss']);
HasManyComment::addFakeRow(['id' => 2, 'user_id' => 1, 'comment' => 'aaa']);
HasManyComment::addFakeRow(['id' => 3, 'user_id' => 2, 'comment' => 'bbb']);
HasManyComment::addFakeRow(['id' => 4, 'user_id' => 2, 'comment' => 'ccc']);
HasManyComment::addFakeRow(['id' => 5, 'user_id' => 3, 'comment' => 'ddd']);
FakeDB::addRow('users', ['id' => 1, 'name' => 'Iman 1']);
FakeDB::addRow('users', ['id' => 2, 'name' => 'Iman 2']);
FakeDB::addRow('users', ['id' => 3, 'name' => 'Iman 3']);
FakeDB::addRow('users', ['id' => 4, 'name' => 'Iman 4']);

FakeDB::addRow('users', ['id' => 1, 'user_id' => 1, 'comment' => 'sss']);
FakeDB::addRow('users', ['id' => 2, 'user_id' => 1, 'comment' => 'aaa']);
FakeDB::addRow('users', ['id' => 3, 'user_id' => 2, 'comment' => 'bbb']);
FakeDB::addRow('users', ['id' => 4, 'user_id' => 2, 'comment' => 'ccc']);
FakeDB::addRow('users', ['id' => 5, 'user_id' => 3, 'comment' => 'ddd']);

$user = HasManyUser::with('comments')->where('id', 1)->first();

Expand Down
Loading

0 comments on commit daad50e

Please sign in to comment.