Skip to content

Commit

Permalink
Merge pull request #13 from seatsio/nami/subaccount-filter
Browse files Browse the repository at this point in the history
Nami/subaccount filter
  • Loading branch information
namfal authored Dec 19, 2018
2 parents 727d98c + 844165f commit 56c6038
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 13 deletions.
55 changes: 55 additions & 0 deletions src/Subaccounts/FilterableSubaccountLister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Seatsio\Subaccounts;

class FilterableSubaccountLister
{

protected $pageFetcher;

public function __construct($pageFetcher)
{
$this->pageFetcher = $pageFetcher;
}

/**
* @param $queryParams SubaccountListParams[]
* @return SubaccountPagedIterator
*/
public function all($queryParams)
{
return new SubaccountPagedIterator($this->pageFetcher, $queryParams);
}

/**
* @param $pageSize int
* @param $queryParams SubaccountListParams[]
* @return SubaccountPage
*/
public function firstPage($pageSize = null, $queryParams)
{
return $this->pageFetcher->fetchAfter(null, $queryParams, $pageSize);
}

/**
* @param $afterId int
* @param $pageSize int
* @param $queryParams SubaccountListParams[]
* @return SubaccountPage
*/
public function pageAfter($afterId, $pageSize = null, $queryParams)
{
return $this->pageFetcher->fetchAfter($afterId, $queryParams, $pageSize);
}

/**
* @param $beforeId int
* @param $pageSize int
* @param $queryParams SubaccountListParams[]
* @return SubaccountPage
*/
public function pageBefore($beforeId, $pageSize = null, $queryParams)
{
return $this->pageFetcher->fetchBefore($beforeId, $queryParams, $pageSize);
}
}
42 changes: 42 additions & 0 deletions src/Subaccounts/SubaccountListParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Seatsio\Subaccounts;

class SubaccountListParams
{
/**
* @var string
*/
public $filter;

/**
* @param $filter string
*/
public function __construct($filter = null)
{
$this->filter = $filter;
}

/**
* @param $filter string
* @return $this
*/
public function withFilter($filter)
{
$this->filter = $filter;
return $this;
}

public function toArray()
{
$result = [];

if ($this->filter !== null) {
$result['filter'] = $this->filter;
}

return $result;
}

}

38 changes: 25 additions & 13 deletions src/Subaccounts/Subaccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,50 +167,62 @@ public function copyChartToSubaccount($fromId, $toId, $chartKey)
}

/**
* @param $subaccountListParams SubaccountListParams
* @return SubaccountPagedIterator
*/
public function listAll()
public function listAll($subaccountListParams = null)
{
return $this->iterator()->all();
return $this->iterator()->all($this->listParamsToArray($subaccountListParams));
}

/**
* @param $pageSize int
* @return SubaccountsPage
* @param $subaccountListParams SubaccountListParams
* @return SubaccountPage
*/
public function listFirstPage($pageSize = null)
public function listFirstPage($pageSize = null, $subaccountListParams = null)
{
return $this->iterator()->firstPage($pageSize);
return $this->iterator()->firstPage($pageSize, $this->listParamsToArray($subaccountListParams));
}

/**
* @param $afterId int
* @param $pageSize int
* @return SubaccountsPage
* @param $subaccountListParams SubaccountListParams
* @return SubaccountPage
*/
public function listPageAfter($afterId, $pageSize = null)
public function listPageAfter($afterId, $pageSize = null, $subaccountListParams = null)
{
return $this->iterator()->pageAfter($afterId, $pageSize);
return $this->iterator()->pageAfter($afterId, $pageSize, $this->listParamsToArray($subaccountListParams));
}

/**
* @param $beforeId int
* @param $pageSize int
* @return SubaccountsPage
* @param $subaccountListParams SubaccountListParams
* @return SubaccountPage
*/
public function listPageBefore($beforeId, $pageSize = null)
public function listPageBefore($beforeId, $pageSize = null, $subaccountListParams = null)
{
return $this->iterator()->pageBefore($beforeId, $pageSize);
return $this->iterator()->pageBefore($beforeId, $pageSize, $this->listParamsToArray($subaccountListParams));
}

