Skip to content

Commit 65cf68c

Browse files
authored
Merge pull request #21 from mkpeacock/master
Add support for filtering customers
2 parents e6fae1f + 39c2c24 commit 65cf68c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Api/Customers/Customer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public function list(array $parameters = [], array $headers = [])
2121
{
2222
$resolver = $this->createOptionsResolver();
2323

24+
$resolver->setDefined('email[is]')
25+
->setAllowedTypes('email[is]', 'string');
26+
27+
$resolver->setDefined('first_name[is]')
28+
->setAllowedTypes('first_name[is]', 'string');
29+
30+
$resolver->setDefined('last_name[is]')
31+
->setAllowedTypes('last_name[is]', 'string');
32+
2433
$url = $this->url('customers');
2534

2635
return $this->get($url, $resolver->resolve($parameters), $headers);

tests/Unit/Api/Customers/CustomerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit\Api\Customers;
44

55
use NathanDunn\Chargebee\Api\Customers\Customer;
6+
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
67
use Tests\Unit\Api\TestCase;
78

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

25+
/**
26+
* @test
27+
* @dataProvider filters
28+
*/
29+
public function should_filter_customers($filters)
30+
{
31+
$expected = $this->getContent(sprintf('%s/data/responses/customer_list.json', __DIR__));
32+
33+
$customer = $this->getApiMock();
34+
$customer->expects($this->once())
35+
->method('get')
36+
->with('https://123456789.chargebee.com/api/v2/customers', $filters)
37+
->will($this->returnValue($expected));
38+
39+
$this->assertEquals($expected, $customer->list($filters));
40+
}
41+
42+
public function filters()
43+
{
44+
return [
45+
'email' => [['email[is]' => 'test@test.com']],
46+
'first name' => [['first_name[is]' => 'John']],
47+
'last name' => [['last_name[is]' => 'Doe']],
48+
];
49+
}
50+
51+
/**
52+
* @test
53+
*/
54+
public function should_reject_unregistered_filters()
55+
{
56+
$filters = ['unkown' => 'field'];
57+
58+
$expected = $this->getContent(sprintf('%s/data/responses/customer_list.json', __DIR__));
59+
60+
$customer = $this->getApiMock();
61+
$customer->expects($this->never())
62+
->method('get');
63+
64+
$this->expectException(UndefinedOptionsException::class);
65+
$customer->list($filters);
66+
}
67+
2468
/** @test */
2569
public function should_find_customer()
2670
{

0 commit comments

Comments
 (0)