Skip to content

Commit

Permalink
Add base interaction command & basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Nov 23, 2017
1 parent 76e24ba commit fbfd414
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/Model/Command/Interaction.php
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

/**
* Save user-invoked interaction
* TODO: add documentation to individual setters
*/
class Interaction extends AbstractCommand
{
const RELEVANCE_LOW = 'low';
const RELEVANCE_MEDIUM = 'medium';
const RELEVANCE_HIGH = 'high';

/** @var string */
private $userId;
/** @var int */
private $count;
/** @var string|null */
private $scenario = null;
/** @var float|null */
private $rotationRate = null;
/** @var int|null */
private $rotationTime = null;
/** @var bool|null */
private $hardRotation = null;
/** @var string|null */
private $minRelevance = null;
/** @var string|null */
private $filter = null;

/**
* Save user-invoked interaction
*/
public function __construct(string $userId, int $count, string $scenario)
{
$this->userId = $userId;
$this->count = $count;
$this->scenario = $scenario;
}

public function setRotationRate(float $rotationRate = null): self
{
// TODO: has to be between 0 and 1
$this->rotationRate = $rotationRate;

return $this;
}

public function setRotationTime(int $rotationTime = null): self
{
$this->rotationTime = $rotationTime;

return $this;
}

public function setHardRotation(bool $hardRotation = null): self
{
$this->hardRotation = $hardRotation;

return $this;
}

public function setMinRelevance(string $minRelevance = null): self
{
$this->minRelevance = $minRelevance;

return $this;
}

public function setFilter(string $filter = null): self
{
$this->filter = $filter;

return $this;
}

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

public function getCommandParameters(): array
{
$payload = [
'user_id' => $this->userId,
'count' => $this->count,
'scenario' => $this->scenario,
];

$this->rotationRate === null ?: $payload['rotation_rate'] = $this->rotationRate;
$this->rotationTime === null ?: $payload['rotation_time'] = $this->rotationTime;
$this->hardRotation === null ?: $payload['hard_rotation'] = $this->hardRotation;
$this->minRelevance === null ?: $payload['min_relevance'] = $this->minRelevance;
$this->filter === null ?: $payload['filter'] = $this->filter;

return $payload;
}

/**
* Save user-invoked interaction
*/
public static function create(string $userId, int $count, string $scenario): self
{
return new static($userId, $count, $scenario);
}
}
95 changes: 95 additions & 0 deletions tests/Model/Command/InteractionTest.php
@@ -0,0 +1,95 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

use PHPUnit\Framework\TestCase;

class InteractionTest extends TestCase
{
/** @var string */
private $userId;
/** @var int */
private $count;
/** @var string */
private $scenario;

public function setUp(): void
{
$this->userId = 'user-' . md5(uniqid());
$this->count = rand();
$this->scenario = 'scenario-' . md5(uniqid());
}

/**
* @test
*/
public function shouldGenerateCorrectSignature(): void
{
// Sample data
$rotationRate = (float) rand();
$rotationTime = rand(11111108000, 2524608000); // 2524608000 is 2050
$filter = md5(uniqid());

/**
* Test plain constructors
*/
$this->assertInteractionObject(new Interaction($this->userId, $this->count, $this->scenario));
$this->assertInteractionObject($this->createBaseCommand());

/**
* Test optional arguments
*/
$command = $this->createBaseCommand()->setRotationRate($rotationRate);
$this->assertInteractionObject($command, 'rotation_rate', $rotationRate);

$command = $this->createBaseCommand()->setRotationTime($rotationTime);
$this->assertInteractionObject($command, 'rotation_time', $rotationTime);

$command = $this->createBaseCommand()->setHardRotation(true);
$this->assertInteractionObject($command, 'hard_rotation', true);

$command = $this->createBaseCommand()->setHardRotation(false);
$this->assertInteractionObject($command, 'hard_rotation', false);

$command = $this->createBaseCommand()->setMinRelevance(Interaction::RELEVANCE_LOW);
$this->assertInteractionObject($command, 'min_relevance', Interaction::RELEVANCE_LOW);

$command = $this->createBaseCommand()->setMinRelevance(Interaction::RELEVANCE_MEDIUM);
$this->assertInteractionObject($command, 'min_relevance', Interaction::RELEVANCE_MEDIUM);

$command = $this->createBaseCommand()->setMinRelevance(Interaction::RELEVANCE_HIGH);
$this->assertInteractionObject($command, 'min_relevance', Interaction::RELEVANCE_HIGH);

$command = $this->createBaseCommand()->setFilter($filter);
$this->assertInteractionObject($command, 'filter', $filter);
}

private function createBaseCommand(): Interaction
{
return Interaction::create($this->userId, $this->count, $this->scenario);
}

/**
* Execute asserts against user merge object
* @param object $object
* @param mixed $optionalValue
*/
private function assertInteractionObject($object, string $optionalArgument = null, $optionalValue = null): void
{
$expected = [
'type' => 'interaction',
'parameters' => [
'user_id' => $this->userId,
'count' => $this->count,
'scenario' => $this->scenario,
],
];

if ($optionalArgument !== null) {
$expected['parameters'][$optionalArgument] = $optionalValue;
}

$this->assertInstanceOf(Interaction::class, $object);
$this->assertSame($expected, $object->jsonSerialize());
}
}

0 comments on commit fbfd414

Please sign in to comment.