From c3510e414b1c2ccd2645d39517850924f03704b1 Mon Sep 17 00:00:00 2001 From: ksvirkou Date: Thu, 9 Jan 2020 15:47:33 +0300 Subject: [PATCH 1/2] code review of contacts --- src/Factory.php | 1 + src/Resources/Contacts.php | 37 ++++ tests/Integration/Resources/ContactsTest.php | 170 ++++++++----------- 3 files changed, 107 insertions(+), 101 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index c5a34352..8d3e9e22 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -19,6 +19,7 @@ * @method \SevenShores\Hubspot\Resources\ContactProperties contactProperties() * @method \SevenShores\Hubspot\Resources\Contacts contacts() * @method \SevenShores\Hubspot\Resources\CrmAssociations crmAssociations() + * @method \SevenShores\Hubspot\Resources\CrmPipelines crmPipelines() * @method \SevenShores\Hubspot\Resources\Email email() * @method \SevenShores\Hubspot\Resources\EmailEvents emailEvents() * @method \SevenShores\Hubspot\Resources\Engagements engagements() diff --git a/src/Resources/Contacts.php b/src/Resources/Contacts.php index 8cde5d1e..7e49daed 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 @@ -21,6 +26,8 @@ public function create(array $properties) } /** + * Update an existing contact. + * * @param int $id the contact id * @param array $properties the contact properties to update * @@ -38,6 +45,8 @@ public function update($id, array $properties) } /** + * Update an existing contact by email. + * * @param string $email the contact's email address * @param array $properties the contact properties to update * @@ -55,6 +64,8 @@ public function updateByEmail($email, array $properties) } /** + * Create or update a contact. + * * @param string $email the contact's email address * @param array $properties the contact properties * @@ -72,6 +83,8 @@ public function createOrUpdate($email, array $properties = []) } /** + * Create or update a group of contacts. + * * @param array $contacts the contacts and properties * @param array $params Array of optional parameters ['auditId'] * @@ -91,6 +104,8 @@ public function createOrUpdateBatch(array $contacts, array $params = []) } /** + * Delete a contact. + * * @param int $id * * @return \SevenShores\Hubspot\Http\Response @@ -172,6 +187,8 @@ public function recentNew(array $params = []) } /** + * Get a contact by vid(id). + * * @param int $id * @param array $params Array of optional parameters ['property', 'propertyMode', 'formSubmissionMode', * 'showListMemberships'] @@ -216,6 +233,8 @@ public function getBatchByIds(array $vids, array $params = []) } /** + * Get a contact by email address. + * * @param string $email * @param array $params Array of optional parameters ['property', 'propertyMode', 'formSubmissionMode', * 'showListMemberships'] @@ -259,6 +278,8 @@ public function getBatchByEmails($emails, array $params = []) } /** + * Get a contact by its user token. + * * @param string $utk * @param array $params Array of optional parameters ['property', 'propertyMode', 'formSubmissionMode', * 'showListMemberships'] @@ -333,6 +354,8 @@ public function search($query, array $params = []) } /** + * @deprecated + * * @return \SevenShores\Hubspot\Http\Response */ public function statistics() @@ -362,4 +385,18 @@ public function merge($id, $vidToMerge) return $this->client->request('post', $endpoint, $options); } + + /** + * 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('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); } } From 6a320d71cfa025dd88453ecc869af7022c985620 Mon Sep 17 00:00:00 2001 From: ksvirkou Date: Thu, 9 Jan 2020 16:09:49 +0300 Subject: [PATCH 2/2] code review --- src/Resources/Contacts.php | 148 ++++++++++++++++++++++++------------- 1 file changed, 97 insertions(+), 51 deletions(-) diff --git a/src/Resources/Contacts.php b/src/Resources/Contacts.php index 7e49daed..33a1877a 100644 --- a/src/Resources/Contacts.php +++ b/src/Resources/Contacts.php @@ -20,9 +20,11 @@ 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]] + ); } /** @@ -39,9 +41,11 @@ 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]] + ); } /** @@ -58,9 +62,11 @@ public function updateByEmail($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]] + ); } /** @@ -77,9 +83,11 @@ public function createOrUpdate($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]] + ); } /** @@ -96,11 +104,12 @@ 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) + ); } /** @@ -139,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) + ); } /** @@ -160,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) + ); } /** @@ -181,9 +196,12 @@ 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) + ); } /** @@ -201,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) + ); } /** @@ -227,9 +248,12 @@ 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) + ); } /** @@ -247,9 +271,12 @@ public function getByEmail($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) + ); } /** @@ -272,9 +299,12 @@ public function getBatchByEmails($emails, array $params = []) $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) + ); } /** @@ -292,9 +322,12 @@ public function getByToken($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) + ); } /** @@ -321,9 +354,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) + ); } /** @@ -348,9 +384,12 @@ public function search($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) + ); } /** @@ -381,9 +420,11 @@ 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, $options); + return $this->client->request( + 'post', + $endpoint, + ['json' => ['vidToMerge' => $vidToMerge]] + ); } /** @@ -397,6 +438,11 @@ public function getLifecycleStageMetrics(array $params = []) { $endpoint = 'https://api.hubapi.com/contacts/v1/contacts/statistics'; - return $this->client->request('get', $endpoint, [], build_query_string($params)); + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); } }