Permalink
Browse files

add media to base product class, get tests passing

  • Loading branch information...
1 parent 189cce2 commit 6dbf1e135a943977de600a514e7a18c02ec108ba @iampersistent iampersistent committed Sep 8, 2012
@@ -0,0 +1,16 @@
+<?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\Asset;
+
+use Vespolina\Entity\Asset\MediaInterface;
+
+class Media implements MediaInterface
+{
+
+}
@@ -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\Asset;
+
+interface MediaInterface
+{
+
+}
@@ -8,6 +8,7 @@
namespace Vespolina\Entity\Product;
+use Vespolina\Entity\Asset\MediaInterface;
use Vespolina\Entity\Identifier\IdentifierInterface;
use Vespolina\Entity\Product\BaseProductInterface;
use Vespolina\Entity\Product\OptionInterface;
@@ -48,6 +49,25 @@ public function getDescription()
return $this->description;
}
+ /**
+ * @inheritdoc
+ */
+ public function addFeature(FeatureInterface $feature)
+ {
+ $type = $feature->getType();
+ $this->features[$type] = $feature;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addFeatures(array $features)
+ {
+ foreach($features as $feature) {
+ $this->addFeature($feature);
+ }
+ }
+
/**
* @inheritdoc
*/
@@ -56,6 +76,18 @@ public function clearFeatures()
$this->features = array();
}
+ /**
+ * @inheritdoc
+ */
+ public function getFeature($type)
+ {
+ if (isset($this->features[$type])) {
+ return $this->features[$type];
+ }
+
+ return null;
+ }
+
/**
* @inheritdoc
*/
@@ -64,6 +96,17 @@ public function getFeatures()
return $this->features;
}
+ /**
+ * @inheritdoc
+ */
+ function removeFeature($feature)
+ {
+ if ($feature instanceof FeatureInterface) {
+ $feature = $feature->getType();
+ }
+ unset($this->features[$feature]);
+ }
+
/**
* @inheritdoc
*/
@@ -203,6 +246,60 @@ public function addIdentifier($identifier, $target = null)
$this->processIdentifiers();
}
+ /**
+ * @inheritdoc
+ */
+ public function addMedia(MediaInterface $media)
+ {
+ $this->media[] = $media;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addMediaCollection(array $media)
+ {
+ $this->media = array_merge($this->media, $media);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function clearMedia()
+ {
+ $this->media = array();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getAllMedia()
+ {
+ return $this->media;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function removeMedia(MediaInterface $media)
+ {
+ foreach ($this->media as $key => $mediaToCompare) {
+ if ($mediaToCompare == $media) {
+ unset($this->media[$key]);
+ break;
+ }
+
+ }
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setMedia(array $media)
+ {
+ $this->media = $media;
+ }
+
/**
* @inheritdoc
*/
@@ -8,6 +8,7 @@
namespace Vespolina\Entity\Product;
+use Vespolina\Entity\Asset\MediaInterface;
use Vespolina\Entity\Product\FeatureInterface;
use Vespolina\Entity\Identifier\IdentifierInterface;
use Vespolina\Entity\Product\OptionInterface;
@@ -128,6 +129,46 @@ function getIdentifierSet($target = null);
*/
function addIdentifier($identifier, $target = null);
+ /**
+ * Add a media to the collection
+ *
+ * @param MediaInterface $media
+ */
+ function addMedia(MediaInterface $media);
+
+ /**
+ * Add a collection of medias
+ *
+ * @param array $medias
+ */
+ function addMediaCollection(array $medias);
+
+ /**
+ * Remove all media from the collection
+ */
+ function clearMedia();
+
+ /**
+ * Return a collection of media
+ *
+ * @return array of MediaInterface
+ */
+ function getAllMedia();
+
+ /**
+ * Remove a media from the collection
+ *
+ * @param MediaInterface $media
+ */
+ function removeMedia(MediaInterface $media);
+
+ /**
+ * Set a collection of media
+ *
+ * @param array $media
+ */
+ function setMedia(array $media);
+
/**
* Use a different name or different technique
*
@@ -20,51 +20,8 @@
*/
class Product extends BaseProduct implements ProductInterface
{
- protected $assets;
protected $slug;
- /**
- * @inheritdoc
- */
- public function addFeature(FeatureInterface $feature)
- {
- $type = $feature->getType();
- $this->features[$type] = $feature;
- }
-
- /**
- * @inheritdoc
- */
- public function addFeatures(array $features)
- {
- foreach($features as $feature) {
- $this->addFeature($feature);
- }
- }
-
- /**
- * @inheritdoc
- */
- public function getFeature($type)
- {
- if (isset($this->features[$type])) {
- return $this->features[$type];
- }
-
- return null;
- }
-
- /**
- * @inheritdoc
- */
- function removeFeature($feature)
- {
- if ($feature instanceof FeatureInterface) {
- $feature = $feature->getType();
- }
- unset($this->features[$feature]);
- }
-
/**
* @inheritdoc
*/
@@ -6,17 +6,18 @@
* with this source code in the file LICENSE.
*/
-use Vespolina\Entity\Feature;
-use Vespolina\Entity\Option;
-use Vespolina\Entity\OptionGroup;
-use Vespolina\Entity\Product;
+use Vespolina\Entity\Asset\Media;
+use Vespolina\Entity\Product\Feature;
+use Vespolina\Entity\Product\Option;
+use Vespolina\Entity\Product\OptionGroup;
+use Vespolina\Entity\Product\BaseProduct;
use Vespolina\Entity\Identifier\SKUIdentifier;
-class ProductTest extends \PHPUnit_Framework_TestCase
+class BaseProductTest extends \PHPUnit_Framework_TestCase
{
public function testFeatureMethods()
{
- $product = new Product();
+ $product = $this->getMockForAbstractClass('Vespolina\Entity\Product\BaseProduct');
$feature1 = new Feature();
$feature1->setType('feature1');
@@ -62,9 +63,39 @@ public function testFeatureMethods()
$this->assertEmpty($product->getFeatures());
}
+ public function testMedia()
+ {
+ $product = $this->getMockForAbstractClass('Vespolina\Entity\Product\BaseProduct');
+ $this->assertNull($product->getAllMedia(), 'make sure we start out empty');
+
+ $media = new Media();
+ $product->addMedia($media);
+ $this->assertContains($media, $product->getAllMedia());
+ $this->assertCount(1, $product->getAllMedia());
+
+ $medias = array();
+ $medias[] = new Media();
+ $medias[] = new Media();
+ $product->addMediaCollection($medias);
+ $this->assertCount(3, $product->getAllMedia());
+ $this->assertContains($media, $product->getAllMedia());
+
+ $product->removeMedia($media);
+ $this->assertNotContains($media, $product->getAllMedia());
+ $this->assertCount(2, $product->getAllMedia());
+
+ $product->clearMedia();
+ $this->assertEmpty($product->getAllMedia());
+
+ $product->addMedia($media);
+ $product->setMedia($medias);
+ $this->assertNotContains($media, $product->getAllMedia(), 'this should have been removed on setting a new array of items');
+ $this->assertCount(2, $product->getAllMedia());
+ }
+
public function testOptionMatrix()
{
- $product = new Product();
+ $product = $this->getMockForAbstractClass('Vespolina\Entity\Product\BaseProduct');
$colorGroup = new OptionGroup();
$colorBlue = $this->createOption('blue', 'color', 'colorBlue');

0 comments on commit 6dbf1e1

Please sign in to comment.