Skip to content

Commit

Permalink
Enhance the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Sep 2, 2023
1 parent 693ee38 commit 826cc3a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/FakeDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function getLatestRow($table)
foreach (FakeDB::$fakeRows[$table] ?? [] as $row) {
}

return $row;
return $row[$table] ?? [];
}

public static function createTable($args)
Expand Down
2 changes: 1 addition & 1 deletion src/FakeEloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function delete()
try {
return $count = parent::delete();
} finally {
if (is_int($count) && $count > 0) {
if (isset($count) && is_int($count) && $count > 0) {
FakeDB::setChangedModel('deleted', $this->model);
}
}
Expand Down
22 changes: 9 additions & 13 deletions tests/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Carbon;
use Imanghafoori\EloquentMockery\FakeDB;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -31,6 +32,7 @@ public function setUp(): void
*/
public function create()
{
Carbon::setTestNow(Carbon::create(2020));
CreatyModel::reguard();
CreatyModel::setEventDispatcher(new Dispatcher());
FakeDB::addRow('users', ['id' => 1]);
Expand All @@ -54,30 +56,24 @@ public function create()
'family' => 'gha',
]);

//$foo = CreatyModel::getCreatedModel();
//$this->assertSame($foo, $bar);
$this->assertTrue($_SERVER['saved']);
$this->assertTrue($_SERVER['saving']);
$this->assertTrue($_SERVER['created']);
$this->assertTrue($_SERVER['creating']);

//$this->assertEquals(3, $foo->id);
//$this->assertEquals('hello', $foo->name);
//$this->assertNull($foo->family);
//$this->assertNotNull($foo->created_at);
//$this->assertNotNull($foo->updated_at);
//$this->assertTrue($foo->exists);
//$this->assertTrue($foo->wasRecentlyCreated);

//$this->assertSame(CreatyModel::getSavedModel(), CreatyModel::getCreatedModel());
//$this->assertNull(CreatyModel::getCreatedModel(1));
//$this->assertNull(CreatyModel::getSavedModel(1));
$model = CreatyModel::query()->find(3);
$this->assertEquals('hello', $model->name);
$this->assertEquals(3, $model->id);
$this->assertNotNull($model->created_at);
$this->assertNotNull($model->updated_at);

$this->assertEquals([
'name' => 'hello',
'updated_at' => '2020-01-01 00:00:00',
'created_at' => '2020-01-01 00:00:00',
'id' => 3,
], FakeDB::getLatestRow('users'));

unset($_SERVER['saved']);
unset($_SERVER['saving']);
unset($_SERVER['created']);
Expand Down
34 changes: 27 additions & 7 deletions tests/Schema/CreateDropTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Schema\Blueprint;
use Imanghafoori\EloquentMockery\FakeConnection;
use Imanghafoori\EloquentMockery\FakeDB;
use Imanghafoori\EloquentMockery\FakeSchemaBuilder;
use PHPUnit\Framework\TestCase;

class CreateDropTableTest extends TestCase
Expand All @@ -23,36 +22,55 @@ public function setUp(): void
/**
* @test
*/
public function schema()
public function create_and_drop_tables()
{
$schema = FakeConnection::resolve()->getSchemaBuilder();

$this->assertEquals([], $schema->getAllTables());

$schema->create('users_o_o', function (Blueprint $blueprint) {
$blueprint->id();
$blueprint->string('name');
$blueprint->unsignedInteger('id', true);
$blueprint->string('name')->comment('comment');
});

$this->assertEquals(['users_o_o'], $schema->getAllTables());

// test the "hasColumn" method:
$this->assertTrue($schema->hasColumn('users_o_o', 'id'));
$this->assertTrue($schema->hasColumn('users_o_o', 'name'));
$this->assertFalse($schema->hasColumn('users_o_o', 'absent'));

// test the "getColumnListing" method:
$this->assertEquals(['id', 'name'], $schema->getColumnListing('users_o_o'));

if (method_exists($schema, 'dropColumns')) {
$this->testDropColumns($schema);
}

$this->renameTest($schema);

$this->dropAllTablesTest($schema);
}

private function testDropColumns($schema)
{
$schema->dropColumns('users_o_o', ['name']);

$this->assertEquals(['id'], $schema->getColumnListing('users_o_o'));

$this->assertTrue($schema->hasTable('users_o_o'));
$this->assertFalse($schema->hasTable('users_q_q'));
}

private function renameTest($schema)
{
$schema->rename('users_o_o', 'users_u_u');

$this->assertTrue($schema->hasTable('users_u_u'));
$this->assertFalse($schema->hasTable('users_o_o'));

$this->dropAllTablesTest($schema);
}

private function dropAllTablesTest(FakeSchemaBuilder $schema): void
private function dropAllTablesTest($schema)
{
$schema->drop('users_u_u');

Expand All @@ -78,8 +96,10 @@ private function dropAllTablesTest(FakeSchemaBuilder $schema): void

$this->assertEquals(['id'], $schema->getColumnListing('users_u_u'));

// Act:
$schema->dropAllTables();

// Assert:
$this->assertFalse($schema->hasTable('users_o_o'));
$this->assertFalse($schema->hasTable('users_u_u'));
}
Expand Down
22 changes: 11 additions & 11 deletions tests/Unit/FakeDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ public function fakeDbAliasColumns()
{
// In this case, the users row does not change
// but the comment key renames from "common" to "cc"
$table = "users";
$table = 'users';
$aliases = [
"common" => "users.common",
"cc" => "comments.common",
'common' => 'users.common',
'cc' => 'comments.common',
];

$userRow = [
"id" => 3,
"common" => "someValue",
'id' => 3,
'common' => 'someValue',
];
$item = [
"users" => $userRow,
"comments" => ["common" => "aValue"],
'users' => $userRow,
'comments' => ['common' => 'aValue'],
];

$resultingItem = [
"users" => $userRow,
"comments" => ["cc" => "aValue"],
'users' => $userRow,
'comments' => ['cc' => 'aValue'],
];

$this->assertEquals($resultingItem, FakeDb::aliasColumns($aliases, $item, $table));
Expand Down Expand Up @@ -86,11 +86,11 @@ public function fakeDbGetLatestRowWorksProperly()

// Assert
$this->assertEquals([
'::table1::' => ['id' => 1],
'id' => 1,
], $result1);

$this->assertEquals([
'::table2::' => ['id' => 5],
'id' => 5,
], $result2);

$this->assertEquals([], $result3);
Expand Down

0 comments on commit 826cc3a

Please sign in to comment.