Skip to content

Commit

Permalink
Merge pull request #16 from seatsio/matti/sorting-and-filtering-statu…
Browse files Browse the repository at this point in the history
…s-changes

Status changes can be sorted and filtered
  • Loading branch information
mroloux authored Feb 5, 2019
2 parents 3e931de + 3e3985e commit 821c1ac
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: php
php:
- 5.5
- 7.2

install: composer install
before_script:
- export TZ=Europe/Brussels
script: vendor/bin/phpunit --configuration tests/phpunit.xml
42 changes: 31 additions & 11 deletions src/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function createMultiple($chartKey, $eventCreationParams)
if ($param->eventKey !== null) {
$eventToCreate->eventKey = $param->eventKey;
}
if(is_bool($param->bookWholeTablesOrTableBookingModes)) {
if (is_bool($param->bookWholeTablesOrTableBookingModes)) {
$eventToCreate->bookWholeTables = $param->bookWholeTablesOrTableBookingModes;
} else if ($param->bookWholeTablesOrTableBookingModes !== null) {
$eventToCreate -> tableBookingModes = $param->bookWholeTablesOrTableBookingModes;
$eventToCreate->tableBookingModes = $param->bookWholeTablesOrTableBookingModes;
}
$request->events[] = $eventToCreate;
}
Expand Down Expand Up @@ -159,16 +159,36 @@ private function iterator()

/**
* @param $eventKey string
* @param $objectId string
* @param $filter string
* @param $sortField string
* @param $sortDirection string
* @return StatusChangeLister
*/
public function statusChanges($eventKey, $objectId = null)
public function statusChanges($eventKey, $filter = null, $sortField = null, $sortDirection = null)
{
if ($objectId === null) {
return new StatusChangeLister(new PageFetcher(\GuzzleHttp\uri_template('/events/{key}/status-changes', array("key" => $eventKey)), $this->client, function () {
return new StatusChangePage();
}));
return new StatusChangeLister(new PageFetcher(\GuzzleHttp\uri_template('/events/{key}/status-changes', array("key" => $eventKey)), $this->client, function () {
return new StatusChangePage();
}, array("filter" => $filter, "sort" => self::toSort($sortField, $sortDirection))));
}

private static function toSort($sortField, $sortDirection)
{
if (!$sortField) {
return null;
}
if ($sortDirection) {
return $sortField . ":" . $sortDirection;
}
return $sortField;
}

/**
* @param $eventKey string
* @param $objectId string
* @return StatusChangeLister
*/
public function statusChangesForObject($eventKey, $objectId)
{
return new StatusChangeLister(new PageFetcher(\GuzzleHttp\uri_template('/events/{key}/objects/{objectId}/status-changes', array("key" => $eventKey, "objectId" => $objectId)), $this->client, function () {
return new StatusChangePage();
}));
Expand Down Expand Up @@ -398,11 +418,11 @@ private static function normalizeObjects($objectOrObjects)
if (count($objectOrObjects) === 0) {
return [];
}
return array_map(function($object) {
if($object instanceof ObjectProperties) {
return array_map(function ($object) {
if ($object instanceof ObjectProperties) {
return $object;
}
if(is_string($object)) {
if (is_string($object)) {
return ["objectId" => $object];
}
return $object;
Expand Down
7 changes: 5 additions & 2 deletions src/PageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class PageFetcher
*/
private $client;
private $pageCreator;
private $queryParams;

public function __construct($url, $client, $pageCreator)
public function __construct($url, $client, $pageCreator, $queryParams = null)
{
$this->url = $url;
$this->client = $client;
$this->pageCreator = $pageCreator;
$this->queryParams = $queryParams;
}

public function fetchAfter($afterId, $queryParams, $pageSize)
Expand All @@ -39,7 +41,8 @@ public function fetch($queryParams, $pageSize)
if ($pageSize) {
$queryParams['limit'] = $pageSize;
}
$res = $this->client->get($this->url, ['query' => $queryParams]);
$mergedQueryParams = $this->queryParams ? array_merge($queryParams, $this->queryParams) : $queryParams;
$res = $this->client->get($this->url, ['query' => $mergedQueryParams]);
$json = \GuzzleHttp\json_decode($res->getBody());
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, $this->pageCreator->__invoke());
Expand Down
2 changes: 1 addition & 1 deletion tests/Events/ListStatusChangesForObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function test()
$this->seatsioClient->events->changeObjectStatus($event->key, "A-2", "s4");
$this->seatsioClient->events->changeObjectStatus($event->key, "A-1", "s3");

$statusChanges = $this->seatsioClient->events->statusChanges($event->key, "A-1")->all();
$statusChanges = $this->seatsioClient->events->statusChangesForObject($event->key, "A-1")->all();
$statuses = \Functional\map($statusChanges, function($statusChange) { return $statusChange->status; });

self::assertEquals(["s3", "s2", "s1"], array_values($statuses));
Expand Down
90 changes: 90 additions & 0 deletions tests/Events/ListStatusChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,94 @@ public function testPropertiesOfStatusChange()
self::assertEquals((object)["foo" => "bar"], $statusChange->extraData);
}

public function testFilter()
{
$chartKey = $this->createTestChart();
$event = $this->seatsioClient->events->create($chartKey);
$this->seatsioClient->events->book($event->key, "A-1");
$this->seatsioClient->events->book($event->key, "A-2");
$this->seatsioClient->events->book($event->key, "B-1");
$this->seatsioClient->events->book($event->key, "A-3");

$statusChanges = $this->seatsioClient->events->statusChanges($event->key, "A-")->all();
$objectIds = \Functional\map($statusChanges, function ($statusChange) {
return $statusChange->objectLabel;
});

self::assertEquals(["A-3", "A-2", "A-1"], array_values($objectIds));
}

public function testSortAsc()
{
$chartKey = $this->createTestChart();
$event = $this->seatsioClient->events->create($chartKey);
$this->seatsioClient->events->book($event->key, "A-1");
$this->seatsioClient->events->book($event->key, "A-2");
$this->seatsioClient->events->book($event->key, "B-1");
$this->seatsioClient->events->book($event->key, "A-3");

$statusChanges = $this->seatsioClient->events->statusChanges($event->key, null, "objectLabel")->all();
$objectIds = \Functional\map($statusChanges, function ($statusChange) {
return $statusChange->objectLabel;
});

self::assertEquals(["A-1", "A-2", "A-3", "B-1"], array_values($objectIds));
}

public function testSortAscPageBefore()
{
$chartKey = $this->createTestChart();
$event = $this->seatsioClient->events->create($chartKey);
$this->seatsioClient->events->book($event->key, "A-1");
$this->seatsioClient->events->book($event->key, "A-2");
$this->seatsioClient->events->book($event->key, "B-1");
$this->seatsioClient->events->book($event->key, "A-3");

$statusChangeLister = $this->seatsioClient->events->statusChanges($event->key, null, "objectLabel");
$allStatusChanges = iterator_to_array($statusChangeLister->all(), false);
$b1ID = $allStatusChanges[2]->id;
$statusChanges = $statusChangeLister->pageBefore($b1ID, 2)->items;
$objectIds = \Functional\map($statusChanges, function ($statusChange) {
return $statusChange->objectLabel;
});

self::assertEquals(["A-1", "A-2"], array_values($objectIds));
}

public function testSortAscPageAfter()
{
$chartKey = $this->createTestChart();
$event = $this->seatsioClient->events->create($chartKey);
$this->seatsioClient->events->book($event->key, "A-1");
$this->seatsioClient->events->book($event->key, "A-2");
$this->seatsioClient->events->book($event->key, "B-1");
$this->seatsioClient->events->book($event->key, "A-3");

$statusChangeLister = $this->seatsioClient->events->statusChanges($event->key, null, "objectLabel");
$allStatusChanges = iterator_to_array($statusChangeLister->all(), false);
$a1ID = $allStatusChanges[0]->id;
$statusChanges = $statusChangeLister->pageAfter($a1ID, 2)->items;
$objectIds = \Functional\map($statusChanges, function ($statusChange) {
return $statusChange->objectLabel;
});

self::assertEquals(["A-2", "A-3"], array_values($objectIds));
}

public function testSortDesc()
{
$chartKey = $this->createTestChart();
$event = $this->seatsioClient->events->create($chartKey);
$this->seatsioClient->events->book($event->key, "A-1");
$this->seatsioClient->events->book($event->key, "A-2");
$this->seatsioClient->events->book($event->key, "B-1");
$this->seatsioClient->events->book($event->key, "A-3");

$statusChanges = $this->seatsioClient->events->statusChanges($event->key, null, "objectLabel", "DESC")->all();
$objectIds = \Functional\map($statusChanges, function ($statusChange) {
return $statusChange->objectLabel;
});

self::assertEquals(["B-1", "A-3", "A-2", "A-1"], array_values($objectIds));
}
}

0 comments on commit 821c1ac

Please sign in to comment.