Skip to content

Commit ea88b15

Browse files
committed
clear model registry when clearing test entries, add functions to temporarily disable registry, only disable registry for some tests
1 parent 191697a commit ea88b15

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

.github/workflows/tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ on:
55
push:
66
branches:
77
- master
8-
- test-driver
98

109
jobs:
1110
tests:

src/Driver/Test/TestDriver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Aternos\Model\Driver\Features\CRUDAbleInterface;
77
use Aternos\Model\Driver\Features\CRUDQueryableInterface;
88
use Aternos\Model\ModelInterface;
9+
use Aternos\Model\ModelRegistry;
910
use Aternos\Model\Query\Query;
1011
use Aternos\Model\Query\QueryResult;
1112
use Exception;
@@ -73,6 +74,7 @@ public function addEntry(string $tableName, array $entry): static
7374
*/
7475
public function clearTables(): static
7576
{
77+
ModelRegistry::getInstance()->clearAll();
7678
$this->tables = [];
7779
return $this;
7880
}

src/Driver/Test/TestTable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Aternos\Model\Driver\Test;
44

55
use Aternos\Model\ModelInterface;
6+
use Aternos\Model\ModelRegistry;
67
use Aternos\Model\Query\DeleteQuery;
78
use Aternos\Model\Query\GroupField;
89
use Aternos\Model\Query\OrderField;
@@ -194,6 +195,7 @@ protected function orderEntries(array $entries, array $order): array
194195
public function clear(): static
195196
{
196197
$this->entries = [];
198+
ModelRegistry::getInstance()->clearModel($this->name);
197199
return $this;
198200
}
199201

src/GenericModel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,22 @@ public static function getModelFromData(array $rawData): ?static
304304
return $model->applyData($rawData);
305305
}
306306

307+
/**
308+
* Disable the registry temporarily
309+
*/
310+
public static function disableRegistry(): void
311+
{
312+
static::$registry = false;
313+
}
314+
315+
/**
316+
* Enable the registry again/temporarily
317+
*/
318+
public static function enableRegistry(): void
319+
{
320+
static::$registry = true;
321+
}
322+
307323
/**
308324
*
309325
*

src/ModelRegistry.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ public function delete(ModelInterface $model): void
6060
unset($this->registry[$model::getName()][$model->getId()]);
6161
}
6262

63+
/**
64+
* @param string $modelName
65+
* @return void
66+
*/
67+
public function clearModel(string $modelName): void
68+
{
69+
if (!isset($this->registry[$modelName])) {
70+
return;
71+
}
72+
unset($this->registry[$modelName]);
73+
}
74+
75+
/**
76+
* @return void
77+
*/
78+
public function clearAll(): void
79+
{
80+
$this->registry = [];
81+
}
82+
6383
/**
6484
* @var ModelRegistry|null
6585
*/

test/src/TestModel.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class TestModel extends \Aternos\Model\GenericModel
66
{
7-
protected static bool $registry = false;
87
public mixed $id;
98
public ?string $text = null;
109
public ?int $number = null;

test/tests/TestDriverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Aternos\Model\Test\Src\TestModel;
1515
use Exception;
1616
use PHPUnit\Framework\TestCase;
17-
use PHPUnit\Util\Test;
1817

1918
class TestDriverTest extends TestCase
2019
{
@@ -503,6 +502,7 @@ public function testSelectGroupAverage(): void
503502
*/
504503
public function testUpdate(): void
505504
{
505+
TestModel::disableRegistry();
506506
$model = TestModel::get("1B");
507507
$this->assertEquals("B", $model->text);
508508

@@ -528,6 +528,7 @@ public function testUpdate(): void
528528
*/
529529
public function testDeleteQuery(): void
530530
{
531+
TestModel::disableRegistry();
531532
$this->assertNotNull(TestModel::get("1B"));
532533

533534
$model = new TestModel();

0 commit comments

Comments
 (0)