117 lines
3.7 KiB
PHP
117 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
|
|
|
|
use Codeception\Attribute\DataProvider;
|
|
use Codeception\Test\Unit;
|
|
use DateTime;
|
|
use DateTimeZone;
|
|
use PDO;
|
|
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
|
|
|
class WorkingHoursViewRepositoryTest extends Unit
|
|
{
|
|
protected PDO $pdoObject;
|
|
|
|
protected function _before(): void
|
|
{
|
|
/** @noinspection SpellCheckingInspection */
|
|
$this->pdoObject = new PDO(
|
|
'pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass'
|
|
);
|
|
parent::_before();
|
|
}
|
|
|
|
/**
|
|
* @return array<array{string, int}>
|
|
*/
|
|
public function listObjectProvider(): array
|
|
{
|
|
return [
|
|
[WorkingHoursYearlyViewRepository::class, 1],
|
|
[WorkingHoursMonthlyViewRepository::class, 3],
|
|
[WorkingHoursWeeklyViewRepository::class, 13],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<array{string, string, int, DateTime}>
|
|
*/
|
|
public function existingObjectProvider(): array
|
|
{
|
|
return [
|
|
[
|
|
WorkingHoursYearlyViewRepository::class,
|
|
'2020',
|
|
61,
|
|
date_create('2020-01-01', new DateTimeZone('UCT')),
|
|
3,
|
|
],
|
|
[
|
|
WorkingHoursMonthlyViewRepository::class,
|
|
'2020 February',
|
|
20,
|
|
date_create('2020-02-01', new DateTimeZone('UCT')),
|
|
1,
|
|
],
|
|
[
|
|
WorkingHoursWeeklyViewRepository::class,
|
|
'2020#03',
|
|
5,
|
|
date_create('2020-01-13', new DateTimeZone('UCT')),
|
|
0,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<array{string, string}>
|
|
*/
|
|
public function notExistingObjectProvider(): array
|
|
{
|
|
return [
|
|
[WorkingHoursYearlyViewRepository::class, '2022',],
|
|
[WorkingHoursMonthlyViewRepository::class, '2020 May',],
|
|
[WorkingHoursWeeklyViewRepository::class, '2020#30',],
|
|
];
|
|
}
|
|
|
|
#[DataProvider(('listObjectProvider'))]
|
|
public function testFindAll(string $periodClass, int $countRecords): void
|
|
{
|
|
$repository = new $periodClass($this->pdoObject);
|
|
$models = $repository->findAll();
|
|
$this->assertIsArray($models);
|
|
$this->assertContainsOnlyInstancesOf(WorkingHoursView::class, $models);
|
|
$this->assertCount($countRecords, $models);
|
|
}
|
|
|
|
#[DataProvider(('existingObjectProvider'))]
|
|
public function testExistingFindByKey(
|
|
string $periodClass,
|
|
string $search,
|
|
int $workingDays,
|
|
DateTime $period,
|
|
int $homeOfficeDays
|
|
): void {
|
|
$repository = new $periodClass($this->pdoObject);
|
|
$model = $repository->findByKey($search);
|
|
$this->assertInstanceOf(WorkingHoursView::class, $model);
|
|
$this->assertEquals($workingDays, $model->getWorkingDays());
|
|
$this->assertEquals($period->format('Ymd'), $model->getPeriod()->format('Ymd'));
|
|
$this->assertEquals($homeOfficeDays, $model->getHomeOfficeDays());
|
|
}
|
|
|
|
#[DataProvider(('notExistingObjectProvider'))]
|
|
public function testNotExistingFindByKey(string $periodClass, string $search): void
|
|
{
|
|
$repository = new $periodClass($this->pdoObject);
|
|
$this->expectException(RepositoryRecordNotFoundException::class);
|
|
$repository->findByKey($search);
|
|
}
|
|
}
|