Permalink
Browse files

add pricing context to entities

  • Loading branch information...
1 parent 4802596 commit 94681fca1cac0946b30fb36d7c1df12d0c8d7e48 @iampersistent iampersistent committed Jul 26, 2012
@@ -0,0 +1,65 @@
+<?php
+/**
+ * (c) 2011-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\Pricing;
+
+/**
+ * PricingContext implements a data container holding price variables needed for calculation
+ *
+ * @author Daniel Kucharski <daniel@xerias.be>
+ */
+
+use Vespolina\Entity\Pricing\PricingContextInterface;
+
+class PricingContext implements PricingContextInterface
+{
+ protected $data;
+ protected $entities;
+
+ public function __construct($data = array())
+ {
+ $this->data = $data;
+ $this->entities = array();
+ }
+
+ public function addEntity($entity)
+ {
+ $this->entities[] = $entity;
+ }
+
+ public function getEntities()
+ {
+ return $this->entities;
+ }
+
+ public function setEntities($entities)
+ {
+ $this->entities = $entities;
+ }
+
+ public function get($key, $default = null)
+ {
+ if (array_key_exists($key, $this->data)) {
+ return $this->data[$key];
+ } elseif ($default) {
+ return $default;
+ } else {
+ return null;
+ }
+ }
+
+ public function set($key, $value)
+ {
+ $this->data[$key] = $value;
+ }
+
+ public function getData()
+ {
+ return $this->data;
+ }
+}
@@ -0,0 +1,17 @@
+<?php
+/**
+ * (c) 2011-2012 Vespolina Project http://www.vespolina-project.org
+ *
+ * (c) Daniel Kucharski <daniel@xerias.be>
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Vespolina\Entity\Pricing;
+
+interface PricingContextInterface
+{
+ function get($key, $default = null);
+
+ function set($key, $value);
+}
@@ -0,0 +1,34 @@
+<?php
+
+use Vespolina\Entity\Pricing\PricingContext;
+
+/**
+ * @author Richard Shank <develop@zestic.com>
+ */
+class PricingContextTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstruct()
+ {
+ $data = array(
+ 'something' => 'nothing',
+ );
+ $context = new PricingContext($data);
+
+ $rp = new \ReflectionProperty($context, 'data');
+ $rp->setAccessible(true);
+ $this->assertSame($data, $rp->getValue($context), "the passed data should be put in the object's data property");
+ }
+
+ public function testGet()
+ {
+ $data = array(
+ 'something' => 'nothing',
+ );
+ $context = new PricingContext($data);
+
+ $this->assertSame('nothing', $context->get('something'), 'simple return of data');
+ $this->assertNull($context->get('bullshit'), 'there is no bullshit so a null should be returned');
+ $this->assertSame('Charlie Sheen', $context->get('winning', 'Charlie Sheen'), "the default value should be returned since there isn't a data set");
+ $this->assertSame('nothing', $context->get('something', 'something'), 'the default should only happen when there is no data');
+ }
+}

0 comments on commit 94681fc

Please sign in to comment.