Skip to content

Commit

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

namespace Lmc\Matej\Model\Command;

/**
* Sorting items is a way how to use Matej to deliver personalized experience to users.
* It allows to sort given list of items according to the user preference.
*/
class Sorting extends AbstractCommand
{
/** @var string */
private $userId;
/** @var string[] */
private $itemIds = [];

private function __construct(string $userId, array $itemIds)
{
$this->userId = $userId;
$this->itemIds = $itemIds;
}

/**
* Sort given item ids for user-based recommendations.
*/
public static function create(string $userId, array $itemIds): self
{
return new static($userId, $itemIds);
}

public function getCommandType(): string
{
return 'sorting';
}

public function getCommandParameters(): array
{
return [
'user_id' => $this->userId,
'item_ids' => $this->itemIds,
];
}
}
37 changes: 37 additions & 0 deletions tests/Model/Command/SortingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

use Lmc\Matej\TestCase;

class SortingTest extends TestCase
{
/** @test */
public function shouldBeInstantiableViaNamedConstructor(): void
{
$userId = 'user-id';
$itemIds = ['item-1', 'item-3', 'item-2'];

$command = Sorting::create($userId, $itemIds);
$this->assertSortingObject($command, $userId, $itemIds);
}

/**
* Execute asserts against user merge object
* @param object $object
*/
private function assertSortingObject($object, string $userId, array $itemIds): void
{
$this->assertInstanceOf(Sorting::class, $object);
$this->assertSame(
[
'type' => 'sorting',
'parameters' => [
'user_id' => $userId,
'item_ids' => $itemIds,
],
],
$object->jsonSerialize()
);
}
}

0 comments on commit 30e8153

Please sign in to comment.