275 lines
9.1 KiB
PHP
275 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
|
|
|
|
use Codeception\Attribute\DataProvider;
|
|
use Codeception\Test\Unit;
|
|
use DateInterval;
|
|
use DateTime;
|
|
use Exception;
|
|
use InvalidArgumentException;
|
|
use PDO;
|
|
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
|
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryBadWhereDataException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
|
|
|
class WorkingHoursRepositoryTest extends Unit
|
|
{
|
|
public const string EXISTING_DATE = '2020-01-28';
|
|
public const string EXISTING_INTERVAL = 'PT7H20M';
|
|
public const string NEW_DATE = '2020-04-01';
|
|
public const string NEW_INTERVAL = 'PT7H59M';
|
|
public const int RECORDS_COUNT = 61;
|
|
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testFindAllWithBlankParameter(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$models = $repository->findAll();
|
|
|
|
$this->assertContainsOnlyInstancesOf(WorkingHours::class, $models);
|
|
$this->assertCount(self::RECORDS_COUNT, $models);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<int|string|null>>
|
|
*/
|
|
public function valideFilterParameter(): array
|
|
{
|
|
return [
|
|
[null, null, 61,],
|
|
['2020-04-01', null, 0,],
|
|
['2020-04-01', '2020-04-30', 0,],
|
|
[null, '2020-04-30', 61,],
|
|
['2020-03-01', null, 22,],
|
|
['2020-03-01', '2020-03-31', 22,],
|
|
['2020-03-01', '2020-03-07', 5,],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param string|null $start
|
|
* @param string|null $ende
|
|
* @param int $count
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
#[DataProvider('valideFilterParameter')]
|
|
public function testFindAllWithValideParameter(?string $start, ?string $ende, int $count): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$models = $repository->findFiltered($start, $ende);
|
|
|
|
$this->assertContainsOnlyInstancesOf(WorkingHours::class, $models);
|
|
$this->assertCount($count, $models);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<int|string|null>>
|
|
*/
|
|
public function invalideFilterParameter(): array
|
|
{
|
|
return [
|
|
['2020-04-31', null],
|
|
[null, '2020-04-31'],
|
|
['2020-04-00', '2020-04-31'],
|
|
['2020-04-01', '2020-04-31'],
|
|
['2020-04-00', '2020-04-30'],
|
|
//
|
|
['2020-04', null],
|
|
[null, '2020-04'],
|
|
['2020-04', '2020-04'],
|
|
['2020-04-01', '2020-04'],
|
|
['2020-04', '2020-04-30'],
|
|
//
|
|
['2020', null],
|
|
[null, '2020'],
|
|
['2020', '2020'],
|
|
['2020-04-01', '2020'],
|
|
['2020', '2020-04-30'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param string|null $start
|
|
* @param string|null $ende
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
#[DataProvider('invalideFilterParameter')]
|
|
public function testFindAllWithInvalideParameter(?string $start, ?string $ende): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$this->expectException(RepositoryBadWhereDataException::class);
|
|
$repository->findFiltered($start, $ende);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
*/
|
|
public function testExistingFindByKey(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $repository->findByKey(self::EXISTING_DATE);
|
|
$this->assertEquals(new DateTime(self::EXISTING_DATE), $model->getWorkingDay());
|
|
$this->assertEquals(new DateInterval(self::EXISTING_INTERVAL), $model->getWorkingTime());
|
|
}
|
|
|
|
public function testNotExistingFindByKey(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$this->expectException(RepositoryRecordNotFoundException::class);
|
|
$repository->findByKey(self::NEW_DATE);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testInsertWithInvalidModel(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->makeEmpty(ModelInterface::class);
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$repository->insert($model);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function testNewInsert(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->make(WorkingHours::class, [
|
|
'workingDay' => new DateTime(self::NEW_DATE),
|
|
'workingTime' => new DateInterval(self::NEW_INTERVAL),
|
|
'isHomeOffice' => false,
|
|
]);
|
|
$repository->insert($model);
|
|
$model = $repository->findByKey(self::NEW_DATE);
|
|
$this->assertEquals(new DateTime(self::NEW_DATE), $model->getWorkingDay());
|
|
$this->assertEquals(new DateInterval(self::NEW_INTERVAL), $model->getWorkingTime());
|
|
$this->assertCount(self::RECORDS_COUNT + 1, $repository->findAll());
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testExistingInsert(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->make(WorkingHours::class, [
|
|
'workingDay' => new DateTime(self::EXISTING_DATE),
|
|
'workingTime' => new DateInterval(self::NEW_INTERVAL),
|
|
'isHomeOffice' => false,
|
|
]);
|
|
$this->expectException(RepositoryRecordAlreadyExistException::class);
|
|
$repository->insert($model);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function testDeleteWithInvalidModel(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->makeEmpty(ModelInterface::class);
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$repository->delete($model);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function testExistsDelete(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->make(WorkingHours::class, [
|
|
'workingDay' => new DateTime(self::EXISTING_DATE),
|
|
'workingTime' => new DateInterval(self::EXISTING_INTERVAL),
|
|
'isHomeOffice' => false,
|
|
]);
|
|
$repository->delete($model);
|
|
$this->expectException(RepositoryRecordNotFoundException::class);
|
|
$repository->findByKey(self::EXISTING_DATE);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function testNotExistsDelete(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->make(WorkingHours::class, [
|
|
'workingDay' => new DateTime(self::NEW_DATE),
|
|
'workingTime' => new DateInterval(self::NEW_INTERVAL),
|
|
'isHomeOffice' => false,
|
|
]);
|
|
$this->expectException(RepositoryRecordNotFoundException::class);
|
|
$repository->delete($model);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function testUpdateWithInvalidModel(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->makeEmpty(ModelInterface::class);
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$repository->update($model);
|
|
}
|
|
|
|
/**
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function testExistsUpdate(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->make(WorkingHours::class, [
|
|
'workingDay' => new DateTime(self::EXISTING_DATE),
|
|
'workingTime' => new DateInterval(self::NEW_INTERVAL),
|
|
'isHomeOffice' => false,
|
|
]);
|
|
$repository->update($model);
|
|
$model_db = $repository->findByKey(self::EXISTING_DATE);
|
|
$this->assertEquals(new DateInterval(self::NEW_INTERVAL), $model_db->getWorkingTime());
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testNotExistsUpdate(): void
|
|
{
|
|
$repository = new WorkingHoursRepository($this->pdoObject);
|
|
$model = $this->make(WorkingHours::class, [
|
|
'workingDay' => new DateTime(self::NEW_DATE),
|
|
'workingTime' => new DateInterval(self::NEW_INTERVAL),
|
|
'isHomeOffice' => false,
|
|
]);
|
|
$this->expectException(RepositoryRecordNotFoundException::class);
|
|
$repository->update($model);
|
|
}
|
|
}
|