Skip to content

Commit

Permalink
Add UserMerge command
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Nov 23, 2017
1 parent 44d0b73 commit 65e8fbe
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Model/Command/UserMerge.php
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

/**
* Merge two users. DELETES source user!
*/
class UserMerge extends AbstractCommand
{
/** @var string */
private $sourceUserId;
/** @var string */
private $targetUserId;

/**
* Merge source user to target user AND DELETE SOURCE USER.
*/
public function __construct(string $sourceUserId, string $targetUserId)
{
$this->sourceUserId = $sourceUserId;
$this->targetUserId = $targetUserId;
}

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

public function getCommandParameters(): array
{
return [
'target_user_id' => $this->targetUserId,
'source_user_id' => $this->sourceUserId,
];
}

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

/**
* Merge source user into target user AND DELETE SOURCE USER.
*/
public static function mergeInto(string $targetUserId, string $sourceUserId): self
{
return new static($sourceUserId, $targetUserId);
}
}
45 changes: 45 additions & 0 deletions tests/Model/Command/UserMergeTest.php
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

use PHPUnit\Framework\TestCase;

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

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

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

$command = new UserMerge($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 65e8fbe

Please sign in to comment.