/**
* @return SubaccountLister
* @return FilterableSubaccountLister
*/
private function iterator()
{
return new SubaccountLister(new PageFetcher('/subaccounts', $this->client, function () {
return new FilterableSubaccountLister(new PageFetcher('/subaccounts', $this->client, function () {
return new SubaccountPage();
}));
}

private function listParamsToArray($subaccountListParams)
{
if ($subaccountListParams == null) {
return [];
}
return $subaccountListParams->toArray();
}

}
88 changes: 88 additions & 0 deletions tests/Subacounts/ListAllSubaccountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,92 @@ public function test()
self::assertEquals([$subaccount3->id, $subaccount2->id, $subaccount1->id], array_values($subaccountIds));
}

public function testWithFilter()
{
$this->seatsioClient->subaccounts->create();
$subaccount2 = $this->seatsioClient->subaccounts->create("subaccount2");
$this->seatsioClient->subaccounts->create();

$subaccounts = $this->seatsioClient->subaccounts->listAll((new SubaccountListParams())->withFilter('subaccount2'));
$subaccountIds = \Functional\map($subaccounts, function($subaccount) { return $subaccount->id; });

self::assertEquals([$subaccount2->id], array_values($subaccountIds));
}

public function testWithFilterContainingSpecialCharacter()
{
$createdSubaccountIds = array();

for ($i = 0; $i < 55; $i++) {
$subaccount = $this->seatsioClient->subaccounts->create("test-/@/" . $i);
if($i == 4 || ($i >= 40 && $i <=49)) {
$createdSubaccountIds[] = $subaccount->id;
}
}

$subaccounts = $this->seatsioClient->subaccounts->listAll((new SubaccountListParams())->withFilter('test-/@/4'));
$retrievedSubaccountIds = \Functional\map($subaccounts, function($subaccount) { return $subaccount->id; });

self::assertEquals($createdSubaccountIds, $retrievedSubaccountIds, "", 0.0, 10, true);
}

public function testWithFilterNoResult()
{
$subaccounts = $this->seatsioClient->subaccounts->listAll((new SubaccountListParams())->withFilter('test'));
$subaccountIds = \Functional\map($subaccounts, function($subaccount) { return $subaccount->id; });

self::assertEmpty(array_values($subaccountIds));
}

public function testWithFilterFirstPage()
{
$this->seatsioClient->subaccounts->create("test-/@/1");
$subaccount2 = $this->seatsioClient->subaccounts->create("test-/@/2");
$this->seatsioClient->subaccounts->create("test-/@/3");

$subaccounts = $this->seatsioClient->subaccounts->listFirstPage(null, (new SubaccountListParams())->withFilter('test-/@/2'));
$subaccountIds = \Functional\map($subaccounts->items, function($subaccount) { return $subaccount->id; });

self::assertEquals([$subaccount2->id], array_values($subaccountIds));
self::assertEmpty($subaccounts->nextPageStartsAfter);
self::assertEmpty($subaccounts->previousPageEndsBefore);
}

public function testWithFilterPreviousPage()
{
$subaccount1 = $this->seatsioClient->subaccounts->create("test-/@/11");
$subaccount2 = $this->seatsioClient->subaccounts->create("test-/@/12");
$subaccount3 = $this->seatsioClient->subaccounts->create("test-/@/33");
$this->seatsioClient->subaccounts->create("test-/@/14");
$this->seatsioClient->subaccounts->create("test-/@/5");
$this->seatsioClient->subaccounts->create("test-/@/6");
$this->seatsioClient->subaccounts->create("test-/@/7");
$this->seatsioClient->subaccounts->create("test-/@/8");

$subaccounts = $this->seatsioClient->subaccounts->listPageAfter($subaccount3->id, null, (new SubaccountListParams())->withFilter('test-/@/1'));
$subaccountIds = \Functional\map($subaccounts->items, function($subaccount) { return $subaccount->id; });

self::assertEquals([$subaccount1->id, $subaccount2->id], $subaccountIds, "", 0.0, 10, true);
self::assertEmpty($subaccounts->nextPageStartsAfter);
self::assertEquals($subaccount2->id, $subaccounts->previousPageEndsBefore);
}

public function testWithFilterNextPage()
{
$subaccount1 = $this->seatsioClient->subaccounts->create("test-/@/11");
$subaccount2 = $this->seatsioClient->subaccounts->create("test-/@/12");
$subaccount3 = $this->seatsioClient->subaccounts->create("test-/@/13");
$this->seatsioClient->subaccounts->create("test-/@/4");
$this->seatsioClient->subaccounts->create("test-/@/5");
$this->seatsioClient->subaccounts->create("test-/@/6");
$this->seatsioClient->subaccounts->create("test-/@/7");
$this->seatsioClient->subaccounts->create("test-/@/8");

$subaccounts = $this->seatsioClient->subaccounts->listPageBefore($subaccount1->id, null, (new SubaccountListParams())->withFilter('test-/@/1'));
$subaccountIds = \Functional\map($subaccounts->items, function($subaccount) { return $subaccount->id; });

self::assertEquals([$subaccount3->id, $subaccount2->id], $subaccountIds, "", 0.0, 10, true);
self::assertEmpty($subaccounts->previousPageEndsBefore);
self::assertEquals($subaccount2->id, $subaccounts->nextPageStartsAfter);
}
}

0 comments on commit 56c6038

Please sign in to comment.