-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from maxbeckers/feature/fallback_handler
Add fallback intent handler
- Loading branch information
Showing
12 changed files
with
119 additions
and
15 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic; | ||
|
||
use MaxBeckers\AmazonAlexa\Helper\ResponseHelper; | ||
use MaxBeckers\AmazonAlexa\Request\Request; | ||
use MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest; | ||
use MaxBeckers\AmazonAlexa\RequestHandler\AbstractRequestHandler; | ||
use MaxBeckers\AmazonAlexa\Response\Response; | ||
|
||
/** | ||
* @author Maximilian Beckers <[email protected]> | ||
*/ | ||
class FallbackRequestHandler extends AbstractRequestHandler | ||
{ | ||
/** | ||
* @var ResponseHelper | ||
*/ | ||
private $responseHelper; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* @param ResponseHelper $responseHelper | ||
* @param string $output | ||
* @param array $supportedApplicationIds | ||
*/ | ||
public function __construct(ResponseHelper $responseHelper, string $output, array $supportedApplicationIds) | ||
{ | ||
$this->responseHelper = $responseHelper; | ||
$this->output = $output; | ||
$this->supportedApplicationIds = $supportedApplicationIds; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function supportsRequest(Request $request): bool | ||
{ | ||
// support amazon fallback request, amazon default intents are prefixed with "AMAZON." | ||
return $request->request instanceof IntentRequest && 'AMAZON.FallbackIntent' === $request->request->intent->name; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function handleRequest(Request $request): Response | ||
{ | ||
return $this->responseHelper->respond($this->output, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
test/Test/RequestHandler/Basic/ExceptionEncounteredRequestHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/Test/RequestHandler/Basic/FallbackRequestHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic; | ||
|
||
use MaxBeckers\AmazonAlexa\Helper\ResponseHelper; | ||
use MaxBeckers\AmazonAlexa\Request\Request; | ||
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\FallbackRequestHandler; | ||
use MaxBeckers\AmazonAlexa\Response\Response; | ||
use MaxBeckers\AmazonAlexa\Response\ResponseBody; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @author Maximilian Beckers <[email protected]> | ||
*/ | ||
class FallbackRequestHandlerTest extends TestCase | ||
{ | ||
public function testSupportsRequestAndOutput() | ||
{ | ||
$responseHelper = $this->getMockBuilder(ResponseHelper::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$request = Request::fromAmazonRequest('{"request":{"type":"IntentRequest", "intent":{"name":"AMAZON.FallbackIntent"}}}', 'true', 'true'); | ||
$output = 'Just a simple Test'; | ||
$requestHandler = new FallbackRequestHandler($responseHelper, $output, ['my_skill_id']); | ||
|
||
$responseBody = new ResponseBody(); | ||
$responseBody->outputSpeech = $output; | ||
$responseHelper->expects(static::once())->method('respond')->willReturn(new Response([], '1.0', $responseBody)); | ||
|
||
static::assertTrue($requestHandler->supportsRequest($request)); | ||
static::assertSame($output, $requestHandler->handleRequest($request)->response->outputSpeech); | ||
} | ||
|
||
public function testNotSupportsRequest() | ||
{ | ||
$request = Request::fromAmazonRequest('{"request":{"type":"IntentRequest", "intent":{"name":"InvalidIntent"}}}', 'true', 'true'); | ||
$output = 'Just a simple Test'; | ||
$requestHandler = new FallbackRequestHandler(new ResponseHelper(), $output, ['my_skill_id']); | ||
|
||
static::assertFalse($requestHandler->supportsRequest($request)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
test/Test/RequestHandler/Basic/NavigateHomeRequestHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
test/Test/RequestHandler/Basic/SessionEndedRequestHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters