From c3436c117feb9b3aa2440a23807a73ae0a6339d7 Mon Sep 17 00:00:00 2001 From: mroloux Date: Thu, 31 Jan 2019 14:48:09 +0100 Subject: [PATCH 1/4] Status changes can be sorted and filtered --- src/Events/Events.php | 42 ++++++++++++---- src/PageFetcher.php | 6 ++- .../Events/ListStatusChangesForObjectTest.php | 2 +- tests/Events/ListStatusChangesTest.php | 50 +++++++++++++++++++ 4 files changed, 86 insertions(+), 14 deletions(-) diff --git a/src/Events/Events.php b/src/Events/Events.php index 6809f2e..43e93b6 100644 --- a/src/Events/Events.php +++ b/src/Events/Events.php @@ -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; } @@ -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(); })); @@ -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; diff --git a/src/PageFetcher.php b/src/PageFetcher.php index 5150b3c..ec9fdd6 100644 --- a/src/PageFetcher.php +++ b/src/PageFetcher.php @@ -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) @@ -39,7 +41,7 @@ public function fetch($queryParams, $pageSize) if ($pageSize) { $queryParams['limit'] = $pageSize; } - $res = $this->client->get($this->url, ['query' => $queryParams]); + $res = $this->client->get($this->url, ['query' => array_merge($queryParams, $this->queryParams)]); $json = \GuzzleHttp\json_decode($res->getBody()); $mapper = SeatsioJsonMapper::create(); return $mapper->map($json, $this->pageCreator->__invoke()); diff --git a/tests/Events/ListStatusChangesForObjectTest.php b/tests/Events/ListStatusChangesForObjectTest.php index b419046..57e89af 100644 --- a/tests/Events/ListStatusChangesForObjectTest.php +++ b/tests/Events/ListStatusChangesForObjectTest.php @@ -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)); diff --git a/tests/Events/ListStatusChangesTest.php b/tests/Events/ListStatusChangesTest.php index 560b4e5..00aff38 100644 --- a/tests/Events/ListStatusChangesTest.php +++ b/tests/Events/ListStatusChangesTest.php @@ -44,4 +44,54 @@ 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 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)); + } } \ No newline at end of file From b6e38596e3bd7be34c55451679035ac88516fade Mon Sep 17 00:00:00 2001 From: mroloux Date: Fri, 1 Feb 2019 09:45:30 +0100 Subject: [PATCH 2/4] Set time zone to something different than UTC, so that tests fail if they don't process dates properly --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 21aa3d7..a71d590 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file From 5879aef09899b49f618f56b34c2f0f33c3da8c4f Mon Sep 17 00:00:00 2001 From: mroloux Date: Fri, 1 Feb 2019 09:49:24 +0100 Subject: [PATCH 3/4] Fixed build --- src/PageFetcher.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PageFetcher.php b/src/PageFetcher.php index ec9fdd6..6aaa715 100644 --- a/src/PageFetcher.php +++ b/src/PageFetcher.php @@ -41,7 +41,8 @@ public function fetch($queryParams, $pageSize) if ($pageSize) { $queryParams['limit'] = $pageSize; } - $res = $this->client->get($this->url, ['query' => array_merge($queryParams, $this->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()); From 3e3985eafdb85c2d9baa9ef3eca8740b18155f28 Mon Sep 17 00:00:00 2001 From: mroloux Date: Tue, 5 Feb 2019 12:13:30 +0100 Subject: [PATCH 4/4] Add tests --- tests/Events/ListStatusChangesTest.php | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Events/ListStatusChangesTest.php b/tests/Events/ListStatusChangesTest.php index 00aff38..8c77c0d 100644 --- a/tests/Events/ListStatusChangesTest.php +++ b/tests/Events/ListStatusChangesTest.php @@ -78,6 +78,46 @@ public function testSortAsc() 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();