Skip to content

Commit

Permalink
GetSlots: check if returned slots match requested day (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored and pepakriz committed Dec 8, 2016
1 parent e0d1670 commit 0f42e01
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/Method/CancelReservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Method/CheckHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
7 changes: 4 additions & 3 deletions src/Method/CreateReservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
17 changes: 12 additions & 5 deletions src/Method/GetSlots.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions src/Method/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
Expand All @@ -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);

}
61 changes: 53 additions & 8 deletions tests/src/Method/GetSlotsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -59,44 +62,86 @@ 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',
)
),
'Int expected, string (string) given.'
);
$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'))
);
}

/**
Expand All @@ -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());
Expand Down

0 comments on commit 0f42e01

Please sign in to comment.