Skip to content

Commit

Permalink
Merge pull request #15 from seatsio/create-multiple-events-in-one-go
Browse files Browse the repository at this point in the history
create multiple events in one go
  • Loading branch information
bverbeken authored Jan 29, 2019
2 parents da7d5bb + 2577f9c commit 3e931de
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Events/EventCreationParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Seatsio\Events;

use Exception;

class EventCreationParams
{

/**
* @var string
*/
public $eventKey;

/**
* @var boolean|object|array
*/
public $bookWholeTablesOrTableBookingModes;

public function __construct($eventKey = null)
{
$this->eventKey = $eventKey;
}

public function setBookWholeTablesOrTableBookingModes($bookWholeTablesOrTableBookingModes)
{
$this->bookWholeTablesOrTableBookingModes = $bookWholeTablesOrTableBookingModes;
return $this;
}

/**
* @param $bookWholeTables bool
* @return EventCreationParams
* @throws Exception
*/
public function setBookWholeTables($bookWholeTables)
{
if (is_bool($bookWholeTables)) {
return $this->setBookWholeTablesOrTableBookingModes($bookWholeTables);
} else {
throw new Exception('expected bool param');
}
}

/**
* @param $tableBookingModes
* @return EventCreationParams
* @throws Exception
*/
public function setTableBookingModes($tableBookingModes)
{
if (is_array($tableBookingModes)) {
return $this->setBookWholeTablesOrTableBookingModes($tableBookingModes);
} else {
throw new Exception('expected array param');
}
}

}
23 changes: 23 additions & 0 deletions src/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ public function create($chartKey, $eventKey = null, $bookWholeTablesOrTableBooki
return $mapper->map($json, new Event());
}

public function createMultiple($chartKey, $eventCreationParams)
{
$request = new \stdClass();
$request->chartKey = $chartKey;
$request->events = array();
foreach ($eventCreationParams as $param) {
$eventToCreate = new \stdClass();
if ($param->eventKey !== null) {
$eventToCreate->eventKey = $param->eventKey;
}
if(is_bool($param->bookWholeTablesOrTableBookingModes)) {
$eventToCreate->bookWholeTables = $param->bookWholeTablesOrTableBookingModes;
} else if ($param->bookWholeTablesOrTableBookingModes !== null) {
$eventToCreate -> tableBookingModes = $param->bookWholeTablesOrTableBookingModes;
}
$request->events[] = $eventToCreate;
}
$res = $this->client->post('/events/actions/create-multiple', ['json' => $request]);
$json = \GuzzleHttp\json_decode($res->getBody());
$mapper = SeatsioJsonMapper::create();
return $mapper->mapArray($json->events, array(), 'Seatsio\Events\Event');
}

/**
* @param $eventKey string
* @return Event
Expand Down
81 changes: 81 additions & 0 deletions tests/Events/CreateEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Seatsio\Events;

use Seatsio\SeatsioClientTest;

class CreateEventsTest extends SeatsioClientTest
{

public function test_multipleEvents()
{
$chartKey = $this->createTestChart();
$params = [new EventCreationParams(), new EventCreationParams()];

$events = $this->seatsioClient->events->createMultiple($chartKey, $params);

self::assertEquals(2, sizeof($events));
foreach ($events as $e) {
self::assertNotNull($e->key);
}
}

public function test_single_event()
{
$chartKey = $this->createTestChart();
$params = [new EventCreationParams()];

$events = $this->seatsioClient->events->createMultiple($chartKey, $params);

self::assertEquals(1, sizeof($events));
$event = $events[0];
self::assertNotEquals(0, $event->id);
self::assertNotNull($event->id);
self::assertNotNull($event->key);
self::assertEquals($chartKey, $event->chartKey);
self::assertFalse($event->bookWholeTables);
self::assertNull($event->supportsBestAvailable);
self::assertNull($event->forSaleConfig);
self::assertNull($event->updatedOn);
}

public function test_event_key_can_be_passed_in()
{
$chartKey = $this->createTestChart();
$params = [new EventCreationParams("event1"), new EventCreationParams("event2")];

$events = $this->seatsioClient->events->createMultiple($chartKey, $params);

self::assertEquals(2, sizeof($events));
self::assertEquals("event1", $events[0]->key);
self::assertEquals("event2", $events[1]->key);
}

public function test_bookWholeTablesCanBePassedIn()
{
$chartKey = $this->createTestChart();
$params = [(new EventCreationParams())->setBookWholeTables(true), (new EventCreationParams())->setBookWholeTables(false)];

$events = $this->seatsioClient->events->createMultiple($chartKey, $params);

self::assertEquals(2, sizeof($events));
self::assertTrue($events[0]->bookWholeTables);
self::assertFalse($events[1]->bookWholeTables);
}

public function test_tableBookingModesCanBePassedIn()
{
$chartKey = $this->createTestChartWithTables();
$params = [(new EventCreationParams())->setTableBookingModes(["T1" => "BY_TABLE", "T2" => "BY_SEAT"]), (new EventCreationParams())->setTableBookingModes(["T1" => "BY_SEAT", "T2" => "BY_TABLE"])];

$events = $this->seatsioClient->events->createMultiple($chartKey, $params);

self::assertEquals(2, sizeof($events));
self::assertFalse($events[0]->bookWholeTables);
self::assertEquals((object)["T1" => "BY_TABLE", "T2" => "BY_SEAT"], $events[0]->tableBookingModes);
self::assertFalse($events[1]->bookWholeTables);
self::assertEquals((object)["T1" => "BY_SEAT", "T2" => "BY_TABLE"], $events[1]->tableBookingModes);
}


}

0 comments on commit 3e931de

Please sign in to comment.