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> */ 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> */ 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), ]); $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), ]); $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), ]); $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), ]); $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), ]); $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); } }