diff --git a/api/src/Repositories/RepositoryBadWhereDataException.php b/api/src/Repositories/RepositoryBadWhereDataException.php new file mode 100644 index 0000000..620452b --- /dev/null +++ b/api/src/Repositories/RepositoryBadWhereDataException.php @@ -0,0 +1,10 @@ +container = $this->makeEmpty(ContainerInterface::class, [ 'has' => true, - 'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass') + '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, - ] + 'getParsedBody' => [ + 'workingDay' => self::EXISTING_DATE, + 'workingTime' => self::EXISTING_INTERVAL, + ], ]); $this->response = $this->makeEmpty(Response::class, [ - 'getBody' => $this->makeEmpty(StreamInterface::class, [ + 'getBody' => $this->makeEmpty(StreamInterface::class, [ 'write' => function (mixed $data) { $this->assertIsString($data); $this->assertJson($data); @@ -65,23 +66,202 @@ class WorkingHoursControllerTest extends Unit public function testConstructWithNonDatabase(): void { $this->container = $this->makeEmpty(ContainerInterface::class, [ - 'has' => false + '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 testBrowse(): void + 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-data' => null, + 'end-data' => null, + ], + ], + [ + [ + 'start-data' => null, + ], + ], + [ + [ + 'end-data' => null, + ], + ], + [ + [ + 'start-data' => '2020-03-01', + 'end-data' => null, + ], + ], + [ + [ + 'start-data' => null, + 'end-data' => '2020-03-31', + ], + ], + // + [ + [ + 'start-data' => '2020-03-00', + 'end-data' => '2020-04-31', + ], + ], + [ + [ + 'start-data' => '2020-03-00', + ], + ], + [ + [ + 'end-data' => '2020-04-31', + ], + ], + [ + [ + 'start-data' => '2020-03-01', + 'end-data' => '2020-04-31', + ], + ], + [ + [ + 'start-data' => '2020-03-00', + 'end-data' => '2020-03-31', + ], + ], + // + [ + [ + 'start-data' => '2020-03', + 'end-data' => '2020-03', + ], + ], + [ + [ + 'start-data' => '2020-03', + ], + ], + [ + [ + 'end-data' => '2020-03', + ], + ], + [ + [ + 'start-data' => '2020-03-01', + 'end-data' => '2020-03', + ], + ], + [ + [ + 'start-data' => '2020-03', + 'end-data' => '2020-03-31', + ], + ], + // + [ + [ + 'start-data' => '2020', + 'end-data' => '2020', + ], + ], + [ + [ + 'start-data' => '2020', + ], + ], + [ + [ + 'end-data' => '2020', + ], + ], + [ + [ + 'start-data' => '2020-03-01', + 'end-data' => '2020', + ], + ], + [ + [ + 'start-data' => '2020', + 'end-data' => '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 @@ -91,7 +271,7 @@ class WorkingHoursControllerTest extends Unit { $controller = new WorkingHoursController($this->container); $response = $controller->update($this->request, $this->response, [ - 'id' => self::EXISTING_DATE + 'id' => self::EXISTING_DATE, ]); $this->assertInstanceOf(Response::class, $response); } @@ -106,23 +286,23 @@ class WorkingHoursControllerTest extends Unit $controller = new WorkingHoursController($this->container); $this->expectException(HttpNotFoundException::class); $controller->update($this->request, $this->response, [ - 'id' => self::NEW_DATE + 'id' => self::NEW_DATE, ]); } /** * @return string[][] */ - public function invalidDataProvider(): array + public function invalidCreatDataProvider(): array { return [ [ self::NEW_DATE, - '07:59' + '07:59', ], [ '2020-13-33', - self::NEW_INTERVAL + self::NEW_INTERVAL, ], ]; } @@ -131,7 +311,7 @@ class WorkingHoursControllerTest extends Unit * @param string $date * @param string $time * - * @dataProvider invalidDataProvider + * @dataProvider invalidCreatDataProvider * * @throws HttpBadRequestException * @throws HttpConflictRequestException @@ -145,7 +325,7 @@ class WorkingHoursControllerTest extends Unit 'getParsedBody' => [ 'workingDay' => $date, 'workingTime' => $time, - ] + ], ]); $controller = new WorkingHoursController($this->container); $this->expectException(HttpBadRequestException::class); @@ -165,7 +345,7 @@ class WorkingHoursControllerTest extends Unit 'getParsedBody' => [ 'workingDay' => self::NEW_DATE, 'workingTime' => self::NEW_INTERVAL, - ] + ], ]); $controller = new WorkingHoursController($this->container); $response = $controller->creat($this->request, $this->response, []); @@ -185,7 +365,7 @@ class WorkingHoursControllerTest extends Unit 'getParsedBody' => [ 'workingDay' => self::EXISTING_DATE, 'workingTime' => self::EXISTING_INTERVAL, - ] + ], ]); $controller = new WorkingHoursController($this->container); $this->expectException(HttpConflictRequestException::class); @@ -201,7 +381,7 @@ class WorkingHoursControllerTest extends Unit { $controller = new WorkingHoursController($this->container); $response = $controller->read($this->request, $this->response, [ - 'id' => self::EXISTING_DATE + 'id' => self::EXISTING_DATE, ]); $this->assertInstanceOf(Response::class, $response); } @@ -216,7 +396,7 @@ class WorkingHoursControllerTest extends Unit $controller = new WorkingHoursController($this->container); $this->expectException(HttpNotFoundException::class); $controller->read($this->request, $this->response, [ - 'id' => self::NEW_DATE + 'id' => self::NEW_DATE, ]); } } diff --git a/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php b/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php index 5561505..7080ee9 100644 --- a/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php +++ b/api/tests/unit/Repositories/WorkingHoursRepositoryTest.php @@ -2,6 +2,7 @@ namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Repositories; +use Codeception\Example; use Codeception\Test\Unit; use DateInterval; use DateTime; @@ -10,6 +11,7 @@ use InvalidArgumentException; use PDO; use TorstenHettstedt\TimekeepingApi\Models\ModelInterface; use TorstenHettstedt\TimekeepingApi\Models\WorkingHours; +use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryBadWhereDataException; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException; use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException; use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository; @@ -22,7 +24,7 @@ class WorkingHoursRepositoryTest extends Unit const NEW_INTERVAL = 'PT7H59M'; const RECORDS_COUNT = 61; - protected PDO $pdoObject; + protected PDO $pdoObject; protected function _before(): void { @@ -34,7 +36,7 @@ class WorkingHoursRepositoryTest extends Unit /** * @throws Exception */ - public function testFindAll(): void + public function testFindAllWithBlankParameter(): void { $repository = new WorkingHoursRepository($this->pdoObject); $models = $repository->findAll(); @@ -43,6 +45,81 @@ class WorkingHoursRepositoryTest extends Unit $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 + * + * @dataProvider valideFilterParameter + * + * @throws Exception + */ + public function testFindAllWithValideParameter(?string $start, ?string $ende, int $count): void + { + $repository = new WorkingHoursRepository($this->pdoObject); + $models = $repository->findFiltered($start, $ende); + $this->assertIsArray($models); + $this->assertContainsOnly(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 + * + * @dataProvider invalideFilterParameter + * + * @throws Exception + */ + public function testFindAllWithInvalideParameter(?string $start, ?string $ende): void + { + $repository = new WorkingHoursRepository($this->pdoObject); + $this->expectException(RepositoryBadWhereDataException::class); + $repository->findFiltered($start, $ende); + } + /** * @throws RepositoryRecordNotFoundException */