10 Commits
14 changed files with 55 additions and 82 deletions
+1
View File
@@ -105,3 +105,4 @@ Temporary Items
/api/vendor/ /api/vendor/
/api/tests/_* /api/tests/_*
/api/tests/*.suite.yml /api/tests/*.suite.yml
/api/.env
-5
View File
@@ -1,5 +0,0 @@
## Datenbank-Einstellungen
DATABASES_HOST=psql.torsten-hettstedt.net
DATABASES_NAME=torsten
DATABASES_USER=web_user
DATABASES_PASS=V6ZGhtdXEbxH8oWD
+5
View File
@@ -0,0 +1,5 @@
## Datenbank-Einstellungen
DATABASES_HOST=psql.domain.tld
DATABASES_NAME=db
DATABASES_USER=user
DATABASES_PASS=1234
-1
View File
@@ -1,6 +1,5 @@
FROM php:8.0-apache FROM php:8.0-apache
#RUN apt update && apt install -y postgresql postgresql-client
RUN apt update && apt install -y libpq-dev RUN apt update && apt install -y libpq-dev
RUN docker-php-ext-install -j$(nproc) pdo pdo_pgsql RUN docker-php-ext-install -j$(nproc) pdo pdo_pgsql
@@ -6,16 +6,15 @@ use Slim\Exception\HttpSpecializedException;
class HttpConflictRequestException extends HttpSpecializedException class HttpConflictRequestException extends HttpSpecializedException
{ {
/** /** @var int */
* @var int
*/
protected $code = 409; protected $code = 409;
/** /** @var string */
* @var string
*/
protected $message = 'Conflict.'; protected $message = 'Conflict.';
/** @var string */
protected $title = '409 Conflict'; protected $title = '409 Conflict';
/** @var string */
protected $description = 'The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource.'; protected $description = 'The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource.';
} }
@@ -15,7 +15,7 @@ use Slim\Exception\HttpNotFoundException;
use Slim\Psr7\Request; use Slim\Psr7\Request;
use Slim\Psr7\Response; use Slim\Psr7\Response;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours; use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository; use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
@@ -23,7 +23,6 @@ class WorkingHoursController
{ {
protected PDO $databases; protected PDO $databases;
protected ContainerInterface $container;
/** /**
* WorkingHoursController constructor. * WorkingHoursController constructor.
@@ -34,11 +33,10 @@ class WorkingHoursController
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
$this->container = $container; if ($container->has('databases') === false) {
if ($this->container->has('databases') === false) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden'); throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
} }
$this->databases = $this->container->get('databases'); $this->databases = $container->get('databases');
} }
/** /**
@@ -71,13 +69,17 @@ class WorkingHoursController
* *
* @return Response * @return Response
* *
* @throws Exception * @throws HttpInternalServerErrorException
* @noinspection PhpUnusedParameterInspection * @noinspection PhpUnusedParameterInspection
*/ */
public function browse(Request $request, Response $response, array $args): Response public function browse(Request $request, Response $response, array $args): Response
{ {
$repository = new WorkingHoursRepository($this->databases); $repository = new WorkingHoursRepository($this->databases);
try {
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK); return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
} catch (Exception $exception) {
throw new HttpInternalServerErrorException($request, 'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
}
} }
/** /**
@@ -121,7 +123,7 @@ class WorkingHoursController
$model = $this->buildModel($request, $body['workingDay'], $body['workingTime']); $model = $this->buildModel($request, $body['workingDay'], $body['workingTime']);
try { try {
$repository->insert($model); $repository->insert($model);
} catch (RepositoryModelAlreadyExists $exception) { } catch (RepositoryRecordAlreadyExistException $exception) {
throw new HttpConflictRequestException($request, 'Der Eintrag ist schon vorhanden.', $exception); throw new HttpConflictRequestException($request, 'Der Eintrag ist schon vorhanden.', $exception);
} catch (Exception $exception) { } catch (Exception $exception) {
throw new HttpInternalServerErrorException($request, 'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception); throw new HttpInternalServerErrorException($request, 'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
@@ -4,6 +4,7 @@ namespace TorstenHettstedt\TimekeepingApi\Controller;
use Exception; use Exception;
use PDO;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Slim\Psr7\Request; use Slim\Psr7\Request;
use Slim\Psr7\Response; use Slim\Psr7\Response;
@@ -14,13 +15,26 @@ use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepositor
class WorkingHoursViewController class WorkingHoursViewController
{ {
protected PDO $databases;
/** /**
* @var ContainerInterface * @var ContainerInterface
*/ */
protected ContainerInterface $container; protected ContainerInterface $container;
/**
* WorkingHoursViewController constructor.
*
* @param ContainerInterface $container
*
* @throws NotDatabasesException
*/
public function __construct(ContainerInterface $container) { public function __construct(ContainerInterface $container) {
$this->container = $container; $this->container = $container;
if ($container->has('databases') === false) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$this->databases = $container->get('databases');
} }
/** /**
@@ -31,14 +45,10 @@ class WorkingHoursViewController
* @return Response * @return Response
* *
* @noinspection PhpUnusedParameterInspection * @noinspection PhpUnusedParameterInspection
* @throws NotDatabasesException
* @throws Exception * @throws Exception
*/ */
public function browseWeekly(Request $request, Response $response, array $args): Response public function browseWeekly(Request $request, Response $response, array $args): Response
{ {
if ($this->container->has('databases') === false) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases')); $repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases'));
return $this->printResponse($response, $repository->findAll()); return $this->printResponse($response, $repository->findAll());
} }
@@ -55,9 +65,6 @@ class WorkingHoursViewController
*/ */
public function browseMonthly(Request $request, Response $response, array $args): Response public function browseMonthly(Request $request, Response $response, array $args): Response
{ {
if ($this->container->has('databases') === false) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases')); $repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases'));
return $this->printResponse($response, $repository->findAll()); return $this->printResponse($response, $repository->findAll());
} }
@@ -74,9 +81,6 @@ class WorkingHoursViewController
*/ */
public function browseYearly(Request $request, Response $response, array $args): Response public function browseYearly(Request $request, Response $response, array $args): Response
{ {
if ($this->container->has('databases') === false) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$repository = new WorkingHoursYearlyViewRepository($this->container->get('databases')); $repository = new WorkingHoursYearlyViewRepository($this->container->get('databases'));
return $this->printResponse($response, $repository->findAll()); return $this->printResponse($response, $repository->findAll());
} }
@@ -1,10 +0,0 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Repositories;
class RepositoryModelAlreadyExists extends RepositoryException
{
}
@@ -0,0 +1,10 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Repositories;
class RepositoryRecordAlreadyExistException extends RepositoryException
{
}
@@ -11,7 +11,7 @@ interface RepositoryWriterInterface
/** /**
* @param T $model * @param T $model
* *
* @throws RepositoryModelAlreadyExists * @throws RepositoryRecordAlreadyExistException
*/ */
public function insert(mixed $model): void; public function insert(mixed $model): void;
@@ -85,7 +85,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
/** /**
* @param WorkingHours|ModelInterface $model * @param WorkingHours|ModelInterface $model
* *
* @throws RepositoryModelAlreadyExists * @throws RepositoryRecordAlreadyExistException
* @throws Exception * @throws Exception
*/ */
public function insert(mixed $model): void public function insert(mixed $model): void
@@ -99,7 +99,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
try { try {
$this->findByKey($workingDay); $this->findByKey($workingDay);
throw new RepositoryModelAlreadyExists(); throw new RepositoryRecordAlreadyExistException();
} /** @noinspection PhpUnusedLocalVariableInspection */ } /** @noinspection PhpUnusedLocalVariableInspection */
catch (RepositoryRecordNotFoundException $exception) { catch (RepositoryRecordNotFoundException $exception) {
$stmt = $this->database->prepare('insert into public."Arbeitszeiten" ("Datum", "Arbeitszeit") values (?, ?) on conflict do nothing'); $stmt = $this->database->prepare('insert into public."Arbeitszeiten" ("Datum", "Arbeitszeit") values (?, ?) on conflict do nothing');
@@ -44,16 +44,16 @@ class WorkingHoursViewControllerTest extends Unit
} }
/** /**
* @throws NotDatabasesException
* @throws Exception * @throws Exception
*/ */
public function testBrowseMonthlyWithNonDatabase(): void public function testConstructWithNonDatabase(): void
{ {
$this->container = $this->makeEmpty(ContainerInterface::class, [ $this->container = $this->makeEmpty(ContainerInterface::class, [
'has' => false 'has' => false
]); ]);
$controller = new WorkingHoursViewController($this->container);
$this->expectException(NotDatabasesException::class); $this->expectException(NotDatabasesException::class);
$controller->browseMonthly($this->request, $this->response, []); new WorkingHoursViewController($this->container);
} }
/** /**
@@ -66,19 +66,6 @@ class WorkingHoursViewControllerTest extends Unit
$this->assertInstanceOf(Response::class, $response); $this->assertInstanceOf(Response::class, $response);
} }
/**
* @throws Exception
*/
public function testBrowseYearlyWithNonDatabase(): void
{
$this->container = $this->makeEmpty(ContainerInterface::class, [
'has' => false
]);
$controller = new WorkingHoursViewController($this->container);
$this->expectException(NotDatabasesException::class);
$controller->browseYearly($this->request, $this->response, []);
}
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -89,19 +76,6 @@ class WorkingHoursViewControllerTest extends Unit
$this->assertInstanceOf(Response::class, $response); $this->assertInstanceOf(Response::class, $response);
} }
/**
* @throws Exception
*/
public function testBrowseWeeklyWithNonDatabase(): void
{
$this->container = $this->makeEmpty(ContainerInterface::class, [
'has' => false
]);
$controller = new WorkingHoursViewController($this->container);
$this->expectException(NotDatabasesException::class);
$controller->browseWeekly($this->request, $this->response, []);
}
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -72,9 +72,7 @@ class ErrorHandlerTest extends Unit
'file' => '/path(to/file', 'file' => '/path(to/file',
'getTitle' => Expected::once('The Title'), 'getTitle' => Expected::once('The Title'),
]); ]);
$this->logger = $this->makeEmpty(LoggerInterface::class, [ $this->logger = $this->makeEmpty(LoggerInterface::class, []);
'error' => Expected::once(),
]);
$middleWare = new ErrorHandler($this->app); $middleWare = new ErrorHandler($this->app);
$middleWare($this->request, $this->exception, true, true, true, $this->logger); $middleWare($this->request, $this->exception, true, true, true, $this->logger);
@@ -91,9 +89,7 @@ class ErrorHandlerTest extends Unit
'code' => 400, 'code' => 400,
'file' => '/path(to/file', 'file' => '/path(to/file',
]); ]);
$this->logger = $this->makeEmpty(LoggerInterface::class, [ $this->logger = $this->makeEmpty(LoggerInterface::class, []);
'error' => Expected::once(),
]);
$middleWare = new ErrorHandler($this->app); $middleWare = new ErrorHandler($this->app);
$middleWare($this->request, $this->exception, true, true, true, $this->logger); $middleWare($this->request, $this->exception, true, true, true, $this->logger);
@@ -8,10 +8,9 @@ use DateTime;
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use PDO; use PDO;
use PDOStatement;
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface; use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours; use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository; use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
@@ -24,7 +23,6 @@ class WorkingHoursRepositoryTest extends Unit
const RECORDS_COUNT = 61; const RECORDS_COUNT = 61;
protected PDO $pdoObject; protected PDO $pdoObject;
protected PDOStatement $pdoStatement;
protected function _before(): void protected function _before(): void
{ {
@@ -104,7 +102,7 @@ class WorkingHoursRepositoryTest extends Unit
'workingDay' => new DateTime(self::EXISTING_DATE), 'workingDay' => new DateTime(self::EXISTING_DATE),
'workingTime' => new DateInterval(self::NEW_INTERVAL), 'workingTime' => new DateInterval(self::NEW_INTERVAL),
]); ]);
$this->expectException(RepositoryModelAlreadyExists::class); $this->expectException(RepositoryRecordAlreadyExistException::class);
$repository->insert($model); $repository->insert($model);
} }