Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(users): add email to MergeIdentifier object #56

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/Object/Users/MergeIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,37 @@ class MergeIdentifier extends BaseObject

public ?UserAlias $user_alias = null;

public ?string $email = null;

/** @var ?string[] */
public ?array $prioritization = 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 && null === $this->email) {
throw new ValidationException('One of "external_id", "user_alias" or "email" fields is required');
}

if (\count(array_filter([$this->external_id, $this->user_alias, $this->email])) > 1) {
throw new ValidationException('Only one of "external_id", "user_alias" and "email" fields must be set');
}

if (null !== $this->external_id && null !== $this->user_alias) {
throw new ValidationException('Only one of "external_id" and "user_alias" fields must be set');
if (null !== $this->email && null === $this->prioritization) {
throw new ValidationException('If an "email" is specified as an identifier, an additional "prioritization" value is required');
}

if (null !== $this->prioritization) {
foreach ($this->prioritization as $prioritizationItem) {
if (!in_array($prioritizationItem, ['identified', 'unidentified', 'most_recently_updated'])) {
throw new ValidationException('The allowed values for the "prioritization" array are: "identified", "unidentified", "most_recently_updated"');
}
}

if (\count(array_intersect($this->prioritization, ['identified', 'unidentified'])) > 1) {
throw new ValidationException('Only one of "identified" and "unidentified" options may exist in the prioritization array');
}
}

$this->user_alias?->validate($strict);
Expand Down
50 changes: 50 additions & 0 deletions tests/Object/Users/MergeIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,54 @@ public function validProvider(): array
[$mergeIdentifier],
];
}

/**
* @dataProvider validEmailIdentifierProvider
* @doesNotPerformAssertions
*/
public function testValidEmailIdentifier(MergeIdentifier $mergeIdentifier): void
{
$mergeIdentifier->validate(true);
}

public function validEmailIdentifierProvider(): array
{
$mergeIdentifier = new MergeIdentifier();
$mergeIdentifier->email = '[email protected]';
$mergeIdentifier->prioritization = ['unidentified', 'most_recently_updated'];

return [
[$mergeIdentifier],
];
}

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

$mergeIdentifier->validate(false);
}

public function invalidEmailIdentifierProvider(): array
{
$mergeIdentifier1 = new MergeIdentifier();
$mergeIdentifier1->email = '[email protected]';

$mergeIdentifier2 = new MergeIdentifier();
$mergeIdentifier2->email = '[email protected]';
$mergeIdentifier2->prioritization = ['email'];

$mergeIdentifier3 = new MergeIdentifier();
$mergeIdentifier3->email = '[email protected]';
$mergeIdentifier3->prioritization = ['identified', 'unidentified'];

return [
[$mergeIdentifier1],
[$mergeIdentifier2],
[$mergeIdentifier3],
];
}
}
Loading