diff --git a/spec/MageTest/MagentoExtension/Fixture/CategorySpec.php b/spec/MageTest/MagentoExtension/Fixture/CategorySpec.php new file mode 100644 index 0000000..9aeaa92 --- /dev/null +++ b/spec/MageTest/MagentoExtension/Fixture/CategorySpec.php @@ -0,0 +1,119 @@ + so we can send you a copy immediately. + * + * @category MageTest + * @package MagentoExtension + * @subpackage Fixure + * + * @copyright Copyright (c) 2012-2013 MageTest team and contributors. + */ +namespace spec\MageTest\MagentoExtension\Fixture; + +use Mockery; +use PhpSpec\ObjectBehavior; +use Prophecy\Argument; + +class CategorySpec extends ObjectBehavior +{ + /** @var \Mage_Catalog_Model_Category $model */ + private $model; + + /** @var \Mage_Catalog_Model_Resource_Category_Collection $collection */ + private $collection; + + function let() + { + $this->model = $categoryModel = Mockery::mock('Mage_Catalog_Model_Category'); + $websiteHelper = Mockery::mock('MageTest\MagentoExtension\Helper\Website'); + $websiteHelper->shouldReceive('getWebsiteIds')->andReturn(array()); + $websiteHelper->shouldReceive('getWebsites')->andReturn(array()); + + $serviceContainer = array( + 'categoryModel' => function() use($categoryModel) { + return $categoryModel; + }, + 'websiteHelper' => function() use($websiteHelper) { + return $websiteHelper; + }, + ); + + $this->beConstructedWith($serviceContainer); + + $this->collection = $collection = Mockery::mock('Mage_Catalog_Model_Resource_Category_Collection'); + $categoryModel->shouldReceive('getCollection')->andReturn($collection); + $categoryModel->shouldReceive('getId')->andReturn(1)->byDefault(); + $categoryModel->shouldReceive('load')->andReturn($categoryModel)->byDefault(); + $categoryModel->shouldReceive('getData')->andReturn(array())->byDefault(); + $categoryModel->shouldReceive('setData')->andReturn($categoryModel)->byDefault(); + $categoryModel->shouldReceive('save')->andReturn($categoryModel)->byDefault(); + $nameAttribute = Mockery::mock('Mage_Eav_Model_Entity_Attribute') + ->shouldReceive('getAttributeCode') + ->andReturn('name') + ->getMock(); + $categoryModel->shouldReceive('getAttributes')->andReturn(array($nameAttribute))->byDefault(); + + $collection->shouldReceive('addFieldToFilter')->andReturn($collection)->byDefault(); + $collection->shouldReceive('addPathsFilter')->andReturn($collection)->byDefault(); + $collection->shouldReceive('getFirstItem')->andReturn($categoryModel)->byDefault(); + } + + function letgo() + { + Mockery::close(); + } + + function it_loads_category_model_by_name_on_default_path() + { + $categoryData = array( + 'name' => 'Category1', + ); + + $this->collection->shouldReceive('addFieldToFilter')->with('name', 'Category1') + ->once() + ->andReturn($this->collection) + ->ordered(); + $this->collection->shouldReceive('addPathsFilter') + ->with(\MageTest\MagentoExtension\Fixture\Category::DEFAULT_ROOT_CATEGORY_ID) + ->once() + ->andReturn($this->collection) + ->ordered(); + $this->collection->shouldReceive('getFirstItem')->once()->ordered(); + + $this->create($categoryData); + } + + function it_loads_category_model_by_name_on_custom_path() + { + $categoryData = array( + 'name' => 'Category1', + 'path' => '1/2/3', + ); + + $this->collection->shouldReceive('addFieldToFilter')->with('name', 'Category1') + ->once() + ->andReturn($this->collection) + ->ordered(); + $this->collection->shouldReceive('addPathsFilter') + ->with('1/2/3') + ->once() + ->andReturn($this->collection) + ->ordered(); + $this->collection->shouldReceive('getFirstItem')->once()->ordered(); + + $this->create($categoryData); + } +} \ No newline at end of file diff --git a/spec/MageTest/MagentoExtension/Fixture/ProductSpec.php b/spec/MageTest/MagentoExtension/Fixture/ProductSpec.php index c9be2ba..dbcb3f0 100644 --- a/spec/MageTest/MagentoExtension/Fixture/ProductSpec.php +++ b/spec/MageTest/MagentoExtension/Fixture/ProductSpec.php @@ -74,7 +74,7 @@ function let() function it_should_throw_exception_if_sku_is_not_defined() { - $this->shouldThrow('\RuntimeException')->during('create', array()); + $this->shouldThrow('\RuntimeException')->duringCreate(array()); } function it_should_create_a_product_given_all_required_attributes() diff --git a/src/MageTest/MagentoExtension/Fixture/Category.php b/src/MageTest/MagentoExtension/Fixture/Category.php index 0a063ba..12c2269 100644 --- a/src/MageTest/MagentoExtension/Fixture/Category.php +++ b/src/MageTest/MagentoExtension/Fixture/Category.php @@ -22,6 +22,7 @@ */ namespace MageTest\MagentoExtension\Fixture; use MageTest\MagentoExtension\Fixture; +use MageTest\MagentoExtension\Helper\Website; /** * Product fixtures functionality provider @@ -37,17 +38,27 @@ class Category implements FixtureInterface const DEFAULT_ROOT_CATEGORY_ID = 1; const DEFAULT_ATTRIBUTE_SET_ID = 3; - private $modelFactory = null; + /** @var \Mage_Catalog_Model_Category $model */ private $model; - private $attributes; + + /** @var array $defaultAttributes */ private $defaultAttributes; + /** @var array $serviceContainer List of factory methods indexed by service name. */ + protected $serviceContainer = []; + /** - * @param $productModelFactory \Closure optional + * @param array $serviceContainer */ - public function __construct($modelFactory = null) + public function __construct($serviceContainer = array()) { - $this->modelFactory = $modelFactory ?: $this->defaultModelFactory(); + $this->serviceContainer['categoryModel'] = isset($serviceContainer['categoryModel']) + ? $serviceContainer['categoryModel'] + : $this->defaultModelFactory(); + + $this->serviceContainer['websiteHelper'] = isset($serviceContainer['websiteHelper']) + ? $serviceContainer['websiteHelper'] + : $this->defaultWebsiteHelperFactory(); } /** @@ -65,10 +76,9 @@ public function create(array $attributes) \Mage::app()->setCurrentStore(\Mage_Core_Model_App::ADMIN_STORE_ID); - $modelFactory = $this->modelFactory; - $this->model = $modelFactory(); + $this->model = $this->serviceContainer['categoryModel'](); - $id = $this->getIdByName($attributes['name']); + $id = $this->getIdByName($attributes['name'], $this->getPath($attributes)); if ($id) { $this->model->load($id); } @@ -129,6 +139,18 @@ public function defaultModelFactory() }; } + /** + * Retrieve default Website helper used in the class + * + * @return \Closure + */ + private function defaultWebsiteHelperFactory() + { + return function() { + return new Website(); + }; + } + protected function getDefaultAttributes() { if ($this->defaultAttributes) { @@ -154,11 +176,10 @@ protected function getDefaultAttributes() protected function getWebsiteIds() { - $ids = array(); - foreach (\Mage::getModel('core/website')->getCollection() as $website) { - $ids[] = $website->getId(); - } - return $ids; + $websiteHelper = $this->serviceContainer['websiteHelper'](); + return array_map(function($website) { + return $website->getId(); + }, $websiteHelper->getWebsites()); } protected function retrieveDefaultAttributeSetId() @@ -168,10 +189,23 @@ protected function retrieveDefaultAttributeSetId() ->getDefaultAttributeSetId(); } - protected function getIdByName($name) + protected function getIdByName($name, $path) { - $category = $this->model->getCollection()->addFieldToFilter('name', $name)->getFirstItem(); + $category = $this->model->getCollection() + ->addFieldToFilter('name', $name) + ->addPathsFilter($path) + ->getFirstItem(); return $category ? $category->getId() : null; } + + /** + * @param array $attributes + * + * @return mixed + */ + protected function getPath(array $attributes) + { + return isset($attributes['path']) ? $attributes['path'] : self::DEFAULT_ROOT_CATEGORY_ID; + } }