-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base interaction command & basic tests
- Loading branch information
Showing
2 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |