mmk2410
/
my-todo-list
Archived
1
0
Fork 0

Compare commits

...

15 Commits

Author SHA1 Message Date
Marcel Kapfer 6885d2160a
Add woodpecker.yml
continuous-integration/woodpecker the build was successful Details
2021-10-22 19:22:19 +02:00
Marcel Kapfer 90328431b9
Added LICENSE and README 2021-09-20 19:07:08 +02:00
Marcel Kapfer c66c7e6dcc
Added support for GitLab CI 2021-09-20 18:47:33 +02:00
Marcel Kapfer e8e4112548
Added todo list 2021-09-20 17:47:41 +02:00
Marcel Kapfer 454d7a1d6d
Added todo list 2021-09-20 17:45:33 +02:00
Marcel Kapfer 1f16b4bfe0
Create todos with an ID 2021-09-20 13:00:01 +02:00
Marcel Kapfer 9e75b6f9bb
Added ID manager 2021-09-20 12:55:34 +02:00
Marcel Kapfer 03713164fa
Added ID manager 2021-09-20 12:54:57 +02:00
Marcel Kapfer 5885fbf492
Added ID manager 2021-09-20 12:53:56 +02:00
Marcel Kapfer c881154db7
Todos can now be important 2021-09-20 11:27:46 +02:00
Marcel Kapfer f723e0e8c4
Refactored Todo::validateState 2021-09-18 22:49:39 +02:00
Marcel Kapfer df0dd98d7c
PhpStrom stuff 2021-09-18 22:46:27 +02:00
Marcel Kapfer 0bdffbeb27
Implemented Todo class 2021-09-18 22:45:49 +02:00
Marcel Kapfer 2358ad70cc
Defined common todo states 2021-09-18 22:45:16 +02:00
Marcel Kapfer 77cd8f62c9
Removed PHPunit learning files 2021-09-18 22:44:43 +02:00
23 changed files with 718 additions and 60 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
vendor/
/.phpunit.result.cache
.phpunit.cache/

14
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,14 @@
# From https://docs.gitlab.com/ee/ci/examples/php.html
image: php:7.4-cli
before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
test:
script:
- php composer.phar run test:gitlab
artifacts:
when: always
reports:
junit: report.xml

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/todo-list.iml" filepath="$PROJECT_DIR$/.idea/todo-list.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpTestFrameworkVersionCache">
<tools_cache>
<tool tool_name="PHPUnit">
<cache>
<versions>
<info id="interpreter-cc25df52-c356-4ba0-a7e8-8584ab3c0c28" version="9.5.9" />
</versions>
</cache>
</tool>
</tools_cache>
</component>
</project>

116
.idea/php.xml Normal file
View File

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/vendor/phpunit/php-timer" />
<path value="$PROJECT_DIR$/vendor/phpunit/phpunit" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-text-template" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-invoker" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-file-iterator" />
<path value="$PROJECT_DIR$/vendor/myclabs/deep-copy" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-code-coverage" />
<path value="$PROJECT_DIR$/vendor/webmozart/assert" />
<path value="$PROJECT_DIR$/vendor/phpspec/prophecy" />
<path value="$PROJECT_DIR$/vendor/nikic/php-parser" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-ctype" />
<path value="$PROJECT_DIR$/vendor/sebastian/type" />
<path value="$PROJECT_DIR$/vendor/sebastian/diff" />
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-docblock" />
<path value="$PROJECT_DIR$/vendor/sebastian/lines-of-code" />
<path value="$PROJECT_DIR$/vendor/phpdocumentor/type-resolver" />
<path value="$PROJECT_DIR$/vendor/sebastian/object-enumerator" />
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-common" />
<path value="$PROJECT_DIR$/vendor/sebastian/comparator" />
<path value="$PROJECT_DIR$/vendor/sebastian/code-unit" />
<path value="$PROJECT_DIR$/vendor/sebastian/cli-parser" />
<path value="$PROJECT_DIR$/vendor/theseer/tokenizer" />
<path value="$PROJECT_DIR$/vendor/sebastian/exporter" />
<path value="$PROJECT_DIR$/vendor/sebastian/version" />
<path value="$PROJECT_DIR$/vendor/composer" />
<path value="$PROJECT_DIR$/vendor/sebastian/complexity" />
<path value="$PROJECT_DIR$/vendor/sebastian/recursion-context" />
<path value="$PROJECT_DIR$/vendor/sebastian/resource-operations" />
<path value="$PROJECT_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
<path value="$PROJECT_DIR$/vendor/sebastian/global-state" />
<path value="$PROJECT_DIR$/vendor/sebastian/environment" />
<path value="$PROJECT_DIR$/vendor/sebastian/object-reflector" />
<path value="$PROJECT_DIR$/vendor/phar-io/version" />
<path value="$PROJECT_DIR$/vendor/phar-io/manifest" />
<path value="$PROJECT_DIR$/vendor/doctrine/instantiator" />
</include_path>
</component>
<component name="PhpInterpreters">
<interpreters>
<interpreter id="cc25df52-c356-4ba0-a7e8-8584ab3c0c28" name="Debian" home="\\wsl$\Debian/usr/bin/php" debugger_id="php.debugger.XDebug">
<remote_data INTERPRETER_PATH="/usr/bin/php" HELPERS_PATH="" INITIALIZED="false" VALID="true" RUN_AS_ROOT_VIA_SUDO="false" DISTRIBUTION_ID="Debian" />
</interpreter>
</interpreters>
</component>
<component name="PhpInterpretersPhpInfoCache">
<phpInfoCache>
<interpreter name="Debian">
<phpinfo binary_type="PHP" php_cli="/usr/bin/php7.4" path_separator=":" remote_host="172.24.192.1" version="7.4.21">
<additional_php_ini>/etc/php/7.4/cli/conf.d/10-opcache.ini, /etc/php/7.4/cli/conf.d/10-pdo.ini, /etc/php/7.4/cli/conf.d/15-xml.ini, /etc/php/7.4/cli/conf.d/20-calendar.ini, /etc/php/7.4/cli/conf.d/20-ctype.ini, /etc/php/7.4/cli/conf.d/20-curl.ini, /etc/php/7.4/cli/conf.d/20-dom.ini, /etc/php/7.4/cli/conf.d/20-exif.ini, /etc/php/7.4/cli/conf.d/20-ffi.ini, /etc/php/7.4/cli/conf.d/20-fileinfo.ini, /etc/php/7.4/cli/conf.d/20-ftp.ini, /etc/php/7.4/cli/conf.d/20-gettext.ini, /etc/php/7.4/cli/conf.d/20-iconv.ini, /etc/php/7.4/cli/conf.d/20-json.ini, /etc/php/7.4/cli/conf.d/20-mbstring.ini, /etc/php/7.4/cli/conf.d/20-phar.ini, /etc/php/7.4/cli/conf.d/20-posix.ini, /etc/php/7.4/cli/conf.d/20-readline.ini, /etc/php/7.4/cli/conf.d/20-shmop.ini, /etc/php/7.4/cli/conf.d/20-simplexml.ini, /etc/php/7.4/cli/conf.d/20-sockets.ini, /etc/php/7.4/cli/conf.d/20-sysvmsg.ini, /etc/php/7.4/cli/conf.d/20-sysvsem.ini, /etc/php/7.4/cli/conf.d/20-sysvshm.ini, /etc/php/7.4/cli/conf.d/20-tokenizer.ini, /etc/php/7.4/cli/conf.d/20-xmlreader.ini, /etc/php/7.4/cli/conf.d/20-xmlwriter.ini, /etc/php/7.4/cli/conf.d/20-xsl.ini, /etc/php/7.4/cli/conf.d/20-zip.ini</additional_php_ini>
<configuration_file>/etc/php/7.4/cli/php.ini</configuration_file>
<configuration_options>
<configuration_option name="include_path" value=".:/usr/share/php" />
</configuration_options>
<debuggers />
<loaded_extensions>
<extension name="Core" />
<extension name="FFI" />
<extension name="PDO" />
<extension name="Phar" />
<extension name="Reflection" />
<extension name="SPL" />
<extension name="SimpleXML" />
<extension name="Zend OPcache" />
<extension name="calendar" />
<extension name="ctype" />
<extension name="curl" />
<extension name="date" />
<extension name="dom" />
<extension name="exif" />
<extension name="fileinfo" />
<extension name="filter" />
<extension name="ftp" />
<extension name="gettext" />
<extension name="hash" />
<extension name="iconv" />
<extension name="json" />
<extension name="libxml" />
<extension name="mbstring" />
<extension name="openssl" />
<extension name="pcntl" />
<extension name="pcre" />
<extension name="posix" />
<extension name="readline" />
<extension name="session" />
<extension name="shmop" />
<extension name="sockets" />
<extension name="sodium" />
<extension name="standard" />
<extension name="sysvmsg" />
<extension name="sysvsem" />
<extension name="sysvshm" />
<extension name="tokenizer" />
<extension name="xml" />
<extension name="xmlreader" />
<extension name="xmlwriter" />
<extension name="xsl" />
<extension name="zip" />
<extension name="zlib" />
</loaded_extensions>
</phpinfo>
</interpreter>
</phpInfoCache>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.0">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>
<component name="PhpUnit">
<phpunit_settings>
<PhpUnitSettings custom_loader_path="$PROJECT_DIR$/vendor/autoload.php" />
</phpunit_settings>
</component>
</project>

