diff --git a/src/Todo.php b/src/Todo.php index 7516b76..b4d2f05 100644 --- a/src/Todo.php +++ b/src/Todo.php @@ -2,6 +2,9 @@ namespace MMK2410\MyTodoList; +use InvalidArgumentException; +use ReflectionClass; + class Todo { const ExceptionMsgInvalidTitle = "Empty task title not allowed"; @@ -9,12 +12,14 @@ class Todo private string $title; private string $state; + private bool $important; - public function __construct(string $title) + public function __construct(string $title, bool $important = false) { $this->validateTitle($title); $this->title = $title; $this->state = TodoStates::Todo; + $this->important = $important; } public function getTitle(): string @@ -39,19 +44,27 @@ class Todo $this->state = $state; } + public function getImportant(): bool { + return $this->important; + } + + public function setImportant(): void { + $this->important = true; + } + private function validateTitle(string $title): void { if (empty($title)) { - throw new \InvalidArgumentException(self::ExceptionMsgInvalidTitle); + throw new InvalidArgumentException(self::ExceptionMsgInvalidTitle); } } private function validateState(string $state): void { - $reflect = new \ReflectionClass(TodoStates::class); + $reflect = new ReflectionClass(TodoStates::class); $constantFound = $reflect->getConstant($state); if ($constantFound === FALSE) { - throw new \InvalidArgumentException(self::ExceptionMsgInvalidState); + throw new InvalidArgumentException(self::ExceptionMsgInvalidState); } } } \ No newline at end of file diff --git a/tests/TodoTest.php b/tests/TodoTest.php index 1b10605..2bf96b1 100644 --- a/tests/TodoTest.php +++ b/tests/TodoTest.php @@ -19,7 +19,7 @@ class TodoTest extends TestCase public function testDontCreateEmptyTodo(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage(Todo::ExceptionMsgInvalidTitle); new Todo(""); } @@ -60,8 +60,26 @@ class TodoTest extends TestCase public function testSetBogusTodoState(): void { $todo = new Todo("stub"); - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage(Todo::ExceptionMsgInvalidState); $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" + ); + } }