Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Aug 31, 2022
1 parent 9eec546 commit 3cffda8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/FakeDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function addRow($row)
};
}

public static function getChangedModel(string $action, $index, $model)
{
return FakeDB::$changedModels[$model][$action][$index] ?? null;
}

public static function setChangedModel(string $action, $model)
{
FakeDB::$changedModels[get_class($model)][$action][] = $model;
}

public static function truncate()
{
self::$fakeRows = [];
Expand Down
7 changes: 3 additions & 4 deletions src/FakeEloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function __construct($query, Model $modelObj)
{
$this->query = $query;
$this->model = $modelObj;
$this->modelClass = get_class($modelObj);
}

public function addSelect($columns = ['*'])
Expand All @@ -29,7 +28,7 @@ public function delete()
}
finally {
if (is_int($count) && $count > 0) {
FakeDB::$changedModels[$this->modelClass]['deleted'][] = $this->model;
FakeDB::setChangedModel('deleted', $this->model);
}
}
}
Expand All @@ -41,14 +40,14 @@ public function forceDelete()
}
finally {
if ($count !== 0) {
FakeDB::$changedModels[$this->modelClass]['deleted'][] = $this->model;
FakeDB::setChangedModel('deleted', $this->model);
}
}
}

public function update(array $values)
{
$this->model->getAttributes() && FakeDB::$changedModels[$this->modelClass]['updated'][] = $this->model;
$this->model->getAttributes() && FakeDB::setChangedModel('updated', $this->model);

return parent::update($values);
}
Expand Down
18 changes: 8 additions & 10 deletions src/MockableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

trait MockableModel
{
public static $changedModels = [];

public static $fakeMode = false;

public static $forceMocks = [];
Expand Down Expand Up @@ -35,7 +33,7 @@ public static function fakeSoftDelete()
{
static::$fakeMode = true;
$callback = function ($model) {
FakeDB::$changedModels[static::class]['softDeleted'][] = $model;
FakeDB::setChangedModel('softDeleted', $model);
};
if (method_exists(static::class, 'softDeleted')) {
static::softDeleted($callback);
Expand All @@ -46,27 +44,27 @@ public static function fakeSoftDelete()

public static function getUpdatedModel($index = 0)
{
return FakeDB::$changedModels[static::class]['updated'][$index] ?? null;
return FakeDB::getChangedModel('updated', $index, static::class);
}

public static function getCreatedModel($index = 0)
{
return FakeDB::$changedModels[static::class]['created'][$index] ?? null;
return FakeDB::getChangedModel('created', $index, static::class);
}

public static function getSavedModel($index = 0)
{
return FakeDB::$changedModels[static::class]['saved'][$index] ?? null;
return FakeDB::getChangedModel('saved', $index, static::class);
}

public static function getSoftDeletedModel($index = 0)
{
return FakeDB::$changedModels[static::class]['softDeleted'][$index] ?? null;
return FakeDB::getChangedModel('softDeleted', $index, static::class);
}

public static function getDeletedModel($index = 0)
{
return FakeDB::$changedModels[static::class]['deleted'][$index] ?? null;
return FakeDB::getChangedModel('deleted', $index, static::class);
}

public function newEloquentBuilder($query)
Expand Down Expand Up @@ -126,10 +124,10 @@ protected function finishSave(array $options)
{
if ($this->isFakeMode()) {
if ($this->wasRecentlyCreated) {
FakeDB::$changedModels[static::class]['created'][] = $this;
FakeDB::setChangedModel('created', $this);
FakeDB::addRow($this->getTable(), $this->getAttributes());
}
FakeDB::$changedModels[static::class]['saved'][] = $this;
FakeDB::setChangedModel('saved', $this);
}

return parent::finishSave($options);
Expand Down

0 comments on commit 3cffda8

Please sign in to comment.