api-erstellen #1

Merged
TorstenHettstedt merged 52 commits from api-erstellen into master 2021-04-07 13:00:46 +02:00
6 changed files with 153 additions and 3 deletions
Showing only changes of commit e4cd00caf1 - Show all commits
-2
View File
@@ -104,7 +104,5 @@ Temporary Items
/api/codeception.yml /api/codeception.yml
/api/vendor/ /api/vendor/
/api/tests/_* /api/tests/_*
/api/tests/acceptance
/api/tests/functional
/api/tests/*.suite.yml /api/tests/*.suite.yml
/api/html/swagger/ /api/html/swagger/
+2 -1
View File
@@ -18,7 +18,8 @@
"require": { "require": {
"slim/slim": "^4.7.1", "slim/slim": "^4.7.1",
"vlucas/phpdotenv": "^4.2", "vlucas/phpdotenv": "^4.2",
"slim/psr7": "^1.3" "slim/psr7": "^1.3",
"jetbrains/phpstorm-attributes": "^1.0.0"
}, },
"require-dev": { "require-dev": {
"phpstan/phpstan": "^0.12.80", "phpstan/phpstan": "^0.12.80",
+5
View File
@@ -0,0 +1,5 @@
parameters:
level: 6
paths:
- src
- tests/unit
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Models;
use JsonSerializable;
interface ModelInterface extends JsonSerializable
{
}
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Models;
use DateInterval;
use DateTime;
use JetBrains\PhpStorm\ArrayShape;
class WorkingHours implements ModelInterface
{
protected ?DateTime $workingDay = null;
protected ?DateInterval $workingTime = null;
/**
* @return DateTime|null
*/
public function getWorkingDay(): ?DateTime
{
return $this->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<string, string>
*/
#[ArrayShape(['workingDay' => "string", 'workingTime' => "string"])]
public function jsonSerialize(): array
{
return [
'workingDay' => $this->getWorkingDay()->format('Y-m-d'),
'workingTime' => $this->getWorkingTime()->format('%H:%I:%S')
];
}
}
@@ -0,0 +1,70 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models;
use Codeception\Test\Unit;
use DateInterval;
use DateTime;
use Error;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
class WorkingHoursTest extends Unit
{
public function testWorkingHoursWithoutContent(): void
{
$obj = new WorkingHours();
$this->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<string, string> $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);
}
}