Skip to content

Commit

Permalink
clear model registry when clearing test entries, add functions to tem…
Browse files Browse the repository at this point in the history
…porarily disable registry, only disable registry for some tests
  • Loading branch information
matthi4s committed Jun 30, 2023
1 parent 191697a commit ea88b15
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
push:
branches:
- master
- test-driver

jobs:
tests:
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/Test/TestDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Aternos\Model\Driver\Features\CRUDAbleInterface;
use Aternos\Model\Driver\Features\CRUDQueryableInterface;
use Aternos\Model\ModelInterface;
use Aternos\Model\ModelRegistry;
use Aternos\Model\Query\Query;
use Aternos\Model\Query\QueryResult;
use Exception;
Expand Down Expand Up @@ -73,6 +74,7 @@ public function addEntry(string $tableName, array $entry): static
*/
public function clearTables(): static
{
ModelRegistry::getInstance()->clearAll();
$this->tables = [];
return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/Test/TestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aternos\Model\Driver\Test;

use Aternos\Model\ModelInterface;
use Aternos\Model\ModelRegistry;
use Aternos\Model\Query\DeleteQuery;
use Aternos\Model\Query\GroupField;
use Aternos\Model\Query\OrderField;
Expand Down Expand Up @@ -194,6 +195,7 @@ protected function orderEntries(array $entries, array $order): array
public function clear(): static
{
$this->entries = [];
ModelRegistry::getInstance()->clearModel($this->name);
return $this;
}

Expand Down
16 changes: 16 additions & 0 deletions src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ public static function getModelFromData(array $rawData): ?static
return $model->applyData($rawData);
}

/**
* Disable the registry temporarily
*/
public static function disableRegistry(): void
{
static::$registry = false;
}

/**
* Enable the registry again/temporarily
*/
public static function enableRegistry(): void
{
static::$registry = true;
}

/**
*
*
Expand Down
20 changes: 20 additions & 0 deletions src/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ public function delete(ModelInterface $model): void
unset($this->registry[$model::getName()][$model->getId()]);
}

/**
* @param string $modelName
* @return void
*/
public function clearModel(string $modelName): void
{
if (!isset($this->registry[$modelName])) {
return;
}
unset($this->registry[$modelName]);
}

/**
* @return void
*/
public function clearAll(): void
{
$this->registry = [];
}

/**
* @var ModelRegistry|null
*/
Expand Down
1 change: 0 additions & 1 deletion test/src/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class TestModel extends \Aternos\Model\GenericModel
{
protected static bool $registry = false;
public mixed $id;
public ?string $text = null;
public ?int $number = null;
Expand Down
3 changes: 2 additions & 1 deletion test/tests/TestDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Aternos\Model\Test\Src\TestModel;
use Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit\Util\Test;

class TestDriverTest extends TestCase
{
Expand Down Expand Up @@ -503,6 +502,7 @@ public function testSelectGroupAverage(): void
*/
public function testUpdate(): void
{
TestModel::disableRegistry();
$model = TestModel::get("1B");
$this->assertEquals("B", $model->text);

Expand All @@ -528,6 +528,7 @@ public function testUpdate(): void
*/
public function testDeleteQuery(): void
{
TestModel::disableRegistry();
$this->assertNotNull(TestModel::get("1B"));

$model = new TestModel();
Expand Down

0 comments on commit ea88b15

Please sign in to comment.