Skip to content

Commit

Permalink
Add check for forbidden item_id in item properties (RAD-720)
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Dec 5, 2017
1 parent 0279f66 commit 1715f12
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/Model/Command/ItemProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ItemProperty extends AbstractCommand
private function __construct(string $itemId, array $properties)
{
$this->setItemId($itemId);
$this->properties = $properties;
$this->setProperties($properties);
}

public static function create(string $itemId, array $properties = []): self
Expand All @@ -32,6 +32,17 @@ protected function setItemId(string $itemId): void
$this->itemId = $itemId;
}

protected function setProperties(array $properties): void
{
Assertion::keyNotExists(
$properties,
'item_id',
'$properties cannot contain key item_id - it is used as primary key to identify the item. Please use different key instead.'
);

$this->properties = $properties;
}

protected function getCommandType(): string
{
return 'item-properties';
Expand Down
5 changes: 5 additions & 0 deletions src/Model/Command/ItemPropertySetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public static function set(string $propertyName): self
protected function setPropertyName(string $propertyName): void
{
Assertion::typeIdentifier($propertyName);
Assertion::notEq(
$propertyName,
'item_id',
'Cannot redefine property item_id - it is used as primary key to identify items. Please use different name.'
);

$this->propertyName = $propertyName;
}
Expand Down
13 changes: 12 additions & 1 deletion tests/Model/Command/ItemPropertySetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lmc\Matej\Model\Command;

use Lmc\Matej\Exception\DomainException;
use PHPUnit\Framework\TestCase;

class ItemPropertySetupTest extends TestCase
Expand All @@ -17,7 +18,7 @@ public function shouldBeInstantiableViaNamedConstructors(
$propertyName = 'examplePropertyName';

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

$this->assertInstanceOf(ItemPropertySetup::class, $command);
$this->assertSame(
Expand All @@ -32,6 +33,16 @@ public function shouldBeInstantiableViaNamedConstructors(
);
}

/**
* @test
* @dataProvider provideConstructorName
*/
public function shouldNotAllowItemIdAsPropertyName(string $constructorName): void
{
$this->expectException(DomainException::class);
ItemPropertySetup::$constructorName('item_id');
}

/**
* @return array[]
*/
Expand Down
14 changes: 10 additions & 4 deletions tests/Model/Command/ItemPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace Lmc\Matej\Model\Command;

use Lmc\Matej\Exception\DomainException;
use PHPUnit\Framework\TestCase;

class ItemPropertyTest extends TestCase
{
/**
* @test
*/
public function shouldNotAllowItemIdInProperties(): void
{
$this->expectException(DomainException::class);
ItemProperty::create('exampleItemId', ['item_id' => 'customItemId']);
}

/**
* @test
* @dataProvider provideProperties
Expand Down Expand Up @@ -36,10 +46,6 @@ public function provideProperties(): array
['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 1715f12

Please sign in to comment.