api-erstellen #1
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
||||
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;
|
||||
|
||||
protected PDO $pdoObject;
|
||||
protected PDOStatement $pdoStatement;
|
||||
|
||||
protected function _before(): void
|
||||
|
TorstenHettstedt marked this conversation as resolved
|
||||
{
|
||||
$this->pdoObject = new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass');
|
||||
parent::_before();
|
||||
}
|
||||
|
||||
public function testFindAll(): void
|
||||
{
|
||||
$repository = new WorkingHoursRepository($this->pdoObject);
|
||||
$models = $repository->findAll();
|
||||
$this->assertIsArray($models);
|
||||
$this->assertContainsOnly(WorkingHours::class, $models);
|
||||
$this->assertCount(self::RECORDS_COUNT, $models);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RepositoryRecordNotFoundException
|
||||
*/
|
||||
public function testExistingFindByKey(): void
|
||||
{
|
||||
$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());
|
||||
}
|
||||
|
||||
public function testNotExistingFindByKey(): void
|
||||
{
|
||||
$repository = new WorkingHoursRepository($this->pdoObject);
|
||||
$this->expectException(RepositoryRecordNotFoundException::class);
|
||||
$repository->findByKey(self::NEW_DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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),
|
||||
]);
|
||||
$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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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),
|
||||
]);
|
||||
$this->expectException(RepositoryModelAlreadyExists::class);
|
||||
$repository->insert($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),
|
||||
]);
|
||||
$repository->delete($model);
|
||||
$this->expectException(RepositoryRecordNotFoundException::class);
|
||||
$repository->findByKey(self::EXISTING_DATE);
|
||||
}
|
||||
|
||||
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),
|
||||
]);
|
||||
$this->expectException(RepositoryRecordNotFoundException::class);
|
||||
$repository->delete($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),
|
||||
]);
|
||||
$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),
|
||||
]);
|
||||
$this->expectException(RepositoryRecordNotFoundException::class);
|
||||
$repository->update($model);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user
Kann raus.