Einführung des Modells.
This commit is contained in:
@@ -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
@@ -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",
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
parameters:
|
||||||
|
level: 6
|
||||||
|
paths:
|
||||||
|
- src
|
||||||
|
- tests/unit
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace TorstenHettstedt\TimekeepingApi\Models;
|
||||||
|
|
||||||
|
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
|
interface ModelInterface extends JsonSerializable
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user