Skip to content

Commit

Permalink
Create tests for geolocation
Browse files Browse the repository at this point in the history
  • Loading branch information
devbanana committed Jun 12, 2019
1 parent a2ad521 commit cbc5418
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/Test/Request/GeolocationRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace MaxBeckers\AmazonAlexa\Test\Request;

use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest;
use MaxBeckers\AmazonAlexa\Response\Card;
use PHPUnit\Framework\TestCase;

/**
* @author Brandon Olivares <[email protected]>
*/
class GeolocationRequestTest extends TestCase
{
public function testGeolocationNeedsPermission()
{
$requestBody = file_get_contents(__DIR__.'/RequestData/geolocationPermission.json');
$request = Request::fromAmazonRequest($requestBody, 'https://s3.amazonaws.com/echo.api/echo-api-cert.pem', 'signature');
$this->assertInstanceOf(IntentRequest::class, $request->request);
$this->assertArrayHasKey('Geolocation', $request->context->system->device->supportedInterfaces);
$this->assertNull($request->context->geolocation);

// Create permissions card.
$card = Card::createAskForPermissionsConsent([Card::PERMISSION_GEOLOCATION]);
$this->assertInstanceOf(Card::class, $card);
}

public function testGetGeolocationData()
{
$requestBody = file_get_contents(__DIR__.'/RequestData/geolocation.json');
$request = Request::fromAmazonRequest($requestBody, 'https://s3.amazonaws.com/echo.api/echo-api-cert.pem', 'signature');
$this->assertInstanceOf(IntentRequest::class, $request->request);
$this->assertArrayHasKey('Geolocation', $request->context->system->device->supportedInterfaces);

$geolocation = $request->context->geolocation;
$this->assertNotNull($geolocation);

$this->assertNotNull($geolocation->locationServices);
$this->assertEquals('ENABLED', $geolocation->locationServices->access);
$this->assertEquals('RUNNING', $geolocation->locationServices->status);
$this->assertEquals(new \DateTime('2019-06-12T19:13:01+00:00'), $geolocation->timestamp);
$this->assertNotNull($geolocation->coordinate);
$this->assertEquals(40.3, $geolocation->coordinate->latitudeInDegrees);
$this->assertEquals(-78.9, $geolocation->coordinate->longitudeInDegrees);
$this->assertEquals(100, $geolocation->coordinate->accuracyInMeters);
$this->assertNotNull($geolocation->altitude);
$this->assertEquals(600, $geolocation->altitude->altitudeInMeters);
$this->assertEquals(100, $geolocation->altitude->accuracyInMeters);
$this->assertNotNull($geolocation->heading);
$this->assertEquals(48.12, $geolocation->heading->directionInDegrees);
$this->assertEquals(5, $geolocation->heading->accuracyInDegrees);
$this->assertNotNull($geolocation->speed);
$this->assertEquals(1, $geolocation->speed->speedInMetersPerSecond);
$this->assertEquals(1.0, $geolocation->speed->accuracyInMetersPerSecond);
}
}
92 changes: 92 additions & 0 deletions test/Test/Request/RequestData/geolocation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"version": "1.0",
"session": {
"new": true,
"sessionId": "sessionId",
"application": {
"applicationId": "applicationId"
},
"attributes": {
"string": {}
},
"user": {
"userId": "userId",
"permissions": {
"consentToken": "consentToken",
"scopes": {
"alexa::devices:all:geolocation:read": {
"status": "GRANTED"
}
}
},
"accessToken": "accessToken"
}
},
"context": {
"System": {
"application": {
"applicationId": "my-application-id"
},
"user": {
"userId": "userId",
"permissions": {
"consentToken": "consentToken",
"scopes": {
"alexa::devices:all:geolocation:read": {
"status": "GRANTED"
}
}
},
"accessToken": "accessToken"
},
"device": {
"deviceId": "deviceId",
"supportedInterfaces": {
"AudioPlayer": {},
"Geolocation": {}
}
},
"apiEndpoint": "apiEndpoint"
},
"AudioPlayer": {
"token": "token",
"offsetInMilliseconds": 0,
"playerActivity": "playerActivity"
},
"Geolocation": {
"locationServices": {
"access": "ENABLED",
"status": "RUNNING"
},
"timestamp": "2019-06-12T19:13:01+00:00",
"coordinate": {
"latitudeInDegrees": 40.3,
"longitudeInDegrees": -78.9,
"accuracyInMeters": 100.0
},
"altitude": {
"altitudeInMeters": 600.0,
"accuracyInMeters": 100.0
},
"heading": {
"directionInDegrees": 48.12,
"accuracyInDegrees": 5.0
},
"speed": {
"speedInMetersPerSecond": 1.0,
"accuracyInMetersPerSecond": 1.0
}
}
},
"request": {
"type": "IntentRequest",
"requestId": "requestId",
"timestamp": "now",
"dialogState": "dialogState",
"locale": "en-US",
"intent": {
"name": "name",
"confirmationStatus": "confirmationStatus"
}
}
}
58 changes: 58 additions & 0 deletions test/Test/Request/RequestData/geolocationPermission.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"version": "1.0",
"session": {
"new": true,
"sessionId": "sessionId",
"application": {
"applicationId": "applicationId"
},
"attributes": {
"string": {}
},
"user": {
"userId": "userId",
"permissions": {
"consentToken": "consentToken"
},
"accessToken": "accessToken"
}
},
"context": {
"System": {
"application": {
"applicationId": "my-application-id"
},
"user": {
"userId": "userId",
"permissions": {
"consentToken": "consentToken"
},
"accessToken": "accessToken"
},
"device": {
"deviceId": "deviceId",
"supportedInterfaces": {
"AudioPlayer": {},
"Geolocation": {}
}
},
"apiEndpoint": "apiEndpoint"
},
"AudioPlayer": {
"token": "token",
"offsetInMilliseconds": 0,
"playerActivity": "playerActivity"
}
},
"request": {
"type": "IntentRequest",
"requestId": "requestId",
"timestamp": "now",
"dialogState": "dialogState",
"locale": "en-US",
"intent": {
"name": "name",
"confirmationStatus": "confirmationStatus"
}
}
}

0 comments on commit cbc5418

Please sign in to comment.