1
0
Fork 0

Implemented Todo class

This commit is contained in:
Marcel Kapfer 2021-09-18 22:45:49 +02:00
parent 2358ad70cc
commit 0bdffbeb27
Signed by: mmk2410
GPG key ID: CADE6F0C09F21B09
2 changed files with 123 additions and 0 deletions

56
src/Todo.php Normal file
View file

@ -0,0 +1,56 @@
<?php declare(strict_types=1);
namespace MMK2410\MyTodoList;
class Todo
{
const ExceptionMsgInvalidTitle = "Empty task title not allowed";
private string $title;
private string $state;
public function __construct(string $title)
{
$this->validateTitle($title);
$this->title = $title;
$this->state = TodoStates::Todo;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): void
{
$this->validateTitle($title);
$this->title = $title;
}
public function getStatus(): string
{
return $this->state;
}
public function setState(string $state): void
{
$this->validateState($state);
$this->state = $state;
}
private function validateTitle(string $title): void
{
if (empty($title)) {
throw new \InvalidArgumentException(self::ExceptionMsgInvalidTitle);
}
}
private function validateState(string $state): void
{
$reflect = new \ReflectionClass(TodoStates::class);
$constantFound = $reflect->getConstant($state);
if ($constantFound === FALSE) {
throw new \InvalidArgumentException("Invalid state tried to set.");
}
}
}

67
tests/TodoTest.php Normal file
View file

@ -0,0 +1,67 @@
<?php declare(strict_types=1);
use MMK2410\MyTodoList\Todo;
use MMK2410\MyTodoList\TodoStates;
use PHPUnit\Framework\TestCase;
class TodoTest extends TestCase
{
public function testCreateTodo(): void
{
$title = "Some task";
$todo = new Todo($title);
$this->assertEquals(
$title,
$todo->getTitle(),
"Expected title '$title', but got '{$todo->getTitle()}'"
);
}
public function testDontCreateEmptyTodo(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(Todo::ExceptionMsgInvalidTitle);
new Todo("");
}
public function testUpdateTitle(): void
{
$anotherTitle = "Another title";
$todo = new Todo("Some title");
$todo->setTitle($anotherTitle);
$this->assertEquals(
$anotherTitle,
$todo->getTitle(),
"Expected new title $anotherTitle, but got {$todo->getTitle()}"
);
}
public function testNewTodoState(): void
{
$todo = new Todo("stub");
$this->assertEquals(
TodoStates::Todo,
$todo->getStatus(),
"Expected todo state 'Todo', but got {$todo->getStatus()}"
);
}
public function testChangeTodoState(): void
{
$todo = new Todo("stub");
$todo->setState(TodoStates::Done);
$this->assertEquals(
TodoStates::Done,
$todo->getStatus(),
"Expected todo state 'Done', but got {$todo->getStatus()}"
);
}
public function testSetBogusTodoState(): void
{
$todo = new Todo("stub");
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid state tried to set.");
$todo->setState("bogus");
}
}