diff --git a/src/Resources/Contacts.php b/src/Resources/Contacts.php index f1cfdf03..ec646343 100644 --- a/src/Resources/Contacts.php +++ b/src/Resources/Contacts.php @@ -2,9 +2,14 @@ namespace SevenShores\Hubspot\Resources; +/** + * @see https://developers.hubspot.com/docs/methods/contacts/contacts-overview + */ class Contacts extends Resource { /** + * Create a new contact. + * * @param array $properties array of contact properties * * @return \SevenShores\Hubspot\Http\Response @@ -15,12 +20,16 @@ public function create(array $properties) { $endpoint = 'https://api.hubapi.com/contacts/v1/contact'; - $options['json'] = ['properties' => $properties]; - - return $this->client->request('post', $endpoint, $options); + return $this->client->request( + 'post', + $endpoint, + ['json' => ['properties' => $properties]] + ); } /** + * Update an existing contact. + * * @param int $id the contact id * @param array $properties the contact properties to update * @@ -32,12 +41,16 @@ public function update($id, array $properties) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/vid/{$id}/profile"; - $options['json'] = ['properties' => $properties]; - - return $this->client->request('post', $endpoint, $options); + return $this->client->request( + 'post', + $endpoint, + ['json' => ['properties' => $properties]] + ); } /** + * Update an existing contact by email. + * * @param string $email the contact's email address * @param array $properties the contact properties to update * @@ -49,12 +62,16 @@ public function updateByEmail(string $email, array $properties) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/email/{$email}/profile"; - $options['json'] = ['properties' => $properties]; - - return $this->client->request('post', $endpoint, $options); + return $this->client->request( + 'post', + $endpoint, + ['json' => ['properties' => $properties]] + ); } /** + * Create or update a contact. + * * @param string $email the contact's email address * @param array $properties the contact properties * @@ -66,12 +83,16 @@ public function createOrUpdate(string $email, array $properties = []) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/{$email}"; - $options['json'] = ['properties' => $properties]; - - return $this->client->request('post', $endpoint, $options); + return $this->client->request( + 'post', + $endpoint, + ['json' => ['properties' => $properties]] + ); } /** + * Create or update a group of contacts. + * * @param array $contacts the contacts and properties * @param array $params Array of optional parameters ['auditId'] * @@ -83,14 +104,17 @@ public function createOrUpdateBatch(array $contacts, array $params = []) { $endpoint = 'https://api.hubapi.com/contacts/v1/contact/batch'; - $queryString = build_query_string($params); - - $options['json'] = $contacts; - - return $this->client->request('post', $endpoint, $options, $queryString); + return $this->client->request( + 'post', + $endpoint, + ['json' => $contacts], + build_query_string($params) + ); } /** + * Delete a contact. + * * @param int $id * * @return \SevenShores\Hubspot\Http\Response @@ -124,9 +148,12 @@ public function all(array $params = []) { $endpoint = 'https://api.hubapi.com/contacts/v1/lists/all/contacts/all'; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** @@ -145,9 +172,12 @@ public function recent(array $params = []) { $endpoint = 'https://api.hubapi.com/contacts/v1/lists/recently_updated/contacts/recent'; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** @@ -166,12 +196,17 @@ public function recentNew(array $params = []) { $endpoint = 'https://api.hubapi.com/contacts/v1/lists/all/contacts/recent'; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** + * Get a contact by vid(id). + * * @param int $id * @param array $params Array of optional parameters ['property', 'propertyMode', 'formSubmissionMode', * 'showListMemberships'] @@ -184,9 +219,12 @@ public function getById($id, array $params = []) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/vid/{$id}/profile"; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** @@ -210,12 +248,17 @@ public function getBatchByIds(array $vids, array $params = []) $params['vid'] = $vids; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** + * Get a contact by email address. + * * @param array $params Array of optional parameters ['property', 'propertyMode', 'formSubmissionMode', * 'showListMemberships'] * @@ -227,9 +270,12 @@ public function getByEmail(string $email, array $params = []) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/email/{$email}/profile"; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** @@ -246,18 +292,23 @@ public function getByEmail(string $email, array $params = []) * * @return \SevenShores\Hubspot\Http\Response */ - public function getBatchByEmails(array $emails, array $params = []) + public function getBatchByEmails($emails, array $params = []) { $endpoint = 'https://api.hubapi.com/contacts/v1/contact/emails/batch/'; $params['email'] = $emails; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** + * Get a contact by its user token. + * * @param array $params Array of optional parameters ['property', 'propertyMode', 'formSubmissionMode', * 'showListMemberships'] * @@ -269,9 +320,12 @@ public function getByToken(string $utk, array $params = []) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/utk/{$utk}/profile"; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** @@ -298,9 +352,12 @@ public function getBatchByTokens(array $utks, array $params = []) $params['utk'] = $utks; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** @@ -325,12 +382,17 @@ public function search(string $query, array $params = []) $params['q'] = $query; - $queryString = build_query_string($params); - - return $this->client->request('get', $endpoint, [], $queryString); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** + * @deprecated + * * @return \SevenShores\Hubspot\Http\Response */ public function statistics() @@ -356,8 +418,29 @@ public function merge($id, $vidToMerge) { $endpoint = "https://api.hubapi.com/contacts/v1/contact/merge-vids/{$id}/"; - $options['json'] = ['vidToMerge' => $vidToMerge]; + return $this->client->request( + 'post', + $endpoint, + ['json' => ['vidToMerge' => $vidToMerge]] + ); + } + + /** + * Get Lifecycle Stage metrics for Contacts. + * + * @see https://developers.hubspot.com/docs/methods/contacts/get-lifecycle-stage-metrics-for-contacts + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getLifecycleStageMetrics(array $params = []) + { + $endpoint = 'https://api.hubapi.com/contacts/v1/contacts/statistics'; - return $this->client->request('post', $endpoint, $options); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } } diff --git a/tests/Integration/Resources/ContactsTest.php b/tests/Integration/Resources/ContactsTest.php index 213a75b2..1a7aa7ed 100644 --- a/tests/Integration/Resources/ContactsTest.php +++ b/tests/Integration/Resources/ContactsTest.php @@ -2,118 +2,109 @@ namespace SevenShores\Hubspot\Tests\Integration\Resources; -use SevenShores\Hubspot\Http\Client; use SevenShores\Hubspot\Resources\Contacts; +use SevenShores\Hubspot\Tests\Integration\Abstraction\EntityTestCase; /** * @internal * @coversNothing */ -class ContactsTest extends \PHPUnit_Framework_TestCase +class ContactsTest extends EntityTestCase { - private $contacts; + /** + * @var SevenShores\Hubspot\Resources\Contacts + */ + protected $resource; - public function setUp() - { - parent::setUp(); - $this->contacts = new Contacts(new Client(['key' => getenv('HUBSPOT_TEST_API_KEY')])); - sleep(1); - } + /** + * @var SevenShores\Hubspot\Resources\Contacts::class + */ + protected $resourceClass = Contacts::class; /** @test */ public function allWithNoParams() { - $response = $this->contacts->all(); + $response = $this->resource->all(); $this->assertEquals(200, $response->getStatusCode()); + $this->assertGreaterThanOrEqual(1, count($response->contacts)); } /** @test */ public function allWithParams() { - $response = $this->contacts->all([ - 'count' => 2, + $response = $this->resource->all([ 'property' => ['firstname', 'lastname'], - 'vidOffset' => 1234, ]); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals(2, count($response->contacts)); - // $this->assertNotNull($response->contacts[0]->properties->email->value); - // $this->assertNotNull($response->contacts[0]->properties->lastname->value); - $this->assertGreaterThanOrEqual(1234, $response->{'vid-offset'}); + $this->assertGreaterThanOrEqual(1, count($response->contacts)); } /** @test */ public function allWithParamsAndArrayAccess() { - $response = $this->contacts->all([ - 'count' => 2, + $response = $this->resource->all([ 'property' => ['firstname', 'lastname'], - 'vidOffset' => 1234, ]); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals(2, count($response['contacts'])); - // $this->assertNotNull($response['contacts'][0]['properties']['firstname']['value']); - // $this->assertNotNull($response['contacts'][0]['properties']['lastname']['value']); - $this->assertGreaterThanOrEqual(1234, $response['vid-offset']); + $this->assertGreaterThanOrEqual(1, count($response->contacts)); } /** @test */ - public function update() + public function create() { - $contact = $this->createContact(); + $this->assertEquals(200, $this->entity->getStatusCode()); + } - $response = $this->contacts->update($contact->vid, [ + /** @test */ + public function update() + { + $response = $this->resource->update($this->entity->vid, [ ['property' => 'firstname', 'value' => 'joe'], ['property' => 'lastname', 'value' => 'user'], ]); $this->assertEquals(204, $response->getStatusCode()); - $this->contacts->delete($contact->vid); } /** @test */ public function updateByEmail() { - $contact = $this->createContact(); - - $response = $this->contacts->updateByEmail($contact->properties->email->value, [ + $response = $this->resource->updateByEmail($this->entity->properties->email->value, [ ['property' => 'firstname', 'value' => 'joe'], ['property' => 'lastname', 'value' => 'user'], ]); $this->assertEquals(204, $response->getStatusCode()); - $this->contacts->delete($contact->vid); } /** @test */ public function createOrUpdate() { - $response = $this->contacts->createOrUpdate('test@hubspot.com', [ + $response = $this->resource->createOrUpdate($this->entity->properties->email->value, [ ['property' => 'firstname', 'value' => 'joe'], ['property' => 'lastname', 'value' => 'user'], ]); $this->assertEquals(200, $response->getStatusCode()); - $this->contacts->delete($response->vid); } /** @test */ public function createOrUpdateBatch() { - $emails = ['test1@hubspot.com', 'test2@hubspot.com']; - $response = $this->contacts->createOrUpdateBatch([ + $secondEmail = 'test2@hubspot.com'; + $response = $this->resource->createOrUpdateBatch([ [ - 'email' => $emails[0], + 'email' => $this->entity->properties->email->value, 'properties' => [ ['property' => 'firstname', 'value' => 'joe'], ['property' => 'lastname', 'value' => 'user'], ], ], [ - 'email' => $emails[1], + 'email' => $secondEmail, 'properties' => [ ['property' => 'firstname', 'value' => 'jane'], ['property' => 'lastname', 'value' => 'user'], @@ -122,16 +113,16 @@ public function createOrUpdateBatch() ]); $this->assertEquals(202, $response->getStatusCode()); - foreach ($this->contacts->getBatchByEmails($emails)->getData() as $contact) { - $this->contacts->delete($contact->vid); - } + sleep(1); + $contact = $this->resource->getByEmail($secondEmail)->getData(); + $this->resource->delete($contact->vid); } /** @test */ public function createOrUpdateBatchWithAuditId() { $emails = ['testWithAuditId3@hubspot.com', 'testWithAuditId4@hubspot.com']; - $response = $this->contacts->createOrUpdateBatch([ + $response = $this->resource->createOrUpdateBatch([ [ 'email' => $emails[0], 'properties' => [ @@ -149,25 +140,25 @@ public function createOrUpdateBatchWithAuditId() ], ['auditId' => 'TEST_CHANGE_SOURCE']); $this->assertEquals(202, $response->getStatusCode()); - foreach ($this->contacts->getBatchByEmails($emails)->getData() as $contact) { - $this->contacts->delete($contact->vid); + foreach ($this->resource->getBatchByEmails($emails)->getData() as $contact) { + $this->resource->delete($contact->vid); } } /** @test */ public function delete() { - $contact = $this->createContact(); - - $response = $this->contacts->delete($contact->vid); + $response = $this->deleteEntity(); $this->assertEquals(200, $response->getStatusCode()); + + $this->entity = null; } /** @test */ public function recent() { - $response = $this->contacts->recent(['count' => 2]); + $response = $this->resource->recent(['count' => 2]); $this->assertEquals(200, $response->getStatusCode()); } @@ -175,111 +166,88 @@ public function recent() /** @test */ public function getById() { - $contact = $this->createContact(); - - $response = $this->contacts->getById($contact->vid); + $response = $this->resource->getById($this->entity->vid); $this->assertEquals(200, $response->getStatusCode()); - $this->contacts->delete($contact->vid); } /** @test */ public function getBatchByIds() { - $contacts = [ - $this->createContact(), - $this->createContact(), - ]; - - $ids = array_reduce($contacts, function ($vids, $contact) { - $vids[] = $contact->vid; + $contact = $this->createEntity(); - return $vids; - }, []); + $ids = [ + $this->entity->vid, + $contact->vid, + ]; - $response = $this->contacts->getBatchByIds($ids); + $response = $this->resource->getBatchByIds($ids); $this->assertEquals(200, $response->getStatusCode()); - foreach ($ids as $id) { - $this->contacts->delete($id); - } + $this->resource->delete($contact->vid); } /** @test */ public function getByEmail() { - $contact = $this->createContact(); - - $response = $this->contacts->getByEmail($contact->properties->email->value); + $response = $this->resource->getByEmail($this->entity->properties->email->value); $this->assertEquals(200, $response->getStatusCode()); - - $this->contacts->delete($contact->vid); } /** @test */ public function getBatchByEmails() { - $contacts = [ - $this->createContact(), - $this->createContact(), - ]; + $contact = $this->createEntity(); - $emails = array_reduce($contacts, function ($values, $contact) { - $values[] = $contact->properties->email->value; - - return $values; - }, []); + $emails = [ + $this->entity->properties->email->value, + $contact->properties->email->value, + ]; - $response = $this->contacts->getBatchByEmails($emails); + $response = $this->resource->getBatchByEmails($emails); $this->assertEquals(200, $response->getStatusCode()); - foreach ($contacts as $contact) { - $this->contacts->delete($contact->vid); - } + $this->resource->delete($contact->vid); } - public function getByToken() + /** @test */ + public function search() { - // TODO: This is harder... - } + $response = $this->resource->search('hub', ['count' => 2]); - public function getBatchByTokens() - { - // TODO: ... and so is this one + $this->assertEquals(200, $response->getStatusCode()); } /** @test */ - public function search() + public function statistics() { - $response = $this->contacts->search('hub', ['count' => 2]); + $response = $this->resource->statistics(); $this->assertEquals(200, $response->getStatusCode()); } /** @test */ - public function statistics() + public function getLifecycleStageMetrics() { - $response = $this->contacts->statistics(); + $response = $this->resource->getLifecycleStageMetrics(); $this->assertEquals(200, $response->getStatusCode()); } - // Lots of tests need an existing object to modify. - protected function createContact() + protected function createEntity() { - sleep(1); - - $response = $this->contacts->create([ + return $this->resource->create([ ['property' => 'email', 'value' => 'rw_test'.uniqid().'@hubspot.com'], ['property' => 'firstname', 'value' => 'joe'], ['property' => 'lastname', 'value' => 'user'], ]); + } - $this->assertEquals(200, $response->getStatusCode()); - - return $response; + protected function deleteEntity() + { + return $this->resource->delete($this->entity->vid); } }