Skip to content

Commit

Permalink
Merge pull request #55 from cultuurnet/feature/III-2331
Browse files Browse the repository at this point in the history
Feature/iii 2331
  • Loading branch information
Luc Wollants committed Nov 7, 2017
2 parents 9f0c5c6 + c259e97 commit b3b743f
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CultureFeed/CultureFeed/Uitpas.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function registerDistributionKeysForOrganizer($cdbid, $distribution_keys)
*/
public function getDistributionKeysForOrganizer($cdbid);

/**
* Get the card systems for a given organizer.
*
* @param string $cdbid The CDBID of the given organizer
* @return CultureFeed_ResultSet The set of card systems
*/
public function getCardSystemsForOrganizer($cdbid);

/**
* Get the price of the UitPas.
*
Expand Down Expand Up @@ -425,6 +433,36 @@ public function updateEvent(CultureFeed_Uitpas_Event_CultureEvent $event);
*/
public function getEvent($id);

/**
* Get the card systems for a given event.
*
* @param string $cdbid The CDBID of the given event
* @return CultureFeed_ResultSet The set of card systems
*/
public function getCardSystemsForEvent($cdbid);

/**
* Add a card system to the event.
*
* @param string $cdbid
* @param string $cardSystemId
* @param string|null $distributionKey
* Only required for manual distribution keys.
*
* @return CultureFeed_Uitpas_Response
*/
public function addCardSystemToEvent($cdbid, $cardSystemId, $distributionKey = null);

/**
* Delete a card system from the event.
*
* @param string $cdbid
* @param string $cardSystemId
*
* @return CultureFeed_Uitpas_Response
*/
public function deleteCardSystemFromEvent($cdbid, $cardSystemId);

/**
* @param string $permanent if permanent only permanent card systems need to be sent over.
* @return CultureFeed_Uitpas_CardSystem[]
Expand Down
77 changes: 77 additions & 0 deletions CultureFeed/CultureFeed/Uitpas/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ public function getDistributionKeysForOrganizer($cdbid) {
return new CultureFeed_ResultSet($total, $distribution_keys);
}

/**
* {@inheritdoc}
*/
public function getCardSystemsForOrganizer($cdbid) {
$result = $this->oauth_client->consumerGetAsXML('uitpas/distributionkey/organiser/' . $cdbid, []);
try {
$xml = new CultureFeed_SimpleXMLElement($result);
} catch (Exception $e) {
throw new CultureFeed_ParseException($result);
}

$cardSystems = [];
foreach ($xml->xpath('/response/cardSystems/cardSystem') as $cardSystemXml) {
$cardSystems[] = CultureFeed_Uitpas_CardSystem::createFromXML($cardSystemXml);
}

$total = count($cardSystems);
return new CultureFeed_ResultSet($total, $cardSystems);
}

/**
* Register a set of distribution keys for an organizer. The entire set (including existing)
* of distribution keys must be provided.
Expand Down Expand Up @@ -318,6 +338,63 @@ public function getEvent($id) {
return CultureFeed_Uitpas_Event_CultureEvent::createFromXML($xml);
}

/**
* {@inheritdoc}
*/
public function getCardSystemsForEvent($cdbid) {
$result = $this->oauth_client->consumerGetAsXML('uitpas/cultureevent/' . $cdbid . '/cardsystems', []);
try {
$xml = new CultureFeed_SimpleXMLElement($result);
} catch (Exception $e) {
throw new CultureFeed_ParseException($result);
}

$cardSystems = [];
foreach ($xml->xpath('/response/cardSystems/cardSystem') as $cardSystemXml) {
$cardSystems[] = CultureFeed_Uitpas_CardSystem::createFromXML($cardSystemXml);
}

$total = count($cardSystems);
return new CultureFeed_ResultSet($total, $cardSystems);
}

/**
* {@inheritdoc}
*/
public function addCardSystemToEvent($cdbid, $cardSystemId, $distributionKey = NULL) {
$postData = array_filter(
[
'cardSystemId' => $cardSystemId,
'distributionKey' => $distributionKey,
]
);

return $this->consumerPostWithSimpleResponse('uitpas/cultureevent/' . $cdbid . '/cardsystems', $postData);
}

/**
* {@inheritdoc}
*/
public function deleteCardSystemFromEvent($cdbid, $cardSystemId) {
$result = $this->oauth_client->request(
'uitpas/cultureevent/' . $cdbid . '/cardsystems/' . $cardSystemId,
[],
'DELETE',
FALSE
);

try {
$xml = new CultureFeed_SimpleXMLElement($result);
}
catch (Exception $e) {
throw new CultureFeed_ParseException($result);
}

$response = CultureFeed_Uitpas_Response::createFromXML($xml->xpath('/response', false));

return $response;
}

