Skip to content

Commit

Permalink
Add UserMerge command
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz authored and OndraM committed Nov 24, 2017
1 parent bd39685 commit 23b8429
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Model/Command/UserMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

/**
* Take all interactions from the source user and merge them to the target user.
* Source user will be DELETED and unknown to Matej from this action.
*/
class UserMerge extends AbstractCommand
{
/** @var string */
private $sourceUserId;
/** @var string */
private $targetUserId;

private function __construct(string $targetUserId, string $sourceUserId)
{
$this->targetUserId = $targetUserId;
$this->sourceUserId = $sourceUserId;
}

/**
* Merge source user into target user AND DELETE SOURCE USER.
*/
public static function mergeInto(string $targetUserId, string $sourceUserIdToBeDeleted): self
{
return new static($targetUserId, $sourceUserIdToBeDeleted);
}

/**
* Merge source user into target user AND DELETE SOURCE USER.
*/
public static function mergeFromSourceToTargetUser(string $sourceUserIdToBeDeleted, string $targetUserId): self
{
return new static($targetUserId, $sourceUserIdToBeDeleted);
}

public function getCommandType(): string
{
return 'user-merge';
}

public function getCommandParameters(): array
{
return [
'target_user_id' => $this->targetUserId,
'source_user_id' => $this->sourceUserId,
];
}
}
40 changes: 40 additions & 0 deletions tests/Model/Command/UserMergeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

use Lmc\Matej\TestCase;

class UserMergeTest extends TestCase
{
/** @test */
public function shouldGenerateCorrectSignature(): void
{
$sourceUserId = 'source-user';
$targetUserId = 'target-user';

$command = UserMerge::mergeInto($targetUserId, $sourceUserId);
$this->assertUserMergeObject($command, $sourceUserId, $targetUserId);

$command = UserMerge::mergeFromSourceToTargetUser($sourceUserId, $targetUserId);
$this->assertUserMergeObject($command, $sourceUserId, $targetUserId);
}

/**
* Execute asserts against user merge object
* @param object $object
*/
private function assertUserMergeObject($object, string $sourceUserId, string $targetUserId): void
{
$this->assertInstanceOf(UserMerge::class, $object);
$this->assertSame(
[
'type' => 'user-merge',
'parameters' => [
'target_user_id' => $targetUserId,
'source_user_id' => $sourceUserId,
],
],
$object->jsonSerialize()
);
}
}

0 comments on commit 23b8429

Please sign in to comment.