Refactor Tests

This commit is contained in:
2025-05-03 09:27:39 +02:00
parent 82aad9766e
commit 41a3c79c91
8 changed files with 230 additions and 203 deletions
@@ -2,7 +2,7 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
use Codeception\Example;
use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit;
use DateInterval;
use DateTime;
@@ -18,18 +18,20 @@ use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
class WorkingHoursRepositoryTest 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;
public const EXISTING_DATE = '2020-01-28';
public const EXISTING_INTERVAL = 'PT7H20M';
public const NEW_DATE = '2020-04-01';
public const NEW_INTERVAL = 'PT7H59M';
public const 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');
$this->pdoObject = new PDO(
'pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass'
);
parent::_before();
}
@@ -40,8 +42,8 @@ class WorkingHoursRepositoryTest extends Unit
{
$repository = new WorkingHoursRepository($this->pdoObject);
$models = $repository->findAll();
$this->assertIsArray($models);
$this->assertContainsOnly(WorkingHours::class, $models);
$this->assertContainsOnlyInstancesOf(WorkingHours::class, $models);
$this->assertCount(self::RECORDS_COUNT, $models);
}
@@ -51,13 +53,13 @@ class WorkingHoursRepositoryTest extends Unit
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,],
[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,],
['2020-03-01', '2020-03-07', 5,],
];
}
@@ -66,16 +68,15 @@ class WorkingHoursRepositoryTest extends Unit
* @param string|null $ende
* @param int $count
*
* @dataProvider valideFilterParameter
*
* @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->assertIsArray($models);
$this->assertContainsOnly(WorkingHours::class, $models);
$this->assertContainsOnlyInstancesOf(WorkingHours::class, $models);
$this->assertCount($count, $models);
}
@@ -85,23 +86,23 @@ class WorkingHoursRepositoryTest extends Unit
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-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-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'],
['2020', null],
[null, '2020'],
['2020', '2020'],
['2020-04-01', '2020'],
['2020', '2020-04-30'],
];
}
@@ -109,10 +110,9 @@ class WorkingHoursRepositoryTest extends Unit
* @param string|null $start
* @param string|null $ende
*
* @dataProvider invalideFilterParameter
*
* @throws Exception
*/
#[DataProvider('invalideFilterParameter')]
public function testFindAllWithInvalideParameter(?string $start, ?string $ende): void
{
$repository = new WorkingHoursRepository($this->pdoObject);
@@ -127,7 +127,6 @@ class WorkingHoursRepositoryTest extends Unit
{
$repository = new WorkingHoursRepository($this->pdoObject);
$model = $repository->findByKey(self::EXISTING_DATE);
$this->assertInstanceOf(WorkingHours::class, $model);
$this->assertEquals(new DateTime(self::EXISTING_DATE), $model->getWorkingDay());
$this->assertEquals(new DateInterval(self::EXISTING_INTERVAL), $model->getWorkingTime());
}
@@ -163,7 +162,6 @@ class WorkingHoursRepositoryTest extends Unit
]);
$repository->insert($model);
$model = $repository->findByKey(self::NEW_DATE);
$this->assertInstanceOf(WorkingHours::class, $model);
$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());
@@ -2,12 +2,12 @@
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit;
use DateTime;
use DateTimeZone;
use Exception;
use PDO;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
use PDO;use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
@@ -25,7 +25,7 @@ class WorkingHoursViewRepositoryTest extends Unit
}
/**
* @return array[]
* @return array<array{string, int}>
*/
public function listObjectProvider(): array
{
@@ -37,7 +37,7 @@ class WorkingHoursViewRepositoryTest extends Unit
}
/**
* @return array[]
* @return array<array{string, string, int, DateTime}>
* @throws Exception
*/
public function existingObjectProvider(): array
@@ -50,7 +50,7 @@ class WorkingHoursViewRepositoryTest extends Unit
}
/**
* @return array[]
* @return array<array{string, string}>
*/
public function notExistingObjectProvider(): array
{
@@ -61,49 +61,30 @@ class WorkingHoursViewRepositoryTest extends Unit
];
}
/**
* @param string $period_class
* @param int $count_records
*
* @dataProvider listObjectProvider
*/
public function testFindAll(string $period_class, int $count_records): void
#[DataProvider(('listObjectProvider'))]
public function testFindAll(string $periodClass, int $countRecords): void
{
$repository = new $period_class($this->pdoObject);
$repository = new $periodClass($this->pdoObject);
$models = $repository->findAll();
$this->assertIsArray($models);
$this->assertContainsOnly(WorkingHoursView::class, $models);
$this->assertCount($count_records, $models);
$this->assertContainsOnlyInstancesOf(WorkingHoursView::class, $models);
$this->assertCount($countRecords, $models);
}
/**
*
* @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
#[DataProvider(('existingObjectProvider'))]
public function testExistingFindByKey(string $periodClass, string $search, int $workingDays, DateTime $period): void
{
$repository = new $period_class($this->pdoObject);
$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'));
}
/**
* @param string $period_class
* @param string $search
*
* @dataProvider notExistingObjectProvider
*
*/
public function testNotExistingFindByKey(string $period_class, string $search): void
#[DataProvider(('notExistingObjectProvider'))]
public function testNotExistingFindByKey(string $periodClass, string $search): void
{
$repository = new $period_class($this->pdoObject);
$repository = new $periodClass($this->pdoObject);
$this->expectException(RepositoryRecordNotFoundException::class);
$repository->findByKey($search);
}