From e4cd00caf1f0e93b5836c98be464fa9711b7b470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Mon, 29 Mar 2021 14:39:42 +0200 Subject: [PATCH] =?UTF-8?q?Einf=C3=BChrung=20des=20Modells.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 - api/composer.json | 3 +- api/phpstan.neon | 5 ++ api/src/Models/ModelInterface.php | 11 ++++ api/src/Models/WorkingHours.php | 65 ++++++++++++++++++++ api/tests/unit/Models/WorkingHoursTest.php | 70 ++++++++++++++++++++++ 6 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 api/phpstan.neon create mode 100644 api/src/Models/ModelInterface.php create mode 100644 api/src/Models/WorkingHours.php create mode 100644 api/tests/unit/Models/WorkingHoursTest.php diff --git a/.gitignore b/.gitignore index 02e1df6..2f3faef 100644 --- a/.gitignore +++ b/.gitignore @@ -104,7 +104,5 @@ Temporary Items /api/codeception.yml /api/vendor/ /api/tests/_* -/api/tests/acceptance -/api/tests/functional /api/tests/*.suite.yml /api/html/swagger/ diff --git a/api/composer.json b/api/composer.json index bb74da7..60d3bac 100644 --- a/api/composer.json +++ b/api/composer.json @@ -18,7 +18,8 @@ "require": { "slim/slim": "^4.7.1", "vlucas/phpdotenv": "^4.2", - "slim/psr7": "^1.3" + "slim/psr7": "^1.3", + "jetbrains/phpstorm-attributes": "^1.0.0" }, "require-dev": { "phpstan/phpstan": "^0.12.80", diff --git a/api/phpstan.neon b/api/phpstan.neon new file mode 100644 index 0000000..f117a55 --- /dev/null +++ b/api/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 6 + paths: + - src + - tests/unit \ No newline at end of file diff --git a/api/src/Models/ModelInterface.php b/api/src/Models/ModelInterface.php new file mode 100644 index 0000000..66ca192 --- /dev/null +++ b/api/src/Models/ModelInterface.php @@ -0,0 +1,11 @@ +workingDay; + } + + /** + * @param DateTime $workingDay + * + * @return WorkingHours + */ + public function setWorkingDay(DateTime $workingDay): WorkingHours + { + $this->workingDay = $workingDay; + return $this; + } + + /** + * @return DateInterval|null + */ + public function getWorkingTime(): ?DateInterval + { + return $this->workingTime; + } + + /** + * @param DateInterval $workingTime + * + * @return WorkingHours + */ + public function setWorkingTime(DateInterval $workingTime): WorkingHours + { + $this->workingTime = $workingTime; + return $this; + } + + /** + * @return array + */ + #[ArrayShape(['workingDay' => "string", 'workingTime' => "string"])] + public function jsonSerialize(): array + { + return [ + 'workingDay' => $this->getWorkingDay()->format('Y-m-d'), + 'workingTime' => $this->getWorkingTime()->format('%H:%I:%S') + ]; + } +} \ No newline at end of file diff --git a/api/tests/unit/Models/WorkingHoursTest.php b/api/tests/unit/Models/WorkingHoursTest.php new file mode 100644 index 0000000..0f7fc5d --- /dev/null +++ b/api/tests/unit/Models/WorkingHoursTest.php @@ -0,0 +1,70 @@ +assertNull($obj->getWorkingDay()); + $this->assertNull($obj->getWorkingTime()); + + $this->expectException(Error::class); + $obj->jsonSerialize(); + } + + /** + * @return array[] + */ + public function workingHoursProvider(): array + { + return [ + [ + new DateTime('2020-11-30'), + new DateInterval("PT8H"), + [ + 'workingDay' => '2020-11-30', + 'workingTime' => '08:00:00', + ], + ], + [ + new DateTime('2021-05-03'), + new DateInterval("PT7H45M"), + [ + 'workingDay' => '2021-05-03', + 'workingTime' => '07:45:00', + ], + ], + ]; + } + + /** + * @param DateTime $date + * @param DateInterval $interval + * @param array $should_json + * + * @dataProvider workingHoursProvider + */ + public function testWorkingHoursWithContent(DateTime $date, DateInterval $interval, array $should_json): void + { + + $obj = new WorkingHours(); + $obj + ->setWorkingDay($date) + ->setWorkingTime($interval); + $this->assertNotNull($obj->getWorkingDay()); + $this->assertNotNull($obj->getWorkingTime()); + + $json = $obj->jsonSerialize(); + $this->assertIsArray($json); + $this->assertEquals($should_json, $json); + } +}