Compare commits
16
Commits
2d3a1074f6
...
0.5.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31a3f4cc01 | ||
|
|
0a59cb60db | ||
|
|
9430f3c74c | ||
|
|
2b31e17e14 | ||
|
|
9e35ab3b07 | ||
|
|
056bf82c7b | ||
|
|
be54a659c9 | ||
|
|
8086b86eb2 | ||
|
|
3e2865dbe7 | ||
|
|
60eb1f92e8 | ||
|
|
9a542c9108 | ||
|
|
a035ea9648 | ||
|
|
46f3a6491d | ||
|
|
e070db18b5 | ||
|
|
a6675605e4 | ||
|
|
520db45d6d |
@@ -105,3 +105,4 @@ Temporary Items
|
||||
/api/vendor/
|
||||
/api/tests/_*
|
||||
/api/tests/*.suite.yml
|
||||
/api/.env
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
## Datenbank-Einstellungen
|
||||
DATABASES_HOST=psql.torsten-hettstedt.net
|
||||
DATABASES_NAME=torsten
|
||||
DATABASES_USER=web_user
|
||||
DATABASES_PASS=V6ZGhtdXEbxH8oWD
|
||||
@@ -0,0 +1,5 @@
|
||||
## Datenbank-Einstellungen
|
||||
DATABASES_HOST=psql.domain.tld
|
||||
DATABASES_NAME=db
|
||||
DATABASES_USER=user
|
||||
DATABASES_PASS=1234
|
||||
@@ -1,6 +1,5 @@
|
||||
FROM php:8.0-apache
|
||||
|
||||
#RUN apt update && apt install -y postgresql postgresql-client
|
||||
RUN apt update && apt install -y libpq-dev
|
||||
RUN docker-php-ext-install -j$(nproc) pdo pdo_pgsql
|
||||
|
||||
|
||||
+6
-19
@@ -8,6 +8,7 @@ use DI\Container;
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Routing\RouteCollectorProxy;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursController;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController;
|
||||
use TorstenHettstedt\TimekeepingApi\Middleware\ErrorHandler;
|
||||
use TorstenHettstedt\TimekeepingApi\Middleware\JsonBodyParserMiddleware;
|
||||
|
||||
@@ -22,25 +23,11 @@ AppFactory::setContainer($container);
|
||||
$app = AppFactory::create();
|
||||
$app->add(new JsonBodyParserMiddleware());
|
||||
|
||||
$app->get(
|
||||
'/',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\HelloWorldController:read'
|
||||
);
|
||||
|
||||
$app->get(
|
||||
'/views/working-hours/weekly',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseWeekly'
|
||||
);
|
||||
|
||||
$app->get(
|
||||
'/views/working-hours/monthly',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseMonthly'
|
||||
);
|
||||
|
||||
$app->get(
|
||||
'/views/working-hours/yearly',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseYearly'
|
||||
);
|
||||
$app->group('/views/working-hours', function (RouteCollectorProxy $group) {
|
||||
$group->get('/weekly', WorkingHoursViewController::class . ':browseWeekly');
|
||||
$group->get('/monthly', WorkingHoursViewController::class . ':browseMonthly');
|
||||
$group->get('/yearly', WorkingHoursViewController::class . ':browseYearly');
|
||||
});
|
||||
|
||||
$app->group('/working-hours', function (RouteCollectorProxy $group) {
|
||||
$group->get('', WorkingHoursController::class . ':browse');
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
use JsonSerializable;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Psr7\Response;
|
||||
|
||||
abstract class AbstractController
|
||||
{
|
||||
|
||||
protected PDO $databases;
|
||||
|
||||
/**
|
||||
* WorkingHoursController constructor.
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @throws NotDatabasesException
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
if ($container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$this->databases = $container->get('databases');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response $response
|
||||
* @param JsonSerializable|JsonSerializable[] $data
|
||||
* @param int $status_code
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function printResponse(Response $response, mixed $data, int $status_code): Response {
|
||||
$payload = json_encode($data);
|
||||
|
||||
$response->getBody()->write($payload);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus($status_code);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php /** @noinspection PhpUnused */
|
||||
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
use Slim\Psr7\Response;
|
||||
use Slim\Psr7\Request;
|
||||
|
||||
class HelloWorldController
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
*/
|
||||
public function read(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$payload = json_encode('Hallo World');
|
||||
|
||||
$response->getBody()->write($payload);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus(200);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<?php
|
||||
<?php /** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
@@ -6,16 +6,15 @@ use Slim\Exception\HttpSpecializedException;
|
||||
|
||||
class HttpConflictRequestException extends HttpSpecializedException
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
/** @var int */
|
||||
protected $code = 409;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
protected $message = 'Conflict.';
|
||||
|
||||
/** @var string */
|
||||
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.';
|
||||
}
|
||||
|
||||
@@ -2,45 +2,23 @@
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Exception\HttpBadRequestException;
|
||||
use Slim\Exception\HttpInternalServerErrorException;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
||||
|
||||
class WorkingHoursController
|
||||
class WorkingHoursController extends AbstractController
|
||||
{
|
||||
|
||||
protected PDO $databases;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
/**
|
||||
* WorkingHoursController constructor.
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @throws NotDatabasesException
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
if ($this->container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$this->databases = $this->container->get('databases');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
@@ -71,13 +49,17 @@ class WorkingHoursController
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
*/
|
||||
public function browse(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$repository = new WorkingHoursRepository($this->databases);
|
||||
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||
try {
|
||||
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 +103,7 @@ class WorkingHoursController
|
||||
$model = $this->buildModel($request, $body['workingDay'], $body['workingTime']);
|
||||
try {
|
||||
$repository->insert($model);
|
||||
} catch (RepositoryModelAlreadyExists $exception) {
|
||||
} catch (RepositoryRecordAlreadyExistException $exception) {
|
||||
throw new HttpConflictRequestException($request, 'Der Eintrag ist schon vorhanden.', $exception);
|
||||
} catch (Exception $exception) {
|
||||
throw new HttpInternalServerErrorException($request, 'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||
@@ -129,24 +111,6 @@ class WorkingHoursController
|
||||
return $this->printResponse($response, $model, StatusCodeInterface::STATUS_CREATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response $response
|
||||
* @param mixed $data
|
||||
* @param int $status_code
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function printResponse(Response $response, mixed $data, int $status_code): Response
|
||||
{
|
||||
$payload = json_encode($data);
|
||||
|
||||
$response->getBody()->write($payload);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus($status_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param string $date
|
||||
|
||||
@@ -4,25 +4,16 @@ namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Slim\Exception\HttpInternalServerErrorException;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
||||
|
||||
class WorkingHoursViewController
|
||||
class WorkingHoursViewController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container) {
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
@@ -30,17 +21,18 @@ class WorkingHoursViewController
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
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->databases);
|
||||
try {
|
||||
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);
|
||||
}
|
||||
$repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases'));
|
||||
return $this->printResponse($response, $repository->findAll());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,16 +42,18 @@ class WorkingHoursViewController
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
* @throws Exception
|
||||
*/
|
||||
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->databases);
|
||||
try {
|
||||
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);
|
||||
}
|
||||
$repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases'));
|
||||
return $this->printResponse($response, $repository->findAll());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,33 +63,18 @@ class WorkingHoursViewController
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
* @throws Exception
|
||||
*/
|
||||
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->databases);
|
||||
try {
|
||||
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);
|
||||
}
|
||||
$repository = new WorkingHoursYearlyViewRepository($this->container->get('databases'));
|
||||
return $this->printResponse($response, $repository->findAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response $response
|
||||
* @param WorkingHoursView[] $data
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function printResponse( Response $response, array $data): Response
|
||||
{
|
||||
$payload = json_encode($data);
|
||||
|
||||
$response->getBody()->write($payload);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus(200);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
<?php
|
||||
<?php /** @noinspection PhpUndefinedClassInspection */
|
||||
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Middleware;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@@ -30,7 +31,13 @@ class ErrorHandler
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
protected function buildJsonArray(Throwable $exception): array
|
||||
#[ArrayShape([
|
||||
'timestamp' => "false|string",
|
||||
'status' => "int",
|
||||
"error" => "string",
|
||||
'message' => "string",
|
||||
'path' => "string"
|
||||
])] protected function buildJsonArray(Throwable $exception): array
|
||||
{
|
||||
$payload = [
|
||||
'timestamp' => date('Y-m-d\TH:m:sP'),
|
||||
|
||||
@@ -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
|
||||
*
|
||||
* @throws RepositoryModelAlreadyExists
|
||||
* @throws RepositoryRecordAlreadyExistException
|
||||
*/
|
||||
public function insert(mixed $model): void;
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
|
||||
/**
|
||||
* @param WorkingHours|ModelInterface $model
|
||||
*
|
||||
* @throws RepositoryModelAlreadyExists
|
||||
* @throws RepositoryRecordAlreadyExistException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insert(mixed $model): void
|
||||
@@ -99,7 +99,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
|
||||
|
||||
try {
|
||||
$this->findByKey($workingDay);
|
||||
throw new RepositoryModelAlreadyExists();
|
||||
throw new RepositoryRecordAlreadyExistException();
|
||||
} /** @noinspection PhpUnusedLocalVariableInspection */
|
||||
catch (RepositoryRecordNotFoundException $exception) {
|
||||
$stmt = $this->database->prepare('insert into public."Arbeitszeiten" ("Datum", "Arbeitszeit") values (?, ?) on conflict do nothing');
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Slim\Exception\HttpBadRequestException;
|
||||
use Slim\Exception\HttpInternalServerErrorException;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\HttpConflictRequestException;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursController;
|
||||
|
||||
class WorkingHoursControllerTest extends Unit
|
||||
{
|
||||
|
||||
const EXISTING_DATE = '2020-01-28';
|
||||
const EXISTING_INTERVAL = '07:20:00';
|
||||
const NEW_DATE = '2020-04-01';
|
||||
const NEW_INTERVAL = '07:59:00';
|
||||
|
||||
protected ContainerInterface $container;
|
||||
protected Request $request;
|
||||
protected Response $response;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _before(): void
|
||||
{
|
||||
parent::_before();
|
||||
/** @noinspection SpellCheckingInspection */
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => true,
|
||||
'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass')
|
||||
]);
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
'getParsedBody' => [
|
||||
'workingDay' => self::EXISTING_DATE,
|
||||
'workingTime' => self::EXISTING_INTERVAL,
|
||||
]
|
||||
]);
|
||||
$this->response = $this->makeEmpty(Response::class, [
|
||||
'getBody' => $this->makeEmpty(StreamInterface::class, [
|
||||
'write' => function (mixed $data) {
|
||||
$this->assertIsString($data);
|
||||
$this->assertJson($data);
|
||||
},
|
||||
]),
|
||||
'withHeader' => $this->makeEmpty(Response::class, [
|
||||
'withStatus' => $this->makeEmpty(Response::class),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testConstructWithNonDatabase(): void
|
||||
{
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => false
|
||||
]);
|
||||
$this->expectException(NotDatabasesException::class);
|
||||
new WorkingHoursController($this->container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowse(): void
|
||||
{
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$response = $controller->browse($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpNotFoundException
|
||||
*/
|
||||
public function testUpdateExistRecord(): void
|
||||
{
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$response = $controller->update($this->request, $this->response, [
|
||||
'id' => self::EXISTING_DATE
|
||||
]);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpNotFoundException
|
||||
*/
|
||||
public function testUpdateNotExistRecord(): void
|
||||
{
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$this->expectException(HttpNotFoundException::class);
|
||||
$controller->update($this->request, $this->response, [
|
||||
'id' => self::NEW_DATE
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function invalidDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
self::NEW_DATE,
|
||||
'07:59'
|
||||
],
|
||||
[
|
||||
'2020-13-33',
|
||||
self::NEW_INTERVAL
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param string $time
|
||||
*
|
||||
* @dataProvider invalidDataProvider
|
||||
*
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatInvalidData(string $date, string $time): void
|
||||
{
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
'getParsedBody' => [
|
||||
'workingDay' => $date,
|
||||
'workingTime' => $time,
|
||||
]
|
||||
]);
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$this->expectException(HttpBadRequestException::class);
|
||||
$controller->creat($this->request, $this->response, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpBadRequestException
|
||||
* @throws NotDatabasesException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatNewRecord(): void
|
||||
{
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
'getParsedBody' => [
|
||||
'workingDay' => self::NEW_DATE,
|
||||
'workingTime' => self::NEW_INTERVAL,
|
||||
]
|
||||
]);
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$response = $controller->creat($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatExistRecord(): void
|
||||
{
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
'getParsedBody' => [
|
||||
'workingDay' => self::EXISTING_DATE,
|
||||
'workingTime' => self::EXISTING_INTERVAL,
|
||||
]
|
||||
]);
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$this->expectException(HttpConflictRequestException::class);
|
||||
$controller->creat($this->request, $this->response, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws HttpNotFoundException
|
||||
* @throws NotDatabasesException
|
||||
*/
|
||||
public function testReadExistRecord(): void
|
||||
{
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$response = $controller->read($this->request, $this->response, [
|
||||
'id' => self::EXISTING_DATE
|
||||
]);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws HttpNotFoundException
|
||||
* @throws NotDatabasesException
|
||||
*/
|
||||
public function testReadNotExistRecord(): void
|
||||
{
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$this->expectException(HttpNotFoundException::class);
|
||||
$controller->read($this->request, $this->response, [
|
||||
'id' => self::NEW_DATE
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController;
|
||||
|
||||
class WorkingHoursViewControllerTest extends Unit
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
protected Request $request;
|
||||
protected Response $response;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _before(): void
|
||||
{
|
||||
parent::_before();
|
||||
/** @noinspection SpellCheckingInspection */
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => true,
|
||||
'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass')
|
||||
]);
|
||||
$this->request = $this->makeEmpty(Request::class, []);
|
||||
$this->response = $this->makeEmpty(Response::class, [
|
||||
'getBody' => $this->makeEmpty(StreamInterface::class, [
|
||||
'write' => function (mixed $data) {
|
||||
$this->assertIsString($data);
|
||||
$this->assertJson($data);
|
||||
},
|
||||
]),
|
||||
'withHeader' => $this->makeEmpty(Response::class, [
|
||||
'withStatus' => $this->makeEmpty(Response::class),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testConstructWithNonDatabase(): void
|
||||
{
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => false
|
||||
]);
|
||||
$this->expectException(NotDatabasesException::class);
|
||||
new WorkingHoursViewController($this->container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowseMonthly(): void
|
||||
{
|
||||
$controller = new WorkingHoursViewController($this->container);
|
||||
$response = $controller->browseMonthly($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowseYearly(): void
|
||||
{
|
||||
$controller = new WorkingHoursViewController($this->container);
|
||||
$response = $controller->browseYearly($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowseWeekly(): void
|
||||
{
|
||||
$controller = new WorkingHoursViewController($this->container);
|
||||
$response = $controller->browseWeekly($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Middleware;
|
||||
|
||||
use Codeception\Stub\Expected;
|
||||
use Codeception\Test\Unit;
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseFactoryInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Slim\App;
|
||||
use Slim\Exception\HttpException;
|
||||
use TorstenHettstedt\TimekeepingApi\Middleware\ErrorHandler;
|
||||
|
||||
class ErrorHandlerTest extends Unit
|
||||
{
|
||||
|
||||
protected App $app;
|
||||
protected ServerRequestInterface $request;
|
||||
protected Exception $exception;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _before(): void
|
||||
{
|
||||
parent::_before();
|
||||
$this->app = $this->makeEmpty(App::class, [
|
||||
'getResponseFactory' => $this->makeEmpty(ResponseFactoryInterface::class, [
|
||||
'createResponse' => $this->makeEmpty(ResponseInterface::class, [
|
||||
'getBody' => $this->makeEmpty(StreamInterface::class, [
|
||||
'write' => function (mixed $data) {
|
||||
$this->assertIsString($data);
|
||||
$this->assertJson($data);
|
||||
},
|
||||
]),
|
||||
]),
|
||||
]),
|
||||
]);
|
||||
$this->request = $this->makeEmpty(ServerRequestInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testUseWithLogger(): void
|
||||
{
|
||||
$this->exception = $this->make(Exception::class, [
|
||||
'message' => '',
|
||||
'code' => 400,
|
||||
'file' => '/path(to/file',
|
||||
]);
|
||||
$this->logger = $this->makeEmpty(LoggerInterface::class, [
|
||||
'error' => Expected::once(),
|
||||
]);
|
||||
|
||||
$middleWare = new ErrorHandler($this->app);
|
||||
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testUseWithHttpException(): void
|
||||
{
|
||||
$this->exception = $this->make(HttpException::class, [
|
||||
'message' => '',
|
||||
'code' => 400,
|
||||
'file' => '/path(to/file',
|
||||
'getTitle' => Expected::once('The Title'),
|
||||
]);
|
||||
$this->logger = $this->makeEmpty(LoggerInterface::class, []);
|
||||
|
||||
$middleWare = new ErrorHandler($this->app);
|
||||
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testUseWithGeneralException(): void
|
||||
{
|
||||
$this->exception = $this->make(Exception::class, [
|
||||
'message' => '',
|
||||
'code' => 400,
|
||||
'file' => '/path(to/file',
|
||||
]);
|
||||
$this->logger = $this->makeEmpty(LoggerInterface::class, []);
|
||||
|
||||
$middleWare = new ErrorHandler($this->app);
|
||||
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,9 @@ use DateTime;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
||||
|
||||
@@ -24,7 +23,6 @@ class WorkingHoursRepositoryTest extends Unit
|
||||
const RECORDS_COUNT = 61;
|
||||
|
||||
protected PDO $pdoObject;
|
||||
protected PDOStatement $pdoStatement;
|
||||
|
||||
protected function _before(): void
|
||||
{
|
||||
@@ -104,7 +102,7 @@ class WorkingHoursRepositoryTest extends Unit
|
||||
'workingDay' => new DateTime(self::EXISTING_DATE),
|
||||
'workingTime' => new DateInterval(self::NEW_INTERVAL),
|
||||
]);
|
||||
$this->expectException(RepositoryModelAlreadyExists::class);
|
||||
$this->expectException(RepositoryRecordAlreadyExistException::class);
|
||||
$repository->insert($model);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user