Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Initial example for configurable product #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/MageTest/Manager/Attributes/Provider/YamlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class YamlProvider implements ProviderInterface
*/
public function readFile($file)
{
$this->yaml = Yaml::parse($file);
return $this->yaml = Yaml::parse($file);
}

/**
Expand Down
89 changes: 89 additions & 0 deletions src/MageTest/Manager/Builders/Configurable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace MageTest\Manager\Builders;

/**
* Class Configurable
* @package MageTest\Manager\Builders
*/
class Configurable extends AbstractBuilder implements BuilderInterface
{
/**
* @return \Mage_Catalog_Model_Product
*/
public function build()
{

$configurableAttributes = $this->attributes['configurable_attributes'];

$configurableProductsData = array();
$attributeIds = array();

foreach($configurableAttributes as $sku => $attributes)
{
$simpleProduct = \Mage::getModel('catalog/product');
$simpleProduct->addData($this->attributes['simple_product_attributes']);
$simpleProduct->setSku($sku);

if(isset($attributes['price'])){
$simpleProduct->setPrice($attributes['price']);
unset($attributes['price']);
}

$childAttributes = array();

foreach($attributes as $attributeCode => $value)
{
if(!isset($attributeDetails[$attributeCode])){
$attributeDetails[$attributeCode] = $this->getAttributeDetails($attributeCode);
$attributeIds[] = $attributeDetails[$attributeCode]['id'];
}

$valueIndex = $attributeDetails[$attributeCode]['options'][$value];

$childAttributes[] = array(
'attribute_id' => $attributeDetails[$attributeCode]['id'],
'label' => $value,
'value_index' => $valueIndex,
'#_value' => $simpleProduct->getPrice()
);

$simpleProduct->setData($attributeCode, $valueIndex);
}

$simpleProduct->save();

$configurableProductsData[$simpleProduct->getId()] = $childAttributes;
}

$this->model->addData($this->attributes['default_attributes']);

$this->model->getTypeInstance()->setUsedProductAttributeIds($attributeIds);
$configurableAttributesData = $this->model->getTypeInstance()->getConfigurableAttributesAsArray();

$this->model->setCanSaveConfigurableAttributes(true);
$this->model->setConfigurableAttributesData($configurableAttributesData);

$this->model->setConfigurableProductsData($configurableProductsData);

return $this->model;
}

private function getAttributeDetails($code)
{
$config = \Mage::getSingleton('eav/config');
$attribute = $config->getAttribute(\Mage_Catalog_Model_Product::ENTITY, $code);
$options = $attribute->getSource()->getAllOptions();

$values = array();

foreach($options as $option){
$values[$option['label']] = $option['value'];
}

return array(
"id" => $attribute->getId(),
"options" => $values
);
}
}
5 changes: 4 additions & 1 deletion src/MageTest/Manager/Builders/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class Order extends AbstractBuilder implements BuilderInterface
*/
public function withProduct($product, $qty = 1)
{
$this->model->addProduct($product, new \Varien_Object(array(
$newProd = Mage::getModel('catalog/product');
$newProd->load($newProd->getIdBySku($product->getSku()));

$this->model->addProduct($newProd, new \Varien_Object(array(
'qty' => $qty
)));
return $this;
Expand Down
86 changes: 36 additions & 50 deletions src/MageTest/Manager/FixtureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public function loadFixture($fixtureType, $userFixtureFile = null)
$attributesProvider->readFile($fixtureFile);
}

$attributes = $attributesProvider->readAttributes();

$builder = $this->getBuilder($attributesProvider->getModelType());
$builder->setAttributes($attributesProvider->readAttributes());

$builder->setAttributes($attributes);

if($attributesProvider->hasFixtureDependencies())
{
Expand All @@ -64,7 +67,7 @@ public function loadFixture($fixtureType, $userFixtureFile = null)
}
}

return $this->create($attributesProvider->getModelType(), $builder);
return $this->create($builder);
}

/**
Expand All @@ -73,34 +76,15 @@ public function loadFixture($fixtureType, $userFixtureFile = null)
* @return mixed
* @throws \InvalidArgumentException
*/
public function create($name, BuilderInterface $builder)
public function create(BuilderInterface $builder)
{
if($this->hasFixture($name))
{
throw new \InvalidArgumentException("Fixture $name has already been set. Please use unique names.");
}

$model = $builder->build();

\Mage::app()->setCurrentStore(\Mage_Core_Model_App::ADMIN_STORE_ID);
$model->save();
\Mage::app()->setCurrentStore(\Mage_Core_Model_App::DISTRO_STORE_ID);

return $this->fixtures[$name] = $model;
}

/**
* @param $name
* @return mixed
* @throws \InvalidArgumentException
*/
public function getFixture($name)
{
if(!$this->hasFixture($name))
{
throw new \InvalidArgumentException("Could not find a fixture: $name");
}
return $this->fixtures[$name];
return $this->fixtures[] = $model;
}

/**
Expand All @@ -110,26 +94,17 @@ public function clear()
{
foreach ($this->fixtures as $fixture) {
\Mage::app()->setCurrentStore(\Mage_Core_Model_App::ADMIN_STORE_ID);

if ($fixture->getTypeId() === \Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$this->clearAssociatedProducts($fixture);
}

$fixture->delete();

\Mage::app()->setCurrentStore(\Mage_Core_Model_App::DISTRO_STORE_ID);
}
$this->fixtures = array();
}

/**
* @param $name
* @return bool
*/
private function hasFixture($name) {
return array_key_exists($name, $this->fixtures);
}

/**
* @param $name
* @return bool
*/
private function hasBuilder($name) {
return array_key_exists($name, $this->builders);
$this->fixtures = array();
}

/**
Expand All @@ -138,17 +113,13 @@ private function hasBuilder($name) {
*/
private function getBuilder($modelType)
{
if($this->hasBuilder($modelType))
{
return $this->builders[$modelType];
}

switch($modelType)
{
case 'customer/address': return $this->builders[$modelType] = new Builders\Address($modelType);
case 'customer/customer': return $this->builders[$modelType] = new Builders\Customer($modelType);
case 'catalog/product': return $this->builders[$modelType] = new Builders\Product($modelType);
case 'sales/quote': return $this->builders[$modelType] = new Builders\Order($modelType);
case 'customer/address': return new Builders\Address($modelType);
case 'customer/customer': return new Builders\Customer($modelType);
case 'catalog/product/simple': return new Builders\Product('catalog/product');
case 'catalog/product/configurable': return new Builders\Configurable('catalog/product');
case 'sales/quote': return new Builders\Order($modelType);
}
}

Expand Down Expand Up @@ -184,8 +155,23 @@ private function getDefaultFixtureTemplate($fixtureType)
{
case 'customer/address': return $filePath . 'Address.yml';
case 'customer/customer': return $filePath . 'Customer.yml';
case 'catalog/product': return $filePath . 'Product.yml';
case 'catalog/product/simple' : return $filePath . 'Product.yml';
case 'catalog/product/configurable' : return $filePath . 'Configurable.yml';
case 'sales/quote': return $filePath . 'Order.yml';
}
}
}

