Skip to content

Commit

Permalink
Add testIterator to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Oct 30, 2023
1 parent 72737d8 commit 48ac3f6
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
command: vendor/bin/phpunit --testsuite "Zendesk API Unit Test Suites"
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/TicketsTest.php
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php

volumes:
vendor:
4 changes: 2 additions & 2 deletions src/Zendesk/API/Resources/Core/CustomRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\FindAll;
use Zendesk\API\Traits\Utility\Pagination\ObpStrategy;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;

/**
* Class CustomRoles
Expand All @@ -15,6 +15,6 @@ class CustomRoles extends ResourceAbstract
use FindAll;

protected function paginationStrategyClass() {
return ObpStrategy::class;
return SinglePageStrategy::class;
}
}
6 changes: 6 additions & 0 deletions src/Zendesk/API/Resources/Core/SharingAgreements.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\FindAll;
use Zendesk\API\Traits\Utility\Pagination\ObpStrategy;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;

/**
* The SharingAgreements class
Expand All @@ -12,4 +14,8 @@
class SharingAgreements extends ResourceAbstract
{
use FindAll;

protected function paginationStrategyClass() {
return SinglePageStrategy::class;
}
}
5 changes: 5 additions & 0 deletions src/Zendesk/API/Resources/Core/Targets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;

/**
* The Tags class exposes methods as detailed on https://developer.zendesk.com/rest_api/docs/core/targets
*/
class Targets extends ResourceAbstract
{
use Defaults;

protected function paginationStrategyClass() {
return SinglePageStrategy::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

abstract class AbstractStrategy
{
// TODO: 100
public const DEFAULT_PAGE_SIZE = 2;
public const DEFAULT_PAGE_SIZE = 100;

/*
* @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()`
*/
protected $clientList;

/*
* The response key where the data is returned
* @var string The response key where the data is returned
*/
protected $resourcesKey;
protected $pageSize;
Expand Down
6 changes: 1 addition & 5 deletions src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public function getPage()
++$this->pageNumber;
$params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize];
$response = $this->clientList->findAll($params);
// TODO: remove
// echo "\npage ids: ";
// foreach ($response->{$this->resourcesKey} as $item) {
// echo $item->id . " ";
// }

return $response->{$this->resourcesKey};
}

Expand Down
12 changes: 3 additions & 9 deletions tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ public function setUp()

public function testIterator()
{
// CBP
// Single Page
$this->mockApiResponses([
new Response(200, [], json_encode([
'custom_roles' => [$this->testResource0, $this->testResource1],
'meta' => ['after_cursor' => '<after_cursor>', 'has_more' => true],
'custom_roles' => [$this->testResource0, $this->testResource1, $this->testResource2]

])),
new Response(200, [], json_encode([
'custom_roles' => [$this->testResource2],
'meta' => ['has_more' => false],

])),
]))
]);

$iterator = $this->client->customRoles()->iterator();
Expand Down
38 changes: 38 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/MacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Zendesk\API\UnitTests\Core;

use GuzzleHttp\Psr7\Response;
use Zendesk\API\UnitTests\BasicTest;

/**
Expand All @@ -10,6 +11,43 @@
*/
class MacrosTest extends BasicTest
{
protected $testResource0;
protected $testResource1;
protected $testResource2;

public function setUp()
{
$this->testResource0 = ['anyField' => 'Any field 0'];
$this->testResource1 = ['anyField' => 'Any field 1'];
$this->testResource2 = ['anyField' => 'Any field 2'];
parent::setUp();
}

public function testIterator()
{
// CBP
$this->mockApiResponses([
new Response(200, [], json_encode([
'macros' => [$this->testResource0, $this->testResource1],
'meta' => ['after_cursor' => '<after_cursor>', 'has_more' => true],

])),
new Response(200, [], json_encode([
'macros' => [$this->testResource2],
'meta' => ['has_more' => false],

])),
]);

$iterator = $this->client->macros()->iterator();

$actual = iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
$this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField);
}

