Permalink
Browse files

remove option functionality except set and clear. It doesn't make sen…

…se to partially set options and it also prevents option validation
  • Loading branch information...
1 parent f43d270 commit bcf75146dcd97156d3749b79e09ae56e25c6a820 @iampersistent iampersistent committed Jul 15, 2012
@@ -0,0 +1,14 @@
+<?php
+/**
+ * (c) 2012 Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Vespolina\Entity\Exception;
+
+class InvalidOptionsException extends \Exception
+{
+
+}
@@ -8,6 +8,7 @@
namespace Vespolina\Entity\Order;
+use Vespolina\Entity\Exception\InvalidOptionsException;
use Vespolina\Entity\Order\ItemInterface;
/**
@@ -101,34 +102,19 @@ public function getState()
return $this->state;
}
- /**
- * Add a option for the product in this item
- *
- * @param string $type
- * @param string $value
- */
- protected function addOption($type, $value)
- {
- $this->options[$type] = $value;
- }
-
- protected function addOptions(array $options)
- {
- $this->options = array_merge($this->options, $options);
- }
-
protected function clearOptions()
{
+ if (!$this->product->validateOptions(array())) {
+ throw new InvalidOptionsException('This product requires options');
+ }
$this->options = array();
}
- protected function removeOption($type)
- {
- unset($this->options[$type]);
- }
-
protected function setOptions(array $options)
{
+ if (!$this->product->validateOptions($options)) {
+ throw new InvalidOptionsException('This combination of options is not valid for the product');
+ }
$this->options = $options;
}
@@ -49,7 +49,7 @@ function getParent();
/**
* Set the parent order for this item
*
- * @param Vespolina\Entity\OrderInterface $parent
+ * @param Vespolina\Entity\Order\OrderInterface $parent
*/
function setParent(OrderInterface $parent);
@@ -12,64 +12,47 @@ public function testOptionMethods()
$rmSetProduct = new \ReflectionMethod($item, 'setProduct');
$rmSetProduct->setAccessible(true);
- $rmSetProduct->invokeArgs($item, array($this->createProductOptionValidate()));
$this->assertNull($item->getOption('noOption'));
- $rmAddOption = new \ReflectionMethod($item, 'addOption');
- $rmAddOption->setAccessible(true);
- $rmAddOption->invokeArgs($item, array('option1', 1));
- $this->assertCount(1, $item->getOptions());
- $this->assertSame(1, $item->getOption('option1'));
-
- $rmAddOption->invokeArgs($item, array('option2', 2));
- $this->assertCount(2, $item->getOptions());
- $this->assertSame(2, $item->getOption('option2'));
-
- $rmRemoveOption = new \ReflectionMethod($item, 'removeOption');
- $rmRemoveOption->setAccessible(true);
- $rmRemoveOption->invoke($item, 'option2');
- $this->assertCount(1, $item->getOptions(), 'remove by option');
- $this->assertNull($item->getOption('option2'));
-
- $options = array(
- 'option2' => 2,
- 'option3' => 3
- );
-
- $rmAddOptions = new \ReflectionMethod($item, 'addOptions');
- $rmAddOptions->setAccessible(true);
- $rmAddOptions->invoke($item, $options);
- $this->assertCount(3, $item->getOptions());
- $this->assertSame(2, $item->getOption('option2'));
- $this->assertSame(3, $item->getOption('option3'));
-
$rmSetOptions = new \ReflectionMethod($item, 'setOptions');
$rmSetOptions->setAccessible(true);
- $rmSetOptions->invoke($item, $options);
- $this->assertCount(2, $item->getOptions());
- $this->assertNull($item->getOption('option1'));
- $this->assertSame(2, $item->getOption('option2'));
- $this->assertSame(3, $item->getOption('option3'));
- $rmRemoveOption->invoke($item, 'option3');
- $this->assertCount(1, $item->getOptions(), 'option should be removed by type');
+ $rmSetProduct->invokeArgs($item, array($this->createProductOptionValidate()));
+ $rmSetOptions->invoke($item, array(
+ 'option2' => 2,
+ 'option3' => 3
+ ));
- $rmRemoveOption->invoke($item, 'nada');
- $this->assertCount(1, $item->getOptions());
+ $options = $item->getOptions();
+ $this->assertCount(2, $options);
+ $this->assertArrayHasKey('option2', $options);
+ $this->assertArrayHasKey('option3', $options);
$rmClearOptions = new \ReflectionMethod($item, 'clearOptions');
$rmClearOptions->setAccessible(true);
+
+ $rmSetProduct->invokeArgs($item, array($this->createProductOptionValidate(false)));
+ $this->setExpectedException('Vespolina\Entity\Exception\InvalidOptionsException');
+ $rmClearOptions->invoke($item);
+ $this->assertSame($options, $item->getOptions(), 'nothing should have been removed if the validation fails');
+
+ $rmSetProduct->invokeArgs($item, array($this->createProductOptionValidate()));
$rmClearOptions->invoke($item);
$this->assertEmpty($item->getOptions());
+
+ $rmSetProduct->invokeArgs($item, array($this->createProductOptionValidate(false)));
+ $this->setExpectedException('Vespolina\Entity\Exception\InvalidOptionsException');
+ $rmSetOptions->invokeArgs($item, array('failure' => 0));
+ $this->assertEmpty($item->getOptions(), 'nothing should be added if the validation fails');
}
- protected function createProductOptionValidate()
+ protected function createProductOptionValidate($returns = true)
{
$product = $this->getMock('Vespolina\Entity\Product');
$product->expects($this->atLeastOnce())
->method('validateOptions')
- ->will($this->returnValue(true));
+ ->will($this->returnValue($returns));
return $product;
}

0 comments on commit bcf7514

Please sign in to comment.