CustomerAdvancedSearchTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Test\Integration;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Test\Setup;
  5. use Braintree;
  6. class CustomerAdvancedSearchTest extends Setup
  7. {
  8. public function test_noMatches()
  9. {
  10. $collection = Braintree\Customer::search([
  11. Braintree\CustomerSearch::company()->is('badname')
  12. ]);
  13. $this->assertEquals(0, $collection->maximumCount());
  14. }
  15. public function test_noRequestsWhenIterating()
  16. {
  17. $resultsReturned = false;
  18. $collection = Braintree\Customer::search([
  19. Braintree\CustomerSearch::firstName()->is('badname')
  20. ]);
  21. foreach($collection as $customer) {
  22. $resultsReturned = true;
  23. break;
  24. }
  25. $this->assertSame(0, $collection->maximumCount());
  26. $this->assertEquals(false, $resultsReturned);
  27. }
  28. public function test_findDuplicateCardsGivenPaymentMethodToken()
  29. {
  30. $creditCardRequest = ['number' => '63049580000009', 'expirationDate' => '05/2012'];
  31. $jim = Braintree\Customer::create(['firstName' => 'Jim', 'creditCard' => $creditCardRequest])->customer;
  32. $joe = Braintree\Customer::create(['firstName' => 'Joe', 'creditCard' => $creditCardRequest])->customer;
  33. $query = [Braintree\CustomerSearch::paymentMethodTokenWithDuplicates()->is($jim->creditCards[0]->token)];
  34. $collection = Braintree\Customer::search($query);
  35. $customerIds = [];
  36. foreach($collection as $customer)
  37. {
  38. $customerIds[] = $customer->id;
  39. }
  40. $this->assertTrue(in_array($jim->id, $customerIds));
  41. $this->assertTrue(in_array($joe->id, $customerIds));
  42. }
  43. public function test_searchOnTextFields()
  44. {
  45. $token = 'cctoken' . rand();
  46. $search_criteria = [
  47. 'firstName' => 'Timmy',
  48. 'lastName' => 'OToole',
  49. 'company' => 'OToole and Son(s)' . rand(),
  50. 'email' => 'timmy@example.com',
  51. 'website' => 'http://example.com',
  52. 'phone' => '3145551234',
  53. 'fax' => '3145551235',
  54. 'cardholderName' => 'Tim Toole',
  55. 'creditCardExpirationDate' => '05/2010',
  56. 'creditCardNumber' => '4111111111111111',
  57. 'paymentMethodToken' => $token,
  58. 'addressFirstName' => 'Thomas',
  59. 'addressLastName' => 'Otool',
  60. 'addressStreetAddress' => '1 E Main St',
  61. 'addressExtendedAddress' => 'Suite 3',
  62. 'addressLocality' => 'Chicago',
  63. 'addressRegion' => 'Illinois',
  64. 'addressPostalCode' => '60622',
  65. 'addressCountryName' => 'United States of America'
  66. ];
  67. $customer = Braintree\Customer::createNoValidate([
  68. 'firstName' => $search_criteria['firstName'],
  69. 'lastName' => $search_criteria['lastName'],
  70. 'company' => $search_criteria['company'],
  71. 'email' => $search_criteria['email'],
  72. 'fax' => $search_criteria['fax'],
  73. 'phone' => $search_criteria['phone'],
  74. 'website' => $search_criteria['website'],
  75. 'creditCard' => [
  76. 'cardholderName' => 'Tim Toole',
  77. 'number' => '4111111111111111',
  78. 'expirationDate' => $search_criteria['creditCardExpirationDate'],
  79. 'token' => $token,
  80. 'billingAddress' => [
  81. 'firstName' => $search_criteria['addressFirstName'],
  82. 'lastName' => $search_criteria['addressLastName'],
  83. 'streetAddress' => $search_criteria['addressStreetAddress'],
  84. 'extendedAddress' => $search_criteria['addressExtendedAddress'],
  85. 'locality' => $search_criteria['addressLocality'],
  86. 'region' => $search_criteria['addressRegion'],
  87. 'postalCode' => $search_criteria['addressPostalCode'],
  88. 'countryName' => 'United States of America'
  89. ]
  90. ]
  91. ]);
  92. $query = [Braintree\CustomerSearch::id()->is($customer->id)];
  93. foreach ($search_criteria AS $criterion => $value) {
  94. $query[] = Braintree\CustomerSearch::$criterion()->is($value);
  95. }
  96. $collection = Braintree\Customer::search($query);
  97. $this->assertEquals(1, $collection->maximumCount());
  98. $this->assertEquals($customer->id, $collection->firstItem()->id);
  99. foreach ($search_criteria AS $criterion => $value) {
  100. $collection = Braintree\Customer::search([
  101. Braintree\CustomerSearch::$criterion()->is($value),
  102. Braintree\CustomerSearch::id()->is($customer->id),
  103. ]);
  104. $this->assertEquals(1, $collection->maximumCount());
  105. $this->assertEquals($customer->id, $collection->firstItem()->id);
  106. $collection = Braintree\Customer::search([
  107. Braintree\CustomerSearch::$criterion()->is('invalid_attribute'),
  108. Braintree\CustomerSearch::id()->is($customer->id),
  109. ]);
  110. $this->assertEquals(0, $collection->maximumCount());
  111. }
  112. }
  113. public function test_createdAt()
  114. {
  115. $customer = Braintree\Customer::createNoValidate();
  116. $past = clone $customer->createdAt;
  117. $past->modify("-1 hour");
  118. $future = clone $customer->createdAt;
  119. $future->modify("+1 hour");
  120. $collection = Braintree\Customer::search([
  121. Braintree\CustomerSearch::id()->is($customer->id),
  122. Braintree\CustomerSearch::createdAt()->between($past, $future),
  123. ]);
  124. $this->assertEquals(1, $collection->maximumCount());
  125. $this->assertEquals($customer->id, $collection->firstItem()->id);
  126. $collection = Braintree\Customer::search([
  127. Braintree\CustomerSearch::id()->is($customer->id),
  128. Braintree\CustomerSearch::createdAt()->lessThanOrEqualTo($future),
  129. ]);
  130. $this->assertEquals(1, $collection->maximumCount());
  131. $this->assertEquals($customer->id, $collection->firstItem()->id);
  132. $collection = Braintree\Customer::search([
  133. Braintree\CustomerSearch::id()->is($customer->id),
  134. Braintree\CustomerSearch::createdAt()->greaterThanOrEqualTo($past),
  135. ]);
  136. $this->assertEquals(1, $collection->maximumCount());
  137. $this->assertEquals($customer->id, $collection->firstItem()->id);
  138. }
  139. public function test_paypalAccountEmail()
  140. {
  141. $http = new HttpClientApi(Braintree\Configuration::$global);
  142. $nonce = $http->nonceForPayPalAccount([
  143. 'paypal_account' => [
  144. 'consent_code' => 'PAYPAL_CONSENT_CODE',
  145. ]
  146. ]);
  147. $customerId = 'UNIQUE_CUSTOMER_ID-' . strval(rand());
  148. $customerResult = Braintree\Customer::create([
  149. 'paymentMethodNonce' => $nonce,
  150. 'id' => $customerId
  151. ]);
  152. $this->assertTrue($customerResult->success);
  153. $customer = $customerResult->customer;
  154. $collection = Braintree\Customer::search([
  155. Braintree\CustomerSearch::id()->is($customer->id),
  156. Braintree\CustomerSearch::paypalAccountEmail()->is('jane.doe@example.com')
  157. ]);
  158. $this->assertEquals(1, $collection->maximumCount());
  159. $this->assertEquals($customer->id, $collection->firstItem()->id);
  160. }
  161. public function test_throwsIfNoOperatorNodeGiven()
  162. {
  163. $this->setExpectedException('InvalidArgumentException', 'Operator must be provided');
  164. Braintree\Customer::search([
  165. Braintree\CustomerSearch::creditCardExpirationDate()
  166. ]);
  167. }
  168. }