/**
* Test the `GET /api/v2/macros/active.json` endpoint
* Lists active macros for the current user
Expand Down
38 changes: 38 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,51 @@

namespace Zendesk\API\UnitTests\Core;

use GuzzleHttp\Psr7\Response;
use Zendesk\API\UnitTests\BasicTest;

/**
* Class OrganizationMembershipsTest
*/
class OrganizationMembershipsTest extends BasicTest
{
protected $testResource0;
protected $testResource1;
protected $testResource2;

public function setUp()
{
$this->testResource0 = ['anyField' => 'Any field 0'];
$this->testResource1 = ['anyField' => 'Any field 1'];
$this->testResource2 = ['anyField' => 'Any field 2'];
parent::setUp();
}

public function testIterator()
{
// CBP
$this->mockApiResponses([
new Response(200, [], json_encode([
'organization_memberships' => [$this->testResource0, $this->testResource1],
'meta' => ['after_cursor' => '<after_cursor>', 'has_more' => true],

])),
new Response(200, [], json_encode([
'organization_memberships' => [$this->testResource2],
'meta' => ['has_more' => false],

])),
]);

$iterator = $this->client->organizationMemberships()->iterator();

$actual = iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
$this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField);
}

/**
* Test find method
*/
Expand Down
38 changes: 38 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/OrganizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,51 @@

namespace Zendesk\API\UnitTests\Core;

use GuzzleHttp\Psr7\Response;
use Zendesk\API\UnitTests\BasicTest;

/**
* Class OrganizationsTest
*/
class OrganizationsTest extends BasicTest
{
protected $testResource0;
protected $testResource1;
protected $testResource2;

public function setUp()
{
$this->testResource0 = ['anyField' => 'Any field 0'];
$this->testResource1 = ['anyField' => 'Any field 1'];
$this->testResource2 = ['anyField' => 'Any field 2'];
parent::setUp();
}

public function testIterator()
{
// CBP
$this->mockApiResponses([
new Response(200, [], json_encode([
'organizations' => [$this->testResource0, $this->testResource1],
'meta' => ['after_cursor' => '<after_cursor>', 'has_more' => true],

])),
new Response(200, [], json_encode([
'organizations' => [$this->testResource2],
'meta' => ['has_more' => false],

])),
]);

$iterator = $this->client->organizations()->iterator();

$actual = iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
$this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField);
}

/**
* test for FindAll with chained user resource.
*/
Expand Down
38 changes: 38 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,51 @@

namespace Zendesk\API\UnitTests\Core;

use GuzzleHttp\Psr7\Response;
use Zendesk\API\UnitTests\BasicTest;

/**
* Requests test class
*/
class RequestsTest extends BasicTest
{
protected $testResource0;
protected $testResource1;
protected $testResource2;

public function setUp()
{
$this->testResource0 = ['anyField' => 'Any field 0'];
$this->testResource1 = ['anyField' => 'Any field 1'];
$this->testResource2 = ['anyField' => 'Any field 2'];
parent::setUp();
}

public function testIterator()
{
// CBP
$this->mockApiResponses([
new Response(200, [], json_encode([
'requests' => [$this->testResource0, $this->testResource1],
'meta' => ['after_cursor' => '<after_cursor>', 'has_more' => true],

])),
new Response(200, [], json_encode([
'requests' => [$this->testResource2],
'meta' => ['has_more' => false],

])),
]);

$iterator = $this->client->requests()->iterator();

$actual = iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
$this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField);
}

/**
* test findAll method
*/
Expand Down
38 changes: 38 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/SatisfactionRatingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,51 @@

namespace Zendesk\API\UnitTests\Core;

use GuzzleHttp\Psr7\Response;
use Zendesk\API\UnitTests\BasicTest;

/**
* Satisfaction Ratings test class
*/
class SatisfactionRatingsTest extends BasicTest
{
protected $testResource0;
protected $testResource1;
protected $testResource2;

public function setUp()
{
$this->testResource0 = ['anyField' => 'Any field 0'];
$this->testResource1 = ['anyField' => 'Any field 1'];
$this->testResource2 = ['anyField' => 'Any field 2'];
parent::setUp();
}

public function testIterator()
{
// CBP
$this->mockApiResponses([
new Response(200, [], json_encode([
'satisfaction_ratings' => [$this->testResource0, $this->testResource1],
'meta' => ['after_cursor' => '<after_cursor>', 'has_more' => true],

])),
new Response(200, [], json_encode([
'satisfaction_ratings' => [$this->testResource2],
'meta' => ['has_more' => false],

])),
]);

$iterator = $this->client->satisfactionRatings()->iterator();

$actual = iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
$this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField);
}

/**
* Test that the correct traits were added by checking the available methods
*/
Expand Down
Loading

0 comments on commit 48ac3f6

Please sign in to comment.