Skip to content

Commit

Permalink
Merge pull request #368 from cmourizard/cmourizard/features/addTicket…
Browse files Browse the repository at this point in the history
…FieldOptionsSupport

Add support of Ticket Field Options API in SDK
  • Loading branch information
samgavinio authored Jan 18, 2018
2 parents 74493bf + 7662fce commit 4e4c5ce
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Zendesk/API/Resources/Core/TicketFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@

use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* The TicketFields class exposes field management methods for tickets
*/
class TicketFields extends ResourceAbstract
{
use InstantiatorTrait;

use Defaults;

protected $resourceName = 'ticket_fields';

/**
* {@inheritdoc}
*/
public static function getValidSubResources()
{
return [
'options' => TicketFieldsOptions::class,
];
}
}
141 changes: 141 additions & 0 deletions src/Zendesk/API/Resources/Core/TicketFieldsOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Zendesk\API\Resources\Core;

use Zendesk\API\Exceptions\CustomException;
use Zendesk\API\Exceptions\MissingParametersException;
use Zendesk\API\Http;
use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\Defaults;

/**
* The TicketFieldsOptions class exposes options methods for ticketsFields
*/
class TicketFieldsOptions extends ResourceAbstract
{
use Defaults {
findAll as traitFindAll;
find as traitFind;
create as traitCreate;
delete as traitDelete;
}

/**
* {@inheritdoc}
*/
protected $objectName = 'custom_field_option';

/**
* {@inheritdoc}
*/
protected $objectNamePlural = 'custom_field_options';

/**
* {@inheritdoc}
*/
protected function setUpRoutes()
{
$this->setRoutes(
[
'findAll' => 'ticket_fields/{fieldId}/options.json',
'find' => 'ticket_fields/{fieldId}/options/{id}.json',
'create' => 'ticket_fields/{fieldId}/options.json',
'update' => 'ticket_fields/{fieldId}/options.json',
'delete' => 'ticket_fields/{fieldId}/options/{id}.json',
]
);
}

/**
* Get the fieldId passed as a parameter or as a chained parameter
*
* @param array $params
*
* @throws MissingParametersException
*/
private function addTicketFieldIdToRouteParams(array $params)
{
if (isset($params['fieldId'])) {
$fieldId = $params['fieldId'];
} else {
$fieldId = $this->getChainedParameter(TicketFields::class);
}

if (empty($fieldId)) {
throw new MissingParametersException(__METHOD__, ['fieldId']);
}

$this->setAdditionalRouteParams(['fieldId' => $fieldId]);
}

/**
* Returns all options for a particular ticket field
*
* @param array $params
*
* @return \stdClass | null
* @throws MissingParametersException
*/
public function findAll(array $params = [])
{
$this->addTicketFieldIdToRouteParams($params);

return $this->traitFindAll($params);
}

/**
* Show a specific option record
*
* @param null|int $id
* @param array $params
* @return null|\stdClass
* @throws MissingParametersException
*/
public function find($id = null, array $params = [])
{
if (empty($id)) {
$id = $this->getChainedParameter(get_class($this));
}

$this->addTicketFieldIdToRouteParams($params);

return $this->traitFind($id);
}

/**
* {@inheritdoc}
*/
public function create(array $params = [])
{
$this->addTicketFieldIdToRouteParams($params);

return $this->traitCreate($params);
}

/**
* {@inheritdoc}
*/
public function update($id = null, array $updateResourceFields = [])
{

if (empty($id)) {
$id = $this->getChainedParameter(get_class($this));
}

$updateResourceFields['id'] = $id;

$this->addTicketFieldIdToRouteParams($updateResourceFields);

return $this->client->post($this->getRoute(__FUNCTION__), [$this->objectName => $updateResourceFields]);
}

/**
* {@inheritdoc}
*/
public function delete($id = null)
{
$this->addTicketFieldIdToRouteParams([]);

return $this->traitDelete($id);
}
}
71 changes: 71 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/TicketFieldsOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Zendesk\API\UnitTests\Core;

use Zendesk\API\Resources\Core\TicketFieldsOptions;
use Zendesk\API\UnitTests\BasicTest;

/**
* TicketFieldsOptionsTest test class
*/
class TicketFieldsOptionsTest extends BasicTest
{

/**
* Tests if the unique routes are called correctly
*/
public function testRoutes()
{
$fieldId = 3124;
$id = 123;
$optionValues = [
'name' => 'one more',
'value'=> 'ça bouge',
];

// FindAll
$this->assertEquals(
"ticket_fields/{$fieldId}/options.json",
$this->client->ticketFields($fieldId)->options()->getRoute(
'findAll',
['fieldId' => $fieldId]
)
);

// Create
$this->assertEquals(
"ticket_fields/{$fieldId}/options.json",
$this->client->ticketFields($fieldId)->options()->getRoute(
'create',
['fieldId' => $fieldId]
)
);

// Find
$this->assertEquals(
"ticket_fields/{$fieldId}/options/{$id}.json",
$this->client->ticketFields($fieldId)->options($id)->getRoute(
'find',
['id' => $id, 'fieldId' => $fieldId]
)
);

// Delete
$this->assertEquals(
"ticket_fields/{$fieldId}/options/{$id}.json",
$this->client->ticketFields($fieldId)->options($id)->getRoute(
'delete',
['id' => $id, 'fieldId' => $fieldId]
)
);

// Update
$this->assertEquals(
"ticket_fields/{$fieldId}/options.json",
$this->client->ticketFields($fieldId)->options($id, $optionValues)->getRoute(
'update',
['id' => $id, 'fieldId' => $fieldId]
)
);
}
}

0 comments on commit 4e4c5ce

Please sign in to comment.