1
0
Fork 0

Todos can now be important

This commit is contained in:
Marcel Kapfer 2021-09-20 11:27:46 +02:00
parent f723e0e8c4
commit c881154db7
Signed by: mmk2410
GPG key ID: CADE6F0C09F21B09
2 changed files with 37 additions and 6 deletions

View file

@ -2,6 +2,9 @@
namespace MMK2410\MyTodoList; namespace MMK2410\MyTodoList;
use InvalidArgumentException;
use ReflectionClass;
class Todo class Todo
{ {
const ExceptionMsgInvalidTitle = "Empty task title not allowed"; const ExceptionMsgInvalidTitle = "Empty task title not allowed";
@ -9,12 +12,14 @@ class Todo
private string $title; private string $title;
private string $state; private string $state;
private bool $important;
public function __construct(string $title) public function __construct(string $title, bool $important = false)
{ {
$this->validateTitle($title); $this->validateTitle($title);
$this->title = $title; $this->title = $title;
$this->state = TodoStates::Todo; $this->state = TodoStates::Todo;
$this->important = $important;
} }
public function getTitle(): string public function getTitle(): string
@ -39,19 +44,27 @@ class Todo
$this->state = $state; $this->state = $state;
} }
public function getImportant(): bool {
return $this->important;
}
public function setImportant(): void {
$this->important = true;
}
private function validateTitle(string $title): void private function validateTitle(string $title): void
{ {
if (empty($title)) { if (empty($title)) {
throw new \InvalidArgumentException(self::ExceptionMsgInvalidTitle); throw new InvalidArgumentException(self::ExceptionMsgInvalidTitle);
} }
} }
private function validateState(string $state): void private function validateState(string $state): void
{ {
$reflect = new \ReflectionClass(TodoStates::class); $reflect = new ReflectionClass(TodoStates::class);
$constantFound = $reflect->getConstant($state); $constantFound = $reflect->getConstant($state);
if ($constantFound === FALSE) { if ($constantFound === FALSE) {
throw new \InvalidArgumentException(self::ExceptionMsgInvalidState); throw new InvalidArgumentException(self::ExceptionMsgInvalidState);
} }
} }
} }

View file

@ -19,7 +19,7 @@ class TodoTest extends TestCase
public function testDontCreateEmptyTodo(): void public function testDontCreateEmptyTodo(): void
{ {
$this->expectException(\InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(Todo::ExceptionMsgInvalidTitle); $this->expectExceptionMessage(Todo::ExceptionMsgInvalidTitle);
new Todo(""); new Todo("");
} }
@ -60,8 +60,26 @@ class TodoTest extends TestCase
public function testSetBogusTodoState(): void public function testSetBogusTodoState(): void
{ {
$todo = new Todo("stub"); $todo = new Todo("stub");
$this->expectException(\InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(Todo::ExceptionMsgInvalidState); $this->expectExceptionMessage(Todo::ExceptionMsgInvalidState);
$todo->setState("bogus"); $todo->setState("bogus");
} }
public function testCreateImportantTodo(): void
{
$todo = new Todo("Some title", true);
$this->assertTrue(
$todo->getImportant(),
"Todo created as important, but it is not"
);
}
public function testMakeTodoImportant(): void {
$todo = new Todo("Some title");
$todo->setImportant();
$this->assertTrue(
$todo->getImportant(),
"Todo changed to important, but it is not"
);
}
} }