From 93f52af52beaaa416eb8f348e6ef7fca31c9e23f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Wed, 31 Mar 2021 15:40:21 +0200 Subject: [PATCH] =?UTF-8?q?Tests=20f=C3=BCr=20den=20Record-Klasse=20geschr?= =?UTF-8?q?ieben.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WorkingHoursRepositoryTest.php | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 api/tests/unit/Repositories/WorkingHoursRepositoryTest.php diff --git a/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php b/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php new file mode 100644 index 0000000..5dc4f62 --- /dev/null +++ b/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php @@ -0,0 +1,150 @@ +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); + } +}