/**
* Performs a consumer authenticated POST request expecting a simple response.
*
Expand Down
51 changes: 51 additions & 0 deletions CultureFeed/test/uitpas/CultureFeed_Uitpas_CardSystemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class CultureFeed_Uitpas_CardSystemsTest extends PHPUnit_Framework_TestCase {

const ORGANIZERCDBID = "47B6FA21-ACB1-EA8F-2C231182C7DD0A19";

public function testGetCardSystemsForOrganizer() {
/** @var CultureFeed_OAuthClient|PHPUnit_Framework_MockObject_MockObject $oauth_client_stub */
$oauth_client_stub = $this->getMock('CultureFeed_OAuthClient');

$get_xml = file_get_contents(dirname(__FILE__) . '/data/cardsystems/get.xml');

$oauth_client_stub->expects($this->once())
->method('consumerGetAsXML')
->will($this->returnValue($get_xml));

$cf = new CultureFeed($oauth_client_stub);

$data = $cf->uitpas()->getCardSystemsForOrganizer(self::ORGANIZERCDBID);

$this->assertInstanceOf('CultureFeed_ResultSet', $data);

$this->assertContainsOnly('CultureFeed_Uitpas_CardSystem', $data->objects);
$this->assertCount(2, $data->objects);
$this->assertEquals(2, $data->total);

/* @var \CultureFeed_Uitpas_CardSystem $cardSystem */
$cardSystem = reset($data->objects);

$this->assertEquals(1, $cardSystem->id);
$this->assertEquals("UiTPAS Dender", $cardSystem->name);

$this->assertContainsOnly('CultureFeed_Uitpas_DistributionKey', $cardSystem->distributionKeys);
$this->assertCount(3, $cardSystem->distributionKeys);

$this->assertEquals(25, $cardSystem->distributionKeys[0]->id);
$this->assertEquals('25% meerdaags (regio)', $cardSystem->distributionKeys[0]->name);

$this->assertEquals(23, $cardSystem->distributionKeys[1]->id);
$this->assertEquals('€1,5 dag (regio)', $cardSystem->distributionKeys[1]->name);

$this->assertEquals(24, $cardSystem->distributionKeys[2]->id);
$this->assertEquals('€3 hele dag (regio)', $cardSystem->distributionKeys[2]->name);

$cardSystem = next($data->objects);

$this->assertEquals(15, $cardSystem->id);
$this->assertEquals("UiTPAS", $cardSystem->name);
$this->assertEmpty($cardSystem->distributionKeys);
}
}
131 changes: 131 additions & 0 deletions CultureFeed/test/uitpas/Event/CardSystemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

