public function testFactorySessionEnded()
 {
     $this->somock->shouldReceive('isValid')->once()->andReturn(true);
     $this->somock->shouldReceive('getHttpRequest')->once()->andReturn(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'session-ended-request.json'));
     $instance = Request::Factory($this->somock);
     $this->assertInstanceOf(\PaulJulio\SlimEcho\RequestSessionEnded::class, $instance);
 }
Exemplo n.º 2
0
 /**
  * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
  * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
  * @param  callable                                 $next     Next middleware
  *
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function __invoke($request, $response, $next)
 {
     $response = $next($request, $response);
     $body = $response->getBody();
     $requestSO = new SlimEcho\RequestSO();
     $requestSO->setHttpRequest($request->getBody());
     $echoRequest = SlimEcho\Request::Factory($requestSO);
     $settingsFN = __DIR__ . DIRECTORY_SEPARATOR . 'settings.ini';
     if (!file_exists($settingsFN)) {
         $body->offsetSet('error', 'Missing settings.ini');
         return $response->withBody($body)->withStatus(400);
     }
     $settingsSO = new SettingsSO();
     $settingsSO->addSettingsFileName($settingsFN);
     $settings = Settings::Factory($settingsSO);
     $appID = $echoRequest->getApplicationID();
     if (!isset($appID) || $appID !== $settings->id) {
         $body->offsetSet('error', 'Incorrect App ID');
         return $response->withBody($body)->withStatus(400);
     }
     $userID = $echoRequest->getUserID();
     if (!isset($userID)) {
         $body->offsetSet('error', 'No UserID found in request');
         return $response->withBody($body)->withStatus(400);
     }
     $user = $this->getUserRecord($userID);
     if ($echoRequest instanceof SlimEcho\RequestLaunch) {
         $echoResponseSO = $this->processLaunch($echoRequest, $user);
     } elseif ($echoRequest instanceof SlimEcho\RequestSessionEnded) {
         $echoResponseSO = $this->processEnded($echoRequest, $user);
     } elseif ($echoRequest instanceof SlimEcho\RequestIntent) {
         $echoResponseSO = $this->processIntent($echoRequest, $user);
     } else {
         $echoResponseSO = $this->processUnknown($echoRequest, $user);
     }
     $echoResponse = SlimEcho\Response::Factory($echoResponseSO);
     $echoResponse->writeToJsonStream($body);
     return $response->withBody($body)->withStatus(200);
 }