1
0
Fork 0

Added todo list

This commit is contained in:
Marcel Kapfer 2021-09-20 17:45:33 +02:00
parent 1f16b4bfe0
commit 454d7a1d6d
Signed by: mmk2410
GPG key ID: CADE6F0C09F21B09
2 changed files with 181 additions and 0 deletions

64
src/TodoList.php Normal file
View file

@ -0,0 +1,64 @@
<?php
namespace MMK2410\MyTodoList;
use InvalidArgumentException;
class TodoList
{
const ExceptionMsgTodoNotFound = "Could not find todo with given ID";
private string $name;
private array $todos;
private int $id;
public function __construct(string $name)
{
$this->id = IdManager::generateID(self::class);
$this->name = $name;
$this->todos = array();
}
public function getID(): int {
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name)
{
$this->name = $name;
}
public function addTodo(Todo $todo): void
{
$this->todos[] = $todo;
}
public function getTodos(): array
{
return $this->todos;
}
public function getTodoById(int $id): ?Todo
{
foreach ($this->todos as $todo) {
if ($todo->getID() == $id) {
return $todo;
}
}
throw new InvalidArgumentException(self::ExceptionMsgTodoNotFound);
}
public function deleteTodoById(int $id): void
{
foreach ($this->todos as $key => $todo) {
if ($todo->getID() == $id) {
unset($this->todos[$key]);
}
}
}
}

117
tests/TodoListTest.php Normal file
View file

@ -0,0 +1,117 @@
<?php declare(strict_types=1);
use MMK2410\MyTodoList\Todo;
use MMK2410\MyTodoList\TodoList;
use PHPUnit\Framework\TestCase;
class TodoListTest extends TestCase
{
public function testCreateTodoList(): void {
$todolist1 = new TodoList("My Todo List");
$todolist2 = new TodoList("My Todo List");
$this->assertNotEquals(
$todolist1->getID(),
$todolist2->getID(),
"Expected todo lists to have different UIDs but they have the same."
);
}
public function testTodoListHasName(): void
{
$name = "My Todo List";
$todolist = new TodoList($name);
$this->assertEquals(
$name,
$todolist->getName(),
"Created todo list with name '$name', but it has name '{$todolist->getName()}'",
);
}
public function testChangeTodoListName(): void
{
$todolist = new TodoList("Old name");
$new_todolist_name = "New name";
$todolist->setName($new_todolist_name);
$this->assertEquals(
$new_todolist_name,
$todolist->getName(),
"Renamed todo list to '$new_todolist_name', but its name is '{$todolist->getName()}'"
);
}
public function testGetEmptyTodos(): void
{
$todolist = new TodoList("Some list");
$todoAmount = sizeof($todolist->getTodos());
$this->assertEquals(
0,
$todoAmount,
"Got wrong amount of todos. Expected 0, got $todoAmount"
);
}
public function testGetTodos(): void
{
$todolist = new TodoList("Some list");
for ($i = 1; $i < 3; $i++) {
$todolist->addTodo(new Todo("Some todo"));
}
$i--; // Decrement $i for using it in the assertion
$todoAmount = sizeof($todolist->getTodos());
$this->assertEquals(
$i,
$todoAmount,
"Got wrong amount of todos. Expected $i, got $todoAmount"
);
}
public function testAddTodo(): void
{
$todolist = new TodoList("Some list");
$todo = new Todo("Some todo");
$todolist->addTodo($todo);
$id_todo = $todo->getID();
$id_todo_from_list = $todolist->getTodos()[0]->getID();
$this->assertEquals(
$id_todo,
$id_todo_from_list,
"Got wrong todo. Expected ID $id_todo, got $id_todo_from_list"
);
}
public function testGetTodoById(): void
{
$todolist = new TodoList("Some list");
$todo = new Todo("Some todo");
$todolist->addTodo($todo);
$id_todo = $todo->getID();
$id_todo_from_list = $todolist->getTodoById($id_todo)->getID();
$this->assertEquals(
$id_todo,
$id_todo_from_list,
"Got wrong todo. Expected ID $id_todo, got $id_todo_from_list"
);
}
public function testGetNonExistentTodoById(): void
{
$todolist = new TodoList("Some list");
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(TodoList::ExceptionMsgTodoNotFound);
$todolist->getTodoById(-1);
}
public function testDeleteTodoById(): void
{
$todolist = new TodoList("Some list");
$todo = new Todo("Delete me");
$todolist->addTodo($todo);
$todolist->deleteTodoById($todo->getID());
$amountTodos = sizeof($todolist->getTodos());
$this->assertEquals(
0,
$amountTodos,
"Expected not todos to be left, but there are/is $amountTodos left"
);
}
}