Skip to content

Commit

Permalink
Merge pull request #482 from zendesk/jramos/version-bump
Browse files Browse the repository at this point in the history
[PEGASUS-2095] Add Webhooks support
  • Loading branch information
7ail authored Feb 10, 2022
2 parents 6e5eba7 + 83a1672 commit 0676e4c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Zendesk/API/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Zendesk\API\Resources\Core\UserFields;
use Zendesk\API\Resources\Core\Users;
use Zendesk\API\Resources\Core\Views;
use Zendesk\API\Resources\Core\Webhooks;
use Zendesk\API\Resources\Embeddable;
use Zendesk\API\Resources\HelpCenter;
use Zendesk\API\Resources\Talk;
Expand Down Expand Up @@ -98,6 +99,7 @@
* @method SuspendedTickets suspendedTickets()
* @method Tags tags($id = null)
* @method Targets targets()
* @method Webhooks webhooks()
* @method Tickets tickets($id = null)
* @method TicketFields ticketFields($id = null)
* @method TicketImports ticketImports()
Expand Down Expand Up @@ -281,6 +283,7 @@ public static function getValidSubResources()
'userFields' => UserFields::class,
'users' => Users::class,
'views' => Views::class,
'webhooks' => Webhooks::class,
];
}

Expand Down
55 changes: 55 additions & 0 deletions src/Zendesk/API/Resources/Core/Webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Zendesk\API\Resources\Core;

use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Resource\FindAll;

/**
* The Tags class exposes methods as detailed on https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/
*/
class Webhooks extends ResourceAbstract
{
use Defaults;
use FindAll {
findAll as traitFindAll;
}

/**
* {@inheritdoc}
*/
protected function setUpRoutes()
{
parent::setUpRoutes();

$this->setRoutes([
'create' => "{$this->resourceName}",
'update' => "{$this->resourceName}/{id}",
'delete' => "{$this->resourceName}",
'findAll' => "{$this->resourceName}",
]);
}

/**
* {@inheritdoc}
*/
public function findAll(array $params = [])
{
$queryParams = array_filter(array_flip($params), [$this, 'filterParams']);
$queryParams = array_merge($params, array_flip($queryParams));

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

/**
* Filter parameters passed and only allow valid query parameters.
*
* @param $param
* @return int
*/
private function filterParams($param)
{
return preg_match("/^sort|page[[a-zA-Z_]*]|filter[[a-zA-Z_]*](\\[\\]?)/", $param);
}
}
38 changes: 38 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/WebhooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ZEndesk\Api\UnitTests\Core;

use Zendesk\Api\UnitTests\BasicTest;

/**
* Webhooks test class
*/
class WebhooksTest extends BasicTest
{
/**
* Test find all method
*/

public function testFindAll()
{
$queryParams = [
'filter[name_contains]' => 'somerule',
];

// We expect invalid parameters are removed.
// We also expect url encoded keys and values
$expectedQueryParams = [];
foreach ($queryParams as $key => $value) {
$expectedQueryParams = array_merge($expectedQueryParams, [urlencode($key) => $value]);
}

$this->assertEndpointCalled(
function () use ($queryParams) {
$this->client->webhooks()->findAll($queryParams);
},
'webhooks',
'GET',
['queryParams' => $expectedQueryParams]
);
}
}

0 comments on commit 0676e4c

Please sign in to comment.