Skip to content

Commit

Permalink
Merge pull request #94 from maxbeckers/feature/fallback_handler
Browse files Browse the repository at this point in the history
Add fallback intent handler
  • Loading branch information
maxbeckers authored Feb 16, 2023
2 parents 22277aa + ff1ceef commit 792d70d
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="test/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="test/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache">
<coverage>
<include>
<directory suffix=".php">./src</directory>
Expand Down
54 changes: 54 additions & 0 deletions src/RequestHandler/Basic/FallbackRequestHandler.php
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);
}
}
14 changes: 7 additions & 7 deletions test/Test/Helper/SsmlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
*/
class SsmlGeneratorTest extends TestCase
{
public static function getVoices()
{
return array_map(function ($item) {
return [$item];
}, SsmlGenerator::VOICES);
}

public function testClear()
{
$ssmlGenerator = new SsmlGenerator();
Expand Down Expand Up @@ -231,13 +238,6 @@ public function testSayWithVoiceInvalid()
$ssmlGenerator->sayWithVoice('invalid', 'Just a Test');
}

public function getVoices()
{
return array_map(function ($item) {
return [$item];
}, SsmlGenerator::VOICES);
}

/**
* @dataProvider getVoices
*/
Expand Down
1 change: 1 addition & 0 deletions test/Test/Request/InputHandlerEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class InputHandlerEventTest extends TestCase
/**
* @throws \MaxBeckers\AmazonAlexa\Exception\MissingRequestDataException
* @throws \MaxBeckers\AmazonAlexa\Exception\MissingRequiredHeaderException
*
* @covers \MaxBeckers\AmazonAlexa\Request\Request\GameEngine\InputHandlerEvent::fromAmazonRequest
* @covers \MaxBeckers\AmazonAlexa\Request\Request\GameEngine\Event\Event::fromAmazonRequest
* @covers \MaxBeckers\AmazonAlexa\Request\Request\GameEngine\Event\InputEvent::fromAmazonRequest
Expand Down
3 changes: 2 additions & 1 deletion test/Test/RequestHandler/Basic/CancelRequestHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic;
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic;

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\CancelRequestHandler;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic;
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic;

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\ExceptionEncounteredRequestHandler;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
use PHPUnit\Framework\TestCase;
Expand Down
43 changes: 43 additions & 0 deletions test/Test/RequestHandler/Basic/FallbackRequestHandlerTest.php
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));
}
}
3 changes: 2 additions & 1 deletion test/Test/RequestHandler/Basic/HelpRequestHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic;
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic;

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\HelpRequestHandler;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic;
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic;

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\NavigateHomeRequestHandler;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic;
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic;

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\SessionEndedRequestHandler;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
use PHPUnit\Framework\TestCase;
Expand Down
3 changes: 2 additions & 1 deletion test/Test/RequestHandler/Basic/StopRequestHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace MaxBeckers\AmazonAlexa\RequestHandler\Basic;
namespace MaxBeckers\AmazonAlexa\Test\RequestHandler\Basic;

use MaxBeckers\AmazonAlexa\Helper\ResponseHelper;
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\RequestHandler\Basic\StopRequestHandler;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit 792d70d

Please sign in to comment.