From d3df8bdc7ea6b17a88738e4af3b9fba37410ada3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Tue, 6 Apr 2021 13:49:42 +0200 Subject: [PATCH] =?UTF-8?q?Tests=20f=C3=BCr=20die=20API=20sind=20vorhanden?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/composer.json | 7 ++- api/tests/_support/Helper/Api.php | 34 +++++++++++++ api/tests/api.suite.dist.yml | 8 +++ .../Views/BrowseWorkingHoursMonthlyCest.php | 21 ++++++++ .../Views/BrowseWorkingHoursWeeklyCest.php | 21 ++++++++ .../Views/BrowseWorkingHoursYearlyCest.php | 21 ++++++++ .../WorkingHours/BrowseWorkingHoursCest.php | 21 ++++++++ .../WorkingHours/CreateWorkingHoursCest.php | 51 +++++++++++++++++++ .../api/WorkingHours/ReadWorkingHoursCest.php | 26 ++++++++++ .../WorkingHours/UpdateWorkingHoursCest.php | 49 ++++++++++++++++++ 10 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 api/tests/_support/Helper/Api.php create mode 100644 api/tests/api.suite.dist.yml create mode 100644 api/tests/api/Views/BrowseWorkingHoursMonthlyCest.php create mode 100644 api/tests/api/Views/BrowseWorkingHoursWeeklyCest.php create mode 100644 api/tests/api/Views/BrowseWorkingHoursYearlyCest.php create mode 100644 api/tests/api/WorkingHours/BrowseWorkingHoursCest.php create mode 100644 api/tests/api/WorkingHours/CreateWorkingHoursCest.php create mode 100644 api/tests/api/WorkingHours/ReadWorkingHoursCest.php create mode 100644 api/tests/api/WorkingHours/UpdateWorkingHoursCest.php diff --git a/api/composer.json b/api/composer.json index a3900ac..c38794f 100644 --- a/api/composer.json +++ b/api/composer.json @@ -27,7 +27,8 @@ "codeception/codeception": "^4.1.18", "codeception/module-phpbrowser": "^1.0.0", "codeception/module-asserts": "^1.0.0", - "codeception/module-db": "^1.1.0" + "codeception/module-db": "^1.1.0", + "codeception/module-rest": "^1.2.8" }, "autoload": { "psr-4": { @@ -36,7 +37,9 @@ }, "autoload-dev": { "psr-4": { - "TorstenHettstedt\\TimekeepingApi\\Tests\\Unit\\": "tests/unit" + "TorstenHettstedt\\TimekeepingApi\\Tests\\Unit\\": "tests/unit", + "TorstenHettstedt\\TimekeepingApi\\Tests\\Api\\": "tests/api", + "Helper\\": "tests/_support/Helper" } } } diff --git a/api/tests/_support/Helper/Api.php b/api/tests/_support/Helper/Api.php new file mode 100644 index 0000000..3e4b310 --- /dev/null +++ b/api/tests/_support/Helper/Api.php @@ -0,0 +1,34 @@ + 'string:date', + 'workingHours' => self::FORMAT_TIME, + ]; + + const WORKING_HOURS_VIEW_JSON_FORMAT = [ + "period" => "string", + "periodDesignation" => "string", + "totalHours" => self::FORMAT_TIME, + "workingDays" => 'integer:>0', + "overtime" => self::FORMAT_TIME, + ]; + + const ERROR_JSON_FORMAT = [ + "timestamp" => "string:date-time", + "status" => 0, + "error" => "string", + "message" => "string", + "path" => "string:path", + ]; +} diff --git a/api/tests/api.suite.dist.yml b/api/tests/api.suite.dist.yml new file mode 100644 index 0000000..e210873 --- /dev/null +++ b/api/tests/api.suite.dist.yml @@ -0,0 +1,8 @@ +actor: ApiTester +modules: + enabled: + - \Helper\Api + - REST: + url: http://localhost:8090/ + depends: PhpBrowser + part: Json \ No newline at end of file diff --git a/api/tests/api/Views/BrowseWorkingHoursMonthlyCest.php b/api/tests/api/Views/BrowseWorkingHoursMonthlyCest.php new file mode 100644 index 0000000..5614233 --- /dev/null +++ b/api/tests/api/Views/BrowseWorkingHoursMonthlyCest.php @@ -0,0 +1,21 @@ +sendGet('/views/working-hours/monthly'); + $I->seeResponseCodeIs(HttpCode::OK); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_VIEW_JSON_FORMAT); + } +} diff --git a/api/tests/api/Views/BrowseWorkingHoursWeeklyCest.php b/api/tests/api/Views/BrowseWorkingHoursWeeklyCest.php new file mode 100644 index 0000000..6a751c5 --- /dev/null +++ b/api/tests/api/Views/BrowseWorkingHoursWeeklyCest.php @@ -0,0 +1,21 @@ +sendGet('/views/working-hours/weekly'); + $I->seeResponseCodeIs(HttpCode::OK); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_VIEW_JSON_FORMAT); + } +} diff --git a/api/tests/api/Views/BrowseWorkingHoursYearlyCest.php b/api/tests/api/Views/BrowseWorkingHoursYearlyCest.php new file mode 100644 index 0000000..4a4c0c1 --- /dev/null +++ b/api/tests/api/Views/BrowseWorkingHoursYearlyCest.php @@ -0,0 +1,21 @@ +sendGet('/views/working-hours/yearly'); + $I->seeResponseCodeIs(HttpCode::OK); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_VIEW_JSON_FORMAT); + } +} diff --git a/api/tests/api/WorkingHours/BrowseWorkingHoursCest.php b/api/tests/api/WorkingHours/BrowseWorkingHoursCest.php new file mode 100644 index 0000000..bcfff03 --- /dev/null +++ b/api/tests/api/WorkingHours/BrowseWorkingHoursCest.php @@ -0,0 +1,21 @@ +sendGet('/working-hours'); + $I->seeResponseCodeIs(HttpCode::OK); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_JSON_FORMAT); + } +} diff --git a/api/tests/api/WorkingHours/CreateWorkingHoursCest.php b/api/tests/api/WorkingHours/CreateWorkingHoursCest.php new file mode 100644 index 0000000..425a8e9 --- /dev/null +++ b/api/tests/api/WorkingHours/CreateWorkingHoursCest.php @@ -0,0 +1,51 @@ +haveHttpHeader('accept', 'application/json'); + $I->haveHttpHeader('content-type', 'application/json'); + $I->sendPost('/working-hours', [ + 'date' => '2020-04-01', + 'workingHours' => '08:00:00', + ]); + $I->seeResponseCodeIs(HttpCode::CREATED); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_JSON_FORMAT); + } + + // tests + public function createWorkingHoursWithNewInvalidRecord(ApiTester $I): void + { + $I->haveHttpHeader('accept', 'application/json'); + $I->haveHttpHeader('content-type', 'application/json'); + $I->sendPost('/working-hours', [ + 'date' => '2020-04-01', + 'workingHours' => 8.0, + ]); + $I->seeResponseCodeIs(HttpCode::BAD_REQUEST); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::ERROR_JSON_FORMAT); + } + + // tests + public function createWorkingHoursWithExistingValidRecord(ApiTester $I): void + { + $I->haveHttpHeader('accept', 'application/json'); + $I->haveHttpHeader('content-type', 'application/json'); + $I->sendPost('/working-hours', [ + 'date' => '2020-01-15', + 'workingHours' => '08:00:00', + ]); + $I->seeResponseCodeIs(HttpCode::CONFLICT); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::ERROR_JSON_FORMAT); + } +} diff --git a/api/tests/api/WorkingHours/ReadWorkingHoursCest.php b/api/tests/api/WorkingHours/ReadWorkingHoursCest.php new file mode 100644 index 0000000..af212be --- /dev/null +++ b/api/tests/api/WorkingHours/ReadWorkingHoursCest.php @@ -0,0 +1,26 @@ +sendGet('/working-hours/2020-01-15'); + $I->seeResponseCodeIs(HttpCode::OK); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_JSON_FORMAT); + } + + public function readNotExistingWorkingHours(ApiTester $I): void + { + $I->sendGet('/working-hours/2020-04-15'); + $I->seeResponseCodeIs(HttpCode::NOT_FOUND); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::ERROR_JSON_FORMAT); + } +} diff --git a/api/tests/api/WorkingHours/UpdateWorkingHoursCest.php b/api/tests/api/WorkingHours/UpdateWorkingHoursCest.php new file mode 100644 index 0000000..3eae5f3 --- /dev/null +++ b/api/tests/api/WorkingHours/UpdateWorkingHoursCest.php @@ -0,0 +1,49 @@ +haveHttpHeader('accept', 'application/json'); + $I->haveHttpHeader('content-type', 'application/json'); + $I->sendPut('/working-hours/2020-01-15', [ + 'date' => '2020-01-15', + 'workingHours' => '10:00:00', + ]); + $I->seeResponseCodeIs(HttpCode::OK); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::WORKING_HOURS_JSON_FORMAT); + } + + public function updateWorkingHoursWithNewValidRecord(ApiTester $I): void + { + $I->haveHttpHeader('accept', 'application/json'); + $I->haveHttpHeader('content-type', 'application/json'); + $I->sendPut('/working-hours/2020-01-15', [ + 'date' => '2020-04-15', + 'workingHours' => '10:00:00', + ]); + $I->seeResponseCodeIs(HttpCode::NOT_FOUND); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::ERROR_JSON_FORMAT); + } + + public function updateWorkingHoursWithNewInvalidRecord(ApiTester $I): void + { + $I->haveHttpHeader('accept', 'application/json'); + $I->haveHttpHeader('content-type', 'application/json'); + $I->sendPut('/working-hours/2020-01-15', [ + 'date' => '2020-01-15', + 'workingHours' => 8.0, + ]); + $I->seeResponseCodeIs(HttpCode::BAD_REQUEST); + $I->seeResponseIsJson(); + $I->seeResponseMatchesJsonType(Api::ERROR_JSON_FORMAT); + } +}