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); return strlen($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); } /** * @return array>> */ public function valideFilterDataProvider(): array { return [ [ [], ], [ [ 'start-date' => '2020-03-01', 'end-date' => '2020-03-31', ], ], [ [ 'start-date' => '2020-03-01', ], ], [ [ 'end-date' => '2020-03-31', ], ], ]; } /** * @param array $queryParams * * @dataProvider valideFilterDataProvider * * @throws HttpInternalServerErrorException * @throws NotDatabasesException * @throws Exception */ public function testBrowseWithValideParameter(array $queryParams): void { $this->request = $this->makeEmpty(Request::class, [ 'getQueryParams' => $queryParams, ]); $controller = new WorkingHoursController($this->container); $response = $controller->browse($this->request, $this->response, []); $this->assertInstanceOf(Response::class, $response); } /** * @return array>> */ public function invalideFilterDataProvider(): array { return [ [ [ 'start-date' => '', 'end-date' => '', ], ], [ [ 'start-date' => '', ], ], [ [ 'end-date' => '', ], ], [ [ 'start-date' => '2020-03-01', 'end-date' => '', ], ], [ [ 'start-date' => '', 'end-date' => '2020-03-31', ], ], // [ [ 'start-date' => '2020-03-00', 'end-date' => '2020-04-31', ], ], [ [ 'start-date' => '2020-03-00', ], ], [ [ 'end-date' => '2020-04-31', ], ], [ [ 'start-date' => '2020-03-01', 'end-date' => '2020-04-31', ], ], [ [ 'start-date' => '2020-03-00', 'end-date' => '2020-03-31', ], ], // [ [ 'start-date' => '2020-03', 'end-date' => '2020-03', ], ], [ [ 'start-date' => '2020-03', ], ], [ [ 'end-date' => '2020-03', ], ], [ [ 'start-date' => '2020-03-01', 'end-date' => '2020-03', ], ], [ [ 'start-date' => '2020-03', 'end-date' => '2020-03-31', ], ], // [ [ 'start-date' => '2020', 'end-date' => '2020', ], ], [ [ 'start-date' => '2020', ], ], [ [ 'end-date' => '2020', ], ], [ [ 'start-date' => '2020-03-01', 'end-date' => '2020', ], ], [ [ 'start-date' => '2020', 'end-date' => '2020-03-31', ], ], ]; } /** * @param array $queryParams * * @dataProvider invalideFilterDataProvider * * @throws HttpInternalServerErrorException * @throws NotDatabasesException * @throws Exception */ public function testBrowseWithInvalideParameter(array $queryParams): void { $this->request = $this->makeEmpty(Request::class, [ 'getQueryParams' => $queryParams, ]); $controller = new WorkingHoursController($this->container); $this->expectException(HttpBadRequestException::class); $controller->browse($this->request, $this->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 invalidCreatDataProvider(): array { return [ [ self::NEW_DATE, '07:59', ], [ '2020-13-33', self::NEW_INTERVAL, ], ]; } /** * @param string $date * @param string $time * * @dataProvider invalidCreatDataProvider * * @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, ]); } }