1
0
Fork 0

Added ID manager

This commit is contained in:
Marcel Kapfer 2021-09-20 12:54:57 +02:00
parent 5885fbf492
commit 03713164fa
Signed by: mmk2410
GPG key ID: CADE6F0C09F21B09
2 changed files with 42 additions and 1 deletions

View file

@ -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];
}
}
}

View file

@ -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'"
);
}
}