Skip to content

Commit

Permalink
feat: add support for /users/merge endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioturdo committed Aug 2, 2023
1 parent 228be73 commit f7c30ad
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Endpoint/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ImmobiliareLabs\BrazeSDK\Request\Users\ExportByIdentifierRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\ExportBySegmentRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\IdentifyRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\MergeRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\RemoveExternalIDRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\RenameExternalIDRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\TrackRequest;
Expand All @@ -17,6 +18,7 @@
use ImmobiliareLabs\BrazeSDK\Response\Users\ExportByIdentifierResponse;
use ImmobiliareLabs\BrazeSDK\Response\Users\ExportBySegmentResponse;
use ImmobiliareLabs\BrazeSDK\Response\Users\IdentifyResponse;
use ImmobiliareLabs\BrazeSDK\Response\Users\MergeResponse;
use ImmobiliareLabs\BrazeSDK\Response\Users\RemoveExternalIDResponse;
use ImmobiliareLabs\BrazeSDK\Response\Users\RenameExternalIDResponse;
use ImmobiliareLabs\BrazeSDK\Response\Users\TrackResponse;
Expand Down Expand Up @@ -55,6 +57,14 @@ public function delete(DeleteRequest $request, bool $resolveResponse = true): De
return $this->makeRequest('POST', '/users/delete', $request, DeleteResponse::class, $resolveResponse);
}

/**
* @see https://www.braze.com/docs/api/endpoints/user_data/post_users_merge/
*/
public function merge(MergeRequest $request, bool $resolveResponse = true): MergeResponse
{
return $this->makeRequest('POST', '/users/merge', $request, MergeResponse::class, $resolveResponse);
}

/**
* @see https://www.braze.com/docs/api/endpoints/user_data/external_id_migration/post_external_ids_rename/
*/
Expand Down
29 changes: 29 additions & 0 deletions src/Object/Users/MergeIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Object\Users;

use ImmobiliareLabs\BrazeSDK\Exception\ValidationException;
use ImmobiliareLabs\BrazeSDK\Object\BaseObject;
use ImmobiliareLabs\BrazeSDK\Object\UserAlias;

class MergeIdentifier extends BaseObject
{
public ?string $external_id = null;

public ?UserAlias $user_alias = null;

public function validate(bool $strict): void
{
parent::validate($strict);

if (null === $this->external_id && null === $this->user_alias) {
throw new ValidationException('One of "external_id" or "user_alias" fields is required');
}

if (null !== $this->external_id && null !== $this->user_alias) {
throw new ValidationException('Only one of "external_id" and "user_alias" fields must be set');
}

$this->user_alias?->validate($strict);
}
}
29 changes: 29 additions & 0 deletions src/Object/Users/MergeUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Object\Users;

use ImmobiliareLabs\BrazeSDK\Exception\ValidationException;
use ImmobiliareLabs\BrazeSDK\Object\BaseObject;

class MergeUpdate extends BaseObject
{
public ?MergeIdentifier $identifier_to_merge = null;

public ?MergeIdentifier $identifier_to_keep = null;

public function validate(bool $strict): void
{
parent::validate($strict);

if (null === $this->identifier_to_merge) {
throw new ValidationException('The "identifier_to_merge" field is required');
}

if (null === $this->identifier_to_keep) {
throw new ValidationException('The "identifier_to_keep" field is required');
}

$this->identifier_to_merge->validate($strict);
$this->identifier_to_keep->validate($strict);
}
}
26 changes: 26 additions & 0 deletions src/Request/Users/MergeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Request\Users;

use ImmobiliareLabs\BrazeSDK\Exception\ValidationException;
use ImmobiliareLabs\BrazeSDK\Object\Users\MergeUpdate;
use ImmobiliareLabs\BrazeSDK\Request\BaseRequest;

class MergeRequest extends BaseRequest
{
/** @var ?MergeUpdate[] */
public ?array $merge_updates = null;

public function validate(bool $strict): void
{
parent::validate($strict);

if (empty($this->merge_updates)) {
throw new ValidationException('The "merge_updates" field must be non empty');
}

foreach ($this->merge_updates as $merge_update) {
$merge_update->validate($strict);
}
}
}
9 changes: 9 additions & 0 deletions src/Response/Users/MergeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Response\Users;

use ImmobiliareLabs\BrazeSDK\Response\BaseResponse;

class MergeResponse extends BaseResponse
{
}
1 change: 1 addition & 0 deletions tests/Endpoint/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function endpointProvider(): array
['createAlias', Users\CreateAliasRequest::class],
['identify', Users\IdentifyRequest::class],
['delete', Users\DeleteRequest::class],
['merge', Users\MergeRequest::class],
['renameExternalID', Users\RenameExternalIDRequest::class],
['removeExternalID', Users\RemoveExternalIDRequest::class],
['exportByIdentifier', Users\ExportByIdentifierRequest::class],
Expand Down
55 changes: 55 additions & 0 deletions tests/Object/Users/MergeIdentifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Test\Object\Users;

use ImmobiliareLabs\BrazeSDK\Exception\ValidationException;
use ImmobiliareLabs\BrazeSDK\Object\UserAlias;
use ImmobiliareLabs\BrazeSDK\Object\Users\MergeIdentifier;
use ImmobiliareLabs\BrazeSDK\Object\Users\NewUserAlias;
use PHPUnit\Framework\TestCase;

