You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@predictionio.apache.org by do...@apache.org on 2018/09/12 18:32:29 UTC
[2/3] predictionio-sdk-php git commit: PSR-2
PSR-2
Project: http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/repo
Commit: http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/commit/2136b2ea
Tree: http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/tree/2136b2ea
Diff: http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/diff/2136b2ea
Branch: refs/heads/PIO-163
Commit: 2136b2ea5f23b040f135af8e0142ceaaf1e267e3
Parents: 8ceca26
Author: Donald Szeto <do...@apache.org>
Authored: Wed Sep 12 10:56:29 2018 -0700
Committer: Donald Szeto <do...@apache.org>
Committed: Wed Sep 12 10:56:29 2018 -0700
----------------------------------------------------------------------
examples/ItemrankImport.php | 21 +-
examples/ItemrankQuery.php | 6 +-
examples/SampleEvent.php | 123 +++---
src/predictionio/BaseClient.php | 96 ++---
src/predictionio/EngineClient.php | 41 +-
src/predictionio/EventClient.php | 490 +++++++++++----------
src/predictionio/Exporter.php | 22 +-
src/predictionio/FileExporter.php | 13 +-
src/predictionio/PredictionIOAPIError.php | 6 +-
tests/Unit/EngineClientTest.php | 63 ++-
tests/Unit/EventClientTest.php | 564 +++++++++++++------------
tests/Unit/ExporterTest.php | 43 +-
tests/Unit/FileExporterTest.php | 30 +-
13 files changed, 802 insertions(+), 716 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/examples/ItemrankImport.php
----------------------------------------------------------------------
diff --git a/examples/ItemrankImport.php b/examples/ItemrankImport.php
index 6d1c551..4bdd0e5 100644
--- a/examples/ItemrankImport.php
+++ b/examples/ItemrankImport.php
@@ -26,24 +26,21 @@ echo($response);
// set user - generate 10 users
for ($u=1; $u<=10; $u++) {
- $response=$client->setUser($u, array('age'=>20+$u, 'gender'=>'M'));
- print_r($response);
+ $response=$client->setUser($u, array('age'=>20+$u, 'gender'=>'M'));
+ print_r($response);
}
// set item - generate 50 items
for ($i=1; $i<=50; $i++) {
- $response=$client->setItem($i, array('itypes'=>array('1')));
- print_r($response);
+ $response=$client->setItem($i, array('itypes'=>array('1')));
+ print_r($response);
}
// record event - each user randomly views 10 items
for ($u=1; $u<=10; $u++) {
- for ($count=0; $count<10; $count++) {
- $i = rand(1,50);
- $response=$client->recordUserActionOnItem('view', $u, $i);
- print_r($response);
- }
+ for ($count=0; $count<10; $count++) {
+ $i = rand(1, 50);
+ $response=$client->recordUserActionOnItem('view', $u, $i);
+ print_r($response);
+ }
}
-
-
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/examples/ItemrankQuery.php
----------------------------------------------------------------------
diff --git a/examples/ItemrankQuery.php b/examples/ItemrankQuery.php
index 7ad60ae..0598a51 100644
--- a/examples/ItemrankQuery.php
+++ b/examples/ItemrankQuery.php
@@ -25,8 +25,6 @@ $response=$client->getStatus();
// Rank item 1 to 5 for each user
for ($i=1; $i<=10; $i++) {
- $response=$client->sendQuery(array('uid'=>$i, 'iids'=>array(1,2,3,4,5)));
- print_r($response);
+ $response=$client->sendQuery(array('uid'=>$i, 'iids'=>array(1,2,3,4,5)));
+ print_r($response);
}
-
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/examples/SampleEvent.php
----------------------------------------------------------------------
diff --git a/examples/SampleEvent.php b/examples/SampleEvent.php
index 01b4a6a..f813a45 100644
--- a/examples/SampleEvent.php
+++ b/examples/SampleEvent.php
@@ -21,64 +21,69 @@ use predictionio\EventClient;
use predictionio\PredictionIOAPIError;
try {
- // check Event Server status
- $client = new EventClient(
- "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O");
- $response=$client->getStatus();
- echo($response);
-
- // set user with event time
- $response=$client->setUser(9, array('age'=>10),
- '2014-01-01T10:20:30.400+08:00');
- print_r($response);
-
- // set user
- $response=$client->setUser(8, array('age'=>20, 'gender'=>'M'));
- print_r($response);
-
- // unset user
- $response=$client->unsetUser(8, array('age'=>20));
- print_r($response);
-
- // delete user
- $response=$client->deleteUser(9);
- print_r($response);
-
- // set item with event time
- $response=$client->setItem(3, array('itypes'=>array('1')),
- '2013-12-20T05:15:25.350+08:00');
- print_r($response);
-
- // set item
- $response=$client->setItem(2, array('itypes'=>array('1')));
- print_r($response);
-
- // unset item
- $response=$client->unsetItem(2, array('itypes'=>array('1')));
- print_r($response);
-
- // delete item
- $response=$client->deleteItem(3, '2000-01-01T01:01:01.001+01:00');
- print_r($response);
-
- // record user action on item
- $response=$client->recordUserActionOnItem('view', 8, 2);
- print_r($response);
-
- // create event
- $response=$client->createEvent(array(
- 'event' => 'my_event',
- 'entityType' => 'user',
- 'entityId' => '8',
- 'properties' => array('prop1'=>1, 'prop2'=>2),
- ));
- print_r($response);
-
- // get event
- $response=$client->getEvent('U_7eotSbeeK0BwshqEfRFAAAAUm-8gOyjP3FR73aBFo');
- print_r($response);
-
+ // check Event Server status
+ $client = new EventClient(
+ "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O"
+ );
+ $response=$client->getStatus();
+ echo($response);
+
+ // set user with event time
+ $response=$client->setUser(
+ 9,
+ array('age'=>10),
+ '2014-01-01T10:20:30.400+08:00'
+ );
+ print_r($response);
+
+ // set user
+ $response=$client->setUser(8, array('age'=>20, 'gender'=>'M'));
+ print_r($response);
+
+ // unset user
+ $response=$client->unsetUser(8, array('age'=>20));
+ print_r($response);
+
+ // delete user
+ $response=$client->deleteUser(9);
+ print_r($response);
+
+ // set item with event time
+ $response=$client->setItem(
+ 3,
+ array('itypes'=>array('1')),
+ '2013-12-20T05:15:25.350+08:00'
+ );
+ print_r($response);
+
+ // set item
+ $response=$client->setItem(2, array('itypes'=>array('1')));
+ print_r($response);
+
+ // unset item
+ $response=$client->unsetItem(2, array('itypes'=>array('1')));
+ print_r($response);
+
+ // delete item
+ $response=$client->deleteItem(3, '2000-01-01T01:01:01.001+01:00');
+ print_r($response);
+
+ // record user action on item
+ $response=$client->recordUserActionOnItem('view', 8, 2);
+ print_r($response);
+
+ // create event
+ $response=$client->createEvent(array(
+ 'event' => 'my_event',
+ 'entityType' => 'user',
+ 'entityId' => '8',
+ 'properties' => array('prop1'=>1, 'prop2'=>2),
+ ));
+ print_r($response);
+
+ // get event
+ $response=$client->getEvent('U_7eotSbeeK0BwshqEfRFAAAAUm-8gOyjP3FR73aBFo');
+ print_r($response);
} catch (PredictionIOAPIError $e) {
- echo $e->getMessage();
+ echo $e->getMessage();
}
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/src/predictionio/BaseClient.php
----------------------------------------------------------------------
diff --git a/src/predictionio/BaseClient.php b/src/predictionio/BaseClient.php
index 877ef44..83da150 100644
--- a/src/predictionio/BaseClient.php
+++ b/src/predictionio/BaseClient.php
@@ -24,59 +24,61 @@ use GuzzleHttp\Exception\ClientException;
* Base client for Event and Engine client
*
*/
-abstract class BaseClient {
- private $baseUrl;
- public $client;
+abstract class BaseClient
+{
+ private $baseUrl;
+ public $client;
- /**
- * @param string $baseUrl Base URL to the server
- * @param float $timeout Timeout of the request in seconds. Use 0 to wait indefinitely
- * @param float $connectTimeout Number of seconds to wait while trying to connect to a server
- */
- public function __construct($baseUrl, $timeout, $connectTimeout) {
- $this->baseUrl = $baseUrl;
- $this->client = new Client([
+ /**
+ * @param string $baseUrl Base URL to the server
+ * @param float $timeout Timeout of the request in seconds. Use 0 to wait indefinitely
+ * @param float $connectTimeout Number of seconds to wait while trying to connect to a server
+ */
+ public function __construct($baseUrl, $timeout, $connectTimeout)
+ {
+ $this->baseUrl = $baseUrl;
+ $this->client = new Client([
'base_uri' => $this->baseUrl,
'timeout' => $timeout,
'connect_timeout' => $connectTimeout
- ]);
-
- }
+ ]);
+ }
- /**
- * Get the status of the Event Server or Engine Instance
- *
- * @return string status
- */
- public function getStatus() {
- return $this->client->get('/')->getBody();
- }
+ /**
+ * Get the status of the Event Server or Engine Instance
+ *
+ * @return string status
+ */
+ public function getStatus()
+ {
+ return $this->client->get('/')->getBody();
+ }
- /**
- * Send a HTTP request to the server
- *
- * @param string $method HTTP request method
- * @param string $url Relative or absolute url
- * @param string $body HTTP request body
- *
- * @return array JSON response
- * @throws PredictionIOAPIError Request error
- */
- protected function sendRequest($method, $url, $body) {
- $options = [
- 'headers' => [
- 'Accept-Encoding' => 'gzip',
- 'Content-Type' => 'application/json',
- ],
- 'body' => $body,
- ];
+ /**
+ * Send a HTTP request to the server
+ *
+ * @param string $method HTTP request method
+ * @param string $url Relative or absolute url
+ * @param string $body HTTP request body
+ *
+ * @return array JSON response
+ * @throws PredictionIOAPIError Request error
+ */
+ protected function sendRequest($method, $url, $body)
+ {
+ $options = [
+ 'headers' => [
+ 'Accept-Encoding' => 'gzip',
+ 'Content-Type' => 'application/json',
+ ],
+ 'body' => $body,
+ ];
- try {
- $response = $this->client->request($method, $url, $options);
- return json_decode($response->getBody(), true);
- } catch (ClientException $e) {
- throw new PredictionIOAPIError($e->getMessage());
+ try {
+ $response = $this->client->request($method, $url, $options);
+ return json_decode($response->getBody(), true);
+ } catch (ClientException $e) {
+ throw new PredictionIOAPIError($e->getMessage());
+ }
}
- }
}
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/src/predictionio/EngineClient.php
----------------------------------------------------------------------
diff --git a/src/predictionio/EngineClient.php b/src/predictionio/EngineClient.php
index 258db0f..7355937 100644
--- a/src/predictionio/EngineClient.php
+++ b/src/predictionio/EngineClient.php
@@ -21,7 +21,8 @@ namespace predictionio;
* Client for connecting to an Engine Instance
*
*/
-class EngineClient extends BaseClient {
+class EngineClient extends BaseClient
+{
/**
* @param string Base URL to the Engine Instance. Default is localhost:8000.
@@ -30,23 +31,25 @@ class EngineClient extends BaseClient {
* @param float Number of seconds to wait while trying to connect to a server.
* Default is 5.
*/
- public function __construct($baseUrl="http://localhost:8000",
- $timeout=0, $connectTimeout=5 ) {
- parent::__construct($baseUrl, $timeout, $connectTimeout);
- }
+ public function __construct(
+ $baseUrl="http://localhost:8000",
+ $timeout=0,
+ $connectTimeout=5
+ ) {
+ parent::__construct($baseUrl, $timeout, $connectTimeout);
+ }
- /**
- * Send prediction query to an Engine Instance
- *
- * @param array Query
- *
- * @return array JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function sendQuery(array $query) {
- return $this->sendRequest("POST", "/queries.json", json_encode($query));
- }
+ /**
+ * Send prediction query to an Engine Instance
+ *
+ * @param array Query
+ *
+ * @return array JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function sendQuery(array $query)
+ {
+ return $this->sendRequest("POST", "/queries.json", json_encode($query));
+ }
}
-
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/src/predictionio/EventClient.php
----------------------------------------------------------------------
diff --git a/src/predictionio/EventClient.php b/src/predictionio/EventClient.php
index b8eb1e2..7a31bc7 100644
--- a/src/predictionio/EventClient.php
+++ b/src/predictionio/EventClient.php
@@ -16,6 +16,7 @@
*/
namespace predictionio;
+
use GuzzleHttp\Client;
use \DateTime;
@@ -23,260 +24,287 @@ use \DateTime;
* Client for connecting to an Event Server
*
*/
-class EventClient extends BaseClient {
- const DATE_TIME_FORMAT = DateTime::ISO8601;
- private $accessKey;
- private $eventUrl;
-
- /**
- * @param string Access Key
- * @param string Base URL to the Event Server. Default is localhost:7070.
- * @param float Timeout of the request in seconds. Use 0 to wait indefinitely
- * Default is 0.
- * @param float Number of seconds to wait while trying to connect to a server.
- * Default is 5.
- */
- public function __construct($accessKey, $baseUrl='http://localhost:7070',
- $timeout=0, $connectTimeout=5 ) {
- parent::__construct($baseUrl, $timeout, $connectTimeout);
- $this->accessKey = $accessKey;
- $this->eventUrl = "/events.json?accessKey=$this->accessKey";
- }
+class EventClient extends BaseClient
+{
+ const DATE_TIME_FORMAT = DateTime::ISO8601;
+ private $accessKey;
+ private $eventUrl;
- private function getEventTime($eventTime) {
- $result = $eventTime;
- if (!isset($eventTime)) {
- $result = (new DateTime('NOW'))->format(self::DATE_TIME_FORMAT);
+ /**
+ * @param string Access Key
+ * @param string Base URL to the Event Server. Default is localhost:7070.
+ * @param float Timeout of the request in seconds. Use 0 to wait indefinitely
+ * Default is 0.
+ * @param float Number of seconds to wait while trying to connect to a server.
+ * Default is 5.
+ */
+ public function __construct(
+ $accessKey,
+ $baseUrl='http://localhost:7070',
+ $timeout=0,
+ $connectTimeout=5
+ ) {
+ parent::__construct($baseUrl, $timeout, $connectTimeout);
+ $this->accessKey = $accessKey;
+ $this->eventUrl = "/events.json?accessKey=$this->accessKey";
}
- return $result;
- }
+ private function getEventTime($eventTime)
+ {
+ $result = $eventTime;
+ if (!isset($eventTime)) {
+ $result = (new DateTime('NOW'))->format(self::DATE_TIME_FORMAT);
+ }
- /**
- * Set a user entity
- *
- * @param int|string User Id
- * @param array Properties of the user entity to set
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function setUser($uid, array $properties=array(), $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
+ return $result;
+ }
- // casting to object so that an empty array would be represented as {}
- if (empty($properties)) $properties = (object)$properties;
+ /**
+ * Set a user entity
+ *
+ * @param int|string User Id
+ * @param array Properties of the user entity to set
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function setUser($uid, array $properties=array(), $eventTime=null)
+ {
+ $eventTime = $this->getEventTime($eventTime);
- $json = json_encode([
- 'event' => '$set',
- 'entityType' => 'user',
- 'entityId' => $uid,
- 'properties' => $properties,
- 'eventTime' => $eventTime,
- ]);
+ // casting to object so that an empty array would be represented as {}
+ if (empty($properties)) {
+ $properties = (object)$properties;
+ }
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ $json = json_encode([
+ 'event' => '$set',
+ 'entityType' => 'user',
+ 'entityId' => $uid,
+ 'properties' => $properties,
+ 'eventTime' => $eventTime,
+ ]);
- /**
- * Unset a user entity
- *
- * @param int|string User Id
- * @param array Properties of the user entity to unset
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function unsetUser($uid, array $properties, $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
- if (empty($properties))
- throw new PredictionIOAPIError('Specify at least one property');
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- $json = json_encode([
- 'event' => '$unset',
- 'entityType' => 'user',
- 'entityId' => $uid,
- 'properties' => $properties,
- 'eventTime' => $eventTime,
- ]);
+ /**
+ * Unset a user entity
+ *
+ * @param int|string User Id
+ * @param array Properties of the user entity to unset
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function unsetUser($uid, array $properties, $eventTime=null)
+ {
+ $eventTime = $this->getEventTime($eventTime);
+ if (empty($properties)) {
+ throw new PredictionIOAPIError('Specify at least one property');
+ }
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ $json = json_encode([
+ 'event' => '$unset',
+ 'entityType' => 'user',
+ 'entityId' => $uid,
+ 'properties' => $properties,
+ 'eventTime' => $eventTime,
+ ]);
- /**
- * Delete a user entity
- *
- * @param int|string User Id
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function deleteUser($uid, $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
+
+ /**
+ * Delete a user entity
+ *
+ * @param int|string User Id
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function deleteUser($uid, $eventTime=null)
+ {
+ $eventTime = $this->getEventTime($eventTime);
- $json = json_encode([
- 'event' => '$delete',
- 'entityType' => 'user',
- 'entityId' => $uid,
- 'eventTime' => $eventTime,
- ]);
+ $json = json_encode([
+ 'event' => '$delete',
+ 'entityType' => 'user',
+ 'entityId' => $uid,
+ 'eventTime' => $eventTime,
+ ]);
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- /**
- * Set an item entity
- *
- * @param int|string Item Id
- * @param array Properties of the item entity to set
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function setItem($iid, array $properties=array(), $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
- if (empty($properties)) $properties = (object)$properties;
- $json = json_encode([
- 'event' => '$set',
- 'entityType' => 'item',
- 'entityId' => $iid,
- 'properties' => $properties,
- 'eventTime' => $eventTime,
- ]);
+ /**
+ * Set an item entity
+ *
+ * @param int|string Item Id
+ * @param array Properties of the item entity to set
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function setItem($iid, array $properties=array(), $eventTime=null)
+ {
+ $eventTime = $this->getEventTime($eventTime);
+ if (empty($properties)) {
+ $properties = (object)$properties;
+ }
+ $json = json_encode([
+ 'event' => '$set',
+ 'entityType' => 'item',
+ 'entityId' => $iid,
+ 'properties' => $properties,
+ 'eventTime' => $eventTime,
+ ]);
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- /**
- * Unset an item entity
- *
- * @param int|string Item Id
- * @param array Properties of the item entity to unset
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function unsetItem($iid, array $properties, $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
- if (empty($properties))
- throw new PredictionIOAPIError('Specify at least one property');
- $json = json_encode([
- 'event' => '$unset',
- 'entityType' => 'item',
- 'entityId' => $iid,
- 'properties' => $properties,
- 'eventTime' => $eventTime,
- ]);
+ /**
+ * Unset an item entity
+ *
+ * @param int|string Item Id
+ * @param array Properties of the item entity to unset
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function unsetItem($iid, array $properties, $eventTime=null)
+ {
+ $eventTime = $this->getEventTime($eventTime);
+ if (empty($properties)) {
+ throw new PredictionIOAPIError('Specify at least one property');
+ }
+ $json = json_encode([
+ 'event' => '$unset',
+ 'entityType' => 'item',
+ 'entityId' => $iid,
+ 'properties' => $properties,
+ 'eventTime' => $eventTime,
+ ]);
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- /**
- * Delete an item entity
- *
- * @param int|string Item Id
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function deleteItem($iid, $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
+ /**
+ * Delete an item entity
+ *
+ * @param int|string Item Id
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function deleteItem($iid, $eventTime=null)
+ {
+ $eventTime = $this->getEventTime($eventTime);
- $json = json_encode([
- 'event' => '$delete',
- 'entityType' => 'item',
- 'entityId' => $iid,
- 'eventTime' => $eventTime,
- ]);
+ $json = json_encode([
+ 'event' => '$delete',
+ 'entityType' => 'item',
+ 'entityId' => $iid,
+ 'eventTime' => $eventTime,
+ ]);
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- /**
- * Record a user action on an item
- *
- * @param string Event name
- * @param int|string User Id
- * @param int|string Item Id
- * @param array Properties of the event
- * @param string Time of the event in ISO 8601 format
- * (e.g. 2014-09-09T16:17:42.937-08:00).
- * Default is the current time.
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function recordUserActionOnItem($event, $uid, $iid,
- array $properties=array(),
- $eventTime=null) {
- $eventTime = $this->getEventTime($eventTime);
- if (empty($properties)) $properties = (object)$properties;
- $json = json_encode([
- 'event' => $event,
- 'entityType' => 'user',
- 'entityId' => $uid,
- 'targetEntityType' => 'item',
- 'targetEntityId' => $iid,
- 'properties' => $properties,
- 'eventTime' => $eventTime,
- ]);
+ /**
+ * Record a user action on an item
+ *
+ * @param string Event name
+ * @param int|string User Id
+ * @param int|string Item Id
+ * @param array Properties of the event
+ * @param string Time of the event in ISO 8601 format
+ * (e.g. 2014-09-09T16:17:42.937-08:00).
+ * Default is the current time.
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function recordUserActionOnItem(
+ $event,
+ $uid,
+ $iid,
+ array $properties=array(),
+ $eventTime=null
+ ) {
+ $eventTime = $this->getEventTime($eventTime);
+ if (empty($properties)) {
+ $properties = (object)$properties;
+ }
+ $json = json_encode([
+ 'event' => $event,
+ 'entityType' => 'user',
+ 'entityId' => $uid,
+ 'targetEntityType' => 'item',
+ 'targetEntityId' => $iid,
+ 'properties' => $properties,
+ 'eventTime' => $eventTime,
+ ]);
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- /**
- * Create an event
- *
- * @param array An array describing the event
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function createEvent(array $data) {
- $json = json_encode($data);
+ /**
+ * Create an event
+ *
+ * @param array An array describing the event
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function createEvent(array $data)
+ {
+ $json = json_encode($data);
- return $this->sendRequest('POST', $this->eventUrl, $json);
- }
+ return $this->sendRequest('POST', $this->eventUrl, $json);
+ }
- /**
- * Retrieve an event
- *
- * @param string Event ID
- *
- * @return string JSON response
- *
- * @throws PredictionIOAPIError Request error
- */
- public function getEvent($eventId) {
- return $this->sendRequest('GET',
- "/events/$eventId.json?accessKey=$this->accessKey", '');
- }
+ /**
+ * Retrieve an event
+ *
+ * @param string Event ID
+ *
+ * @return string JSON response
+ *
+ * @throws PredictionIOAPIError Request error
+ */
+ public function getEvent($eventId)
+ {
+ return $this->sendRequest(
+ 'GET',
+ "/events/$eventId.json?accessKey=$this->accessKey",
+ ''
+ );
+ }
}
-
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/src/predictionio/Exporter.php
----------------------------------------------------------------------
diff --git a/src/predictionio/Exporter.php b/src/predictionio/Exporter.php
index 3924a9b..c965dc7 100644
--- a/src/predictionio/Exporter.php
+++ b/src/predictionio/Exporter.php
@@ -17,12 +17,12 @@
namespace predictionio;
-
-trait Exporter {
-
+trait Exporter
+{
abstract public function export($json);
- public function jsonEncode($data) {
+ public function jsonEncode($data)
+ {
return json_encode($data);
}
@@ -39,10 +39,15 @@ trait Exporter {
* @param array $properties
* @param $eventTime
*/
- public function createEvent($event, $entityType, $entityId,
- $targetEntityType=null, $targetEntityId=null, array $properties=null,
- $eventTime=null) {
-
+ public function createEvent(
+ $event,
+ $entityType,
+ $entityId,
+ $targetEntityType=null,
+ $targetEntityId=null,
+ array $properties=null,
+ $eventTime=null
+ ) {
if (!isset($eventTime)) {
$eventTime = new \DateTime();
} elseif (!($eventTime instanceof \DateTime)) {
@@ -73,5 +78,4 @@ trait Exporter {
$this->export($json);
}
-
}
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/src/predictionio/FileExporter.php
----------------------------------------------------------------------
diff --git a/src/predictionio/FileExporter.php b/src/predictionio/FileExporter.php
index 227e649..8e820dc 100644
--- a/src/predictionio/FileExporter.php
+++ b/src/predictionio/FileExporter.php
@@ -22,21 +22,24 @@ namespace predictionio;
*
* @package predictionio
*/
-class FileExporter {
-
+class FileExporter
+{
use Exporter;
private $file;
- public function __construct($fileName) {
+ public function __construct($fileName)
+ {
$this->file = fopen($fileName, 'w');
}
- public function export($json) {
+ public function export($json)
+ {
fwrite($this->file, "$json\n");
}
- public function close() {
+ public function close()
+ {
fclose($this->file);
}
}
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/src/predictionio/PredictionIOAPIError.php
----------------------------------------------------------------------
diff --git a/src/predictionio/PredictionIOAPIError.php b/src/predictionio/PredictionIOAPIError.php
index a65d6a9..b51414f 100644
--- a/src/predictionio/PredictionIOAPIError.php
+++ b/src/predictionio/PredictionIOAPIError.php
@@ -16,10 +16,10 @@
*/
namespace predictionio;
+
/**
* Thrown when there is an error with the request.
*/
-class PredictionIOAPIError extends \Exception {
-
+class PredictionIOAPIError extends \Exception
+{
}
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/tests/Unit/EngineClientTest.php
----------------------------------------------------------------------
diff --git a/tests/Unit/EngineClientTest.php b/tests/Unit/EngineClientTest.php
index a389576..2f5fbbf 100644
--- a/tests/Unit/EngineClientTest.php
+++ b/tests/Unit/EngineClientTest.php
@@ -25,36 +25,35 @@ use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
-
-class EngineClientTest extends \PHPUnit_Framework_TestCase {
- /** @var EngineClient $engineClient */
- protected $engineClient;
- protected $container = [];
-
- protected function setUp() {
- $history=Middleware::History($this->container);
- $mock=new MockHandler([new Response(200)]);
- $handler=HandlerStack::create($mock);
- $handler->push($history);
- $this->engineClient=new EngineClient();
- $existingOptions = $this->engineClient->client->getConfig();
- $existingOptions['handler'] = $handler;
- $mockClient=new Client($existingOptions);
- $this->engineClient->client = $mockClient;
- }
-
- public function testSendQuery() {
- $this->engineClient->sendQuery(array('uid'=>5, 'iids'=>array(1,2,3)));
- $result=array_shift($this->container);
- /** @var Request[] $result['request'] */
- $body=json_decode($result['request']->getBody(), true);
-
- $this->assertEquals(5,$body['uid']);
- $this->assertEquals(array(1,2,3),$body['iids']);
- $this->assertEquals('POST',$result['request']->getMethod());
- $this->assertEquals('http://localhost:8000/queries.json',$result['request']->getUri());
- }
-
+class EngineClientTest extends \PHPUnit_Framework_TestCase
+{
+ /** @var EngineClient $engineClient */
+ protected $engineClient;
+ protected $container = [];
+
+ protected function setUp()
+ {
+ $history=Middleware::History($this->container);
+ $mock=new MockHandler([new Response(200)]);
+ $handler=HandlerStack::create($mock);
+ $handler->push($history);
+ $this->engineClient=new EngineClient();
+ $existingOptions = $this->engineClient->client->getConfig();
+ $existingOptions['handler'] = $handler;
+ $mockClient=new Client($existingOptions);
+ $this->engineClient->client = $mockClient;
+ }
+
+ public function testSendQuery()
+ {
+ $this->engineClient->sendQuery(array('uid'=>5, 'iids'=>array(1,2,3)));
+ $result=array_shift($this->container);
+ /** @var Request[] $result['request'] */
+ $body=json_decode($result['request']->getBody(), true);
+
+ $this->assertEquals(5, $body['uid']);
+ $this->assertEquals(array(1,2,3), $body['iids']);
+ $this->assertEquals('POST', $result['request']->getMethod());
+ $this->assertEquals('http://localhost:8000/queries.json', $result['request']->getUri());
+ }
}
-
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/tests/Unit/EventClientTest.php
----------------------------------------------------------------------
diff --git a/tests/Unit/EventClientTest.php b/tests/Unit/EventClientTest.php
index 7f3319e..6c442ea 100644
--- a/tests/Unit/EventClientTest.php
+++ b/tests/Unit/EventClientTest.php
@@ -25,243 +25,263 @@ use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use predictionio\EventClient;
-class EventClientTest extends \PHPUnit_Framework_TestCase {
- /** @var EventClient $eventClient */
- protected $eventClient;
- protected $container = [];
-
- protected function setUp() {
- $history=Middleware::History($this->container);
- $mock=new MockHandler([new Response(200)]);
- $handler=HandlerStack::create($mock);
- $handler->push($history);
- $this->eventClient=new EventClient(
- "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O");
- $existingOptions = $this->eventClient->client->getConfig();
- $existingOptions['handler'] = $handler;
- $mockClient=new Client($existingOptions);
- $this->eventClient->client = $mockClient;
- }
-
- public function testSetUser() {
- $this->eventClient->setUser(1,array('age'=>20));
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$set',$body['event']);
- $this->assertEquals('user',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertEquals(20,$body['properties']['age']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testSetUserWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->setUser(1,array('age'=>20), $eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$set',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- public function testUnsetUser() {
- $this->eventClient->unsetUser(1,array('age'=>20));
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$unset',$body['event']);
- $this->assertEquals('user',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertEquals(20,$body['properties']['age']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testUnsetUserWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->unsetUser(1,array('age'=>20), $eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$unset',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- /**
- * @expectedException \predictionio\PredictionIOAPIError
- */
- public function testUnsetUserWithoutProperties() {
- $this->eventClient->unsetUser(1, array());
- }
-
- public function testDeleteUser() {
- $this->eventClient->deleteUser(1);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$delete',$body['event']);
- $this->assertEquals('user',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testDeleteUserWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->deleteUser(1, $eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$delete',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- public function testSetItem() {
- $this->eventClient->setItem(1,array('type'=>'book'));
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$set',$body['event']);
- $this->assertEquals('item',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertEquals('book',$body['properties']['type']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testSetItemWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->setItem(1,array('type'=>'book'), $eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$set',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- public function testUnsetItem() {
- $this->eventClient->unsetItem(1,array('type'=>'book'));
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$unset',$body['event']);
- $this->assertEquals('item',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertEquals('book',$body['properties']['type']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testUnsetItemWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->unsetItem(1,array('type'=>'book'), $eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$unset',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- /**
- * @expectedException \predictionio\PredictionIOAPIError
- */
- public function testUnsetItemWithoutProperties() {
- $this->eventClient->unsetItem(1, array());
- }
-
- public function testDeleteItem() {
- $this->eventClient->deleteItem(1);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$delete',$body['event']);
- $this->assertEquals('item',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testDeleteItemWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->deleteItem(1, $eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('$delete',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- public function testRecordAction() {
- $this->eventClient->recordUserActionOnItem('view',1,888, array('count'=>2));
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('view',$body['event']);
- $this->assertEquals('user',$body['entityType']);
- $this->assertEquals(1,$body['entityId']);
- $this->assertEquals('item',$body['targetEntityType']);
- $this->assertEquals(888,$body['targetEntityId']);
- $this->assertEquals(2,$body['properties']['count']);
- $this->assertNotNull($body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testRecordActionWithEventTime() {
- $eventTime='1982-09-25T01:23:45+0800';
-
- $this->eventClient->recordUserActionOnItem('view',1, 8, array(),$eventTime);
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('view',$body['event']);
- $this->assertEquals($eventTime, $body['eventTime']);
- }
-
- public function testCreateEvent() {
- $this->eventClient->createEvent(array(
+class EventClientTest extends \PHPUnit_Framework_TestCase
+{
+ /** @var EventClient $eventClient */
+ protected $eventClient;
+ protected $container = [];
+
+ protected function setUp()
+ {
+ $history=Middleware::History($this->container);
+ $mock=new MockHandler([new Response(200)]);
+ $handler=HandlerStack::create($mock);
+ $handler->push($history);
+ $this->eventClient=new EventClient(
+ "j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O"
+ );
+ $existingOptions = $this->eventClient->client->getConfig();
+ $existingOptions['handler'] = $handler;
+ $mockClient=new Client($existingOptions);
+ $this->eventClient->client = $mockClient;
+ }
+
+ public function testSetUser()
+ {
+ $this->eventClient->setUser(1, array('age'=>20));
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$set', $body['event']);
+ $this->assertEquals('user', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertEquals(20, $body['properties']['age']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testSetUserWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->setUser(1, array('age'=>20), $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$set', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ public function testUnsetUser()
+ {
+ $this->eventClient->unsetUser(1, array('age'=>20));
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$unset', $body['event']);
+ $this->assertEquals('user', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertEquals(20, $body['properties']['age']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testUnsetUserWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->unsetUser(1, array('age'=>20), $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$unset', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ /**
+ * @expectedException \predictionio\PredictionIOAPIError
+ */
+ public function testUnsetUserWithoutProperties()
+ {
+ $this->eventClient->unsetUser(1, array());
+ }
+
+ public function testDeleteUser()
+ {
+ $this->eventClient->deleteUser(1);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$delete', $body['event']);
+ $this->assertEquals('user', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testDeleteUserWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->deleteUser(1, $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$delete', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ public function testSetItem()
+ {
+ $this->eventClient->setItem(1, array('type'=>'book'));
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$set', $body['event']);
+ $this->assertEquals('item', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertEquals('book', $body['properties']['type']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testSetItemWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->setItem(1, array('type'=>'book'), $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$set', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ public function testUnsetItem()
+ {
+ $this->eventClient->unsetItem(1, array('type'=>'book'));
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$unset', $body['event']);
+ $this->assertEquals('item', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertEquals('book', $body['properties']['type']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testUnsetItemWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->unsetItem(1, array('type'=>'book'), $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$unset', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ /**
+ * @expectedException \predictionio\PredictionIOAPIError
+ */
+ public function testUnsetItemWithoutProperties()
+ {
+ $this->eventClient->unsetItem(1, array());
+ }
+
+ public function testDeleteItem()
+ {
+ $this->eventClient->deleteItem(1);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$delete', $body['event']);
+ $this->assertEquals('item', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testDeleteItemWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->deleteItem(1, $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('$delete', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ public function testRecordAction()
+ {
+ $this->eventClient->recordUserActionOnItem('view', 1, 888, array('count'=>2));
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('view', $body['event']);
+ $this->assertEquals('user', $body['entityType']);
+ $this->assertEquals(1, $body['entityId']);
+ $this->assertEquals('item', $body['targetEntityType']);
+ $this->assertEquals(888, $body['targetEntityId']);
+ $this->assertEquals(2, $body['properties']['count']);
+ $this->assertNotNull($body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testRecordActionWithEventTime()
+ {
+ $eventTime='1982-09-25T01:23:45+0800';
+
+ $this->eventClient->recordUserActionOnItem('view', 1, 8, array(), $eventTime);
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('view', $body['event']);
+ $this->assertEquals($eventTime, $body['eventTime']);
+ }
+
+ public function testCreateEvent()
+ {
+ $this->eventClient->createEvent(array(
'event' => 'my_event',
'entityType' => 'user',
'entityId' => 'uid',
@@ -271,42 +291,40 @@ class EventClientTest extends \PHPUnit_Framework_TestCase {
'prop4'=>true,
'prop5'=>array('a','b','c'),
'prop6'=>4.56
- ),
+ ),
'eventTime' => '2004-12-13T21:39:45.618-07:00'
));
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('my_event',$body['event']);
- $this->assertEquals('user',$body['entityType']);
- $this->assertEquals('uid',$body['entityId']);
- $this->assertEquals(1,$body['properties']['prop1']);
- $this->assertEquals('value2',$body['properties']['prop2']);
- $this->assertEquals(array(1,2,3),$body['properties']['prop3']);
- $this->assertEquals(true,$body['properties']['prop4']);
- $this->assertEquals(array('a','b','c'),$body['properties']['prop5']);
- $this->assertEquals(4.56,$body['properties']['prop6']);
- $this->assertEquals('2004-12-13T21:39:45.618-07:00',$body['eventTime']);
- $this->assertEquals('POST',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',$request->getUri());
- }
-
- public function testGetEvent() {
- $this->eventClient->getEvent('event_id');
- $result=array_shift($this->container);
- /** @var Request $request */
- $request=$result['request'];
- $body=json_decode($request->getBody(), true);
-
- $this->assertEquals('GET',$request->getMethod());
- $this->assertEquals('http://localhost:7070/events/event_id.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',
- $request->getUri());
- }
-
-
-
-
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('my_event', $body['event']);
+ $this->assertEquals('user', $body['entityType']);
+ $this->assertEquals('uid', $body['entityId']);
+ $this->assertEquals(1, $body['properties']['prop1']);
+ $this->assertEquals('value2', $body['properties']['prop2']);
+ $this->assertEquals(array(1,2,3), $body['properties']['prop3']);
+ $this->assertEquals(true, $body['properties']['prop4']);
+ $this->assertEquals(array('a','b','c'), $body['properties']['prop5']);
+ $this->assertEquals(4.56, $body['properties']['prop6']);
+ $this->assertEquals('2004-12-13T21:39:45.618-07:00', $body['eventTime']);
+ $this->assertEquals('POST', $request->getMethod());
+ $this->assertEquals('http://localhost:7070/events.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O', $request->getUri());
+ }
+
+ public function testGetEvent()
+ {
+ $this->eventClient->getEvent('event_id');
+ $result=array_shift($this->container);
+ /** @var Request $request */
+ $request=$result['request'];
+ $body=json_decode($request->getBody(), true);
+
+ $this->assertEquals('GET', $request->getMethod());
+ $this->assertEquals(
+ 'http://localhost:7070/events/event_id.json?accessKey=j4jIdbq59JsF2f4CXwwkIiVHNFnyNvWXqMqXxcIbQDqFRz5K0fe9e3QfqjKwvW3O',
+ $request->getUri()
+ );
+ }
}
-?>
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/tests/Unit/ExporterTest.php
----------------------------------------------------------------------
diff --git a/tests/Unit/ExporterTest.php b/tests/Unit/ExporterTest.php
index bd36fe5..7f3690a 100644
--- a/tests/Unit/ExporterTest.php
+++ b/tests/Unit/ExporterTest.php
@@ -17,10 +17,10 @@
namespace predictionio\tests\Unit;
-
use predictionio\Exporter;
-class TestExporter {
+class TestExporter
+{
use Exporter {
jsonEncode as traitJsonEncode;
}
@@ -28,31 +28,37 @@ class TestExporter {
public $json;
public $data;
- public function __construct() {
+ public function __construct()
+ {
$this->json = [];
$this->data = [];
}
- public function jsonEncode($data) {
+ public function jsonEncode($data)
+ {
$this->data[] = $data;
return $this->traitJsonEncode($data);
}
- public function export($json) {
+ public function export($json)
+ {
$this->json[] = $json;
}
}
-class ExporterTest extends \PHPUnit_Framework_TestCase {
+class ExporterTest extends \PHPUnit_Framework_TestCase
+{
/** @var TestExporter $exporter */
private $exporter;
- protected function setUp() {
+ protected function setUp()
+ {
$this->exporter = new TestExporter();
}
- public function testTimeIsNow() {
+ public function testTimeIsNow()
+ {
$time = new \DateTime();
$this->exporter->createEvent('event', 'entity-type', 'entity-id');
@@ -71,7 +77,8 @@ class ExporterTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue(preg_match($pattern, $json) === 1, 'json');
}
- public function testTimeIsString() {
+ public function testTimeIsString()
+ {
$time = new \DateTime('2015-04-01');
$this->exporter->createEvent('event', 'entity-type', 'entity-id', null, null, null, '2015-04-01');
@@ -90,7 +97,8 @@ class ExporterTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue(preg_match($pattern, $json) === 1, 'json');
}
- public function testTimeIsDateTime() {
+ public function testTimeIsDateTime()
+ {
$time = new \DateTime('2015-04-01');
$this->exporter->createEvent('event', 'entity-type', 'entity-id', null, null, null, $time);
@@ -109,11 +117,19 @@ class ExporterTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue(preg_match($pattern, $json) === 1, 'json');
}
- public function testOptionalFields() {
+ public function testOptionalFields()
+ {
$time = new \DateTime('2015-04-01');
- $this->exporter->createEvent('event', 'entity-type', 'entity-id',
- 'target-entity-type', 'target-entity-id', ['property' => true], $time);
+ $this->exporter->createEvent(
+ 'event',
+ 'entity-type',
+ 'entity-id',
+ 'target-entity-type',
+ 'target-entity-id',
+ ['property' => true],
+ $time
+ );
$this->assertEquals(1, count($this->exporter->data));
$data = $this->exporter->data[0];
@@ -131,5 +147,4 @@ class ExporterTest extends \PHPUnit_Framework_TestCase {
$pattern = '/^{"event":"event","entityType":"entity-type","entityId":"entity-id","eventTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}","targetEntityType":"target-entity-type","targetEntityId":"target-entity-id","properties":{"property":true}}$/';
$this->assertTrue(preg_match($pattern, $json) === 1, 'json');
}
-
}
http://git-wip-us.apache.org/repos/asf/predictionio-sdk-php/blob/2136b2ea/tests/Unit/FileExporterTest.php
----------------------------------------------------------------------
diff --git a/tests/Unit/FileExporterTest.php b/tests/Unit/FileExporterTest.php
index a088816..f7f43cd 100644
--- a/tests/Unit/FileExporterTest.php
+++ b/tests/Unit/FileExporterTest.php
@@ -17,11 +17,10 @@
namespace predictionio\tests\Unit;
-
use predictionio\FileExporter;
-class FileExporterTest extends \PHPUnit_Framework_TestCase {
-
+class FileExporterTest extends \PHPUnit_Framework_TestCase
+{
public function setUp()
{
register_shutdown_function(function () {
@@ -31,12 +30,27 @@ class FileExporterTest extends \PHPUnit_Framework_TestCase {
});
}
- public function testExporter() {
+ public function testExporter()
+ {
$exporter = new FileExporter('temp.file');
- $exporter->createEvent('event-1', 'entity-type-1', 'entity-id-1',
- null, null, null, '2015-04-01');
- $exporter->createEvent('event-2', 'entity-type-2', 'entity-id-2',
- 'target-entity-type-2', 'target-entity-id-2', ['property' => 'blue'], '2015-04-01');
+ $exporter->createEvent(
+ 'event-1',
+ 'entity-type-1',
+ 'entity-id-1',
+ null,
+ null,
+ null,
+ '2015-04-01'
+ );
+ $exporter->createEvent(
+ 'event-2',
+ 'entity-type-2',
+ 'entity-id-2',
+ 'target-entity-type-2',
+ 'target-entity-id-2',
+ ['property' => 'blue'],
+ '2015-04-01'
+ );
$exporter->close();
$exported = file_get_contents('temp.file');