110 lines
3.4 KiB
PHP
110 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
|
|
|
|
use Codeception\Test\Unit;
|
|
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
|
|
{
|
|
const EXISTING_DATE = '2020-01-28';
|
|
const EXISTING_INTERVAL = 'PT7H20M';
|
|
const NEW_DATE = '2020-04-01';
|
|
const NEW_INTERVAL = 'PT7H59M';
|
|
const RECORDS_COUNT = 61;
|
|
|
|
protected PDO $pdoObject;
|
|
|
|
protected function _before(): void
|
|
{
|
|
$this->pdoObject = new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass');
|
|
parent::_before();
|
|
}
|
|
|
|
/**
|
|
* @return array[]
|
|
*/
|
|
public function listObjectProvider(): array
|
|
{
|
|
return [
|
|
[WorkingHoursYearlyViewRepository::class, 1],
|
|
[WorkingHoursMonthlyViewRepository::class, 3],
|
|
[WorkingHoursWeeklyViewRepository::class, 13],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array[]
|
|
*/
|
|
public function existingObjectProvider(): array
|
|
{
|
|
return [
|
|
[WorkingHoursYearlyViewRepository::class, '2020', 61],
|
|
[WorkingHoursMonthlyViewRepository::class, '2020 February', 20],
|
|
[WorkingHoursWeeklyViewRepository::class, '2020#03', 5],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array[]
|
|
*/
|
|
public function notExistingObjectProvider(): array
|
|
{
|
|
return [
|
|
[WorkingHoursYearlyViewRepository::class, '2022',],
|
|
[WorkingHoursMonthlyViewRepository::class, '2020 May',],
|
|
[WorkingHoursWeeklyViewRepository::class, '2020#30',],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param string $period_class
|
|
* @param int $count_records
|
|
*
|
|
* @dataProvider listObjectProvider
|
|
*/
|
|
public function testFindAll(string $period_class, int $count_records): void
|
|
{
|
|
$repository = new $period_class($this->pdoObject);
|
|
$models = $repository->findAll();
|
|
$this->assertIsArray($models);
|
|
$this->assertContainsOnly(WorkingHoursView::class, $models);
|
|
$this->assertCount($count_records, $models);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @dataProvider existingObjectProvider
|
|
*
|
|
* @param string $period_class
|
|
* @param string $search
|
|
* @param int $workingDays
|
|
*/
|
|
public function testExistingFindByKey(string $period_class, string $search, int $workingDays): void
|
|
{
|
|
$repository = new $period_class($this->pdoObject);
|
|
$model = $repository->findByKey($search);
|
|
$this->assertInstanceOf(WorkingHoursView::class, $model);
|
|
$this->assertEquals($workingDays, $model->getWorkingDays());
|
|
}
|
|
|
|
/**
|
|
* @param string $period_class
|
|
* @param string $search
|
|
*
|
|
* @dataProvider notExistingObjectProvider
|
|
*
|
|
*/
|
|
public function testNotExistingFindByKey(string $period_class, string $search): void
|
|
{
|
|
$repository = new $period_class($this->pdoObject);
|
|
$this->expectException(RepositoryRecordNotFoundException::class);
|
|
$repository->findByKey($search);
|
|
}
|
|
}
|