Skip to content

Commit

Permalink
Add exception when merging same user ids (RAD-907)
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Jan 26, 2018
1 parent 27c2af6 commit 3069bf7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<!-- There is always Unreleased section on the top. Subsections (Added, Changed, Fixed, Removed) should be added as needed. -->

## Unreleased
### Fixed
- Check for equality of user ids in `UserMerge` - so that we don't "merge" user into the same user id effectively deleting it.

## 1.3.0 - 2018-01-19
### Changed
Expand Down
18 changes: 18 additions & 0 deletions src/Model/Command/UserMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ private function __construct(string $targetUserId, string $sourceUserId)
{
$this->setTargetUserId($targetUserId);
$this->setSourceUserId($sourceUserId);

$this->assertUserIdsNotEqual();
}

/**
Expand Down Expand Up @@ -65,6 +67,15 @@ protected function setTargetUserId(string $targetUserId): void
$this->targetUserId = $targetUserId;
}

private function assertUserIdsNotEqual(): void
{
Assertion::notEq(
$this->sourceUserId,
$this->targetUserId,
'You have to provide different source and target user id in UserMerge ("%s" set for both)'
);
}

protected function getCommandType(): string
{
return 'user-merge';
Expand All @@ -77,4 +88,11 @@ protected function getCommandParameters(): array
'source_user_id' => $this->sourceUserId,
];
}

public function jsonSerialize(): array
{
$this->assertUserIdsNotEqual();

return parent::jsonSerialize();
}
}
9 changes: 9 additions & 0 deletions tests/unit/Model/Command/UserMergeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lmc\Matej\Model\Command;

use Assert\InvalidArgumentException;
use Lmc\Matej\UnitTestCase;

class UserMergeTest extends UnitTestCase
Expand All @@ -19,6 +20,14 @@ public function shouldGenerateCorrectSignature(): void
$this->assertUserMergeCommand($command, $sourceUserId, $targetUserId);
}

public function shouldThrowExceptionWhenMergingSameUsers(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You have to provide different source and target user id in UserMerge ("test-user" set for both)');

UserMerge::mergeInto('test-user', 'test-user');
}

/**
* Execute asserts against user merge command
* @param UserMerge $command
Expand Down

0 comments on commit 3069bf7

Please sign in to comment.