diff --git a/src/Method/CancelReservation.php b/src/Method/CancelReservation.php index 3344926..18a43dd 100644 --- a/src/Method/CancelReservation.php +++ b/src/Method/CancelReservation.php @@ -38,12 +38,13 @@ protected function constructRequestData($data) } /** - * @param mixed $data + * @param array $requestData + * @param mixed $responseData * @return array */ - protected function constructResponseData($data) + protected function constructResponseData(array $requestData, $responseData) { - Validator::checkNull($data); + Validator::checkNull($responseData); return array(); } diff --git a/src/Method/CheckHealth.php b/src/Method/CheckHealth.php index cf0f892..9b79c11 100644 --- a/src/Method/CheckHealth.php +++ b/src/Method/CheckHealth.php @@ -30,10 +30,11 @@ protected function constructRequestData($data) } /** - * @param mixed $data + * @param array $requestData + * @param mixed $responseData * @return array */ - protected function constructResponseData($data) + protected function constructResponseData(array $requestData, $responseData) { return array(); } diff --git a/src/Method/CreateReservation.php b/src/Method/CreateReservation.php index 8d7bf4a..665eb4e 100644 --- a/src/Method/CreateReservation.php +++ b/src/Method/CreateReservation.php @@ -38,12 +38,13 @@ protected function constructRequestData($data) } /** - * @param mixed $data + * @param array $requestData + * @param mixed $responseData * @return array */ - protected function constructResponseData($data) + protected function constructResponseData(array $requestData, $responseData) { - Validator::checkNull($data); + Validator::checkNull($responseData); return array(); } diff --git a/src/Method/GetSlots.php b/src/Method/GetSlots.php index e8dd3f1..b7a36cf 100644 --- a/src/Method/GetSlots.php +++ b/src/Method/GetSlots.php @@ -35,12 +35,13 @@ protected function constructRequestData($data) } /** - * @param mixed $data + * @param array $requestData + * @param mixed $responseData * @return array */ - protected function constructResponseData($data) + protected function constructResponseData(array $requestData, $responseData) { - Validator::checkStructure($data, array( + Validator::checkStructure($responseData, array( array( 'startDateTime' => Validator::TYPE_DATE_TIME, 'endDateTime' => Validator::TYPE_DATE_TIME, @@ -49,19 +50,25 @@ protected function constructResponseData($data) )); $result = array(); + $requestedDate = $requestData[0]->format('Y-m-d'); - foreach ($data as $index => $slot) { + foreach ($responseData as $index => $slot) { $whichSlot = sprintf('(slot #%d)', $index + 1); $startDateTime = $slot['startDateTime']->format(DateTime::W3C); $endDateTime = $slot['endDateTime']->format(DateTime::W3C); + $startDate = $slot['startDateTime']->format('Y-m-d'); $capacity = $slot['capacity']; if ($startDateTime >= $endDateTime) { throw new ValidationException(sprintf('Slot startDateTime (%s) must be before endDateTime (%s) %s', $startDateTime, $endDateTime, $whichSlot)); } + if ($requestedDate !== $startDate) { + throw new ValidationException(sprintf('Slot startDateTime (%s) does not match requested day (%s) %s', $startDate, $requestedDate, $whichSlot)); + } + if ($capacity < 0) { - throw new ValidationException(sprintf('Slot capacity (%s) must be non-negative %s', $capacity, $whichSlot)); + throw new ValidationException(sprintf('Slot capacity (%d) must be non-negative %s', $capacity, $whichSlot)); } $result[] = array( diff --git a/src/Method/Method.php b/src/Method/Method.php index 02830a7..8c22c5e 100644 --- a/src/Method/Method.php +++ b/src/Method/Method.php @@ -35,7 +35,7 @@ public function call(Request $request) } try { - return $this->constructResponseData(call_user_func_array( + return $this->constructResponseData($requestData, call_user_func_array( $this->callback, $requestData )); @@ -56,9 +56,10 @@ abstract public function getName(); abstract protected function constructRequestData($data); /** - * @param mixed $data + * @param array $requestData Return value of constructRequestData + * @param mixed $responseData * @return array */ - abstract protected function constructResponseData($data); + abstract protected function constructResponseData(array $requestData, $responseData); } diff --git a/tests/src/Method/GetSlotsTest.php b/tests/src/Method/GetSlotsTest.php index 5e123cf..c0b146e 100644 --- a/tests/src/Method/GetSlotsTest.php +++ b/tests/src/Method/GetSlotsTest.php @@ -41,6 +41,9 @@ public function testSuccess() public function testFailures() { $now = new DateTime(); + $before = new DateTime('-1 hour'); + $tomorrow = new DateTime('tomorrow'); + $nowString = $now->format(DateTime::W3C); $this->checkException( array(), @@ -59,25 +62,25 @@ public function testFailures() ); $this->checkException( array( - 'date' => $now->format(DateTime::W3C), + 'date' => $nowString, 'parameters' => array(), ), array( array( - 'startDateTime' => new DateTime(), + 'startDateTime' => $now, ) ), 'Missing keys (endDateTime, capacity)' ); $this->checkException( array( - 'date' => $now->format(DateTime::W3C), + 'date' => $nowString, 'parameters' => array(), ), array( array( - 'startDateTime' => new DateTime(), - 'endDateTime' => new DateTime(), + 'startDateTime' => $now, + 'endDateTime' => $now, 'capacity' => 'string', ) ), @@ -85,18 +88,60 @@ public function testFailures() ); $this->checkException( array( - 'date' => $now->format(DateTime::W3C), + 'date' => $nowString, 'parameters' => array(), ), array( array( 'startDateTime' => 'not-a-datetime', - 'endDateTime' => new DateTime(), + 'endDateTime' => $now, 'capacity' => 1, ) ), 'DateTime expected, string (not-a-datetime) given.' ); + $this->checkException( + array( + 'date' => $nowString, + 'parameters' => array(), + ), + array( + array( + 'startDateTime' => $before, + 'endDateTime' => $now, + 'capacity' => -1, + ) + ), + 'Slot capacity (-1) must be non-negative (slot #1)' + ); + $this->checkException( + array( + 'date' => $nowString, + 'parameters' => array(), + ), + array( + array( + 'startDateTime' => $now, + 'endDateTime' => $before, + 'capacity' => 1, + ) + ), + sprintf('Slot startDateTime (%s) must be before endDateTime (%s) (slot #1)', $nowString, $before->format(DateTime::W3C)) + ); + $this->checkException( + array( + 'date' => $tomorrow->format(DateTime::W3C), + 'parameters' => array(), + ), + array( + array( + 'startDateTime' => $before, + 'endDateTime' => $now, + 'capacity' => 1, + ) + ), + sprintf('Slot startDateTime (%s) does not match requested day (%s) (slot #1)', $now->format('Y-m-d'), $tomorrow->format('Y-m-d')) + ); } /** @@ -112,7 +157,7 @@ private function checkException(array $requestData, array $responseData, $error) return $responseData; }); $method->call($request); - $this->fail('Expected exception to be thrown'); + $this->fail(sprintf('Expected exception with "%s" to be thrown', $error)); } catch (MethodException $e) { $this->assertContains($error, $e->getMessage());