Skip to content

Commit

Permalink
Merge pull request #2 from dwickstrom/feature-fix-troubles-with-creat…
Browse files Browse the repository at this point in the history
…ing-multiple-fixtures

Try to fix the multiplier functionality
  • Loading branch information
David Wickström committed May 6, 2015
2 parents 488b43a + 7ea1934 commit 873bce1
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function it_should_read_yaml_file()

function it_should_get_the_magento_model_from_the_yaml_file()
{
$this->getModelType()->shouldReturn('customer/address');
$this->getResourceName()->shouldReturn('customer/address');
}

function it_should_read_any_dependencies_on_other_fixtures()
Expand All @@ -54,7 +54,7 @@ function it_should_says_if_there_are_fixture_dependencies()
function it_should_load_a_php_fixture()
{
$this->readFile(getcwd() . '/tests/fixtures/order.php');
$this->getModelType()->shouldReturn('sales/quote');
$this->getResourceName()->shouldReturn('sales/quote');
$this->hasFixtureDependencies()->shouldBe(true);
$this->getFixtureDependencies()->shouldReturn(['catalog/product', 'customer/address']);
}
Expand Down
10 changes: 5 additions & 5 deletions src/MageTest/Manager/Attributes/Provider/AttributesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function __construct(FixtureValidator $validator = null)
*/
public function readAttributes()
{
return $this->model[$this->getModelType()]['attributes'];
return $this->model[$this->getResourceName()]['attributes'];
}

/**
* @return mixed
*/
public function getModelType()
public function getResourceName()
{
return key($this->model);
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public function readFile($file)
*/
public function hasFixtureDependencies()
{
$type = $this->getModelType();
$type = $this->getResourceName();
return isset($this->model[$type]['depends']) && $this->model[$type]['depends'] != null;
}

Expand All @@ -90,8 +90,8 @@ public function hasFixtureDependencies()
*/
public function getFixtureDependencies()
{
$dependencies = isset($this->model[$this->getModelType()]['depends'])
? $this->model[$this->getModelType()]['depends']
$dependencies = isset($this->model[$this->getResourceName()]['depends'])
? $this->model[$this->getResourceName()]['depends']
: null;
if (!is_array($dependencies) && !is_null($dependencies)) {
return [$dependencies];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait OverrideAttributes
*/
public function overrideAttributes(array $attributes, $appendNew = true)
{
$type = $this->getModelType();
$type = $this->getResourceName();

foreach ($this->model[$type]['attributes'] as $key => $value) {
if (array_key_exists($key, $attributes)) {
Expand All @@ -39,7 +39,7 @@ public function overrideAttributes(array $attributes, $appendNew = true)
*/
private function appendNewAttributes(array $attributes)
{
$type = $this->getModelType();
$type = $this->getResourceName();
foreach ($attributes as $key => $value) {
if (!array_key_exists($key, $this->model[$type])) {
$this->model[$type]['attributes'][$key] = $value;
Expand All @@ -50,6 +50,6 @@ private function appendNewAttributes(array $attributes)
/**
* @return string
*/
abstract public function getModelType();
abstract public function getResourceName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function readAttributes();
* Returns magento model required for fixture
* @return mixed
*/
public function getModelType();
public function getResourceName();

/*
* Reads fixture attributes from file
Expand Down
2 changes: 1 addition & 1 deletion src/MageTest/Manager/Builders/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Address extends AbstractBuilder implements BuilderInterface
* @param \Mage_Customer_Model_Customer $customer
* @return $this
*/
public function withCustomer($customer)
public function withCustomer(\Mage_Customer_Model_Customer $customer)
{
$this->attributes['customer_id'] = $customer->getId();
$this->attributes['firstname'] = $customer->getFirstname();
Expand Down
3 changes: 3 additions & 0 deletions src/MageTest/Manager/Builders/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ protected function filterStockData(&$attributes)
return $attributes;
}

/**
* @throws \Exception
*/
private function setStockData()
{
$stockItem = Mage::getModel('cataloginventory/stock_item');
Expand Down
40 changes: 33 additions & 7 deletions src/MageTest/Manager/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class Factory
/**
* @var
*/
private static $multiplier;
public static $multiplier = 0;

private static $with;

/**
* @var FixtureManager
Expand All @@ -30,26 +32,42 @@ class Factory
* @param \MageTest\Manager\FixtureManager|null $fixtureManager
* @param \MageTest\Manager\ProviderInterface|null $provider
* @param null $multiplier
* @param null $with
*/
public function __construct(
FixtureManager $fixtureManager = null,
ProviderInterface $provider = null,
$multiplier = null
$multiplier = null,
$with = null
) {
static::$multiplier = $multiplier;
static::$with = $with;
$this->fixtureManager = $fixtureManager ? : new FixtureManager($provider ? : new AttributesProvider);
}

/**
* @param $model
* @param $resourceName
* @param array $overrides
* @param null $fixtureFile
* @return mixed
*/
public static function make($model, array $overrides = array(), $fixtureFile = null)
public static function make($resourceName, array $overrides = array(), $fixtureFile = null)
{
return (new static(null, null, static::$multiplier))
->fixtureManager->loadFixture($model, $fixtureFile, $overrides, static::$multiplier);
return (new static(null, null, static::$multiplier, static::$with))
->fixtureManager
->setMultiplierId($resourceName)
->setFixtureDependency(static::$with)
->loadFixture($resourceName, $fixtureFile, $overrides, static::$multiplier)
;
}

/**
* @param \Mage_Core_Model_Abstract $model
* @return static
*/
public static function with(\Mage_Core_Model_Abstract $model)
{
return new static(null, null, null, $model);
}

/**
Expand All @@ -66,7 +84,7 @@ public static function clear()
*/
public static function times($multiplier)
{
return new static(null, null, $multiplier);
return new static(null, null, $multiplier, static::$with);
}

/**
Expand Down Expand Up @@ -94,4 +112,12 @@ public static function setFixture(\Mage_Core_Model_Abstract $model)
return (new static)->fixtureManager->setFixture($model);
}

/**
*
*/
public static function unsetFixtures()
{
FixtureManager::$globalFixtureRegistry = [];
}

}
Loading

0 comments on commit 873bce1

Please sign in to comment.