Skip to content

Commit

Permalink
Merge pull request #21 from mkpeacock/master
Browse files Browse the repository at this point in the history
Add support for filtering customers
  • Loading branch information
nthndnn authored Jul 26, 2018
2 parents e6fae1f + 39c2c24 commit 65cf68c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Api/Customers/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public function list(array $parameters = [], array $headers = [])
{
$resolver = $this->createOptionsResolver();

$resolver->setDefined('email[is]')
->setAllowedTypes('email[is]', 'string');

$resolver->setDefined('first_name[is]')
->setAllowedTypes('first_name[is]', 'string');

$resolver->setDefined('last_name[is]')
->setAllowedTypes('last_name[is]', 'string');

$url = $this->url('customers');

return $this->get($url, $resolver->resolve($parameters), $headers);
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Api/Customers/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\Api\Customers;

use NathanDunn\Chargebee\Api\Customers\Customer;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Tests\Unit\Api\TestCase;

class CustomerTest extends TestCase
Expand All @@ -21,6 +22,49 @@ public function should_list_customers()
$this->assertEquals($expected, $customer->list());
}

/**
* @test
* @dataProvider filters
*/
public function should_filter_customers($filters)
{
$expected = $this->getContent(sprintf('%s/data/responses/customer_list.json', __DIR__));

$customer = $this->getApiMock();
$customer->expects($this->once())
->method('get')
->with('https://123456789.chargebee.com/api/v2/customers', $filters)
->will($this->returnValue($expected));

$this->assertEquals($expected, $customer->list($filters));
}

public function filters()
{
return [
'email' => [['email[is]' => 'test@test.com']],
'first name' => [['first_name[is]' => 'John']],
'last name' => [['last_name[is]' => 'Doe']],
];
}

/**
* @test
*/
public function should_reject_unregistered_filters()
{
$filters = ['unkown' => 'field'];

$expected = $this->getContent(sprintf('%s/data/responses/customer_list.json', __DIR__));

$customer = $this->getApiMock();
$customer->expects($this->never())
->method('get');

$this->expectException(UndefinedOptionsException::class);
$customer->list($filters);
}

/** @test */
public function should_find_customer()
{
Expand Down

0 comments on commit 65cf68c

Please sign in to comment.