diff --git a/README.md b/README.md index cf22724..26e19b5 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ or ``` { "require": { - "recombee/php-api-client": "^3.0.0" + "recombee/php-api-client": "^3.1.0" } } ``` @@ -55,9 +55,12 @@ try $res = $client->send(new Reqs\Batch($purchase_requests)); //Use Batch for faster processing of larger data // Get 5 recommendations for user 'user-25' - $recommended = $client->send(new Reqs\RecommendItemsToUser('user-25', 5)); + $response = $client->send(new Reqs\RecommendItemsToUser('user-25', 5)); + echo 'Recommended items: ' . json_encode($response, JSON_PRETTY_PRINT) . "\n"; - echo 'Recommended items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n"; + // User scrolled down - get next 3 recommended items + $response = $client->send(new Reqs\RecommendNextItems($response['recommId'], 3)); + echo 'Next recommended items: ' . json_encode($response, JSON_PRETTY_PRINT) . "\n"; } catch(Ex\ApiException $e) { @@ -157,7 +160,7 @@ $recommended = $client->send( // Perform personalized full-text search with a user's search query (e.g. 'computers') $matches = $client->send( - new Reqs\SearchItems('user-42', 'computers', 5) + new Reqs\SearchItems('user-42', 'computers', 5, ['scenario' => 'search_top']) ); echo 'Matched items: ' . json_encode($matches, JSON_PRETTY_PRINT) . "\n"; diff --git a/composer.json b/composer.json index 22b4b7d..9210c94 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ } ], "require": { - "php": ">=5.3.0", - "rmccue/requests": "^1.7.0" + "php": ">=7.2.0", + "guzzlehttp/guzzle": "^7.2.0" }, "require-dev": { - "phpunit/phpunit": "^6.1.0", + "phpunit/phpunit": "^9.2.2", "phpdocumentor/phpdocumentor": "2.*" }, "autoload": { diff --git a/src/RecommApi/Client.php b/src/RecommApi/Client.php index c0534cf..44f9153 100644 --- a/src/RecommApi/Client.php +++ b/src/RecommApi/Client.php @@ -22,6 +22,7 @@ class Client{ protected $protocol; protected $base_uri; protected $options; + protected $guzzle_client; /** * @ignore @@ -52,10 +53,12 @@ public function __construct($account, $token, $protocol = 'https', $options= arr else if (isset($this->options['baseUri'])) $this->base_uri = $this->options['baseUri']; $this->user_agent = $this->getUserAgent(); + + $this->guzzle_client = new \GuzzleHttp\Client(); } protected function getUserAgent() { - $user_agent = 'recombee-php-api-client/3.0.0'; + $user_agent = 'recombee-php-api-client/3.1.0'; if (isset($this->options['serviceName'])) $user_agent .= ' '.($this->options['serviceName']); return $user_agent; @@ -103,7 +106,11 @@ public function send(Requests\Request $request) { break; } } - catch(\Requests_Exception $e) + catch(\GuzzleHttp\Exception\ConnectException $e) + { + throw new ApiTimeoutException($request); + } + catch(\GuzzleHttp\Exception\GuzzleException $e) { if(strpos($e->getMessage(), 'cURL error 28') !== false) throw new ApiTimeoutException($request); if(strpos($e->getMessage(), 'timed out') !== false) throw new ApiTimeoutException($request); @@ -125,56 +132,58 @@ protected function getHttpHeaders() { return array_merge(array('User-Agent' => $this->user_agent), $this->getOptionalHttpHeaders()); } - protected function getOptionalRequestOptions() { + protected function getRequestOptions() { + $options = array('http_errors' => false); if (isset($this->options['requestsOptions'])) - return $this->options['requestsOptions']; - return array(); + $options = array_merge($options, $this->options['requestsOptions']); + return $options; } + protected function put($uri, $timeout, $body) { - $options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions()); + $options = array_merge(array('timeout' => $timeout), $this->getRequestOptions()); $headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders()); - - $response = \Requests::put($uri, $headers, $body, $options); + $response = $this->guzzle_client->request('PUT', $uri, array_merge($options, ['body' => $body, 'headers' => $headers])); $this->checkErrors($response); - return $response->body; + return (string) $response->getBody(); } protected function get($uri, $timeout) { - $options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions()); + $options = array_merge(array('timeout' => $timeout), $this->getRequestOptions()); $headers = $this->getHttpHeaders(); - $response = \Requests::get($uri, $headers, $options); + $response = $this->guzzle_client->request('GET', $uri, array_merge($options, ['headers' => $headers])); $this->checkErrors($response); - return json_decode($response->body, true); + return json_decode($response->getBody(), true); } protected function delete($uri, $timeout) { - $options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions()); + $options = array_merge(array('timeout' => $timeout), $this->getRequestOptions()); $headers = $this->getHttpHeaders(); - $response = \Requests::delete($uri, $headers, $options); + $response = $this->guzzle_client->request('DELETE', $uri, array_merge($options, ['headers' => $headers])); $this->checkErrors($response); - return $response->body; + return (string) $response->getBody(); } protected function post($uri, $timeout, $body) { - $options = array_merge(array('timeout' => $timeout), $this->getOptionalRequestOptions()); + $options = array_merge(array('timeout' => $timeout), $this->getRequestOptions()); $headers = array_merge(array('Content-Type' => 'application/json'), $this->getHttpHeaders()); - $response = \Requests::post($uri, $headers, $body, $options); + + $response = $this->guzzle_client->request('POST', $uri, array_merge($options, ['body' => $body, 'headers' => $headers])); $this->checkErrors($response); - $json = json_decode($response->body, true); + $json = json_decode($response->getBody(), true); if($json !== null && json_last_error() == JSON_ERROR_NONE) return $json; else - return $response->body; + return (string) $response->getBody(); } protected function checkErrors($response) { - $status_code = $response->status_code; + $status_code = $response->getStatusCode(); if($status_code == 200 || $status_code == 201) return; - throw new ResponseException($this->request, $status_code, $response->body); + throw new ResponseException($this->request, $status_code, $response->getBody()); } protected function sendMultipartBatch($request) { diff --git a/src/RecommApi/Requests/AddBookmark.php b/src/RecommApi/Requests/AddBookmark.php index 1c2b5e8..f6b586e 100644 --- a/src/RecommApi/Requests/AddBookmark.php +++ b/src/RecommApi/Requests/AddBookmark.php @@ -35,7 +35,7 @@ class AddBookmark extends Request { */ protected $recomm_id; /** - * @var $additional_data A dictionary of additional data for the interaction. + * @var array $additional_data A dictionary of additional data for the interaction. */ protected $additional_data; /** @@ -59,7 +59,7 @@ class AddBookmark extends Request { * - Type: string * - Description: If this bookmark is based on a recommendation request, `recommId` is the id of the clicked recommendation. * - *additionalData* - * - Type: + * - Type: array * - Description: A dictionary of additional data for the interaction. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/AddCartAddition.php b/src/RecommApi/Requests/AddCartAddition.php index 56a8529..d870ea4 100644 --- a/src/RecommApi/Requests/AddCartAddition.php +++ b/src/RecommApi/Requests/AddCartAddition.php @@ -43,7 +43,7 @@ class AddCartAddition extends Request { */ protected $recomm_id; /** - * @var $additional_data A dictionary of additional data for the interaction. + * @var array $additional_data A dictionary of additional data for the interaction. */ protected $additional_data; /** @@ -73,7 +73,7 @@ class AddCartAddition extends Request { * - Type: string * - Description: If this cart addition is based on a recommendation request, `recommId` is the id of the clicked recommendation. * - *additionalData* - * - Type: + * - Type: array * - Description: A dictionary of additional data for the interaction. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/AddDetailView.php b/src/RecommApi/Requests/AddDetailView.php index 080c37f..a041ef4 100644 --- a/src/RecommApi/Requests/AddDetailView.php +++ b/src/RecommApi/Requests/AddDetailView.php @@ -39,7 +39,7 @@ class AddDetailView extends Request { */ protected $recomm_id; /** - * @var $additional_data A dictionary of additional data for the interaction. + * @var array $additional_data A dictionary of additional data for the interaction. */ protected $additional_data; /** @@ -66,7 +66,7 @@ class AddDetailView extends Request { * - Type: string * - Description: If this detail view is based on a recommendation request, `recommId` is the id of the clicked recommendation. * - *additionalData* - * - Type: + * - Type: array * - Description: A dictionary of additional data for the interaction. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/AddPurchase.php b/src/RecommApi/Requests/AddPurchase.php index a80ea98..da8e383 100644 --- a/src/RecommApi/Requests/AddPurchase.php +++ b/src/RecommApi/Requests/AddPurchase.php @@ -47,7 +47,7 @@ class AddPurchase extends Request { */ protected $recomm_id; /** - * @var $additional_data A dictionary of additional data for the interaction. + * @var array $additional_data A dictionary of additional data for the interaction. */ protected $additional_data; /** @@ -80,7 +80,7 @@ class AddPurchase extends Request { * - Type: string * - Description: If this purchase is based on a recommendation request, `recommId` is the id of the clicked recommendation. * - *additionalData* - * - Type: + * - Type: array * - Description: A dictionary of additional data for the interaction. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/AddRating.php b/src/RecommApi/Requests/AddRating.php index 94c2d39..d0e4017 100644 --- a/src/RecommApi/Requests/AddRating.php +++ b/src/RecommApi/Requests/AddRating.php @@ -39,7 +39,7 @@ class AddRating extends Request { */ protected $recomm_id; /** - * @var $additional_data A dictionary of additional data for the interaction. + * @var array $additional_data A dictionary of additional data for the interaction. */ protected $additional_data; /** @@ -64,7 +64,7 @@ class AddRating extends Request { * - Type: string * - Description: If this rating is based on a recommendation request, `recommId` is the id of the clicked recommendation. * - *additionalData* - * - Type: + * - Type: array * - Description: A dictionary of additional data for the interaction. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/ItemBasedRecommendation.php b/src/RecommApi/Requests/ItemBasedRecommendation.php index b00bb21..0a3ac4c 100644 --- a/src/RecommApi/Requests/ItemBasedRecommendation.php +++ b/src/RecommApi/Requests/ItemBasedRecommendation.php @@ -116,7 +116,7 @@ class ItemBasedRecommendation extends Request { */ protected $rotation_time; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -209,7 +209,7 @@ class ItemBasedRecommendation extends Request { * - Type: float * - Description: **Expert option** If the *targetUserId* is provided: Taking *rotationRate* into account, specifies how long time it takes to an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized. Default: `7200.0`. * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/RecommendItemsToItem.php b/src/RecommApi/Requests/RecommendItemsToItem.php index 85e5d4b..b8d1e42 100644 --- a/src/RecommApi/Requests/RecommendItemsToItem.php +++ b/src/RecommApi/Requests/RecommendItemsToItem.php @@ -11,8 +11,11 @@ /** * Recommends set of items that are somehow related to one given item, *X*. Typical scenario is when user *A* is viewing *X*. Then you may display items to the user that he might be also interested in. Recommend items to item request gives you Top-N such items, optionally taking the target user *A* into account. + * The returned items are sorted by relevance (first item being the most relevant). + * Besides the recommended items, also a unique `recommId` is returned in the response. It can be used to: + * - Let Recombee know that this recommendation was successful (e.g. user clicked one of the recommended items). See [Reported metrics](https://docs.recombee.com/admin_ui.html#reported-metrics). + * - Get subsequent recommended items when the user scrolls down (*infinite scroll*) or goes to the next page. See [Recommend Next Items](https://docs.recombee.com/api.html#recommend-next-items). * It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters. - * The returned items are sorted by relevancy (first item being the most relevant). */ class RecommendItemsToItem extends Request { @@ -76,7 +79,8 @@ class RecommendItemsToItem extends Request { * "url": "myshop.com/mixer-42" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -103,7 +107,8 @@ class RecommendItemsToItem extends Request { * "price": 39 * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -119,7 +124,7 @@ class RecommendItemsToItem extends Request { */ protected $booster; /** - * @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. + * @var string|array $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. * Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). @@ -134,7 +139,7 @@ class RecommendItemsToItem extends Request { */ protected $diversity; /** - * @var string $min_relevance **Expert option** If the *targetUserId* is provided: Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevancy, and may return less than *count* items when there is not enough data to fulfill it. + * @var string $min_relevance **Expert option** If the *targetUserId* is provided: Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it. */ protected $min_relevance; /** @@ -146,7 +151,7 @@ class RecommendItemsToItem extends Request { */ protected $rotation_time; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -212,7 +217,8 @@ class RecommendItemsToItem extends Request { * "url": "myshop.com/mixer-42" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *includedProperties* @@ -238,7 +244,8 @@ class RecommendItemsToItem extends Request { * "price": 39 * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *filter* @@ -250,7 +257,7 @@ class RecommendItemsToItem extends Request { * - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes. * Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). * - *logic* - * - Type: string| + * - Type: string|array * - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. @@ -263,7 +270,7 @@ class RecommendItemsToItem extends Request { * - Description: **Expert option** Real number from [0.0, 1.0] which determines how much mutually dissimilar should the recommended items be. The default value is 0.0, i.e., no diversification. Value 1.0 means maximal diversification. * - *minRelevance* * - Type: string - * - Description: **Expert option** If the *targetUserId* is provided: Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevancy, and may return less than *count* items when there is not enough data to fulfill it. + * - Description: **Expert option** If the *targetUserId* is provided: Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it. * - *rotationRate* * - Type: float * - Description: **Expert option** If the *targetUserId* is provided: If your users browse the system in real-time, it may easily happen that you wish to offer them recommendations multiple times. Here comes the question: how much should the recommendations change? Should they remain the same, or should they rotate? Recombee API allows you to control this per-request in backward fashion. You may penalize an item for being recommended in the near past. For the specific user, `rotationRate=1` means maximal rotation, `rotationRate=0` means absolutely no rotation. You may also use, for example `rotationRate=0.2` for only slight rotation of recommended items. @@ -271,7 +278,7 @@ class RecommendItemsToItem extends Request { * - Type: float * - Description: **Expert option** If the *targetUserId* is provided: Taking *rotationRate* into account, specifies how long time it takes to an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized. * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * - *returnAbGroup* * - Type: bool diff --git a/src/RecommApi/Requests/RecommendItemsToUser.php b/src/RecommApi/Requests/RecommendItemsToUser.php index d403318..cd5ed5a 100644 --- a/src/RecommApi/Requests/RecommendItemsToUser.php +++ b/src/RecommApi/Requests/RecommendItemsToUser.php @@ -12,8 +12,11 @@ /** * Based on user's past interactions (purchases, ratings, etc.) with the items, recommends top-N items that are most likely to be of high value for a given user. * The most typical use cases are recommendations at homepage, in some "Picked just for you" section or in email. + * The returned items are sorted by relevance (first item being the most relevant). + * Besides the recommended items, also a unique `recommId` is returned in the response. It can be used to: + * - Let Recombee know that this recommendation was successful (e.g. user clicked one of the recommended items). See [Reported metrics](https://docs.recombee.com/admin_ui.html#reported-metrics). + * - Get subsequent recommended items when the user scrolls down (*infinite scroll*) or goes to the next page. See [Recommend Next Items](https://docs.recombee.com/api.html#recommend-next-items). * It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters. - * The returned items are sorted by relevancy (first item being the most relevant). */ class RecommendItemsToUser extends Request { @@ -61,7 +64,8 @@ class RecommendItemsToUser extends Request { * "url": "myshop.com/mixer-42" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -88,7 +92,8 @@ class RecommendItemsToUser extends Request { * "price": 39 * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -104,7 +109,7 @@ class RecommendItemsToUser extends Request { */ protected $booster; /** - * @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. + * @var string|array $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. * Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). @@ -115,7 +120,7 @@ class RecommendItemsToUser extends Request { */ protected $diversity; /** - * @var string $min_relevance **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevancy, and may return less than *count* items when there is not enough data to fulfill it. + * @var string $min_relevance **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it. */ protected $min_relevance; /** @@ -127,7 +132,7 @@ class RecommendItemsToUser extends Request { */ protected $rotation_time; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -180,7 +185,8 @@ class RecommendItemsToUser extends Request { * "url": "myshop.com/mixer-42" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *includedProperties* @@ -206,7 +212,8 @@ class RecommendItemsToUser extends Request { * "price": 39 * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *filter* @@ -218,7 +225,7 @@ class RecommendItemsToUser extends Request { * - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes. * Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). * - *logic* - * - Type: string| + * - Type: string|array * - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. @@ -228,7 +235,7 @@ class RecommendItemsToUser extends Request { * - Description: **Expert option** Real number from [0.0, 1.0] which determines how much mutually dissimilar should the recommended items be. The default value is 0.0, i.e., no diversification. Value 1.0 means maximal diversification. * - *minRelevance* * - Type: string - * - Description: **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevancy, and may return less than *count* items when there is not enough data to fulfill it. + * - Description: **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it. * - *rotationRate* * - Type: float * - Description: **Expert option** If your users browse the system in real-time, it may easily happen that you wish to offer them recommendations multiple times. Here comes the question: how much should the recommendations change? Should they remain the same, or should they rotate? Recombee API allows you to control this per-request in backward fashion. You may penalize an item for being recommended in the near past. For the specific user, `rotationRate=1` means maximal rotation, `rotationRate=0` means absolutely no rotation. You may also use, for example `rotationRate=0.2` for only slight rotation of recommended items. Default: `0.1`. @@ -236,7 +243,7 @@ class RecommendItemsToUser extends Request { * - Type: float * - Description: **Expert option** Taking *rotationRate* into account, specifies how long time it takes to an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized. Default: `7200.0`. * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * - *returnAbGroup* * - Type: bool diff --git a/src/RecommApi/Requests/RecommendNextItems.php b/src/RecommApi/Requests/RecommendNextItems.php new file mode 100644 index 0000000..c33fa5a --- /dev/null +++ b/src/RecommApi/Requests/RecommendNextItems.php @@ -0,0 +1,84 @@ +recomm_id = $recomm_id; + $this->count = $count; + $this->timeout = 3000; + $this->ensure_https = false; + } + + /** + * Get used HTTP method + * @return static Used HTTP method + */ + public function getMethod() { + return Request::HTTP_POST; + } + + /** + * Get URI to the endpoint + * @return string URI to the endpoint + */ + public function getPath() { + return "/{databaseId}/recomms/next/items/{$this->recomm_id}"; + } + + /** + * Get query parameters + * @return array Values of query parameters (name of parameter => value of the parameter) + */ + public function getQueryParameters() { + $params = array(); + return $params; + } + + /** + * Get body parameters + * @return array Values of body parameters (name of parameter => value of the parameter) + */ + public function getBodyParameters() { + $p = array(); + $p['count'] = $this->count; + return $p; + } + +} +?> diff --git a/src/RecommApi/Requests/RecommendUsersToItem.php b/src/RecommApi/Requests/RecommendUsersToItem.php index 797ceaa..e75b3bf 100644 --- a/src/RecommApi/Requests/RecommendUsersToItem.php +++ b/src/RecommApi/Requests/RecommendUsersToItem.php @@ -56,7 +56,8 @@ class RecommendUsersToItem extends Request { * "sex": "M" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -81,7 +82,8 @@ class RecommendUsersToItem extends Request { * "country": "CAN" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -97,7 +99,7 @@ class RecommendUsersToItem extends Request { */ protected $booster; /** - * @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. + * @var string|array $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. * Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). @@ -108,7 +110,7 @@ class RecommendUsersToItem extends Request { */ protected $diversity; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -157,7 +159,8 @@ class RecommendUsersToItem extends Request { * "sex": "M" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *includedProperties* @@ -181,7 +184,8 @@ class RecommendUsersToItem extends Request { * "country": "CAN" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *filter* @@ -193,7 +197,7 @@ class RecommendUsersToItem extends Request { * - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes. * Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). * - *logic* - * - Type: string| + * - Type: string|array * - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. @@ -202,7 +206,7 @@ class RecommendUsersToItem extends Request { * - Type: float * - Description: **Expert option** Real number from [0.0, 1.0] which determines how much mutually dissimilar should the recommended items be. The default value is 0.0, i.e., no diversification. Value 1.0 means maximal diversification. * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * - *returnAbGroup* * - Type: bool diff --git a/src/RecommApi/Requests/RecommendUsersToUser.php b/src/RecommApi/Requests/RecommendUsersToUser.php index 572ce76..d507bf9 100644 --- a/src/RecommApi/Requests/RecommendUsersToUser.php +++ b/src/RecommApi/Requests/RecommendUsersToUser.php @@ -56,8 +56,9 @@ class RecommendUsersToUser extends Request { * "sex": "M" * } * } - * ] - * } + * ], + * "numberNextRecommsCalls": 0 + * } * ``` */ protected $return_properties; @@ -81,7 +82,8 @@ class RecommendUsersToUser extends Request { * "country": "CAN" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -97,7 +99,7 @@ class RecommendUsersToUser extends Request { */ protected $booster; /** - * @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. + * @var string|array $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. * Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). @@ -120,7 +122,7 @@ class RecommendUsersToUser extends Request { */ protected $rotation_time; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -169,8 +171,9 @@ class RecommendUsersToUser extends Request { * "sex": "M" * } * } - * ] - * } + * ], + * "numberNextRecommsCalls": 0 + * } * ``` * - *includedProperties* * - Type: array @@ -193,7 +196,8 @@ class RecommendUsersToUser extends Request { * "country": "CAN" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *filter* @@ -205,7 +209,7 @@ class RecommendUsersToUser extends Request { * - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes. * Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). * - *logic* - * - Type: string| + * - Type: string|array * - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. @@ -223,7 +227,7 @@ class RecommendUsersToUser extends Request { * - Type: float * - Description: **Expert option** Taking *rotationRate* into account, specifies how long time it takes to an user to recover from the penalization. For example, `rotationTime=7200.0` means that users recommended less than 2 hours ago are penalized. * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * - *returnAbGroup* * - Type: bool diff --git a/src/RecommApi/Requests/SearchItems.php b/src/RecommApi/Requests/SearchItems.php index ce7a93c..bfe6adb 100644 --- a/src/RecommApi/Requests/SearchItems.php +++ b/src/RecommApi/Requests/SearchItems.php @@ -13,8 +13,11 @@ * Full-text personalized search. The results are based on the provided `searchQuery` and also on the user's past interactions (purchases, ratings, etc.) with the items (items more suitable for the user are preferred in the results). * All the string and set item properties are indexed by the search engine. * This endpoint should be used in a search box at your website/app. It can be called multiple times as the user is typing the query in order to get the most viable suggestions based on current state of the query, or once after submitting the whole query. + * The returned items are sorted by relevance (first item being the most relevant). + * Besides the recommended items, also a unique `recommId` is returned in the response. It can be used to: + * - Let Recombee know that this search was successful (e.g. user clicked one of the recommended items). See [Reported metrics](https://docs.recombee.com/admin_ui.html#reported-metrics). + * - Get subsequent search results when the user scrolls down or goes to the next page. See [Recommend Next Items](https://docs.recombee.com/api.html#recommend-next-items). * It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters. - * The returned items are sorted by relevancy (first item being the most relevant). */ class SearchItems extends Request { @@ -66,7 +69,8 @@ class SearchItems extends Request { * "url": "myshop.com/mixer-42" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -93,7 +97,8 @@ class SearchItems extends Request { * "price": 39 * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` */ @@ -109,14 +114,14 @@ class SearchItems extends Request { */ protected $booster; /** - * @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. + * @var string|array $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. * Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). */ protected $logic; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -170,7 +175,8 @@ class SearchItems extends Request { * "url": "myshop.com/mixer-42" * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *includedProperties* @@ -196,7 +202,8 @@ class SearchItems extends Request { * "price": 39 * } * } - * ] + * ], + * "numberNextRecommsCalls": 0 * } * ``` * - *filter* @@ -208,13 +215,13 @@ class SearchItems extends Request { * - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes. * Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). * - *logic* - * - Type: string| + * - Type: string|array * - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case. * See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details. * The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users. * Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * - *returnAbGroup* * - Type: bool diff --git a/src/RecommApi/Requests/SetViewPortion.php b/src/RecommApi/Requests/SetViewPortion.php index f76d44b..05d7e6a 100644 --- a/src/RecommApi/Requests/SetViewPortion.php +++ b/src/RecommApi/Requests/SetViewPortion.php @@ -44,7 +44,7 @@ class SetViewPortion extends Request { */ protected $recomm_id; /** - * @var $additional_data A dictionary of additional data for the interaction. + * @var array $additional_data A dictionary of additional data for the interaction. */ protected $additional_data; /** @@ -72,7 +72,7 @@ class SetViewPortion extends Request { * - Type: string * - Description: If this view portion is based on a recommendation request, `recommId` is the id of the clicked recommendation. * - *additionalData* - * - Type: + * - Type: array * - Description: A dictionary of additional data for the interaction. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/src/RecommApi/Requests/UserBasedRecommendation.php b/src/RecommApi/Requests/UserBasedRecommendation.php index 1609795..a467153 100644 --- a/src/RecommApi/Requests/UserBasedRecommendation.php +++ b/src/RecommApi/Requests/UserBasedRecommendation.php @@ -103,7 +103,7 @@ class UserBasedRecommendation extends Request { */ protected $rotation_time; /** - * @var $expert_settings Dictionary of custom options. + * @var array $expert_settings Dictionary of custom options. */ protected $expert_settings; /** @@ -185,7 +185,7 @@ class UserBasedRecommendation extends Request { * - Type: float * - Description: **Expert option** Taking *rotationRate* into account, specifies how long time it takes to an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized. Default: `7200.0`. * - *expertSettings* - * - Type: + * - Type: array * - Description: Dictionary of custom options. * @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional */ diff --git a/tests/AddEntityTestCase.php b/tests/AddEntityTestCase.php index fa2f077..2476061 100644 --- a/tests/AddEntityTestCase.php +++ b/tests/AddEntityTestCase.php @@ -20,7 +20,7 @@ public function testAddEntity() { $resp = $this->client->send($req); //it fails with invalid entity id - $req = $this->createRequest('$$$not_valid$$$'); + $req = $this->createRequest('***not_valid$$$'); try { $this->client->send($req); diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 612a8ff..406698c 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -10,7 +10,6 @@ class BatchTest extends RecombeeTestCase { public function testBatch() { $reqs = [ - new Reqs\ResetDatabase, new Reqs\AddItemProperty('num', 'int'), new Reqs\AddItemProperty('time', 'timestamp'), new Reqs\SetItemValues('item1', [ @@ -39,12 +38,12 @@ public function testBatch() { array_push($codes, $r['code']); } - $this->assertEquals([200, 201, 201, 200, 409, 200, 200, 200, 200, 200, 404, 200, 200, 200], $codes); - sort($repl[6]['json']); + $this->assertEquals([201, 201, 200, 409, 200, 200, 200, 200, 200, 404, 200, 200, 200], $codes); + sort($repl[5]['json']); $this->assertEquals(['item1', 'item2'], $repl[6]['json']); - sort($repl[7]['json']); + sort($repl[6]['json']); $this->assertEquals(['item2'], $repl[7]['json']); - sort($repl[9]['json']); + sort($repl[8]['json']); $this->assertEquals(array(), $repl[9]['json']); $this->assertEquals(['item2'], $repl[13]['json']); diff --git a/tests/DeleteEntityTestCase.php b/tests/DeleteEntityTestCase.php index 428afe7..cd2579a 100644 --- a/tests/DeleteEntityTestCase.php +++ b/tests/DeleteEntityTestCase.php @@ -29,7 +29,7 @@ public function testDeleteEntity() { } //it fails with invalid entity id - $req = $this->createRequest('$$$not_valid$$$'); + $req = $this->createRequest('***not_valid$$$'); try { $this->client->send($req); diff --git a/tests/DeletePropertyTestCase.php b/tests/DeletePropertyTestCase.php index 287dcdb..bee8c43 100644 --- a/tests/DeletePropertyTestCase.php +++ b/tests/DeletePropertyTestCase.php @@ -29,7 +29,7 @@ public function testDeleteProperty() { } //it fails with invalid property - $req = $this->createRequest('$$$not_valid$$$'); + $req = $this->createRequest('***not_valid$$$'); try { $this->client->send($req); diff --git a/tests/InteractionsTestCase.php b/tests/InteractionsTestCase.php index 38103ef..289105d 100644 --- a/tests/InteractionsTestCase.php +++ b/tests/InteractionsTestCase.php @@ -7,7 +7,7 @@ class InteractionsTestCase extends RecombeeTestCase { - protected function setUp() { + protected function setUp(): void { parent::setUp(); $client = new Client('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L'); diff --git a/tests/ItemBasedRecommendationTest.php b/tests/ItemBasedRecommendationTest.php index 4b933a7..38db0ee 100644 --- a/tests/ItemBasedRecommendationTest.php +++ b/tests/ItemBasedRecommendationTest.php @@ -2,7 +2,7 @@ namespace Recombee\RecommApi\Tests; use Recombee\RecommApi\Requests\ItemBasedRecommendation; -class ItemBasedRecommendationTest extends RecommendationTestCase { +class ItemBasedRecommendationTest extends RecommendationDeprecatedTestCase { protected function createRequest($item_id, $count, $optional=array()) { $optional = array_merge($optional, ['targetUserId' => 'entity_id']); diff --git a/tests/ItemToItemRecommendationTestCase.php b/tests/ItemToItemRecommendationTestCase.php index eb96552..3f40e44 100644 --- a/tests/ItemToItemRecommendationTestCase.php +++ b/tests/ItemToItemRecommendationTestCase.php @@ -9,7 +9,7 @@ use Recombee\RecommApi\Exceptions as Exc; use Recombee\RecommApi\Requests as Reqs; -abstract class ItemToItemRecommendationTestCase extends RecombeeTestCase { +abstract class ItemToItemRecommendationTestCase extends RecommendationDataTestCase { abstract protected function createRequest($item_id,$target_user_id,$count,$optional=array()); diff --git a/tests/NextItemsRecommendationTestCase.php b/tests/NextItemsRecommendationTestCase.php new file mode 100644 index 0000000..14a9294 --- /dev/null +++ b/tests/NextItemsRecommendationTestCase.php @@ -0,0 +1,31 @@ + true]); + $resp = $this->client->send($req); + $req = $this->createRequest($resp['recommId'],3); + $resp = $this->client->send($req); + $this->assertCount(3, $resp['recomms']); + $req = $this->createRequest($resp['recommId'],3); + $resp = $this->client->send($req); + $this->assertCount(3, $resp['recomms']); + + } +} + +?> diff --git a/tests/RecombeeTestCase.php b/tests/RecombeeTestCase.php index b5c7bb0..1de7fbc 100644 --- a/tests/RecombeeTestCase.php +++ b/tests/RecombeeTestCase.php @@ -10,7 +10,7 @@ class RecombeeTestCase extends \PHPUnit\Framework\TestCase { protected $client; - protected function setUp() { + protected function setUp(): void { $this->client = new Client('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L'); diff --git a/tests/RecommendNextItemsTest.php b/tests/RecommendNextItemsTest.php new file mode 100644 index 0000000..2f2c6e8 --- /dev/null +++ b/tests/RecommendNextItemsTest.php @@ -0,0 +1,11 @@ + diff --git a/tests/RecommendationDataTestCase.php b/tests/RecommendationDataTestCase.php new file mode 100644 index 0000000..b659b84 --- /dev/null +++ b/tests/RecommendationDataTestCase.php @@ -0,0 +1,43 @@ +send(new Reqs\Batch($user_requests)); + $client->send(new Reqs\Batch([new Reqs\AddItemProperty('answer', 'int'), new Reqs\AddItemProperty('id2', 'string'), new Reqs\AddItemProperty('empty', 'string')])); + + $item_requests = array_map(function($itemId) {return new Reqs\SetItemValues($itemId, ['answer' => 42, 'id2' => $itemId, '!cascadeCreate' => true]);}, $my_item_ids); + $client->send(new Reqs\Batch($item_requests)); + $client->send(new Reqs\Batch($my_purchases)); + } +} +?> \ No newline at end of file diff --git a/tests/RecommendationDeprecatedTestCase.php b/tests/RecommendationDeprecatedTestCase.php index 31a0a4b..3e319dd 100644 --- a/tests/RecommendationDeprecatedTestCase.php +++ b/tests/RecommendationDeprecatedTestCase.php @@ -9,7 +9,7 @@ use Recombee\RecommApi\Exceptions as Exc; use Recombee\RecommApi\Requests as Reqs; -abstract class RecommendationDeprecatedTestCase extends RecombeeTestCase { +abstract class RecommendationDeprecatedTestCase extends RecommendationDataTestCase { abstract protected function createRequest($user_id,$count,$optional=array()); diff --git a/tests/RecommendationTestCase.php b/tests/RecommendationTestCase.php index d1327cd..505bbc7 100644 --- a/tests/RecommendationTestCase.php +++ b/tests/RecommendationTestCase.php @@ -6,60 +6,28 @@ use Recombee\RecommApi\Requests as Reqs; use Recombee\RecommApi\Exceptions as Exc; -abstract class RecommendationTestCase extends RecombeeTestCase { +abstract class RecommendationTestCase extends RecommendationDataTestCase { abstract protected function createRequest($entity_id, $count, $optional = array()); - protected function setUp() { - parent::setUp(); - - $num = 1000; - $probability_purchased = 0.007; - - $my_user_ids = array(); - $my_item_ids = array(); - for($i=0; $i < $num; $i++) { - array_push($my_user_ids, "user-{$i}"); - array_push($my_item_ids, "item-{$i}"); - } - - $my_purchases = array(); - foreach ($my_user_ids as $user_id) { - foreach ($my_item_ids as $item_id) { - if(mt_rand() / mt_getrandmax() < $probability_purchased) - array_push($my_purchases, new Reqs\AddPurchase($user_id, $item_id)); - } - } - - $client = new Client('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L'); - - $user_requests = array_map(function($userId) {return new Reqs\AddUser($userId);}, $my_user_ids); - $client->send(new Reqs\Batch($user_requests)); - $client->send(new Reqs\Batch([new Reqs\AddItemProperty('answer', 'int'), new Reqs\AddItemProperty('id2', 'string'), new Reqs\AddItemProperty('empty', 'string')])); - - $item_requests = array_map(function($itemId) {return new Reqs\SetItemValues($itemId, ['answer' => 42, 'id2' => $itemId, '!cascadeCreate' => true]);}, $my_item_ids); - $client->send(new Reqs\Batch($item_requests)); - $client->send(new Reqs\Batch($my_purchases)); - } - public function testBasicRecomm() { $req = $this->createRequest('entity_id', 9); $resp = $this->client->send($req); - $this->assertCount(9, $resp); + $this->assertCount(9, $resp['recomms']); } public function testRotation() { $req = $this->createRequest('entity_id', 9); $recommended1 = $this->client->send($req); - $this->assertCount(9, $recommended1); + $this->assertCount(9, $recommended1['recomms']); $req = $this->createRequest('entity_id', 9, ['rotationTime' => 10000, 'rotationRate' => 1]); $recommended2 = $this->client->send($req); - $this->assertCount(9, $recommended2); + $this->assertCount(9, $recommended2['recomms']); foreach ($recommended1 as $item_id) { - $this->assertNotContains($item_id, $recommended2); + $this->assertNotContains($item_id, $recommended2['recomms']); } } @@ -71,25 +39,25 @@ public function testInBatch() { } $recommendations = $this->client->send(new Reqs\Batch($reqs)); foreach ($recommendations as $recommendation) { - $this->assertCount(9, $recommendation['json']); + $this->assertCount(9, $recommendation['json']['recomms']); } } public function testReturningProperties() { $req = $this->createRequest('entity_id', 9, ['returnProperties' => true, 'includedProperties'=>['answer', 'id2', 'empty']]); - $recommended = $this->client->send($req); + $recommended = $this->client->send($req)['recomms']; foreach ($recommended as $rec) { - $this->assertEquals($rec['id2'], $rec['id']); - $this->assertEquals(42, $rec['answer']); - $this->assertArrayHasKey('empty', $rec); + $this->assertEquals($rec['values']['id2'], $rec['id']); + $this->assertEquals(42, $rec['values']['answer']); + $this->assertArrayHasKey('empty', $rec['values']); } $req = $this->createRequest('entity_id', 9, ['returnProperties' => true, 'includedProperties'=>'answer,id2']); - $recommended = $this->client->send($req); + $recommended = $this->client->send($req)['recomms']; foreach ($recommended as $rec) { - $this->assertEquals($rec['id2'], $rec['id']); - $this->assertEquals(42, $rec['answer']); - $this->assertArrayNotHasKey('empty', $rec); + $this->assertEquals($rec['values']['id2'], $rec['id']); + $this->assertEquals(42, $rec['values']['answer']); + $this->assertArrayNotHasKey('empty', $rec['values']); } } } diff --git a/tests/SearchTestCase.php b/tests/SearchTestCase.php index acb0cde..62df46b 100644 --- a/tests/SearchTestCase.php +++ b/tests/SearchTestCase.php @@ -9,7 +9,7 @@ use Recombee\RecommApi\Exceptions as Exc; use Recombee\RecommApi\Requests as Reqs; -abstract class SearchTestCase extends RecombeeTestCase { +abstract class SearchTestCase extends RecommendationDataTestCase { abstract protected function createRequest($user_id,$search_query,$count,$optional=array());