Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature/bundle fixture #65

Open
wants to merge 6 commits into
base: develop
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
90 changes: 90 additions & 0 deletions src/MageTest/MagentoExtension/Context/MagentoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use MageTest\MagentoExtension\Service\Session;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Gherkin\Node\TableNode;
use MageTest\MagentoExtension\Fixture\BundleProduct;

/**
* MagentoContext
Expand Down Expand Up @@ -162,6 +163,7 @@ public function theCacheIsClean()
public function theProductsExist(TableNode $table)
{
$hash = $table->getHash();

foreach ($hash as $row) {
if (isset($row['is_in_stock'])) {
if (!isset($row['qty'])) {
Expand All @@ -181,6 +183,94 @@ public function theProductsExist(TableNode $table)
}
}

/**
* @Given /^the following bundle products exist:$/
*/
public function theFollowingBundleProductsExist(TableNode $table)
{
$hash = $table->getHash();

foreach ($hash as $row)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing curly brackets

$this->factory->create('bundle_product', $row);
}

/**
* @Given /^the bundle with sku "([^"]*)" have the following option data:$/
*/
public function theBundleWithSkuHaveTheFollowingOptionData($sku, TableNode $table)
{
$bundleInRegistry = \Mage::registry(BundleProduct::REGISTRY_FIXTURE_PREFIX . $sku);

if (is_null($bundleInRegistry))
throw new \InvalidArgumentException("Bundle product with sku $sku could not be found in registry");

$hash = $table->getHash();

foreach ($hash as $row) {
$optionRawData = array();
$optionRawData[0] = $row;
$optionRawData[0]['option_id'] = '';
$optionRawData[0]['delete'] = '';

$bundleInRegistry->setCanSaveConfigurableAttributes(false);
$bundleInRegistry->setCanSaveCustomOptions(true);
$bundleInRegistry->setBundleOptionsData($optionRawData);
}
}

/**
* @Given /^the following products are added to the bundle with "([^"]*)" sku:$/
*/
public function theFollowingProductsAreAddedToTheBundleWithSku($sku, TableNode $table)
{
\Mage::app()->setCurrentStore(\Mage_Core_Model_App::ADMIN_STORE_ID);

$bundleInRegistry = \Mage::registry(BundleProduct::REGISTRY_FIXTURE_PREFIX . $sku);

if (is_null($bundleInRegistry))
throw new \InvalidArgumentException("Bundle product with sku $sku could not be found in registry");

$model = \Mage::getModel('catalog/product');
$hash = $table->getHash();

$selectionRawData = array();
$selectionRawData[0] = array();
$bundleProductId = $model->getIdBySku($sku);
$bundledProductIds = array();

if($bundleProductId > 0) {
$bundledProductIds = $bundleInRegistry->getTypeInstance(true)->getChildrenIds($bundleInRegistry->getId(), false);
}

foreach ($hash as $index => $row) {
$productId = $model->getIdBySku($row['sku']);
// this is to prevent SQLSTATE[23000]: Integrity constraint violation
if (BundleProduct::isSelectionProduct($productId, $bundledProductIds))
continue;

if ((int) $productId == 0)
throw new \Exception("Simple product with sku {$row['sku']} could not be found");

unset($row['sku']);

$row['product_id'] = $productId;
$row['selection_id'] = '';
$row['option_id'] = '';
$row['delete'] = '';

$selectionRawData[0][$index] = $row;
}

if (count($selectionRawData[0]) > 0) {
\Mage::register('current_product', $bundleInRegistry, true);
$bundleInRegistry->setBundleSelectionsData($selectionRawData);
$bundleInRegistry->setCanSaveBundleSelections(true);
$bundleInRegistry->setAffectBundleProductSelections(true);
$bundleInRegistry->save();
\Mage::app()->setCurrentStore(\Mage_Core_Model_App::DISTRO_STORE_ID);
}
}

public function setApp(MageApp $app)
{
$this->app = $app;
Expand Down
Loading