diff options
-rw-r--r-- | src/Core/Communicator.php | 7 | ||||
-rw-r--r-- | src/Core/OwnableInterface.php | 12 | ||||
-rw-r--r-- | src/Recipient/Membership.php | 2 | ||||
-rw-r--r-- | src/Recipient/ProcessedContact.php | 43 | ||||
-rw-r--r-- | src/Recipient/ProcessedGroup.php | 33 | ||||
-rw-r--r-- | src/Recipient/ProcessedMembership.php | 20 | ||||
-rw-r--r-- | src/Recipient/RecipientFactory.php | 3 | ||||
-rw-r--r-- | tests/Recipient/MembershipTest.php | 79 | ||||
-rw-r--r-- | tests/Recipient/ProcessedContactTest.php | 3 | ||||
-rw-r--r-- | tests/Recipient/ProcessedGroupTest.php | 6 | ||||
-rw-r--r-- | tests/Util/Util.php | 12 | ||||
-rw-r--r-- | tests/resources/processed_contact/list_processed_contact.json | 2 | ||||
-rw-r--r-- | tests/resources/processed_contact/minimal_processed_contact.json | 2 | ||||
-rw-r--r-- | tests/resources/processed_contact/processed_contact.json | 2 |
14 files changed, 198 insertions, 28 deletions
diff --git a/src/Core/Communicator.php b/src/Core/Communicator.php index 915ba39..81b0331 100644 --- a/src/Core/Communicator.php +++ b/src/Core/Communicator.php @@ -16,6 +16,7 @@ use IP1\RESTClient\Recipient\ProcessedBlacklistEntry; use IP1\RESTClient\Core\ProcessedComponentInterface; use IP1\RESTClient\Core\UpdatableComponentInterface; use IP1\RESTClient\Core\ProcessableComponentInterface; +use \Httpful\Response; /** * Handles request to the API and converts the responses into the data classes. @@ -212,7 +213,7 @@ class Communicator * @param boolean $https Whether the the API call should use HTTPS or not(HTTP). * @return string The response from the API. */ - private function sendRequest(string $endPoint, string $method, string $content = "", bool $https = false): string + private function sendRequest(string $endPoint, string $method, string $content = "", bool $https = false): Response { $url = ($https ? "https://" : "http://") . self::DOMAIN . "/" .$endPoint; $request = \Httpful\Request::init($method, 'application/json'); @@ -226,8 +227,8 @@ class Communicator $response = $request->send(); if ($response->hasErrors()) { - $this->$errorResponses[] = $response; + $this->errorResponses[] = $response; } - return $response->__toString(); + return $response; } } diff --git a/src/Core/OwnableInterface.php b/src/Core/OwnableInterface.php new file mode 100644 index 0000000..c54281b --- /dev/null +++ b/src/Core/OwnableInterface.php @@ -0,0 +1,12 @@ +<?php +namespace IP1\RESTClient\Core; + +interface OwnableInterface +{ + + /** + * Returns ID of account owning the implemented object. + * @return ID of account owning the implemented object. + */ + public function getOwnerID(): string; +} diff --git a/src/Recipient/Membership.php b/src/Recipient/Membership.php index 4bd3be8..82647dd 100644 --- a/src/Recipient/Membership.php +++ b/src/Recipient/Membership.php @@ -64,7 +64,7 @@ class Membership implements ProcessableComponentInterface { $returnArray = [ 'Group' => $this->groupID, - 'Contact' => $this->ContactID, + 'Contact' => $this->contactID, ]; return $returnArray; } diff --git a/src/Recipient/ProcessedContact.php b/src/Recipient/ProcessedContact.php index 87a637f..1565a50 100644 --- a/src/Recipient/ProcessedContact.php +++ b/src/Recipient/ProcessedContact.php @@ -17,11 +17,15 @@ use IP1\RESTClient\Recipient\RecipientFactory; use IP1\RESTClient\Recipient\MembershipRelationInterface; use IP1\RESTClient\Core\Communicator; use IP1\RESTClient\Core\ClassValidationArray; +use IP1\RESTClient\Core\OwnableInterface; /** * A Contact that has been added to the API. Has all the options that a normal Contact has. */ -class ProcessedContact extends Contact implements UpdatableComponentInterface, MembershipRelationInterface +class ProcessedContact extends Contact implements + UpdatableComponentInterface, + MembershipRelationInterface, + OwnableInterface { /** * The ID of the Contact given by the API. @@ -38,6 +42,12 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M * @var \DateTime $updated */ private $updated; + + /** + * The Account ID of the Contact's owner. + * @var string $ownerID + */ + private $ownerID; /** * An array of Memberships that the Group has. * @@ -73,6 +83,7 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M * @param string $firstName The first name of the contact in question. * @param string $phoneNumber Contact phone number: with country code and without spaces and dashes. * @param integer $contactID Contact ID. + * @param string $ownerID ID of account owning the Contact. * @param ?string $lastName Contact last name. * @param ?string $title The contacts title. * @param ?string $organization Contact company or other organization. @@ -85,6 +96,7 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M string $firstName, string $phoneNumber, int $contactID, + string $ownerID, ?string $lastName, ?string $title, ?string $organization, @@ -94,6 +106,7 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M ?\DateTime $updated = null ) { parent::__construct($firstName, $phoneNumber, $lastName, $title, $organization, $email, $notes); + $this->ownerID = $ownerID; $this->contactID = $contactID; $this->created = $created ?? null; $this->updated = $updated ?? null; @@ -112,7 +125,7 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M if ($communicator !== null) { $membershipJSON = $communicator->get("api/contacts/".$this->contactID."/memberships"); $membershipStd = json_decode($membershipJSON); - $memberships = []; + $memberships = new ClassValidationArray(); foreach ($membershipStd as $value) { $memberships[] = RecipientFactory::createProcessedMembershipFromStdClass($value); } @@ -213,6 +226,13 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M return $this->contactID; } /** + * @return string OwnerID + */ + public function getOwnerID(): string + { + return $this->ownerID; + } + /** * Serializes the object to a value that can be serialized natively by json_encode(). * @return array Associative. * @link http://php.net/manual/en/jsonserializable.jsonserialize.php @@ -220,17 +240,14 @@ class ProcessedContact extends Contact implements UpdatableComponentInterface, M public function jsonSerialize(): array { $contactArray = parent::jsonSerialize(); - $returnArray = array_merge( - ['ID' => $this->contactID], - $contactArray, - [ - 'Modified' => isset($this->created) ? $this->updated->format("Y-m-d\TH:i:s."). - substr($this->updated->format('u'), 0, 3) : null, - 'Created' => isset($this->created) ? $this->created->format("Y-m-d\TH:i:s."). - substr($this->updated->format('u'), 0, 3) : null, - ] - ); - return array_filter($returnArray); + $contactArray['ID'] = $this->contactID; + $contactArray['OwnerID'] = $this->ownerID; + $contactArray['Modified'] = isset($this->created) ? $this->updated->format("Y-m-d\TH:i:s."). + substr($this->updated->format('u'), 0, 3) : null; + $contactArray['Created'] = isset($this->created) ? $this->created->format("Y-m-d\TH:i:s."). + substr($this->updated->format('u'), 0, 3) : null; + + return array_filter($contactArray); } /** * Takes the given argument and replaces strings such as {id} to an actual value. diff --git a/src/Recipient/ProcessedGroup.php b/src/Recipient/ProcessedGroup.php index 48e8605..87a14d2 100644 --- a/src/Recipient/ProcessedGroup.php +++ b/src/Recipient/ProcessedGroup.php @@ -15,11 +15,12 @@ use IP1\RESTClient\Core\Communicator; use IP1\RESTClient\Core\UpdatableComponentInterface; use IP1\RESTClient\Core\ClassValidationArray; use IP1\RESTClient\Recipient\ProcessedMembership; +use IP1\RESTClient\Core\OwnableInterface; /** * */ -class ProcessedGroup extends Group implements UpdatableComponentInterface, MembershipRelationInterface +class ProcessedGroup extends Group implements UpdatableComponentInterface, MembershipRelationInterface, OwnableInterface { /** * The ID of the Group given by the API. @@ -37,6 +38,11 @@ class ProcessedGroup extends Group implements UpdatableComponentInterface, Membe */ private $updated; /** + * ID of account owning the Group + * @var string $ownerID + */ + private $ownerID; + /** * An array of memberships that the Group has. * * It is empty by default but is filled when the function getMemberships() is called if a Communicator is given @@ -66,30 +72,47 @@ class ProcessedGroup extends Group implements UpdatableComponentInterface, Membe protected $contactsFetched = false; const IS_READ_ONLY = false; /** - * ProcessedGroup Contstructor + * ProcessedGroup Contstructor. * @param string $name Name of the Group. * @param string $color A hexadecimal color. * @param integer $groupID An ID generated by the API. + * @param string $ownerID ID of account owning the Group. * @param \DateTime $created When the Group was initially created. * @param \DateTime $updated When the Group was last updated. */ - public function __construct(string $name, string $color, int $groupID, \DateTime $created, \DateTime $updated) - { + public function __construct( + string $name, + string $color, + int $groupID, + string $ownerID, + \DateTime $created, + \DateTime $updated + ) { + parent::__construct($name, $color); $this->groupID = $groupID; + $this->ownerID = $ownerID; $this->created = $created; $this->updated = $updated; $this->memberships = new ClassValidationArray(); $this->contacts = new ClassValidationArray(); } /** - * @return integer Group ID + * @return integer Group ID. */ public function getID(): int { return $this->groupID; } /** + * Returns ID of account owning the Group. + * @return string + */ + public function getOwnerID(): string + { + return $this->ownerID; + } + /** * Returns whether the object is read only or not. * @return boolean Whether the object is read only or not. */ diff --git a/src/Recipient/ProcessedMembership.php b/src/Recipient/ProcessedMembership.php index 28160d6..590ef0f 100644 --- a/src/Recipient/ProcessedMembership.php +++ b/src/Recipient/ProcessedMembership.php @@ -12,12 +12,13 @@ namespace IP1\RESTClient\Recipient; use IP1\RESTClient\Core\ProcessedComponentInterface; +use IP1\RESTClient\Core\OwnableInterface; /** * ProcessedMembership class. * Is the relation between contacts and groups. */ -class ProcessedMembership extends Membership implements ProcessedComponentInterface +class ProcessedMembership extends Membership implements ProcessedComponentInterface, OwnableInterface { /** @@ -31,16 +32,23 @@ class ProcessedMembership extends Membership implements ProcessedComponentInterf */ private $created; /** + * ID of account owning the membership + * @var string $ownerID + */ + private $ownerID; + /** * ProcessedMembership Constructor * @param integer $groupID The ID of the group. * @param integer $contactID The ID of the contact. * @param integer $membershipID The ID of the membership. + * @param string $ownerID ID of account owning the membership. * @param \DateTime $created When the Membership was created. */ - public function __construct(int $groupID, int $contactID, int $membershipID, \DateTime $created) + public function __construct(int $groupID, int $contactID, int $membershipID, string $ownerID, \DateTime $created) { parent::__construct($groupID, $contactID); $this->membershipID = $membershipID; + $this->ownerID = $ownerID; $this->created = $created; } @@ -53,6 +61,14 @@ class ProcessedMembership extends Membership implements ProcessedComponentInterf return $this->membershipID; } /** + * Returns ID of account owning the Membership + * @return string ID of account owning the Membership + */ + public function getOwnerID(): string + { + return $this->ownerID; + } + /** * @param \DateTimeZone $timezone The timezone that the user wants to get the DateTime in. Default is UTC. * @return \DateTime|null When the Component was added. */ diff --git a/src/Recipient/RecipientFactory.php b/src/Recipient/RecipientFactory.php index 05de224..0003b44 100644 --- a/src/Recipient/RecipientFactory.php +++ b/src/Recipient/RecipientFactory.php @@ -125,6 +125,7 @@ class RecipientFactory $stdContact->FirstName, $stdContact->Phone, $stdContact->ID, + $stdContact->OwnerID, $stdContact->LastName ?? null, $stdContact->Title ?? null, $stdContact->Organization ?? null, @@ -155,6 +156,7 @@ class RecipientFactory $stdGroup->Name, $stdGroup->Color, $stdGroup->ID, + $stdGroup->OwnerID, new \DateTime($stdGroup->Created), new \DateTime($stdGroup->Modified) ); @@ -189,6 +191,7 @@ class RecipientFactory $stdMembership->Group, $stdMembership->Contact, $stdMembership->ID, + $stdMembership->OwnerID, new \DateTime($stdMembership->Created) ); } diff --git a/tests/Recipient/MembershipTest.php b/tests/Recipient/MembershipTest.php new file mode 100644 index 0000000..081c0e7 --- /dev/null +++ b/tests/Recipient/MembershipTest.php @@ -0,0 +1,79 @@ +<?php +namespace IP1\RESTClient\Test\Recipient; + +use IP1\RESTClient\Recipient\Group; +use IP1\RESTClient\Recipient\Contact; +use IP1\RESTClient\Recipient\Membership; +use IP1\RESTClient\Test\Core\AbstractEnviromentProvider; +use IP1\RESTClient\Test\Util\Util; +use IP1\RESTClient\Core\ClassValidationArray; + +class MembershipTest extends AbstractEnviromentProvider +{ + + /** + * @dataProvider getMembershipArguments + */ + public function testConstructor($groupID, $contactID) + { + $m = new Membership($groupID, $contactID); + $this->assertEquals($groupID, $m->getGroupID()); + $this->assertEquals($contactID, $m->getContactID()); + } + public function getMembershipArguments(): array + { + $argumentArray = []; + for ($i = 0; $i < 50; $i++) { + $argumentArray[] = [random_int(0, PHP_INT_MAX), random_int(0, PHP_INT_MAX)]; + } + return $argumentArray; + } + /** + * @group api + */ + public function testAPI() + { + $contacts = []; + $groups = []; + $memberships = []; + for ($i = 0; $i < 10; $i++) { + $c = new Contact(Util::getRandomAlphaString(), Util::getRandomPhoneNumber()); + $contacts[] = $this->getCommunicator()->add($c); + $g = new Group(Util::getRandomAlphaString(), Util::getRandomHex()); + $groups[] = $this->getCommunicator()->add($g); + $m = new Membership($groups[$i]->getID(), $contacts[$i]->getID()); + $memberships[] = $this->getCommunicator()->add($m); + } + $this->assertEquals(10, count($contacts)); + $this->assertEquals(10, count($groups)); + $this->assertEquals(10, count($memberships)); + for ($i = 0; $i < count($memberships); $i++) { + $contactMemberships = $contacts[$i]->getMemberships($this->getCommunicator()); + $this->assertEquals(1, count($contactMemberships)); + $this->assertEquals($contactMemberships[0]->getContactID(), $contacts[$i]->getID()); + $this->assertEquals($contactMemberships[0]->getGroupID(), $groups[$i]->getID()); + + $contactGroups = $contacts[$i]->getGroups($this->getCommunicator()); + $this->assertEquals(1, count($contactGroups)); + $this->assertEquals(new ClassValidationArray($groups[$i]), $contactGroups); + } + } + public function tearDown() + { + if ($this->isCommunicatorEnabled()) { + $memberships = json_decode($this->getCommunicator()->get('api/memberships')); + foreach ($memberships as $key => $value) { + $this->getCommunicator()->delete('api/memberships/'.$value->ID); + } + + $contacts = json_decode($this->getCommunicator()->get('api/memberships')); + foreach ($contacts as $key => $value) { + $this->getCommunicator()->delete('api/memberships/'.$value->ID); + } + $groups = json_decode($this->getCommunicator()->get('api/groups')); + foreach ($groups as $key => $value) { + $this->getCommunicator()->delete('api/groups/'.$value->ID); + } + } + } +} diff --git a/tests/Recipient/ProcessedContactTest.php b/tests/Recipient/ProcessedContactTest.php index bbb77ee..7e7443f 100644 --- a/tests/Recipient/ProcessedContactTest.php +++ b/tests/Recipient/ProcessedContactTest.php @@ -77,6 +77,7 @@ class ProcessedContactTest extends AbstractEnviromentProvider new \DateTime($this->completeContactStd->Created, new DateTimeZone("UTC")), $contact->getCreated() ); + $this->assertEquals($this->completeContactStd->OwnerID, $contact->getOwnerID()); $this->assertEquals($this->completeContactStd->ID, $contact->getID()); $this->assertEquals(ProcessedContact::IS_READ_ONLY, $contact->isReadOnly()); $this->assertEquals(false, $contact->isReadOnly()); @@ -110,6 +111,7 @@ class ProcessedContactTest extends AbstractEnviromentProvider $this->assertEquals($newContact->getOrganization(), $alteredContact->getOrganization()); $this->assertEquals($newContact->getPhoneNumber(), $alteredContact->getPhoneNumber()); $this->assertEquals($newContact->getTitle(), $alteredContact->getTitle()); + $this->assertEquals($newContact->getOwnerID(), $alteredContact->getOwnerID()); $this->assertEquals($newContact->getID(), $alteredContact->getID()); $deletedContact = $this->getCommunicator()->remove($newContact); @@ -121,6 +123,7 @@ class ProcessedContactTest extends AbstractEnviromentProvider $this->assertEquals($newContact->getOrganization(), $deletedContact->getOrganization()); $this->assertEquals($newContact->getPhoneNumber(), $deletedContact->getPhoneNumber()); $this->assertEquals($newContact->getTitle(), $deletedContact->getTitle()); + $this->assertEquals($newContact->getOwnerID(), $deletedContact->getOwnerID()); $this->assertEquals($newContact->getID(), $deletedContact->getID()); } /** diff --git a/tests/Recipient/ProcessedGroupTest.php b/tests/Recipient/ProcessedGroupTest.php index 8dd6460..094c635 100644 --- a/tests/Recipient/ProcessedGroupTest.php +++ b/tests/Recipient/ProcessedGroupTest.php @@ -31,6 +31,7 @@ class ProcessedGroupTest extends AbstractEnviromentProvider $this->assertTrue(0 < $group->getID()); $this->assertEquals(\DateTime::class, get_class($group->getCreated())); $this->assertEquals(\DateTime::class, get_class($group->getUpdated())); + $this->assertStringStartsWith("ip1-", $group->getOwnerID()); $this->assertEquals($group->getCreated(), $group->getCreated(new \DateTimeZone("UTC"))); $this->assertEquals($group->getUpdated(), $group->getUpdated(new \DateTimeZone("UTC"))); if ($group->getCreated(new \DateTimeZone("Indian/Reunion")) !== $group->getCreated()) { @@ -52,6 +53,7 @@ class ProcessedGroupTest extends AbstractEnviromentProvider Util::getRandomAlphaString(), Util::getRandomHex(), random_int(1, PHP_INT_MAX), + Util::getRandomAccountID(), Util::getRandomDateTime(), Util::getRandomDateTime() ); @@ -80,6 +82,7 @@ class ProcessedGroupTest extends AbstractEnviromentProvider $processedGroup = $this->getCommunicator()->add($group); $this->assertEquals($group->getName(), $processedGroup->getName()); $this->assertEquals($group->getColor(), $processedGroup->getColor()); + $this->assertStringStartsWith("ip1-", $processedGroup->getOwnerID()); $this->assertTrue(is_int($processedGroup->getID())); $this->assertTrue(0 < $processedGroup->getID()); @@ -88,7 +91,7 @@ class ProcessedGroupTest extends AbstractEnviromentProvider public function editGroupToAPI(ProcessedGroup $processedGroup): ProcessedGroup { $editedGroup = $this->getCommunicator()->edit($processedGroup); - + $this->assertEquals($processedGroup->getOwnerID(), $editedGroup->getOwnerID()); $this->assertEquals($processedGroup->getName(), $editedGroup->getName()); $this->assertEquals($processedGroup->getColor(), $editedGroup->getColor()); if ($editedGroup->getUpdated() !== $processedGroup->getUpdated()) { @@ -107,6 +110,7 @@ class ProcessedGroupTest extends AbstractEnviromentProvider $this->assertEquals($editedGroup->getColor(), $removedGroup->getColor()); $this->assertEquals($editedGroup->getCreated(), $removedGroup->getCreated()); $this->assertEquals($editedGroup->getUpdated(), $removedGroup->getUpdated()); + $this->assertEquals($editedGroup->getOwnerID(), $removedGroup->getOwnerID()); $this->assertEquals($editedGroup->getContacts(), $removedGroup->getContacts()); $this->assertEquals($editedGroup->getMemberships(), $removedGroup->getMemberships()); $this->assertEquals($editedGroup->memberShipsFetched(), $removedGroup->memberShipsFetched()); diff --git a/tests/Util/Util.php b/tests/Util/Util.php index af9f85f..855f3ce 100644 --- a/tests/Util/Util.php +++ b/tests/Util/Util.php @@ -39,4 +39,16 @@ class Util } return $color; } + public static function getRandomAccountID(): string + { + return "ip1-".random_int(10000, 99999); + } + public static function getRandomPhoneNumber(): string + { + $retval = ''; + for ($i=0; $i < 11; $i++) { + $retval.=random_int(0, 9); + } + return $retval; + } } diff --git a/tests/resources/processed_contact/list_processed_contact.json b/tests/resources/processed_contact/list_processed_contact.json index ee6e9af..e2ab2da 100644 --- a/tests/resources/processed_contact/list_processed_contact.json +++ b/tests/resources/processed_contact/list_processed_contact.json @@ -1 +1 @@ -{"ID": 1735500,"FirstName": "Jack","LastName": "Sparrow","Organization": "Black Pearl Co.","Phone": "12025550134","Email": "jack@example.com"} +{"ID": 1735500,"OwnerID": "ip1-xxxxx","FirstName": "Jack","LastName": "Sparrow","Organization": "Black Pearl Co.","Phone": "12025550134","Email": "jack@example.com"} diff --git a/tests/resources/processed_contact/minimal_processed_contact.json b/tests/resources/processed_contact/minimal_processed_contact.json index 3a6ac87..a1a2b8a 100644 --- a/tests/resources/processed_contact/minimal_processed_contact.json +++ b/tests/resources/processed_contact/minimal_processed_contact.json @@ -1 +1 @@ -{"ID": 1735500,"FirstName": "Jack","Phone": "12025550134"} +{"ID": 1735500,"FirstName": "Jack","Phone": "12025550134","OwnerID": "ip1-xxxxx"} diff --git a/tests/resources/processed_contact/processed_contact.json b/tests/resources/processed_contact/processed_contact.json index 47abbf2..9b47edc 100644 --- a/tests/resources/processed_contact/processed_contact.json +++ b/tests/resources/processed_contact/processed_contact.json @@ -1 +1 @@ -{"Title":"Captain","Notes":"Why is the rum gone?","Created":"2017-03-02T10:39:53.497","Modified":"2017-03-02T10:39:53.497","ID":1735500,"FirstName":"Jack","LastName":"Sparrow","Organization":"Black Pearl Co.","Phone":"12025550134","Email":"jack@example.com"} +{"Title":"Captain","Notes":"Why is the rum gone?","Created":"2017-03-02T10:39:53.497","Modified":"2017-03-02T10:39:53.497","ID":1735500,"OwnerID": "ip1-xxxxx","FirstName":"Jack","LastName":"Sparrow","Organization":"Black Pearl Co.","Phone":"12025550134","Email":"jack@example.com"} |