Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions spec/MageTest/MagentoExtension/Fixture/CategorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* BehatMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License, that is bundled with this
* package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
*
* http://opensource.org/licenses/MIT
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email
* to <magetest@sessiondigital.com> 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);
}
}
2 changes: 1 addition & 1 deletion spec/MageTest/MagentoExtension/Fixture/ProductSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
64 changes: 49 additions & 15 deletions src/MageTest/MagentoExtension/Fixture/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace MageTest\MagentoExtension\Fixture;
use MageTest\MagentoExtension\Fixture;
use MageTest\MagentoExtension\Helper\Website;

/**
* Product fixtures functionality provider
Expand All @@ -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();
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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;
}
}