| 1 |
<?php |
| 2 |
|
| 3 |
namespace Square\Tests\Integration; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use RuntimeException; |
| 8 |
use Square\Customers\Cards\Requests\DeleteCardsRequest; |
| 9 |
use Square\Customers\Cards\Requests\CreateCustomerCardRequest; |
| 10 |
use Square\Customers\CustomAttributeDefinitions\Requests\CreateCustomerCustomAttributeDefinitionRequest; |
| 11 |
use Square\Customers\CustomAttributeDefinitions\Requests\DeleteCustomAttributeDefinitionsRequest ; |
| 12 |
use Square\Customers\CustomAttributes\Requests\DeleteCustomAttributesRequest; |
| 13 |
use Square\Customers\CustomAttributes\Requests\ListCustomAttributesRequest; |
| 14 |
use Square\Customers\CustomAttributes\Requests\UpsertCustomerCustomAttributeRequest; |
| 15 |
use Square\Customers\Groups\Requests\CreateCustomerGroupRequest; |
| 16 |
use Square\Customers\Groups\Requests\AddGroupsRequest; |
| 17 |
use Square\Customers\Groups\Requests\DeleteGroupsRequest; |
| 18 |
use Square\Customers\Groups\Requests\RemoveGroupsRequest; |
| 19 |
use Square\Customers\Requests\CreateCustomerRequest; |
| 20 |
use Square\Customers\Requests\DeleteCustomersRequest; |
| 21 |
use Square\Customers\Requests\GetCustomersRequest; |
| 22 |
use Square\Customers\Requests\SearchCustomersRequest; |
| 23 |
use Square\Customers\Requests\UpdateCustomerRequest; |
| 24 |
use Square\Exceptions\SquareApiException; |
| 25 |
use Square\Exceptions\SquareException; |
| 26 |
use Square\SquareClient; |
| 27 |
use Square\Types\Address; |
| 28 |
use Square\Types\CustomAttribute; |
| 29 |
use Square\Types\CustomAttributeDefinition; |
| 30 |
use Square\Types\CustomerGroup; |
| 31 |
|
| 32 |
class CustomersTest extends TestCase |
| 33 |
{ |
| 34 |
private static SquareClient $client; |
| 35 |
private static string $customerId; |
| 36 |
private static string $customerGroupId; |
| 37 |
private static string $customAttributeKey; |
| 38 |
|
| 39 |
/** |
| 40 |
* @throws SquareException |
| 41 |
* @throws SquareApiException |
| 42 |
*/ |
| 43 |
public static function setUpBeforeClass(): void |
| 44 |
{ |
| 45 |
self::$client = Helpers::createClient(); |
| 46 |
|
| 47 |
// Create test customer |
| 48 |
$customerResponse = self::$client->customers->create(new CreateCustomerRequest([ |
| 49 |
'idempotencyKey' => uniqid(), |
| 50 |
'givenName' => 'Amelia', |
| 51 |
'familyName' => 'Earhart', |
| 52 |
'phoneNumber' => '1-212-555-4240', |
| 53 |
'note' => 'a customer', |
| 54 |
'address' => new Address([ |
| 55 |
'addressLine1' => '500 Electric Ave', |
| 56 |
'addressLine2' => 'Suite 600', |
| 57 |
'locality' => 'New York', |
| 58 |
'administrativeDistrictLevel1' => 'NY', |
| 59 |
'postalCode' => '10003', |
| 60 |
'country' => 'US', |
| 61 |
]), |
| 62 |
])); |
| 63 |
$customer = $customerResponse->getCustomer(); |
| 64 |
if (!$customer) { |
| 65 |
throw new RuntimeException('Failed to create test customer.'); |
| 66 |
} |
| 67 |
$customerId = $customer->getId(); |
| 68 |
if (!$customerId) { |
| 69 |
throw new RuntimeException('Customer ID is null.'); |
| 70 |
} |
| 71 |
self::$customerId = $customerId; |
| 72 |
|
| 73 |
// Create a customer group for testing |
| 74 |
$createGroupResponse = self::$client->customers->groups->create(new CreateCustomerGroupRequest([ |
| 75 |
'idempotencyKey' => uniqid(), |
| 76 |
'group' => new CustomerGroup([ |
| 77 |
'name' => 'Test Group ' . uniqid(), |
| 78 |
]), |
| 79 |
])); |
| 80 |
$group = $createGroupResponse->getGroup(); |
| 81 |
if ($group === null) { |
| 82 |
throw new RuntimeException('Failed to create test customer group.'); |
| 83 |
} |
| 84 |
$groupId = $group->getId(); |
| 85 |
if ($groupId === null) { |
| 86 |
throw new RuntimeException('Group ID is null.'); |
| 87 |
} |
| 88 |
self::$customerGroupId = $groupId; |
| 89 |
|
| 90 |
// Create custom attribute definition |
| 91 |
self::$customAttributeKey = 'favorite-drink-' . uniqid(); |
| 92 |
self::$client->customers->customAttributeDefinitions->create(new CreateCustomerCustomAttributeDefinitionRequest([ |
| 93 |
'customAttributeDefinition' => new CustomAttributeDefinition([ |
| 94 |
'key' => self::$customAttributeKey, |
| 95 |
'name' => 'Favorite Drink ' . uniqid(), |
| 96 |
'description' => "A customer's favorite drink", |
| 97 |
'visibility' => 'VISIBILITY_READ_WRITE_VALUES', |
| 98 |
'schema' => [ |
| 99 |
'$ref' => 'https://developer-production-s.squarecdn.com/schemas/v1/common.json#squareup.common.String', |
| 100 |
], |
| 101 |
]), |
| 102 |
])); |
| 103 |
} |
| 104 |
|
| 105 |
public static function tearDownAfterClass(): void |
| 106 |
{ |
| 107 |
try { |
| 108 |
self::$client->customers->delete(new DeleteCustomersRequest(['customerId' => self::$customerId])); |
| 109 |
} catch (Exception) { |
| 110 |
} |
| 111 |
try { |
| 112 |
self::$client->customers->groups->delete(new DeleteGroupsRequest(['groupId' => self::$customerGroupId])); |
| 113 |
} catch (Exception) { |
| 114 |
} |
| 115 |
try { |
| 116 |
self::$client->customers->customAttributeDefinitions |
| 117 |
->delete(new DeleteCustomAttributeDefinitionsRequest (['key' => self::$customAttributeKey])); |
| 118 |
} catch (Exception) { |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @throws SquareApiException |
| 124 |
* @throws SquareException |
| 125 |
*/ |
| 126 |
public function testSearchCustomers(): void |
| 127 |
{ |
| 128 |
$searchRequest = new SearchCustomersRequest(); |
| 129 |
$searchRequest->setLimit(1); |
| 130 |
$response = self::$client->customers->search($searchRequest); |
| 131 |
|
| 132 |
$this->assertNotNull($response->getCustomers()); |
| 133 |
$this->assertGreaterThan(0, count($response->getCustomers())); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @throws SquareException |
| 138 |
* @throws SquareApiException |
| 139 |
*/ |
| 140 |
public function testRetrieveCustomer(): void |
| 141 |
{ |
| 142 |
$retrieveRequest = new GetCustomersRequest(['customerId' => self::$customerId]); |
| 143 |
$response = self::$client->customers->get($retrieveRequest); |
| 144 |
|
| 145 |
$customer = $response->getCustomer(); |
| 146 |
$this->assertNotNull($customer, 'Failed to retrieve customer.'); |
| 147 |
$this->assertEquals(self::$customerId, $customer->getId()); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @throws SquareApiException |
| 152 |
* @throws SquareException |
| 153 |
*/ |
| 154 |
public function testUpdateCustomer(): void |
| 155 |
{ |
| 156 |
$updateRequest = new UpdateCustomerRequest([ |
| 157 |
'customerId' => self::$customerId, |
| 158 |
'givenName' => 'Updated Amelia', |
| 159 |
'familyName' => 'Updated Earhart', |
| 160 |
]); |
| 161 |
$response = self::$client->customers->update($updateRequest); |
| 162 |
|
| 163 |
$customer = $response->getCustomer(); |
| 164 |
$this->assertNotNull($customer, 'Failed to update customer.'); |
| 165 |
$this->assertEquals('Updated Amelia', $customer->getGivenName()); |
| 166 |
$this->assertEquals('Updated Earhart', $customer->getFamilyName()); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @throws SquareApiException |
| 171 |
* @throws SquareException |
| 172 |
*/ |
| 173 |
public function testCreateCustomerCard(): void |
| 174 |
{ |
| 175 |
$createCardRequest = new CreateCustomerCardRequest([ |
| 176 |
'customerId' => self::$customerId, |
| 177 |
'cardNonce' => 'cnon:card-nonce-ok', |
| 178 |
]); |
| 179 |
$createCardResponse = self::$client->customers->cards->create($createCardRequest); |
| 180 |
|
| 181 |
$this->assertNotNull($createCardResponse->getCard()); |
| 182 |
$customerCardId = $createCardResponse->getCard()->getId(); |
| 183 |
$this->assertNotNull($customerCardId); |
| 184 |
|
| 185 |
$deleteCardResponse = self::$client->customers->cards->delete(new DeleteCardsRequest([ |
| 186 |
'customerId' => self::$customerId, |
| 187 |
'cardId' => $customerCardId, |
| 188 |
])); |
| 189 |
$this->assertNotNull($deleteCardResponse); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @throws SquareApiException |
| 194 |
* @throws SquareException |
| 195 |
*/ |
| 196 |
public function testAddCustomerToGroup(): void |
| 197 |
{ |
| 198 |
$addGroupResponse = self::$client->customers->groups->add(new AddGroupsRequest([ |
| 199 |
'customerId' => self::$customerId, |
| 200 |
'groupId' => self::$customerGroupId, |
| 201 |
])); |
| 202 |
$this->assertNotNull($addGroupResponse); |
| 203 |
|
| 204 |
$removeGroupResponse = self::$client->customers->groups->remove(new RemoveGroupsRequest([ |
| 205 |
'customerId' => self::$customerId, |
| 206 |
'groupId' => self::$customerGroupId, |
| 207 |
])); |
| 208 |
$this->assertNotNull($removeGroupResponse); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @throws SquareApiException |
| 213 |
* @throws SquareException |
| 214 |
*/ |
| 215 |
public function testCreateCustomerCustomAttribute(): void |
| 216 |
{ |
| 217 |
$createAttrResponse = self::$client->customers->customAttributes->upsert(new UpsertCustomerCustomAttributeRequest([ |
| 218 |
'customerId' => self::$customerId, |
| 219 |
'key' => self::$customAttributeKey, |
| 220 |
'customAttribute' => new CustomAttribute([ |
| 221 |
'value' => 'Double-shot breve', |
| 222 |
]), |
| 223 |
])); |
| 224 |
|
| 225 |
$this->assertNotNull($createAttrResponse->getCustomAttribute()); |
| 226 |
$this->assertEquals('Double-shot breve', $createAttrResponse->getCustomAttribute()->getValue()); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* @throws SquareApiException |
| 231 |
* @throws SquareException |
| 232 |
*/ |
| 233 |
public function testUpdateCustomerCustomAttribute(): void |
| 234 |
{ |
| 235 |
$updateAttrResponse = self::$client->customers->customAttributes->upsert(new UpsertCustomerCustomAttributeRequest([ |
| 236 |
'customerId' => self::$customerId, |
| 237 |
'key' => self::$customAttributeKey, |
| 238 |
'customAttribute' => new CustomAttribute([ |
| 239 |
'value' => 'Black coffee' |
| 240 |
]) |
| 241 |
])); |
| 242 |
|
| 243 |
$customAttribute = $updateAttrResponse->getCustomAttribute(); |
| 244 |
$this->assertNotNull($customAttribute); |
| 245 |
$this->assertEquals('Black coffee', $customAttribute->getValue()); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* @throws SquareApiException |
| 250 |
* @throws SquareException |
| 251 |
*/ |
| 252 |
public function testListCustomerCustomAttributes(): void |
| 253 |
{ |
| 254 |
self::$client->customers->customAttributes->upsert(new UpsertCustomerCustomAttributeRequest([ |
| 255 |
'customerId' => self::$customerId, |
| 256 |
'key' => self::$customAttributeKey, |
| 257 |
'customAttribute' => new CustomAttribute([ |
| 258 |
'value' => 'Double-shot breve', |
| 259 |
]), |
| 260 |
])); |
| 261 |
|
| 262 |
$pager = self::$client->customers->customAttributes->list(new ListCustomAttributesRequest([ |
| 263 |
'customerId' => self::$customerId, |
| 264 |
'withDefinitions' => true, |
| 265 |
])); |
| 266 |
$this->assertNotNull($pager); |
| 267 |
|
| 268 |
/** @var CustomAttribute[] $attributes */ |
| 269 |
$attributes = iterator_to_array($pager->getIterator()); |
| 270 |
$this->assertNotNull($attributes); |
| 271 |
$this->assertGreaterThan(0, count($attributes)); |
| 272 |
|
| 273 |
$deleteAttrResponse = self::$client->customers->customAttributes->delete(new DeleteCustomAttributesRequest([ |
| 274 |
'customerId' => self::$customerId, |
| 275 |
'key' => self::$customAttributeKey, |
| 276 |
])); |
| 277 |
$this->assertNotNull($deleteAttrResponse); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @throws SquareApiException |
| 282 |
* @throws SquareException |
| 283 |
*/ |
| 284 |
public function testDeleteCustomerCustomAttribute(): void |
| 285 |
{ |
| 286 |
self::$client->customers->customAttributes->upsert(new UpsertCustomerCustomAttributeRequest([ |
| 287 |
'customerId' => self::$customerId, |
| 288 |
'key' => self::$customAttributeKey, |
| 289 |
'customAttribute' =>new CustomAttribute( [ |
| 290 |
'value' => 'Double-shot breve', |
| 291 |
]), |
| 292 |
])); |
| 293 |
|
| 294 |
$response = self::$client->customers->customAttributes->delete(new DeleteCustomAttributesRequest([ |
| 295 |
'customerId' => self::$customerId, |
| 296 |
'key' => self::$customAttributeKey, |
| 297 |
])); |
| 298 |
$this->assertNotNull($response); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* @throws SquareApiException |
| 303 |
* @throws SquareException |
| 304 |
*/ |
| 305 |
public function testDeleteCustomer(): void |
| 306 |
{ |
| 307 |
$deleteRequest = new DeleteCustomersRequest(['customerId' => self::$customerId]); |
| 308 |
$response = self::$client->customers->delete($deleteRequest); |
| 309 |
|
| 310 |
$this->assertNotNull($response); |
| 311 |
} |
| 312 |
} |
| 313 |
|