-
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.
- Loading branch information
Showing
5 changed files
with
232 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,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(), | ||
]; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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 self($propertyName, self::PROPERTY_TYPE_INT); | ||
} | ||
|
||
public static function double(string $propertyName): self | ||
{ | ||
return new self($propertyName, self::PROPERTY_TYPE_DOUBLE); | ||
} | ||
|
||
public static function string(string $propertyName): self | ||
{ | ||
return new self($propertyName, self::PROPERTY_TYPE_STRING); | ||
} | ||
|
||
public static function boolean(string $propertyName): self | ||
{ | ||
return new self($propertyName, self::PROPERTY_TYPE_BOOLEAN); | ||
} | ||
|
||
public static function timestamp(string $propertyName): self | ||
{ | ||
return new self($propertyName, self::PROPERTY_TYPE_TIMESTAMP); | ||
} | ||
|
||
public static function set(string $propertyName): self | ||
{ | ||
return new self($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, | ||
]; | ||
} | ||
} |
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,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], | ||
]; | ||
} | ||
} |
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,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'], | ||
], | ||
]; | ||
} | ||
} |