Skip to content

Commit

Permalink
Merge pull request #11 from OndraM/feature/commands
Browse files Browse the repository at this point in the history
Commands to setup and update database items
  • Loading branch information
OndraM committed Nov 20, 2017
2 parents 96d11b3 + a2771ea commit d26cb12
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Model/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

abstract class AbstractCommand implements \JsonSerializable
{
/**
* Get command type identifier. Must be one of those defined by Matej API schema.
*/
abstract protected function getCommandType(): string;

/**
* Get data content of the command. Must follow the format defined by Matej API schema.
*/
abstract protected function getCommandParameters(): array;

public function jsonSerialize(): array
{
return [
'type' => $this->getCommandType(),
'parameters' => $this->getCommandParameters(),
];
}
}
41 changes: 41 additions & 0 deletions src/Model/Command/ItemProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

/**
* Command to save different item content properties to Matej.
*/
class ItemProperty extends AbstractCommand
{
/** @var string */
private $itemId;
/** @var array */
private $properties;

private function __construct(string $itemId, array $properties)
{
// TODO: assert itemId format

$this->itemId = $itemId;
$this->properties = $properties;
}

public static function create(string $itemId, array $properties = []): self
{
return new static($itemId, $properties);
}

protected function getCommandType(): string
{
return 'item-properties';
}

protected function getCommandParameters(): array
{
$parameters = $this->properties;

$parameters['item_id'] = $this->itemId;

return $parameters;
}
}
73 changes: 73 additions & 0 deletions src/Model/Command/ItemPropertySetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

/**
* Command to add or delete item property in the database.
*/
class ItemPropertySetup extends AbstractCommand
{
const PROPERTY_TYPE_INT = 'int';
const PROPERTY_TYPE_DOUBLE = 'double';
const PROPERTY_TYPE_STRING = 'string';
const PROPERTY_TYPE_BOOLEAN = 'boolean';
const PROPERTY_TYPE_TIMESTAMP = 'timestamp';
const PROPERTY_TYPE_SET = 'set';

/** @var string */
private $propertyName;
/** @var string */
private $propertyType;

private function __construct(string $propertyName, string $propertyType)
{
// TODO: assert propertyName format
// TODO: assert propertyType is one of PROPERTY_TYPE_*

$this->propertyName = $propertyName;
$this->propertyType = $propertyType;
}

public static function int(string $propertyName): self
{
return new static($propertyName, self::PROPERTY_TYPE_INT);
}

public static function double(string $propertyName): self
{
return new static($propertyName, self::PROPERTY_TYPE_DOUBLE);
}

public static function string(string $propertyName): self
{
return new static($propertyName, self::PROPERTY_TYPE_STRING);
}

public static function boolean(string $propertyName): self
{
return new static($propertyName, self::PROPERTY_TYPE_BOOLEAN);
}

public static function timestamp(string $propertyName): self
{
return new static($propertyName, self::PROPERTY_TYPE_TIMESTAMP);
}

public static function set(string $propertyName): self
{
return new static($propertyName, self::PROPERTY_TYPE_SET);
}

protected function getCommandType(): string
{
return 'item-properties-setup';
}

protected function getCommandParameters(): array
{
return [
'property_name' => $this->propertyName,
'property_type' => $this->propertyType,
];
}
}
49 changes: 49 additions & 0 deletions tests/Model/Command/ItemPropertySetupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

use PHPUnit\Framework\TestCase;

class ItemPropertySetupTest extends TestCase
{
/**
* @test
* @dataProvider provideConstructorName
*/
public function shouldBeInstantiableViaNamedConstructors(
string $constructorName,
string $expectedPropertyType
): void {
$propertyName = 'examplePropertyName';

/** @var ItemPropertySetup $command */
$command = forward_static_call([ItemPropertySetup::class, $constructorName], $propertyName);

$this->assertInstanceOf(ItemPropertySetup::class, $command);
$this->assertSame(
[
'type' => 'item-properties-setup',
'parameters' => [
'property_name' => 'examplePropertyName',
'property_type' => $expectedPropertyType,
],
],
$command->jsonSerialize()
);
}

/**
* @return array[]
*/
public function provideConstructorName(): array
{
return [
['int', ItemPropertySetup::PROPERTY_TYPE_INT],
['double', ItemPropertySetup::PROPERTY_TYPE_DOUBLE],
['string', ItemPropertySetup::PROPERTY_TYPE_STRING],
['boolean', ItemPropertySetup::PROPERTY_TYPE_BOOLEAN],
['timestamp', ItemPropertySetup::PROPERTY_TYPE_TIMESTAMP],
['set', ItemPropertySetup::PROPERTY_TYPE_SET],
];
}
}
45 changes: 45 additions & 0 deletions tests/Model/Command/ItemPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Lmc\Matej\Model\Command;

use PHPUnit\Framework\TestCase;

class ItemPropertyTest extends TestCase
{
/**
* @test
* @dataProvider provideProperties
*/
public function shouldBeInstantiableViaNamedConstructor(array $properties, array $expectedParameters): void
{
$command = ItemProperty::create('exampleItemId', $properties);

$this->assertInstanceOf(ItemProperty::class, $command);
$this->assertSame(
[
'type' => 'item-properties',
'parameters' => $expectedParameters,
],
$command->jsonSerialize()
);
}

/**
* @return array[]
*/
public function provideProperties(): array
{
return [
'No item properties' => [[], ['item_id' => 'exampleItemId']],
'One item property' => [['date' => 1510756952], ['date' => 1510756952, 'item_id' => 'exampleItemId']],
'Multiple item properties' => [
['item1' => 'value1', 'item2' => 'value2'],
['item1' => 'value1', 'item2' => 'value2', 'item_id' => 'exampleItemId'],
],
'Should not allow to override item_id' => [
['item_id' => 'customItemId'],
['item_id' => 'exampleItemId'],
],
];
}
}

0 comments on commit d26cb12

Please sign in to comment.