47
.idea/todo-list.iml Normal file
View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="MMK2410\MyTodoList\" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-invoker" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
<excludeFolder url="file://$MODULE_DIR$/vendor/webmozart/assert" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
<excludeFolder url="file://$MODULE_DIR$/vendor/nikic/php-parser" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-ctype" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/type" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/diff" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/lines-of-code" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/type-resolver" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/object-enumerator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-common" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/code-unit" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/cli-parser" />
<excludeFolder url="file://$MODULE_DIR$/vendor/theseer/tokenizer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/version" />
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/complexity" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/environment" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/object-reflector" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phar-io/version" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phar-io/manifest" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

7
.woodpecker.yml Normal file
View File

@ -0,0 +1,7 @@
pipeline:
test:
image: php:7.4-cli
pull: true
commands:
- bash ci/docker_install.sh > /dev/null
- php composer.phar run test

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2021 Marcel Kapfer <opensource@mmk2410.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

10
README.md Normal file
View File

@ -0,0 +1,10 @@
# My ToDo List
My todo list is a PHP project that I develop for training PHP.
## Development
Currently, I use PHP 7.4 (maybe I will switch to PHP 8 soon) and follow a TDD approach.
The program will store its data in a MySQL database that will be connected and accessed using the Doctrine DBAL.
Goal of the PHP application is a RESTful API build with some web framework like Symphony or Laravel.
This will be used by a frontend that won't be powered by PHP but (as of now) TypeScript and some fancy JavaScript framework like Angular, React or Vue.

23
ci/docker_install.sh Normal file
View File

@ -0,0 +1,23 @@
#!/bin/bash
# From https://docs.gitlab.com/ee/ci/examples/php.html
# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && exit 0
set -xe
# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
apt-get install git wget unzip -yqq
# Install & enable Xdebug for code coverage reports
pecl install xdebug
docker-php-ext-enable xdebug
# Install composer dependencies
wget https://composer.github.io/installer.sig -O - -q | tr -d '\n' > installer.sig
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php'); unlink('installer.sig');"
php composer.phar install

View File

@ -4,5 +4,18 @@
},
"require-dev": {
"phpunit/phpunit": "^9"
},
"scripts": {
"test": [
"phpunit --configuration phpunit.xml"
],
"test:coverage": [
"@putenv XDEBUG_MODE=coverage",
"phpunit --configuration phpunit.xml --coverage-text"
],
"test:gitlab": [
"@putenv XDEBUG_MODE=coverage",
"phpunit --configuration phpunit.xml --log-junit report.xml --coverage-text --colors=never"
]
}
}

22
phpunit.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>

View File

