Skip to content

Commit

Permalink
Display a valid response if sequence is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Norris1z committed Jan 19, 2018
1 parent 2c9f087 commit 249d26e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Hubtel/USSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public static function process(Request $request, array $sequences)
if (sizeof($sequences) === 0) {
throw new \InvalidArgumentException("Sequences must not be an empty array");
}

if(!is_numeric($request->getSequence()) || !isset($sequences[(int)$request->getSequence() - 1])) {
return Response::createInstance("Sorry!. Something went wrong",RequestTypes::RELEASE);
}

$sequence = $request->getSequence() - 1;

Expand Down
32 changes: 32 additions & 0 deletions tests/USSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,36 @@ public function is_json($value = null) {

return $ret;
}

/** @test */
public function it_returns_a_something_went_wrong_response_if_the_sequence_from_the_request_is_not_numeric() {
$this->request->setSequence("some string sequence");
$response = USSD::process($this->request,[
new class implements \Hubtel\USSD\SequenceInterface{

public function handle(Request $request)
{
return \Hubtel\USSD\Response::createInstance("Welcome to Volatile",\Hubtel\USSD\RequestTypes::RESPONSE,"Some Info");
}
}
]);
$this->assertEquals($response->getMessage(),"Sorry!. Something went wrong");
$this->assertEquals($response->getType(),\Hubtel\USSD\RequestTypes::RELEASE);
}

/** @test */
public function it_returns_a_something_went_wrong_response_if_the_sequence_is_not_in_the_array() {
$this->request->setSequence("some invalid sequence");
$response = USSD::process($this->request,[
new class implements \Hubtel\USSD\SequenceInterface{

public function handle(Request $request)
{
return \Hubtel\USSD\Response::createInstance("Welcome to Volatile",\Hubtel\USSD\RequestTypes::RESPONSE,"Some Info");
}
}
]);
$this->assertEquals($response->getMessage(),"Sorry!. Something went wrong");
$this->assertEquals($response->getType(),\Hubtel\USSD\RequestTypes::RELEASE);
}
}

0 comments on commit 249d26e

Please sign in to comment.