From e070db18b5f12ad043d136a05c9c2094b34881cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Tue, 6 Apr 2021 23:21:44 +0200 Subject: [PATCH] =?UTF-8?q?Test=20erweitert=20um=20die=20Coverage=20zu=20e?= =?UTF-8?q?rh=C3=B6hen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/WorkingHoursControllerTest.php | 222 ++++++++++++++++++ .../WorkingHoursViewControllerTest.php | 114 +++++++++ .../unit/Middleware/ErrorHandlerTest.php | 101 ++++++++ 3 files changed, 437 insertions(+) create mode 100644 api/tests/unit/Controller/WorkingHoursControllerTest.php create mode 100644 api/tests/unit/Controller/WorkingHoursViewControllerTest.php create mode 100644 api/tests/unit/Middleware/ErrorHandlerTest.php diff --git a/api/tests/unit/Controller/WorkingHoursControllerTest.php b/api/tests/unit/Controller/WorkingHoursControllerTest.php new file mode 100644 index 0000000..0615cc1 --- /dev/null +++ b/api/tests/unit/Controller/WorkingHoursControllerTest.php @@ -0,0 +1,222 @@ +container = $this->makeEmpty(ContainerInterface::class, [ + 'has' => true, + 'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass') + ]); + $this->request = $this->makeEmpty(Request::class, [ + 'getParsedBody' => [ + 'workingDay' => self::EXISTING_DATE, + 'workingTime' => self::EXISTING_INTERVAL, + ] + ]); + $this->response = $this->makeEmpty(Response::class, [ + 'getBody' => $this->makeEmpty(StreamInterface::class, [ + 'write' => function (mixed $data) { + $this->assertIsString($data); + $this->assertJson($data); + }, + ]), + 'withHeader' => $this->makeEmpty(Response::class, [ + 'withStatus' => $this->makeEmpty(Response::class), + ]), + ]); + } + + /** + * @throws NotDatabasesException + * @throws Exception + */ + public function testConstructWithNonDatabase(): void + { + $this->container = $this->makeEmpty(ContainerInterface::class, [ + 'has' => false + ]); + $this->expectException(NotDatabasesException::class); + new WorkingHoursController($this->container); + } + + /** + * @throws NotDatabasesException + * @throws Exception + */ + public function testBrowse(): void + { + $controller = new WorkingHoursController($this->container); + $response = $controller->browse($this->request, $this->response, []); + $this->assertInstanceOf(Response::class, $response); + } + + /** + * @throws NotDatabasesException + * @throws HttpBadRequestException + * @throws HttpNotFoundException + */ + public function testUpdateExistRecord(): void + { + $controller = new WorkingHoursController($this->container); + $response = $controller->update($this->request, $this->response, [ + 'id' => self::EXISTING_DATE + ]); + $this->assertInstanceOf(Response::class, $response); + } + + /** + * @throws NotDatabasesException + * @throws HttpBadRequestException + * @throws HttpNotFoundException + */ + public function testUpdateNotExistRecord(): void + { + $controller = new WorkingHoursController($this->container); + $this->expectException(HttpNotFoundException::class); + $controller->update($this->request, $this->response, [ + 'id' => self::NEW_DATE + ]); + } + + /** + * @return string[][] + */ + public function invalidDataProvider(): array + { + return [ + [ + self::NEW_DATE, + '07:59' + ], + [ + '2020-13-33', + self::NEW_INTERVAL + ], + ]; + } + + /** + * @param string $date + * @param string $time + * + * @dataProvider invalidDataProvider + * + * @throws HttpBadRequestException + * @throws HttpConflictRequestException + * @throws HttpInternalServerErrorException + * @throws NotDatabasesException + * @throws Exception + */ + public function testCreatInvalidData(string $date, string $time): void + { + $this->request = $this->makeEmpty(Request::class, [ + 'getParsedBody' => [ + 'workingDay' => $date, + 'workingTime' => $time, + ] + ]); + $controller = new WorkingHoursController($this->container); + $this->expectException(HttpBadRequestException::class); + $controller->creat($this->request, $this->response, []); + } + + /** + * @throws HttpBadRequestException + * @throws NotDatabasesException + * @throws HttpInternalServerErrorException + * @throws HttpConflictRequestException + * @throws Exception + */ + public function testCreatNewRecord(): void + { + $this->request = $this->makeEmpty(Request::class, [ + 'getParsedBody' => [ + 'workingDay' => self::NEW_DATE, + 'workingTime' => self::NEW_INTERVAL, + ] + ]); + $controller = new WorkingHoursController($this->container); + $response = $controller->creat($this->request, $this->response, []); + $this->assertInstanceOf(Response::class, $response); + } + + /** + * @throws HttpBadRequestException + * @throws HttpConflictRequestException + * @throws HttpInternalServerErrorException + * @throws NotDatabasesException + * @throws Exception + */ + public function testCreatExistRecord(): void + { + $this->request = $this->makeEmpty(Request::class, [ + 'getParsedBody' => [ + 'workingDay' => self::EXISTING_DATE, + 'workingTime' => self::EXISTING_INTERVAL, + ] + ]); + $controller = new WorkingHoursController($this->container); + $this->expectException(HttpConflictRequestException::class); + $controller->creat($this->request, $this->response, []); + } + + /** + * @throws HttpInternalServerErrorException + * @throws HttpNotFoundException + * @throws NotDatabasesException + */ + public function testReadExistRecord(): void + { + $controller = new WorkingHoursController($this->container); + $response = $controller->read($this->request, $this->response, [ + 'id' => self::EXISTING_DATE + ]); + $this->assertInstanceOf(Response::class, $response); + } + + /** + * @throws HttpInternalServerErrorException + * @throws HttpNotFoundException + * @throws NotDatabasesException + */ + public function testReadNotExistRecord(): void + { + $controller = new WorkingHoursController($this->container); + $this->expectException(HttpNotFoundException::class); + $controller->read($this->request, $this->response, [ + 'id' => self::NEW_DATE + ]); + } +} diff --git a/api/tests/unit/Controller/WorkingHoursViewControllerTest.php b/api/tests/unit/Controller/WorkingHoursViewControllerTest.php new file mode 100644 index 0000000..56c431f --- /dev/null +++ b/api/tests/unit/Controller/WorkingHoursViewControllerTest.php @@ -0,0 +1,114 @@ +container = $this->makeEmpty(ContainerInterface::class, [ + 'has' => true, + 'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass') + ]); + $this->request = $this->makeEmpty(Request::class, []); + $this->response = $this->makeEmpty(Response::class, [ + 'getBody' => $this->makeEmpty(StreamInterface::class, [ + 'write' => function (mixed $data) { + $this->assertIsString($data); + $this->assertJson($data); + }, + ]), + 'withHeader' => $this->makeEmpty(Response::class, [ + 'withStatus' => $this->makeEmpty(Response::class), + ]), + ]); + } + + /** + * @throws Exception + */ + public function testBrowseMonthlyWithNonDatabase(): void + { + $this->container = $this->makeEmpty(ContainerInterface::class, [ + 'has' => false + ]); + $controller = new WorkingHoursViewController($this->container); + $this->expectException(NotDatabasesException::class); + $controller->browseMonthly($this->request, $this->response, []); + } + + /** + * @throws Exception + */ + public function testBrowseMonthly(): void + { + $controller = new WorkingHoursViewController($this->container); + $response = $controller->browseMonthly($this->request, $this->response, []); + $this->assertInstanceOf(Response::class, $response); + } + + /** + * @throws Exception + */ + public function testBrowseYearlyWithNonDatabase(): void + { + $this->container = $this->makeEmpty(ContainerInterface::class, [ + 'has' => false + ]); + $controller = new WorkingHoursViewController($this->container); + $this->expectException(NotDatabasesException::class); + $controller->browseYearly($this->request, $this->response, []); + } + + /** + * @throws Exception + */ + public function testBrowseYearly(): void + { + $controller = new WorkingHoursViewController($this->container); + $response = $controller->browseYearly($this->request, $this->response, []); + $this->assertInstanceOf(Response::class, $response); + } + + /** + * @throws Exception + */ + public function testBrowseWeeklyWithNonDatabase(): void + { + $this->container = $this->makeEmpty(ContainerInterface::class, [ + 'has' => false + ]); + $controller = new WorkingHoursViewController($this->container); + $this->expectException(NotDatabasesException::class); + $controller->browseWeekly($this->request, $this->response, []); + } + + /** + * @throws Exception + */ + public function testBrowseWeekly(): void + { + $controller = new WorkingHoursViewController($this->container); + $response = $controller->browseWeekly($this->request, $this->response, []); + $this->assertInstanceOf(Response::class, $response); + } +} diff --git a/api/tests/unit/Middleware/ErrorHandlerTest.php b/api/tests/unit/Middleware/ErrorHandlerTest.php new file mode 100644 index 0000000..4e99e69 --- /dev/null +++ b/api/tests/unit/Middleware/ErrorHandlerTest.php @@ -0,0 +1,101 @@ +app = $this->makeEmpty(App::class, [ + 'getResponseFactory' => $this->makeEmpty(ResponseFactoryInterface::class, [ + 'createResponse' => $this->makeEmpty(ResponseInterface::class, [ + 'getBody' => $this->makeEmpty(StreamInterface::class, [ + 'write' => function (mixed $data) { + $this->assertIsString($data); + $this->assertJson($data); + }, + ]), + ]), + ]), + ]); + $this->request = $this->makeEmpty(ServerRequestInterface::class); + } + + /** + * @throws Exception + */ + public function testUseWithLogger(): void + { + $this->exception = $this->make(Exception::class, [ + 'message' => '', + 'code' => 400, + 'file' => '/path(to/file', + ]); + $this->logger = $this->makeEmpty(LoggerInterface::class, [ + 'error' => Expected::once(), + ]); + + $middleWare = new ErrorHandler($this->app); + $middleWare($this->request, $this->exception, true, true, true, $this->logger); + } + + /** + * @throws Exception + */ + public function testUseWithHttpException(): void + { + $this->exception = $this->make(HttpException::class, [ + 'message' => '', + 'code' => 400, + 'file' => '/path(to/file', + 'getTitle' => Expected::once('The Title'), + ]); + $this->logger = $this->makeEmpty(LoggerInterface::class, [ + 'error' => Expected::once(), + ]); + + $middleWare = new ErrorHandler($this->app); + $middleWare($this->request, $this->exception, true, true, true, $this->logger); + + } + + /** + * @throws Exception + */ + public function testUseWithGeneralException(): void + { + $this->exception = $this->make(Exception::class, [ + 'message' => '', + 'code' => 400, + 'file' => '/path(to/file', + ]); + $this->logger = $this->makeEmpty(LoggerInterface::class, [ + 'error' => Expected::once(), + ]); + + $middleWare = new ErrorHandler($this->app); + $middleWare($this->request, $this->exception, true, true, true, $this->logger); + } +}