public function testFactoryValidSO()
 {
     $this->somock->shouldReceive('isValid')->once()->andReturn(true);
     $this->somock->shouldReceive('getVersion')->once()->andReturn('test version');
     $v1 = ['test attrs' => ['a' => '1', 'b' => '2']];
     $this->somock->shouldReceive('getAttributes')->once()->andReturn($v1);
     $m2 = Mockery::mock(ResponseSpeech::class);
     $this->somock->shouldReceive('getOutputSpeech')->once()->andReturn($m2);
     $m3 = Mockery::mock(ResponseSpeech::class);
     $this->somock->shouldReceive('getReprompt')->once()->andReturn($m3);
     $m4 = Mockery::mock(ResponseCard::class);
     $this->somock->shouldReceive('getCard')->once()->andReturn($m4);
     $this->somock->shouldReceive('isContinueSession')->once()->andReturn(true);
     $instance = Response::Factory($this->somock);
     $this->assertInstanceOf(Response::class, $instance);
     $r = new ReflectionClass(Response::class);
     $p0 = $r->getProperty('version');
     $p1 = $r->getProperty('sessionAttributes');
     $p2 = $r->getProperty('outputSpeech');
     $p3 = $r->getProperty('reprompt');
     $p4 = $r->getProperty('card');
     $p5 = $r->getProperty('endSession');
     $p0->setAccessible(true);
     $p1->setAccessible(true);
     $p2->setAccessible(true);
     $p3->setAccessible(true);
     $p4->setAccessible(true);
     $p5->setAccessible(true);
     $this->assertEquals('test version', $p0->getValue($instance));
     $this->assertSame($v1, $p1->getValue($instance));
     $this->assertSame($m2, $p2->getValue($instance));
     $this->assertSame($m3, $p3->getValue($instance));
     $this->assertSame($m4, $p4->getValue($instance));
     $this->assertFalse($p5->getValue($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);
 }