/**
* @param $fixture
*/
private function clearAssociatedProducts($fixture)
{
$childIds = $fixture->getTypeInstance()->getChildrenIds($fixture->getId());

foreach ($childIds[0] as $value) {
$product = \Mage::getModel('catalog/product')->load($value);
$product->delete();
$product->clearInstance();
}
}
}
35 changes: 35 additions & 0 deletions src/MageTest/Manager/Fixtures/Configurable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
catalog/product/configurable:
default_attributes:
sku: test-conf
attribute_set_id: 9
name: product name
weight: 2
price: 100
description: Product description
short_description: Product short description
tax_class_id: 4
type_id: configurable
visibility: 4
status: 1
stock_data: { use_config_manage_stock: 0, manage_stock: 1, is_in_stock: 1 }
website_ids: [1]
simple_product_attributes:
website_ids: [1]
attribute_set_id: 9
name: product name
tax_class_id: 4
status: 1
weight: 2
price: 100
description: Product description
short_description: Product short description
type_id: simple
visibility: 1
stock_data: { use_config_manage_stock: 0, manage_stock: 1, min_sale_qty: 1, max_sale_qty: 2, is_in_stock: 1, qty: 999 }
configurable_attributes:
test-conf-1:
color: Green
test-conf-2:
color: Blue
test-conf-3:
color: Black
2 changes: 1 addition & 1 deletion src/MageTest/Manager/Fixtures/Order.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sales/quote (customer/address catalog/product):
sales/quote (customer/address catalog/product/simple):
shipping_method: flatrate_flatrate
payment_method: checkmo
5 changes: 3 additions & 2 deletions src/MageTest/Manager/Fixtures/Product.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
catalog/product:
catalog/product/simple:
sku: testsku123
attribute_set_id: 9
name: product name
Expand All @@ -10,5 +10,6 @@ catalog/product:
type_id: simple
visibility: 4
status: 1
stock_data: { is_in_stock: 1, qty: 99999 }
media_gallery: { images: [], values: [] }
stock_data: { use_config_manage_stock: 0, manage_stock: 1, is_in_stock: 1, qty: 999}
website_ids: [1]
10 changes: 6 additions & 4 deletions tests/MageTest/Manager/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ protected function setUp()

