-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from cmourizard/cmourizard/features/addTicket…
…FieldOptionsSupport Add support of Ticket Field Options API in SDK
- Loading branch information
Showing
3 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
tests/Zendesk/API/UnitTests/Core/TicketFieldsOptionsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
) | ||
); | ||
} | ||
} |