refactor/update #30

Merged
TorstenHettstedt merged 15 commits from refactor/update into master 2025-06-16 15:48:34 +02:00
8 changed files with 230 additions and 203 deletions
Showing only changes of commit 41a3c79c91 - Show all commits
@@ -0,0 +1,52 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
use Codeception\Test\Unit;
use Exception;
use PDO;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\StreamInterface;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
class AbstractControllerTest extends Unit
{
protected const FICTIONAL_STATUS_CODE = 666;
protected ContainerInterface $container;
protected Request $request;
protected Response|MockObject $response;
/**
* @throws Exception
* @throws \PHPUnit\Framework\MockObject\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->response = $this->createMock(Response::class);
$this->response->expects($this->any())->method('getStatusCode')->willReturn(self::FICTIONAL_STATUS_CODE);
$this->response->expects($this->any())->method('getBody')->willReturn(
$this->makeEmpty(StreamInterface::class, [
'write' => function (mixed $data) {
$this->assertIsString($data);
$this->assertJson($data);
return strlen($data);
},
'getContents' => 'abc',
])
);
$this->response->expects($this->any())->method('withHeader')->willReturn($this->response);
$this->response->expects($this->any())->method('withStatus')->willReturn($this->response);
}
}
@@ -2,14 +2,13 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller; namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
use Codeception\Example; use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit;
use Exception; use Exception;
use PDO; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Message\StreamInterface; use Psr\Container\NotFoundExceptionInterface;
use Slim\Exception\HttpBadRequestException; use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpInternalServerErrorException;
use Slim\Exception\HttpNotFoundException; use Slim\Exception\HttpNotFoundException;
use Slim\Psr7\Request; use Slim\Psr7\Request;
use Slim\Psr7\Response; use Slim\Psr7\Response;
@@ -17,51 +16,38 @@ use TorstenHettstedt\TimekeepingApi\Controller\HttpConflictRequestException;
use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException; use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException;
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursController; use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursController;
class WorkingHoursControllerTest extends Unit class WorkingHoursControllerTest extends AbstractControllerTest
{ {
const EXISTING_DATE = '2020-01-28'; protected const FICTIONAL_STATUS_CODE = 666;
const EXISTING_INTERVAL = '07:20:00'; public const EXISTING_DATE = '2020-01-28';
const NEW_DATE = '2020-04-01'; public const EXISTING_INTERVAL = '07:20:00';
const NEW_INTERVAL = '07:59:00'; public const NEW_DATE = '2020-04-01';
public const NEW_INTERVAL = '07:59:00';
protected ContainerInterface $container; protected ContainerInterface $container;
protected Request $request; protected Request $request;
protected Response $response; protected Response|MockObject $response;
TorstenHettstedt marked this conversation as resolved Outdated
Outdated
Review

muss das noch sein?

muss das noch sein?
/** /**
* @throws Exception * @throws Exception
* @throws \PHPUnit\Framework\MockObject\Exception
*/ */
protected function _before(): void protected function _before(): void
{ {
parent::_before(); 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->request = $this->makeEmpty(Request::class, [
'getParsedBody' => [ 'getParsedBody' => [
'workingDay' => self::EXISTING_DATE, 'workingDay' => self::EXISTING_DATE,
'workingTime' => self::EXISTING_INTERVAL, '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);
return strlen($data);
},
]),
'withHeader' => $this->makeEmpty(Response::class, [
'withStatus' => $this->makeEmpty(Response::class),
]),
]);
} }
/** /**
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function testConstructWithNonDatabase(): void public function testConstructWithNonDatabase(): void
@@ -104,12 +90,12 @@ class WorkingHoursControllerTest extends Unit
/** /**
* @param array<string, string> $queryParams * @param array<string, string> $queryParams
* *
* @dataProvider valideFilterDataProvider * @throws ContainerExceptionInterface
*
* @throws HttpInternalServerErrorException
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
#[DataProvider('valideFilterDataProvider')]
public function testBrowseWithValideParameter(array $queryParams): void public function testBrowseWithValideParameter(array $queryParams): void
{ {
$this->request = $this->makeEmpty(Request::class, [ $this->request = $this->makeEmpty(Request::class, [
@@ -117,7 +103,8 @@ class WorkingHoursControllerTest extends Unit
]); ]);
$controller = new WorkingHoursController($this->container); $controller = new WorkingHoursController($this->container);
$response = $controller->browse($this->request, $this->response, []); $response = $controller->browse($this->request, $this->response, []);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
/** /**
@@ -247,12 +234,12 @@ class WorkingHoursControllerTest extends Unit
/** /**
* @param array<string, string> $queryParams * @param array<string, string> $queryParams
* *
* @dataProvider invalideFilterDataProvider * @throws ContainerExceptionInterface
*
* @throws HttpInternalServerErrorException
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
#[DataProvider('invalideFilterDataProvider')]
public function testBrowseWithInvalideParameter(array $queryParams): void public function testBrowseWithInvalideParameter(array $queryParams): void
{ {
$this->request = $this->makeEmpty(Request::class, [ $this->request = $this->makeEmpty(Request::class, [
@@ -264,9 +251,10 @@ class WorkingHoursControllerTest extends Unit
} }
/** /**
* @throws ContainerExceptionInterface
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws HttpBadRequestException * @throws NotFoundExceptionInterface
* @throws HttpNotFoundException * @throws Exception
*/ */
public function testUpdateExistRecord(): void public function testUpdateExistRecord(): void
{ {
@@ -274,13 +262,15 @@ class WorkingHoursControllerTest extends Unit
$response = $controller->update($this->request, $this->response, [ $response = $controller->update($this->request, $this->response, [
'id' => self::EXISTING_DATE, 'id' => self::EXISTING_DATE,
]); ]);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
/** /**
* @throws ContainerExceptionInterface
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws HttpBadRequestException * @throws NotFoundExceptionInterface
* @throws HttpNotFoundException * @throws Exception
*/ */
public function testUpdateNotExistRecord(): void public function testUpdateNotExistRecord(): void
{ {
@@ -312,14 +302,12 @@ class WorkingHoursControllerTest extends Unit
* @param string $date * @param string $date
* @param string $time * @param string $time
* *
* @dataProvider invalidCreatDataProvider * @throws ContainerExceptionInterface
*
* @throws HttpBadRequestException
* @throws HttpConflictRequestException
* @throws HttpInternalServerErrorException
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
#[DataProvider('invalidCreatDataProvider')]
public function testCreatInvalidData(string $date, string $time): void public function testCreatInvalidData(string $date, string $time): void
{ {
$this->request = $this->makeEmpty(Request::class, [ $this->request = $this->makeEmpty(Request::class, [
@@ -334,10 +322,9 @@ class WorkingHoursControllerTest extends Unit
} }
/** /**
* @throws HttpBadRequestException * @throws ContainerExceptionInterface
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws HttpInternalServerErrorException * @throws NotFoundExceptionInterface
* @throws HttpConflictRequestException
* @throws Exception * @throws Exception
*/ */
public function testCreatNewRecord(): void public function testCreatNewRecord(): void
@@ -350,14 +337,14 @@ class WorkingHoursControllerTest extends Unit
]); ]);
$controller = new WorkingHoursController($this->container); $controller = new WorkingHoursController($this->container);
$response = $controller->creat($this->request, $this->response, []); $response = $controller->creat($this->request, $this->response, []);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
/** /**
* @throws HttpBadRequestException * @throws ContainerExceptionInterface
* @throws HttpConflictRequestException
* @throws HttpInternalServerErrorException
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function testCreatExistRecord(): void public function testCreatExistRecord(): void
@@ -374,9 +361,10 @@ class WorkingHoursControllerTest extends Unit
} }
/** /**
* @throws HttpInternalServerErrorException * @throws ContainerExceptionInterface
* @throws HttpNotFoundException
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public function testReadExistRecord(): void public function testReadExistRecord(): void
{ {
@@ -384,13 +372,15 @@ class WorkingHoursControllerTest extends Unit
$response = $controller->read($this->request, $this->response, [ $response = $controller->read($this->request, $this->response, [
'id' => self::EXISTING_DATE, 'id' => self::EXISTING_DATE,
]); ]);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
/** /**
* @throws HttpInternalServerErrorException * @throws ContainerExceptionInterface
* @throws HttpNotFoundException
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public function testReadNotExistRecord(): void public function testReadNotExistRecord(): void
{ {
@@ -2,50 +2,30 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller; namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
use Codeception\Test\Unit;
use Exception; use Exception;
use PDO; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Message\StreamInterface; use Psr\Container\NotFoundExceptionInterface;
use Slim\Psr7\Request; use Slim\Psr7\Request;
use Slim\Psr7\Response;
use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException; use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException;
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController; use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController;
class WorkingHoursViewControllerTest extends Unit class WorkingHoursViewControllerTest extends AbstractControllerTest
{ {
protected ContainerInterface $container;
protected Request $request;
protected Response $response;
/** /**
* @throws Exception * @throws Exception
* @throws \PHPUnit\Framework\MockObject\Exception
*/ */
protected function _before(): void protected function _before(): void
{ {
parent::_before(); parent::_before();
/** @noinspection SpellCheckingInspection */ $this->request = $this->makeEmpty(Request::class);
$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);
return strlen($data);
},
]),
'withHeader' => $this->makeEmpty(Response::class, [
'withStatus' => $this->makeEmpty(Response::class),
]),
]);
} }
/** /**
* @throws NotDatabasesException * @throws NotDatabasesException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function testConstructWithNonDatabase(): void public function testConstructWithNonDatabase(): void
@@ -58,32 +38,44 @@ class WorkingHoursViewControllerTest extends Unit
} }
/** /**
* @throws NotDatabasesException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function testBrowseMonthly(): void public function testBrowseMonthly(): void
{ {
$controller = new WorkingHoursViewController($this->container); $controller = new WorkingHoursViewController($this->container);
$response = $controller->browseMonthly($this->request, $this->response, []); $response = $controller->browseMonthly($this->request, $this->response, []);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
/** /**
* @throws NotDatabasesException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function testBrowseYearly(): void public function testBrowseYearly(): void
{ {
$controller = new WorkingHoursViewController($this->container); $controller = new WorkingHoursViewController($this->container);
$response = $controller->browseYearly($this->request, $this->response, []); $response = $controller->browseYearly($this->request, $this->response, []);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
/** /**
* @throws NotDatabasesException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function testBrowseWeekly(): void public function testBrowseWeekly(): void
{ {
$controller = new WorkingHoursViewController($this->container); $controller = new WorkingHoursViewController($this->container);
$response = $controller->browseWeekly($this->request, $this->response, []); $response = $controller->browseWeekly($this->request, $this->response, []);
$this->assertInstanceOf(Response::class, $response); $this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
$this->assertNotEmpty($response->getBody()->getContents());
} }
} }
@@ -5,6 +5,7 @@ namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Middleware;
use Codeception\Stub\Expected; use Codeception\Stub\Expected;
use Codeception\Test\Unit; use Codeception\Test\Unit;
use Exception; use Exception;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@@ -20,6 +21,7 @@ use TorstenHettstedt\TimekeepingApi\Middleware\ErrorHandler;
class ErrorHandlerTest extends Unit class ErrorHandlerTest extends Unit
{ {
/** @var App<ContainerInterface> */
protected App $app; protected App $app;
protected ServerRequestInterface $request; protected ServerRequestInterface $request;
protected Exception $exception; protected Exception $exception;
@@ -88,11 +90,10 @@ 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);
$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);
} }
/** /**
@@ -105,7 +106,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);
$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);
+7 -11
View File
@@ -2,6 +2,7 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models; namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models;
use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit; use Codeception\Test\Unit;
use DateInterval; use DateInterval;
use DateTime; use DateTime;
@@ -22,7 +23,7 @@ class WorkingHoursTest extends Unit
} }
/** /**
* @return array[] * @return array<array<DateTime|DateInterval|array<string,string>>>
*/ */
public function workingHoursProvider(): array public function workingHoursProvider(): array
{ {
@@ -49,17 +50,13 @@ class WorkingHoursTest extends Unit
/** /**
* @param DateTime $date * @param DateTime $date
* @param DateInterval $interval * @param DateInterval $interval
* @param array<string, string> $should_json * @param array<string, string> $shouldJson
*
* @dataProvider workingHoursProvider
*/ */
public function testWorkingHoursWithContent(DateTime $date, DateInterval $interval, array $should_json): void #[DataProvider('workingHoursProvider')]
public function testWorkingHoursWithContent(DateTime $date, DateInterval $interval, array $shouldJson): void
{ {
$obj = new WorkingHours(); $obj = new WorkingHours();
$obj $obj->setWorkingDay($date)->setWorkingTime($interval);
->setWorkingDay($date)
->setWorkingTime($interval);
$this->assertNotNull($obj->getWorkingDay()); $this->assertNotNull($obj->getWorkingDay());
$this->assertInstanceOf(DateTime::class, $obj->getWorkingDay()); $this->assertInstanceOf(DateTime::class, $obj->getWorkingDay());
@@ -70,7 +67,6 @@ class WorkingHoursTest extends Unit
$this->assertEquals($interval, $obj->getWorkingTime()); $this->assertEquals($interval, $obj->getWorkingTime());
$json = $obj->jsonSerialize(); $json = $obj->jsonSerialize();
$this->assertIsArray($json); $this->assertEquals($shouldJson, $json);
$this->assertEquals($should_json, $json);
} }
} }
+39 -22
View File
@@ -2,6 +2,7 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models; namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models;
use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit; use Codeception\Test\Unit;
use DateInterval; use DateInterval;
use DateTime; use DateTime;
@@ -13,7 +14,7 @@ class WorkingHoursViewTest extends Unit
{ {
/** /**
* @return array[] * @return array<array{DateTime,PeriodDesignationEnum,int,DateInterval,DateInterval,array<string,mixed>}>
*/ */
public function workingHoursProvider(): array public function workingHoursProvider(): array
{ {
@@ -25,7 +26,11 @@ class WorkingHoursViewTest extends Unit
$absence_time->invert = 1; $absence_time->invert = 1;
return [ return [
[ [
$date, PeriodDesignationEnum::WEEKLY(), $work_days, $total_hours, $overtime, $date,
PeriodDesignationEnum::WEEKLY,
$work_days,
$total_hours,
$overtime,
[ [
'period' => '2020#49', 'period' => '2020#49',
'periodDesignation' => 'weekly', 'periodDesignation' => 'weekly',
@@ -35,7 +40,11 @@ class WorkingHoursViewTest extends Unit
], ],
], ],
[ [
$date, PeriodDesignationEnum::WEEKLY(), $work_days, $total_hours, $absence_time, $date,
PeriodDesignationEnum::WEEKLY,
$work_days,
$total_hours,
$absence_time,
[ [
'period' => '2020#49', 'period' => '2020#49',
'periodDesignation' => 'weekly', 'periodDesignation' => 'weekly',
@@ -45,7 +54,11 @@ class WorkingHoursViewTest extends Unit
], ],
], ],
[ [
$date, PeriodDesignationEnum::MONTHLY(), $work_days, $total_hours, $overtime, $date,
PeriodDesignationEnum::MONTHLY,
$work_days,
$total_hours,
$overtime,
[ [
'period' => '2020-11', 'period' => '2020-11',
'periodDesignation' => 'monthly', 'periodDesignation' => 'monthly',
@@ -55,7 +68,11 @@ class WorkingHoursViewTest extends Unit
], ],
], ],
[ [
$date, PeriodDesignationEnum::MONTHLY(), $work_days, $total_hours, $absence_time, $date,
PeriodDesignationEnum::MONTHLY,
$work_days,
$total_hours,
$absence_time,
[ [
'period' => '2020-11', 'period' => '2020-11',
'periodDesignation' => 'monthly', 'periodDesignation' => 'monthly',
@@ -65,7 +82,12 @@ class WorkingHoursViewTest extends Unit
], ],
], ],
[ [
$date, PeriodDesignationEnum::YEARLY(), $work_days, $total_hours, $overtime, [ $date,
PeriodDesignationEnum::YEARLY,
$work_days,
$total_hours,
$overtime,
[
'period' => '2020', 'period' => '2020',
'periodDesignation' => 'yearly', 'periodDesignation' => 'yearly',
'workingDays' => 15, 'workingDays' => 15,
@@ -74,7 +96,12 @@ class WorkingHoursViewTest extends Unit
], ],
], ],
[ [
$date, PeriodDesignationEnum::YEARLY(), $work_days, $total_hours, $absence_time, [ $date,
PeriodDesignationEnum::YEARLY,
$work_days,
$total_hours,
$absence_time,
[
'period' => '2020', 'period' => '2020',
'periodDesignation' => 'yearly', 'periodDesignation' => 'yearly',
'workingDays' => 15, 'workingDays' => 15,
@@ -91,45 +118,35 @@ class WorkingHoursViewTest extends Unit
* @param int $workingDays * @param int $workingDays
* @param DateInterval $totalHours * @param DateInterval $totalHours
* @param DateInterval $overtime * @param DateInterval $overtime
* @param array<string, string> $should_json * @param array<string, string> $shouldJson
*
* @dataProvider workingHoursProvider
*/ */
#[DataProvider('workingHoursProvider')]
public function testWorkingHoursViewWithContent( public function testWorkingHoursViewWithContent(
DateTimeInterface $period, DateTimeInterface $period,
PeriodDesignationEnum $periodDesignation, PeriodDesignationEnum $periodDesignation,
int $workingDays, int $workingDays,
DateInterval $totalHours, DateInterval $totalHours,
DateInterval $overtime, DateInterval $overtime,
array $should_json array $shouldJson
): void ): void {
{
$obj = new WorkingHoursView($period, $periodDesignation, $workingDays, $totalHours, $overtime); $obj = new WorkingHoursView($period, $periodDesignation, $workingDays, $totalHours, $overtime);
$this->assertNotNull($obj->getPeriod());
$this->assertInstanceOf(DateTimeInterface::class, $obj->getPeriod()); $this->assertInstanceOf(DateTimeInterface::class, $obj->getPeriod());
$this->assertEquals($period, $obj->getPeriod()); $this->assertEquals($period, $obj->getPeriod());
$this->assertNotNull($obj->getPeriodDesignation());
$this->assertInstanceOf(PeriodDesignationEnum::class, $obj->getPeriodDesignation()); $this->assertInstanceOf(PeriodDesignationEnum::class, $obj->getPeriodDesignation());
$this->assertEquals($periodDesignation, $obj->getPeriodDesignation()); $this->assertEquals($periodDesignation, $obj->getPeriodDesignation());
$this->assertNotNull($obj->getWorkingDays());
$this->assertIsInt($obj->getWorkingDays());
$this->assertEquals($workingDays, $obj->getWorkingDays()); $this->assertEquals($workingDays, $obj->getWorkingDays());
$this->assertNotNull($obj->getTotalHours());
$this->assertInstanceOf(DateInterval::class, $obj->getTotalHours()); $this->assertInstanceOf(DateInterval::class, $obj->getTotalHours());
$this->assertEquals($totalHours, $obj->getTotalHours()); $this->assertEquals($totalHours, $obj->getTotalHours());
$this->assertNotNull($obj->getOvertime());
$this->assertInstanceOf(DateInterval::class, $obj->getOvertime()); $this->assertInstanceOf(DateInterval::class, $obj->getOvertime());
$this->assertEquals($overtime, $obj->getOvertime()); $this->assertEquals($overtime, $obj->getOvertime());
$json = $obj->jsonSerialize(); $json = $obj->jsonSerialize();
$this->assertIsArray($json); $this->assertEquals($shouldJson, $json);
$this->assertEquals($should_json, $json);
} }
} }
@@ -2,7 +2,7 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories; namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
use Codeception\Example; use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit; use Codeception\Test\Unit;
use DateInterval; use DateInterval;
use DateTime; use DateTime;
@@ -18,18 +18,20 @@ use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
class WorkingHoursRepositoryTest extends Unit class WorkingHoursRepositoryTest extends Unit
{ {
const EXISTING_DATE = '2020-01-28'; public const EXISTING_DATE = '2020-01-28';
const EXISTING_INTERVAL = 'PT7H20M'; public const EXISTING_INTERVAL = 'PT7H20M';
const NEW_DATE = '2020-04-01'; public const NEW_DATE = '2020-04-01';
const NEW_INTERVAL = 'PT7H59M'; public const NEW_INTERVAL = 'PT7H59M';
const RECORDS_COUNT = 61; public const RECORDS_COUNT = 61;
protected PDO $pdoObject; protected PDO $pdoObject;
protected function _before(): void protected function _before(): void
{ {
/** @noinspection SpellCheckingInspection */ /** @noinspection SpellCheckingInspection */
$this->pdoObject = new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass'); $this->pdoObject = new PDO(
'pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass'
);
parent::_before(); parent::_before();
} }
@@ -40,8 +42,8 @@ class WorkingHoursRepositoryTest extends Unit
{ {
$repository = new WorkingHoursRepository($this->pdoObject); $repository = new WorkingHoursRepository($this->pdoObject);
$models = $repository->findAll(); $models = $repository->findAll();
$this->assertIsArray($models);
$this->assertContainsOnly(WorkingHours::class, $models); $this->assertContainsOnlyInstancesOf(WorkingHours::class, $models);
$this->assertCount(self::RECORDS_COUNT, $models); $this->assertCount(self::RECORDS_COUNT, $models);
} }
@@ -66,16 +68,15 @@ class WorkingHoursRepositoryTest extends Unit
* @param string|null $ende * @param string|null $ende
* @param int $count * @param int $count
* *
* @dataProvider valideFilterParameter
*
* @throws Exception * @throws Exception
*/ */
#[DataProvider('valideFilterParameter')]
public function testFindAllWithValideParameter(?string $start, ?string $ende, int $count): void public function testFindAllWithValideParameter(?string $start, ?string $ende, int $count): void
{ {
$repository = new WorkingHoursRepository($this->pdoObject); $repository = new WorkingHoursRepository($this->pdoObject);
$models = $repository->findFiltered($start, $ende); $models = $repository->findFiltered($start, $ende);
$this->assertIsArray($models);
$this->assertContainsOnly(WorkingHours::class, $models); $this->assertContainsOnlyInstancesOf(WorkingHours::class, $models);
$this->assertCount($count, $models); $this->assertCount($count, $models);
} }
@@ -85,22 +86,22 @@ class WorkingHoursRepositoryTest extends Unit
public function invalideFilterParameter(): array public function invalideFilterParameter(): array
{ {
return [ return [
['2020-04-31',null], ['2020-04-31', null],
[null, '2020-04-31'], [null, '2020-04-31'],
['2020-04-00','2020-04-31'], ['2020-04-00', '2020-04-31'],
['2020-04-01','2020-04-31'], ['2020-04-01', '2020-04-31'],
['2020-04-00','2020-04-30'], ['2020-04-00', '2020-04-30'],
// //
['2020-04', null], ['2020-04', null],
[null, '2020-04'], [null, '2020-04'],
['2020-04', '2020-04'], ['2020-04', '2020-04'],
['2020-04-01','2020-04'], ['2020-04-01', '2020-04'],
['2020-04', '2020-04-30'], ['2020-04', '2020-04-30'],
// //
['2020', null], ['2020', null],
[null, '2020'], [null, '2020'],
['2020', '2020'], ['2020', '2020'],
['2020-04-01','2020'], ['2020-04-01', '2020'],
['2020', '2020-04-30'], ['2020', '2020-04-30'],
]; ];
} }
@@ -109,10 +110,9 @@ class WorkingHoursRepositoryTest extends Unit
* @param string|null $start * @param string|null $start
* @param string|null $ende * @param string|null $ende
* *
* @dataProvider invalideFilterParameter
*
* @throws Exception * @throws Exception
*/ */
#[DataProvider('invalideFilterParameter')]
public function testFindAllWithInvalideParameter(?string $start, ?string $ende): void public function testFindAllWithInvalideParameter(?string $start, ?string $ende): void
{ {
$repository = new WorkingHoursRepository($this->pdoObject); $repository = new WorkingHoursRepository($this->pdoObject);
@@ -127,7 +127,6 @@ class WorkingHoursRepositoryTest extends Unit
{ {
$repository = new WorkingHoursRepository($this->pdoObject); $repository = new WorkingHoursRepository($this->pdoObject);
$model = $repository->findByKey(self::EXISTING_DATE); $model = $repository->findByKey(self::EXISTING_DATE);
$this->assertInstanceOf(WorkingHours::class, $model);
$this->assertEquals(new DateTime(self::EXISTING_DATE), $model->getWorkingDay()); $this->assertEquals(new DateTime(self::EXISTING_DATE), $model->getWorkingDay());
$this->assertEquals(new DateInterval(self::EXISTING_INTERVAL), $model->getWorkingTime()); $this->assertEquals(new DateInterval(self::EXISTING_INTERVAL), $model->getWorkingTime());
} }
@@ -163,7 +162,6 @@ class WorkingHoursRepositoryTest extends Unit
]); ]);
$repository->insert($model); $repository->insert($model);
$model = $repository->findByKey(self::NEW_DATE); $model = $repository->findByKey(self::NEW_DATE);
$this->assertInstanceOf(WorkingHours::class, $model);
$this->assertEquals(new DateTime(self::NEW_DATE), $model->getWorkingDay()); $this->assertEquals(new DateTime(self::NEW_DATE), $model->getWorkingDay());
$this->assertEquals(new DateInterval(self::NEW_INTERVAL), $model->getWorkingTime()); $this->assertEquals(new DateInterval(self::NEW_INTERVAL), $model->getWorkingTime());
$this->assertCount(self::RECORDS_COUNT + 1, $repository->findAll()); $this->assertCount(self::RECORDS_COUNT + 1, $repository->findAll());
@@ -2,12 +2,12 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories; namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit; use Codeception\Test\Unit;
use DateTime; use DateTime;
use DateTimeZone; use DateTimeZone;
use Exception; use Exception;
use PDO; use PDO;use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository; use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository; use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
@@ -25,7 +25,7 @@ class WorkingHoursViewRepositoryTest extends Unit
} }
/** /**
* @return array[] * @return array<array{string, int}>
*/ */
public function listObjectProvider(): array public function listObjectProvider(): array
{ {
@@ -37,7 +37,7 @@ class WorkingHoursViewRepositoryTest extends Unit
} }
/** /**
* @return array[] * @return array<array{string, string, int, DateTime}>
* @throws Exception * @throws Exception
*/ */
public function existingObjectProvider(): array public function existingObjectProvider(): array
@@ -50,7 +50,7 @@ class WorkingHoursViewRepositoryTest extends Unit
} }
/** /**
* @return array[] * @return array<array{string, string}>
*/ */
public function notExistingObjectProvider(): array public function notExistingObjectProvider(): array
{ {
@@ -61,49 +61,30 @@ class WorkingHoursViewRepositoryTest extends Unit
]; ];
} }
/** #[DataProvider(('listObjectProvider'))]
* @param string $period_class public function testFindAll(string $periodClass, int $countRecords): void
* @param int $count_records
*
* @dataProvider listObjectProvider
*/
public function testFindAll(string $period_class, int $count_records): void
{ {
$repository = new $period_class($this->pdoObject); $repository = new $periodClass($this->pdoObject);
$models = $repository->findAll(); $models = $repository->findAll();
$this->assertIsArray($models); $this->assertIsArray($models);
$this->assertContainsOnly(WorkingHoursView::class, $models); $this->assertContainsOnlyInstancesOf(WorkingHoursView::class, $models);
$this->assertCount($count_records, $models); $this->assertCount($countRecords, $models);
} }
/** #[DataProvider(('existingObjectProvider'))]
* public function testExistingFindByKey(string $periodClass, string $search, int $workingDays, DateTime $period): void
* @dataProvider existingObjectProvider
*
* @param string $period_class
* @param string $search
* @param int $workingDays
* @param DateTime $period
*/
public function testExistingFindByKey(string $period_class, string $search, int $workingDays, DateTime $period): void
{ {
$repository = new $period_class($this->pdoObject); $repository = new $periodClass($this->pdoObject);
$model = $repository->findByKey($search); $model = $repository->findByKey($search);
$this->assertInstanceOf(WorkingHoursView::class, $model); $this->assertInstanceOf(WorkingHoursView::class, $model);
$this->assertEquals($workingDays, $model->getWorkingDays()); $this->assertEquals($workingDays, $model->getWorkingDays());
$this->assertEquals($period->format('Ymd'), $model->getPeriod()->format('Ymd')); $this->assertEquals($period->format('Ymd'), $model->getPeriod()->format('Ymd'));
} }
/** #[DataProvider(('notExistingObjectProvider'))]
* @param string $period_class public function testNotExistingFindByKey(string $periodClass, string $search): void
* @param string $search
*
* @dataProvider notExistingObjectProvider
*
*/
public function testNotExistingFindByKey(string $period_class, string $search): void
{ {
$repository = new $period_class($this->pdoObject); $repository = new $periodClass($this->pdoObject);
$this->expectException(RepositoryRecordNotFoundException::class); $this->expectException(RepositoryRecordNotFoundException::class);
$repository->findByKey($search); $repository->findByKey($search);
} }