Skip to content

Commit

Permalink
chore: Add test code for input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eomiso committed Jul 11, 2022
1 parent 19d9a63 commit 6061731
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/ZaiClient/Exceptions/BatchSizeLimitExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
class BatchSizeLimitExceededException extends \Exception
{
public function __construct()
public function __construct($records_num)
{
$message = sprintf("Number of total records cannot exceed 50, but your Event holds %d.");
$message = sprintf("Number of total records cannot exceed 50, but your Event holds %d.", $records_num);
parent::__construct($message, 2);
}

Expand Down
4 changes: 4 additions & 0 deletions src/ZaiClient/Requests/CartaddEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ZaiKorea\ZaiClient\Requests\BaseEvent;
use ZaiKorea\ZaiClient\Requests\EventInBatch;
use ZaiKorea\ZaiClient\Configs\Config;
use ZaiKorea\ZaiClient\Exceptions\BatchSizeLimitExceededException;

/**
* @final
Expand Down Expand Up @@ -79,6 +80,9 @@ public function __construct($customer_id, $item_ids, $options = array())
$tmp_timestamp += Config::EPSILON;
}

if (count($events) > 50)
throw new BatchSizeLimitExceededException(count($events));

if (count($events) == 1)
$this->setPayload($events[0]);
else
Expand Down
5 changes: 4 additions & 1 deletion src/ZaiClient/Requests/CustomEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ZaiKorea\ZaiClient\Requests\BaseEvent;
use ZaiKorea\ZaiClient\Requests\EventInBatch;
use ZaiKorea\ZaiClient\Configs\Config;
use ZaiKorea\ZaiClient\Exceptions\BatchSizeLimitExceededException;

/**
* @final
Expand Down Expand Up @@ -107,7 +108,6 @@ public function __construct(
)
);
}

array_push($events, new EventInBatch(
$customer_id,
$custom_action['item_id'],
Expand All @@ -118,6 +118,9 @@ public function __construct(
$tmp_timestamp += Config::EPSILON;
}

if (count($events) > 50)
throw new BatchSizeLimitExceededException(count($events));

if (count($events) == 1)
$this->setPayload($events[0]);
else
Expand Down
2 changes: 1 addition & 1 deletion src/ZaiClient/Requests/EventInBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct($user_id, $item_id, $timestamp, $event_type, $event_
throw new \InvalidArgumentException('Length of user id must be between 1 and 100.');

if (!(strlen($this->item_id) > 0 && strlen($this->item_id) <= 100))
throw new \InvalidArgumentException('Length of user id must be between 1 and 100.');
throw new \InvalidArgumentException('Length of item id must be between 1 and 100.');

if (!(strlen($this->event_type) > 0 && strlen($this->event_type) <= 100))
throw new \InvalidArgumentException('Length of event type must be between 1 and 100.');
Expand Down
4 changes: 4 additions & 0 deletions src/ZaiClient/Requests/LikeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ZaiKorea\ZaiClient\Requests\BaseEvent;
use ZaiKorea\ZaiClient\Requests\EventInBatch;
use ZaiKorea\ZaiClient\Configs\Config;
use ZaiKorea\ZaiClient\Exceptions\BatchSizeLimitExceededException;

/**
* @final
Expand Down Expand Up @@ -80,6 +81,9 @@ public function __construct($customer_id, $item_ids, $options = array())
$tmp_timestamp += Config::EPSILON;
}

if (count($events) > 50)
throw new BatchSizeLimitExceededException(count($events));

if (count($events) == 1)
$this->setPayload($events[0]);
else
Expand Down
2 changes: 1 addition & 1 deletion src/ZaiClient/Requests/PurchaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function __construct($customer_id, $orders = array(), $options = array())
}

if (count($events) > 50)
throw new BatchSizeLimitExceededException();
throw new BatchSizeLimitExceededException(count($events));

if (count($events) == 1)
$this->setPayload($events[0]);
Expand Down
4 changes: 4 additions & 0 deletions src/ZaiClient/Requests/RateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ZaiKorea\ZaiClient\Requests\BaseEvent;
use ZaiKorea\ZaiClient\Requests\EventInBatch;
use ZaiKorea\ZaiClient\Configs\Config;
use ZaiKorea\ZaiClient\Exceptions\BatchSizeLimitExceededException;

/**
* @final
Expand Down Expand Up @@ -97,6 +98,9 @@ public function __construct($customer_id, $rate_actions = array(), $options = ar
$tmp_timestamp += Config::EPSILON;
}

if (count($events) > 50)
throw new BatchSizeLimitExceededException(count($events));

if (count($events) == 1)
$this->setPayload($events[0]);
else
Expand Down
12 changes: 6 additions & 6 deletions src/ZaiClient/Requests/RelatedItemsRecommendationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ class RelatedItemsRecommendationRequest extends RecommendationRequest

public function __construct($item_id, $limit, $options = array())
{
if (!(is_null(null) || strlen($item_id) > 0 && strlen($item_id) <= 100))
if (is_null($item_id) || !(strlen($item_id) > 0 && strlen($item_id) <= 100))
throw new \InvalidArgumentException('Length of item id must be between 1 and 100.');

if (!(0 < $limit && $limit <= 1000000))
throw new \InvalidArgumentException('Limit must be between 1 and 1000,000.');

if (!is_array($options))
throw new \InvalidArgumentException("Options must be given as an array.");
if (isset($options['offset'])) {

if (!(0 <= $options['offset'] && $options['offset'] <= 1000000))
throw new \InvalidArgumentException('Offset must be between 0 and 1000,000.');
}
if (isset($options['recommendation_type'])) {
if ($options['recommendation_type'] == null || !(0 < strlen($options['recommendation_type'] && strlen($options['recommendation_type']) <= 100)))
if (isset($options['recommendation_type'])) { // php tip! isset() returns false if the value of $options['recommendation_type'] is null
if (!(0 < strlen($options['recommendation_type'] && strlen($options['recommendation_type']) <= 100)))
throw new \InvalidArgumentException('Length of recommendation type must be between 1 and 100.');
}

$this->item_id = $item_id;
$this->limit = $limit;

if (!is_array($options))
throw new \InvalidArgumentException("options must be given as an array.");

$this->recommendation_type = isset($options['recommendation_type']) ? $options['recommendation_type'] : self::DEFAULT_RECOMMENDATION_TYPE;
$this->offset = isset($options['offset']) ? $options['offset'] : self::DEFAULT_OFFSET;
}
Expand Down
11 changes: 6 additions & 5 deletions src/ZaiClient/Requests/RerankingRecommendationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@ public function __construct($user_id, $item_ids, $limit, $options = array())
if (!(is_null(null) || (0 < $limit && $limit <= 1000000)))
throw new \InvalidArgumentException('Limit must be null or between 1 and 1000,000.');

if (!is_array($options))
throw new \InvalidArgumentException("Options must be given as an array.");

if (isset($options['offset'])) {
if (!(0 <= $options['offset'] && $options['offset'] <= 1000000))
throw new \InvalidArgumentException('Offset must be between 0 and 1000,000.');
}
if (isset($options['recommendation_type'])) {
if ($options['recommendation_type'] == null || !(0 < strlen($options['recommendation_type'] && strlen($options['recommendation_type']) <= 100)))

if (isset($options['recommendation_type'])) { // php tip! isset() returns false if the value of $options['recommendation_type'] is null
if (!(0 < strlen($options['recommendation_type'] && strlen($options['recommendation_type']) <= 100)))
throw new \InvalidArgumentException('Length of recommendation type must be between 1 and 100.');
}

$this->user_id = $user_id;
$this->item_ids = $item_ids; // This should be an array
$this->limit = $limit;

if (!is_array($options))
throw new \InvalidArgumentException("options must be given as an array.");

$this->recommendation_type = isset($options['recommendation_type']) ? $options['recommendation_type'] : self::DEFAULT_RECOMMENDATION_TYPE;
$this->offset = isset($options['offset']) ? $options['offset'] : self::DEFAULT_OFFSET;
}
Expand Down
11 changes: 6 additions & 5 deletions src/ZaiClient/Requests/UserRecommendationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ public function __construct($user_id, $limit, $options = array())
if (!(0 < $limit && $limit <= 1000000))
throw new \InvalidArgumentException('Limit must be between 1 and 1000,000.');

if (!is_array($options))
throw new \InvalidArgumentException('Options must be given as an array.');

if (isset($options['offset'])) {
if (!(0 <= $options['offset'] && $options['offset'] <= 1000000))
throw new \InvalidArgumentException('Offset must be between 0 and 1000,000.');
}
if (isset($options['recommendation_type'])) {
if ($options['recommendation_type'] == null || !(0 < strlen($options['recommendation_type'] && strlen($options['recommendation_type']) <= 100)))

if (isset($options['recommendation_type'])) { // php tip! isset() returns false if the value of $options['recommendation_type'] is null
if (!(0 < strlen($options['recommendation_type'] && strlen($options['recommendation_type']) <= 100)))
throw new \InvalidArgumentException('Length of recommendation type must be between 1 and 100.');
}

$this->user_id = $user_id;
$this->limit = $limit;

if (!is_array($options))
throw new \InvalidArgumentException('options must be givent as an array. $options given instead.');

$this->recommendation_type = isset($options['recommendation_type']) ? $options['recommendation_type'] : self::DEFAULT_RECOMMENDATION_TYPE;
$this->offset = isset($options['offset']) ? $options['offset'] : self::DEFAULT_OFFSET;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ZaiClient/Requests/ViewEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ZaiKorea\ZaiClient\Requests\BaseEvent;
use ZaiKorea\ZaiClient\Requests\EventInBatch;
use ZaiKorea\ZaiClient\Configs\Config;
use ZaiKorea\ZaiClient\Exceptions\BatchSizeLimitExceededException;

class ViewEvent extends BaseEvent
{
Expand Down Expand Up @@ -77,6 +78,9 @@ public function __construct($customer_id, $item_ids, $options = array())
$tmp_timestamp += Config::EPSILON;
}

if (count($events) > 50)
throw new BatchSizeLimitExceededException(count($events));

if (count($events) == 1)
$this->setPayload($events[0]);
else
Expand Down
59 changes: 59 additions & 0 deletions tests/EventLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use ZaiKorea\ZaiClient\Requests\RateEvent;
use ZaiKorea\ZaiClient\Requests\CustomEvent;
use ZaiKorea\ZaiClient\Exceptions\ZaiClientException;
use ZaiKorea\ZaiClient\Exceptions\BatchSizeLimitExceededException;
use ZaiKorea\ZaiClient\Configs\Config;

define('SECRET', getenv('ZAI_TEST'));

require_once 'TestUtils.php';

class EventLogTest extends TestCase
{
private $client_id = 'test';
Expand Down Expand Up @@ -471,4 +474,60 @@ public function testBadOrdersTypeOnPurchaseEventWithEmptyArray()
$purchase_event = new PurchaseEvent($customer_id, $orders);
$client->addEventLog($purchase_event);
}

public function testBadCustomerIdOnViewEvent()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Length of user id must be between 1 and 100.');

$client = new ZaiClient($this->client_id, SECRET);
$customer_id = generateRandomString(101);

$item_id = ['P12345'];
$view_event = new ViewEvent($customer_id, $item_id);
$client->addEventLog($view_event);
}

public function testBadItemIdOnViewEvent()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Length of item id must be between 1 and 100.');

$client = new ZaiClient($this->client_id, SECRET);
$customer_id = 'php-raise-error';

$item_id = [generateRandomString(101)];
$view_event = new ViewEvent($customer_id, $item_id);
$client->addEventLog($view_event);
}

public function testBadEventTypeCustomerEvent()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Length of event type must be between 1 and 100.');

$client = new ZaiClient($this->client_id, SECRET);
$customer_id = 'php-raise-error';

$custom_event_type = generateRandomString(102);
$custom_action = ['item_id' => 'P99999', 'value' => 9];
$custom_event = new CustomEvent($customer_id, $custom_event_type, $custom_action);
$client->addEventLog($custom_event);
}

public function testBatchLimiteExceededException()
{
$this->expectException(BatchSizeLimitExceededException::class);
$this->expectExceptionMessage(sprintf("Number of total records cannot exceed 50, but your Event holds %d.", 55));

$client = new ZaiClient($this->client_id, SECRET);
$customer_id = 'php-raise-error';

$orders = array(
['item_id' => 'P1234567', 'price' => 11000, 'count' => 52],
['item_id' => 'P5678901', 'price' => 11000, 'count' => 3]
);
$purchase_event = new PurchaseEvent($customer_id, $orders);
$client->addEventLog($purchase_event);
}
}
Loading

0 comments on commit 6061731

Please sign in to comment.