class CultureFeed_Uitpas_Event_CardSystemsTest extends PHPUnit_Framework_TestCase {
const EVENTCDBID = "47B6FA21-ACB1-EA8F-2C231182C7DD0A19";

public function testGetCardSystemsForEvent() {
/** @var CultureFeed_OAuthClient|PHPUnit_Framework_MockObject_MockObject $oauth_client_stub */
$oauth_client_stub = $this->getMock('CultureFeed_OAuthClient');

$get_xml = file_get_contents(dirname(__FILE__) . '/../data/cultureevent/getCardSystems.xml');

$oauth_client_stub->expects($this->once())
->method('consumerGetAsXML')
->will($this->returnValue($get_xml));

$cf = new CultureFeed($oauth_client_stub);

$data = $cf->uitpas()->getCardSystemsForEvent(self::EVENTCDBID);

$this->assertInstanceOf('CultureFeed_ResultSet', $data);

$this->assertContainsOnly('CultureFeed_Uitpas_CardSystem', $data->objects);
$this->assertCount(1, $data->objects);
$this->assertEquals(1, $data->total);

/* @var \CultureFeed_Uitpas_CardSystem $cardSystem */
$cardSystem = reset($data->objects);

$this->assertEquals(1, $cardSystem->id);
$this->assertEquals("UiTPAS Dender", $cardSystem->name);

$this->assertContainsOnly('CultureFeed_Uitpas_DistributionKey', $cardSystem->distributionKeys);
$this->assertCount(1, $cardSystem->distributionKeys);

$this->assertEquals(27, $cardSystem->distributionKeys[0]->id);
$this->assertEquals('Speelplein HA', $cardSystem->distributionKeys[0]->name);
}

/**
* Test the registering of an event.
*/
public function testAddCardSystemToEvent() {

$response = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<response>
<code>ACTION_SUCCEEDED</code>
<cardSystems>
<cardSystem>
<id>1</id>
<name>UiTPAS Dender</name>
<distributionKeys>
<distributionKey>
<id>27</id>
<name>Speelplein HA</name>
<conditions>
<condition>
<definition>KANSARM</definition>
<operator>IN</operator>
<value>MY_CARDSYSTEM</value>
</condition>
</conditions>
<tariff>3.0</tariff>
<priceClasses>
<priceClass>
<name>Basistarief</name>
<price>10.0</price>
<tariff>3.0</tariff>
</priceClass>
</priceClasses>
<automatic>false</automatic>
</distributionKey>
</distributionKeys>
</cardSystem>
</cardSystems>
</response>
XML;

/** @var CultureFeed_OAuthClient|PHPUnit_Framework_MockObject_MockObject $oauth_client_stub */
$oauth_client_stub = $this->getMock('CultureFeed_OAuthClient');
$oauth_client_stub
->expects($this->once())
->method('consumerPostAsXml')
->with($this->equalTo('uitpas/cultureevent/' . self::EVENTCDBID . '/cardsystems'))
->will($this->returnValue($response));

$cf = new CultureFeed($oauth_client_stub);

$response = $cf->uitpas()->addCardSystemToEvent(self::EVENTCDBID, 1, 27);

$this->assertInstanceOf('\CultureFeed_Uitpas_Response', $response);

$this->assertEquals('ACTION_SUCCEEDED', $response->code);
$this->assertNull($response->message);
}

public function testDeleteCardSystemFromEvent() {

$response = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<response>
<code>ACTION_SUCCEEDED</code>
<cardSystems/>
</response>
XML;

$cardSystemId = 1;

/** @var CultureFeed_OAuthClient|PHPUnit_Framework_MockObject_MockObject $oauth_client_stub */
$oauth_client_stub = $this->getMock('CultureFeed_OAuthClient');
$oauth_client_stub
->expects($this->once())
->method('request')
->with(
'uitpas/cultureevent/' . self::EVENTCDBID . '/cardsystems/' . $cardSystemId,
[],
'DELETE',
FALSE
)
->willReturn($response);

$cf = new CultureFeed($oauth_client_stub);

$response = $cf->uitpas()->deleteCardSystemFromEvent(self::EVENTCDBID, $cardSystemId);

$this->assertInstanceOf('\CultureFeed_Uitpas_Response', $response);

$this->assertEquals('ACTION_SUCCEEDED', $response->code);
$this->assertNull($response->message);
}
}
31 changes: 31 additions & 0 deletions CultureFeed/test/uitpas/data/cardsystems/get.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<cardSystems>
<cardSystem>
<distributionKeys>
<distributionKey>
<global>false</global>
<id>25</id>
<name>25% meerdaags (regio)</name>
</distributionKey>
<distributionKey>
<global>false</global>
<id>23</id>
<name>€1,5 dag (regio)</name>
</distributionKey>
<distributionKey>
<global>false</global>
<id>24</id>
<name>€3 hele dag (regio)</name>
</distributionKey>
</distributionKeys>
<id>1</id>
<name>UiTPAS Dender</name>
</cardSystem>
<cardSystem>
<distributionKeys/>
<id>15</id>
<name>UiTPAS</name>
</cardSystem>
</cardSystems>
</response>
32 changes: 32 additions & 0 deletions CultureFeed/test/uitpas/data/cultureevent/getCardSystems.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<code>ACTION_SUCCEEDED</code>
<cardSystems>
<cardSystem>
<id>1</id>
<name>UiTPAS Dender</name>
<distributionKeys>
<distributionKey>
<id>27</id>
<name>Speelplein HA</name>
<conditions>
<condition>
<definition>KANSARM</definition>
<operator>IN</operator>
<value>MY_CARDSYSTEM</value>
</condition>
</conditions>
<tariff>3.0</tariff>
<priceClasses>
<priceClass>
<name>Basistarief</name>
<price>10.0</price>
<tariff>3.0</tariff>
</priceClass>
</priceClasses>
<automatic>false</automatic>
</distributionKey>
</distributionKeys>
</cardSystem>
</cardSystems>
</response>

0 comments on commit b3b743f

Please sign in to comment.