Added ID manager
This commit is contained in:
parent
5885fbf492
commit
03713164fa
2 changed files with 42 additions and 1 deletions
|
@ -4,5 +4,24 @@ namespace MMK2410\MyTodoList;
|
|||
|
||||
class IdManager
|
||||
{
|
||||
private static array $ids = array();
|
||||
|
||||
public static function generateID(string $classname): int
|
||||
{
|
||||
if (!array_key_exists($classname, self::$ids)) {
|
||||
self::$ids[$classname] = 0;
|
||||
return 0;
|
||||
} else {
|
||||
return ++self::$ids[$classname];
|
||||
}
|
||||
}
|
||||
|
||||
public static function getCurrentId(string $classname)
|
||||
{
|
||||
if (!array_key_exists($classname, self::$ids)) {
|
||||
return 0;
|
||||
} else {
|
||||
return self::$ids[$classname];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
<?php
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
|
||||
use MMK2410\MyTodoList\IdManager;
|
||||
|
@ -6,5 +6,27 @@ use PHPUnit\Framework\TestCase;
|
|||
|
||||
class IdManagerTest extends TestCase
|
||||
{
|
||||
|
||||
public function testGenerateTwoIds(): void
|
||||
{
|
||||
$id1 = IdManager::generateID(IdManagerTest::class . "::testGenerateTwoIds");
|
||||
$id2 = IdManager::generateID(IdManagerTest::class . "::testGenerateTwoIds");
|
||||
self::assertNotEquals(
|
||||
$id1,
|
||||
$id2,
|
||||
"ID 1 '$id1' and ID 2 '$id2' are equal, but the shouldn't."
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetCurrentId(): void
|
||||
{
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
IdManager::generateID(IdManagerTest::class . "::testGetCurrentId()");
|
||||
}
|
||||
$currentId = IdManager::getCurrentId(IdManagerTest::class . "::testGetCurrentId()");
|
||||
self::assertEquals(
|
||||
2,
|
||||
$currentId,
|
||||
"Expected current ID to be '2', but got '$currentId'"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue