diff --git a/src/Todo.php b/src/Todo.php new file mode 100644 index 0000000..b5629ac --- /dev/null +++ b/src/Todo.php @@ -0,0 +1,56 @@ +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."); + } + } +} \ No newline at end of file diff --git a/tests/TodoTest.php b/tests/TodoTest.php new file mode 100644 index 0000000..e93ff69 --- /dev/null +++ b/tests/TodoTest.php @@ -0,0 +1,67 @@ +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"); + } +}