public function testAssignAddressToCustomer()
{
$customer = $this->manager->getFixture('customer/customer');
$customer = $this->addressFixture->getCustomer();

$this->customerLogin($customer->getEmail(), $customer->getPassword());
//hard coded due to hashing
$this->customerLogin($customer->getEmail(), '123123pass');

$this->assertSession()->pageTextContains($this->addressFixture->getPostcode());
}

public function testDeleteAddressOfCustomer()
{
$customer = $this->manager->getFixture('customer/customer');
$customer = $this->addressFixture->getCustomer();

$this->customerLogin($customer->getEmail(), $customer->getPassword());
//hard coded due to hashing
$this->customerLogin($customer->getEmail(), '123123pass');

$this->manager->clear();

Expand Down
21 changes: 21 additions & 0 deletions tests/MageTest/Manager/ConfigurableProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace MageTest\Manager;

class ConfigurableProductTest extends WebTestCase
{
private $configurableProductFixture;

protected function setUp()
{
parent::setUp();
$this->configurableProductFixture = $this->manager->loadFixture('catalog/product/configurable');
}

public function testCreateConfigurableProduct()
{
$session = $this->getSession();
$session->visit(getenv('BASE_URL') . '/catalog/product/view/id/' . $this->configurableProductFixture->getId());

$this->assertSession()->statusCodeEquals(200);
}
}
7 changes: 3 additions & 4 deletions tests/MageTest/Manager/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ProductTest extends WebTestCase
protected function setUp()
{
parent::setUp();
$this->productFixture = $this->manager->loadFixture('catalog/product');
$this->productFixture = $this->manager->loadFixture('catalog/product/simple');
}

public function testCreateSimpleProduct()
Expand All @@ -27,16 +27,15 @@ public function testDeleteSimpleProduct()
$this->assertSession()->statusCodeEquals(404);
}

public function testCreateSimpleProductWithImage()
/* public function testCreateSimpleProductWithImage()
{
$imageURL = getcwd() . '/tests/MageTest/Manager/Assets/370x370.jpg';

$this->productFixture->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$this->productFixture->addImageToMediaGallery($imageURL, array('image','thumbnail','small_image'), false, false);
$this->productFixture->save();

$session = $this->getSession();
$session->visit(getenv('BASE_URL') . '/catalog/product/view/id/' . $this->productFixture->getId());
$this->assertSession()->elementExists('css', '#image');
}
}*/
}
2 changes: 1 addition & 1 deletion tests/MageTest/Manager/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class WebTestCase extends PHPUnit_Framework_Testcase

protected function setUp()
{
\Mage::init();
\Mage::app();
$this->mink = new Mink(array(
'goutte' => new Session(new GoutteDriver())
));
Expand Down