class MergeIdentifierTest extends TestCase
{
/**
* @dataProvider validProvider
* @doesNotPerformAssertions
*/
public function testValid(MergeIdentifier $mergeIdentifier): void
{
$mergeIdentifier->validate(true);
}

/**
* @dataProvider validProvider
*/
public function testNoIdentifier(MergeIdentifier $mergeIdentifier): void
{
$this->expectException(ValidationException::class);

$mergeIdentifier->external_id = null;

$mergeIdentifier->validate(false);
}

/**
* @dataProvider validProvider
*/
public function testBothIdentifier(MergeIdentifier $mergeIdentifier): void
{
$this->expectException(ValidationException::class);

$mergeIdentifier->user_alias = new UserAlias();

$mergeIdentifier->validate(false);
}

public function validProvider(): array
{
$mergeIdentifier = new MergeIdentifier();
$mergeIdentifier->external_id = 'external_id';

return [
[$mergeIdentifier],
];
}
}
64 changes: 64 additions & 0 deletions tests/Object/Users/MergeUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Test\Object\Users;

use ImmobiliareLabs\BrazeSDK\Exception\ValidationException;
use ImmobiliareLabs\BrazeSDK\Object\UserAlias;
use ImmobiliareLabs\BrazeSDK\Object\Users\MergeIdentifier;
use ImmobiliareLabs\BrazeSDK\Object\Users\MergeUpdate;
use ImmobiliareLabs\BrazeSDK\Object\Users\NewUserAlias;
use PHPUnit\Framework\TestCase;

class MergeUpdateTest extends TestCase
{
/**
* @dataProvider validProvider
* @doesNotPerformAssertions
*/
public function testValid(MergeUpdate $mergeUpdate): void
{
$mergeUpdate->validate(true);
}

/**
* @dataProvider validProvider
*/
public function testNoIdentifierToMerge(MergeUpdate $mergeUpdate): void
{
$this->expectException(ValidationException::class);

$mergeUpdate->identifier_to_merge = null;

$mergeUpdate->validate(false);
}

/**
* @dataProvider validProvider
*/
public function testNoIdentifierToKeep(MergeUpdate $mergeUpdate): void
{
$this->expectException(ValidationException::class);

$mergeUpdate->identifier_to_keep = null;

$mergeUpdate->validate(false);
}

public function validProvider(): array
{
$mergeUpdate = new MergeUpdate();

$mergeUpdate->identifier_to_merge = new MergeIdentifier();
$mergeUpdate->identifier_to_merge->user_alias = new UserAlias();
$mergeUpdate->identifier_to_merge->user_alias->alias_label = 'label';
$mergeUpdate->identifier_to_merge->user_alias->alias_name = 'name';

$mergeUpdate->identifier_to_keep = new MergeIdentifier();
$mergeUpdate->identifier_to_keep->external_id = 'external-id-to-keep';


return [
[$mergeUpdate],
];
}
}
67 changes: 67 additions & 0 deletions tests/Request/Users/MergeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Test\Request\Users;

use ImmobiliareLabs\BrazeSDK\Exception\ValidationException;
use ImmobiliareLabs\BrazeSDK\Object\UserAlias;
use ImmobiliareLabs\BrazeSDK\Object\Users\MergeIdentifier;
use ImmobiliareLabs\BrazeSDK\Object\Users\MergeUpdate;
use ImmobiliareLabs\BrazeSDK\Request\Users\IdentifyRequest;
use ImmobiliareLabs\BrazeSDK\Request\Users\MergeRequest;
use PHPUnit\Framework\TestCase;

class MergeRequestTest extends TestCase
{
/**
* @dataProvider validProvider
* @doesNotPerformAssertions
*/
public function testValid(MergeRequest $mergeRequest): void
{
$mergeRequest->validate(true);
}

/**
* @dataProvider validProvider
*/
public function testMergeUpdatesNull(MergeRequest $mergeRequest): void
{
$this->expectException(ValidationException::class);

$mergeRequest->merge_updates = null;

$mergeRequest->validate(false);
}

/**
* @dataProvider validProvider
*/
public function testMergeUpdatesEmpty(MergeRequest $mergeRequest): void
{
$this->expectException(ValidationException::class);

$mergeRequest->merge_updates = [];

$mergeRequest->validate(false);
}

public function validProvider(): array
{
$mergeUpdate = new MergeUpdate();

$mergeUpdate->identifier_to_merge = new MergeIdentifier();
$mergeUpdate->identifier_to_merge->user_alias = new UserAlias();
$mergeUpdate->identifier_to_merge->user_alias->alias_label = 'label';
$mergeUpdate->identifier_to_merge->user_alias->alias_name = 'name';

$mergeUpdate->identifier_to_keep = new MergeIdentifier();
$mergeUpdate->identifier_to_keep->external_id = 'external-id-to-keep';

$mergeRequest = new MergeRequest();
$mergeRequest->merge_updates = [$mergeUpdate];

return [
[$mergeRequest],
];
}
}

0 comments on commit f7c30ad

Please sign in to comment.