@ -1,33 +0,0 @@
<?php declare(strict_types=1);
namespace MMK2410\MyTodoList;
final class Email
{
private $email;
private function __construct(string $email)
{
$this->ensureIsValidEmail($email);
$this->email = $email;
}
public static function fromString(string $email): self
{
return new self($email);
}
public function __toString(): string
{
return $this->email;
}
private function ensureIsValidEmail(string $email): void
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException(
sprintf("%s is not a valid email address", $email)
);
}
}
}

27
src/IdManager.php Normal file
View File

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace MMK2410\MyTodoList;
class IdManager
{
private static array $ids = array();
public static function generateID(string $classname): int
{
if (!array_key_exists($classname, self::$ids)) {
self::$ids[$classname] = 0;
return 0;
} else {
return ++self::$ids[$classname];
}
}
public static function getCurrentId(string $classname)
{
if (!array_key_exists($classname, self::$ids)) {
return 0;
} else {
return self::$ids[$classname];
}
}
}

77
src/Todo.php Normal file
View File

@ -0,0 +1,77 @@
<?php declare(strict_types=1);
namespace MMK2410\MyTodoList;
use InvalidArgumentException;
use ReflectionClass;
class Todo
{
const ExceptionMsgInvalidTitle = "Empty task title not allowed";
const ExceptionMsgInvalidState = "Invalid state tried to set.";
private string $title;
private string $state;
private bool $important;
private int $id;
public function __construct(string $title, bool $important = false)
{
$this->validateTitle($title);
$this->id = IdManager::generateID(self::class);
$this->title = $title;
$this->state = TodoStates::Todo;
$this->important = $important;
}
public function getID(): int
{
return $this->id;
}
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;
}
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);
}
}
private function validateState(string $state): void
{
$reflect = new ReflectionClass(TodoStates::class);
$constantFound = $reflect->getConstant($state);
if ($constantFound === FALSE) {
throw new InvalidArgumentException(self::ExceptionMsgInvalidState);
}
}
}

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]);
}
}
}
}

9
src/TodoStates.php Normal file
View File

@ -0,0 +1,9 @@
<?php
namespace MMK2410\MyTodoList;
abstract class TodoStates
{
const Todo = "Todo";
const Done = "Done";
}

View File

@ -1,27 +0,0 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use MMK2410\MyTodoList\Email;
final class EmailTest extends TestCase
{
private $email = 'user@example.com';
public function testCanBeCreatedFromValidEmailAddress(): void {
$this->assertInstanceOf(
Email::class,
Email::fromString($this->email)
);
}
public function testCannotBeCreatedFromInvalidEmailAddress(): void {
$this->expectException(InvalidArgumentException::class);
Email::fromString('invalid');
}
public function testCanBeUsedAsString(): void {
$this->assertEquals(
$this->email,
Email::fromString($this->email)
);
}
}

32
tests/IdManagerTest.php Normal file
View File

@ -0,0 +1,32 @@
<?php declare(strict_types=1);
use MMK2410\MyTodoList\IdManager;
use PHPUnit\Framework\TestCase;
class IdManagerTest extends TestCase
{
public function testGenerateTwoIds(): void
{
$id1 = IdManager::generateID(IdManagerTest::class . "::testGenerateTwoIds");
$id2 = IdManager::generateID(IdManagerTest::class . "::testGenerateTwoIds");
self::assertNotEquals(
$id1,
$id2,
"ID 1 '$id1' and ID 2 '$id2' are equal, but the shouldn't."
);
}
public function testGetCurrentId(): void
{
for ($i = 0; $i < 3; $i++) {
IdManager::generateID(IdManagerTest::class . "::testGetCurrentId()");
}
$currentId = IdManager::getCurrentId(IdManagerTest::class . "::testGetCurrentId()");
self::assertEquals(
2,
$currentId,
"Expected current ID to be '2', but got '$currentId'"
);
}
}

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"
);
}
}

95
tests/TodoTest.php Normal file
View File

@ -0,0 +1,95 @@
<?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 {
$todo1 = new Todo("Some title");
$todo2 = new Todo("Some title");
$this->assertNotEquals(
$todo1->getID(),
$todo2->getID(),
"Expected todos to have different UIDs but they have the same."
);
}
public function testTodoHasTitle(): 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(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"
);
}
}