From 800470f77e1afb2080188cef4b44488c286a66ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Wickstr=C3=B6m?= Date: Tue, 5 May 2015 21:04:55 +0200 Subject: [PATCH 1/3] Try to fix the multiplier functionality --- .../Provider/AttributesProvider.php | 10 +- .../Provider/OverrideAttributes.php | 6 +- .../Attributes/Provider/ProviderInterface.php | 2 +- src/MageTest/Manager/Builders/Product.php | 3 + src/MageTest/Manager/Factory.php | 38 ++- src/MageTest/Manager/FixtureManager.php | 295 ++++++++++-------- tests/fixtures/address.php | 9 +- 7 files changed, 214 insertions(+), 149 deletions(-) diff --git a/src/MageTest/Manager/Attributes/Provider/AttributesProvider.php b/src/MageTest/Manager/Attributes/Provider/AttributesProvider.php index 0b483f7..ba7c75a 100644 --- a/src/MageTest/Manager/Attributes/Provider/AttributesProvider.php +++ b/src/MageTest/Manager/Attributes/Provider/AttributesProvider.php @@ -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); } @@ -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; } @@ -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]; diff --git a/src/MageTest/Manager/Attributes/Provider/OverrideAttributes.php b/src/MageTest/Manager/Attributes/Provider/OverrideAttributes.php index 81c5e99..ce22456 100644 --- a/src/MageTest/Manager/Attributes/Provider/OverrideAttributes.php +++ b/src/MageTest/Manager/Attributes/Provider/OverrideAttributes.php @@ -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)) { @@ -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; @@ -50,6 +50,6 @@ private function appendNewAttributes(array $attributes) /** * @return string */ - abstract public function getModelType(); + abstract public function getResourceName(); } diff --git a/src/MageTest/Manager/Attributes/Provider/ProviderInterface.php b/src/MageTest/Manager/Attributes/Provider/ProviderInterface.php index eb11f8a..2596d23 100644 --- a/src/MageTest/Manager/Attributes/Provider/ProviderInterface.php +++ b/src/MageTest/Manager/Attributes/Provider/ProviderInterface.php @@ -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 diff --git a/src/MageTest/Manager/Builders/Product.php b/src/MageTest/Manager/Builders/Product.php index c5ac51c..a31f287 100644 --- a/src/MageTest/Manager/Builders/Product.php +++ b/src/MageTest/Manager/Builders/Product.php @@ -71,6 +71,9 @@ protected function filterStockData(&$attributes) return $attributes; } + /** + * @throws \Exception + */ private function setStockData() { $stockItem = Mage::getModel('cataloginventory/stock_item'); diff --git a/src/MageTest/Manager/Factory.php b/src/MageTest/Manager/Factory.php index c814249..10bf465 100644 --- a/src/MageTest/Manager/Factory.php +++ b/src/MageTest/Manager/Factory.php @@ -14,7 +14,9 @@ class Factory /** * @var */ - private static $multiplier; + public static $multiplier = 0; + + private static $with; /** * @var FixtureManager @@ -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); } /** @@ -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 = []; + } + } diff --git a/src/MageTest/Manager/FixtureManager.php b/src/MageTest/Manager/FixtureManager.php index 34ad164..4831015 100644 --- a/src/MageTest/Manager/FixtureManager.php +++ b/src/MageTest/Manager/FixtureManager.php @@ -19,7 +19,12 @@ final class FixtureManager /** * @var array */ - private static $fixtures = array(); + public static $globalFixtureRegistry = array(); + + /** + * @var array + */ + private $fixtures = array(); /** * @var array @@ -36,6 +41,8 @@ final class FixtureManager */ private $storage; + private $multiplier = array(); + /** * @param ProviderInterface $attributesProvider * @param Storage $storage @@ -47,94 +54,112 @@ public function __construct(ProviderInterface $attributesProvider, Storage $stor } /** - * @param $fixtureType - * @param null $userFixtureFile + * @param $resourceName + * @param null $providedFixtureFile * @param array $overrides * @param $multiplier * @return mixed */ - public function loadFixture($fixtureType, $userFixtureFile = null, array $overrides = null, $multiplier = null) + public function loadFixture($resourceName, $providedFixtureFile = null, array $overrides = null, $multiplier = null) { - $attributesProvider = clone $this->attributesProvider; - - // Fetch a given fixture file - if ($userFixtureFile) { - $this->fixtureFileExists($userFixtureFile); - $attributesProvider->readFile($userFixtureFile); - } else { - // Fall back to a custom default, or to a default default - $attributesProvider->readFile($this->getFallbackFixture($fixtureType)); + // First time we enter this method then we will specify how many models we want to make + // and if no argument is specified then we will want to build just 1 model + if (!$this->multiplier[$resourceName]) { + $this->multiplier[$resourceName] = $multiplier ? : 1; + $this->fixtures[$resourceName] = []; } - // ...and override attributes + add non-existing ones too + // Load an appropriate fixture file + $attributesProvider = $this->getAttributesProvider($resourceName, $providedFixtureFile); + + // ...and apply any attribute overrides + add attributes not present in the fixture file if ($overrides) { $attributesProvider->overrideAttributes($overrides); } - // Fetch a matching builder instance - $builder = $this->getBuilder($attributesProvider->getModelType()); - - // Set the attributes for the builder to construct a model with - $builder->setAttributes($attributesProvider->readAttributes()); + // Load the correct builder and set attributes on that instance + $builder = $this->prepareBuilder($attributesProvider); // Load any dependencies recursively if ($attributesProvider->hasFixtureDependencies()) { foreach ($attributesProvider->getFixtureDependencies() as $dependency) { $withDependency = 'with' . $this->getDependencyModel($dependency); - if ($this->hasFixture($dependency)) { - // When building models that has a dependency, it is nice to be able to - // first build the dependency and then the dependant model. The trick is - // to check if there is a model already built and if so, reuse that guy - // when building the dependant class - if (is_array(self::$fixtures[$dependency])) { - // If they key holds an array of models, then just use the first one - $builder->$withDependency(reset(self::$fixtures[$dependency])); - } else { - $builder->$withDependency(self::$fixtures[$dependency]); - } + if ($this->isLoaded($dependency)) { + $builder->$withDependency($this->fetchDependency($dependency)); } else { - // Otherwise, just go ahead and create a new model $builder->$withDependency($this->loadFixture($dependency)); } } } - return $this->create($attributesProvider->getModelType(), $builder, $multiplier); + return $this->create($attributesProvider->getResourceName(), $builder); + } + + /** + * @param $resourceName + * @param BuilderInterface $builder + * @return mixed + */ + private function create($resourceName, BuilderInterface $builder) + { + if ($this->multiplier[$resourceName] > 1) { + $this->invokeBuild($resourceName, $builder); + return $this->loadFixture($resourceName); + } + $this->invokeBuild($resourceName, $builder); + return $this->fixtures[$resourceName]; + } + + /** + * @param $resourceName + * @param BuilderInterface $builder + * @return void + */ + private function invokeBuild($resourceName, BuilderInterface $builder) + { + $model = $builder->build(); + Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); + $this->register($resourceName, $this->saveModel($model)); + Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID); + $this->multiplier[$resourceName]--; + } + + /** + * @param $resourceName + * @param $model + * @return void + */ + private function register($resourceName, $model) + { + $this->fixtures[$resourceName][] = $model; + static::$globalFixtureRegistry[] = $model; } /** * Returns a single model previously loaded * - * @param $name + * @param $resourceName * @param $number If it has several of same type, get model with $number * @throws InvalidArgumentException * @return mixed */ - public function getFixture($name, $number = null) + public function getFixture($resourceName, $number = null) { - if (!$this->hasFixture($name)) { - throw new InvalidArgumentException("Could not find a fixture: $name"); + if (!$this->isLoaded($resourceName)) { + throw new InvalidArgumentException("Could not find a fixture: $resourceName"); } // A number was given, and indeed the fixtures key is an array, // then go ahead and return the wanted number - if ($number && is_array(static::$fixtures[$name])) { - return static::$fixtures[$name][$number]; + if ($number && is_array($this->fixtures[$resourceName])) { + return $this->fixtures[$resourceName][$number]; } - // If no number is specified as argument, then return the first one off + // If no number is specified as argument, then return the last one off // fixtures the array - if (is_array(static::$fixtures[$name])) { - return static::$fixtures[$name][0]; + if (is_array($this->fixtures[$resourceName])) { + return end($this->fixtures[$resourceName]); } // Lastly, if its not an array and no number was given, just return // the fixture that was queried for - return static::$fixtures[$name]; - } - - /** - * @param \Mage_Core_Model_Abstract $model - */ - public function setFixture(\Mage_Core_Model_Abstract $model) - { - self::$fixtures[$model->getResourceName()] = $model; + return $this->fixtures[$resourceName]; } /** @@ -142,35 +167,7 @@ public function setFixture(\Mage_Core_Model_Abstract $model) */ public function getFixtures() { - return static::$fixtures; - } - - /** - * @param $name - * @param BuilderInterface $builder - * @param $multiplier - * @return mixed - */ - private function create($name, BuilderInterface $builder, $multiplier) - { - if ($multiplier > 1) { - $models = array(); - while ($multiplier) { - $model = $builder->build(); - Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); - $models[] = $this->saveModel($model); - $multiplier--; - } - Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID); - Factory::resetMultiplier(); - - return static::$fixtures[$name] = $models; - } - $model = $builder->build(); - Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); - $this->saveModel($model); - Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID); - return static::$fixtures[$name] = $model; + return static::$globalFixtureRegistry; } /** @@ -178,23 +175,16 @@ private function create($name, BuilderInterface $builder, $multiplier) */ public function clear() { - foreach (static::$fixtures as $model) { - if (is_array($model)) { - foreach ($model as $fixture) { - Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); - $fixture->delete(); - Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID); - } - } else { - Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); - $model->delete(); - Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID); - } + foreach (static::$globalFixtureRegistry as $model) { + Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); + $model->delete(); + Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID); } - static::$fixtures = array(); + static::$globalFixtureRegistry = array(); $this->storage->truncate(); } + /** * Clean db */ @@ -209,22 +199,21 @@ public function prepareDb() } /** - * @param $name + * @param $resourceName * @return bool */ - private function hasFixture($name) + private function isLoaded($resourceName) { - return array_key_exists($name, static::$fixtures); + return array_key_exists($resourceName, $this->fixtures); } - /** - * @param $name + * @param $resourceName * @return bool */ - private function hasBuilder($name) + private function hasBuilder($resourceName) { - return array_key_exists($name, $this->builders); + return array_key_exists($resourceName, $this->builders); } /** @@ -233,10 +222,6 @@ private function hasBuilder($name) */ private function getBuilder($modelType) { - if ($this->hasBuilder($modelType)) { - return $this->builders[$modelType]; - } - switch ($modelType) { case 'admin/user': return $this->builders[$modelType] = new Builders\Admin($modelType, $this->storage); @@ -256,26 +241,15 @@ private function getBuilder($modelType) } /** - * @param $fixtureFile - * @throws InvalidArgumentException - */ - private function fixtureFileExists($fixtureFile) - { - if (!file_exists($fixtureFile)) { - throw new InvalidArgumentException("The fixture file: $fixtureFile does not exist. Please check path."); - } - } - - /** - * @param $fixtureType + * @param $resourceName * @return string * @throws Exception */ - private function getFallbackFixture($fixtureType) + private function loadFixtureFile($resourceName) { foreach (FixtureFallback::locationSequence() as $directory) { foreach (FixtureFallback::$sequence as $type) { - if (file_exists($fixture = $directory . DIRECTORY_SEPARATOR . FixtureFallback::getFileName($fixtureType, $type))) { + if (file_exists($fixture = $directory . DIRECTORY_SEPARATOR . FixtureFallback::getFileName($resourceName, $type))) { return $fixture; } } @@ -284,36 +258,99 @@ private function getFallbackFixture($fixtureType) } /** - * @param $dependency + * @param $resourceName * @return string */ - private function getDependencyModel($dependency) + private function getDependencyModel($resourceName) { $attributesProvider = clone $this->attributesProvider; - $attributesProvider->readFile($this->getFallbackFixture($dependency)); - $dependencyType = $attributesProvider->getModelType(); + $attributesProvider->readFile($this->loadFixtureFile($resourceName)); + $dependencyType = $attributesProvider->getResourceName(); return $this->parseDependencyModel($dependencyType); } /** - * @param $dependencyType + * @param $resourceName * @return string */ - private function parseDependencyModel($dependencyType) + private function parseDependencyModel($resourceName) { - preg_match("/\/(.*)/", $dependencyType, $matches); + preg_match("/\/(.*)/", $resourceName, $matches); return ucfirst(end($matches)); } /** - * @param $model - * @return mixed + * @param \Mage_Core_Model_Abstract $model + * @return \Mage_Core_Model_Abstract */ - private function saveModel($model) + private function saveModel(\Mage_Core_Model_Abstract $model) { - $model->getResource()->save($model); + $model->getResource()->save($model); $this->storage->persistIdentifier($model); - return $model; + return $model; + } + + /** + * @param $resourceName + * @param $providedFixtureFile + * @return ProviderInterface + * @throws Exception + */ + private function getAttributesProvider($resourceName, $providedFixtureFile) + { + $attributesProvider = clone $this->attributesProvider; + + // Fetch a given fixture file + if ($providedFixtureFile && file_exists($providedFixtureFile)) { + $attributesProvider->readFile($providedFixtureFile); + } else { + // Fall back to a custom default, or to a default default + $attributesProvider->readFile($this->loadFixtureFile($resourceName)); + } + return $attributesProvider; + } + + /** + * @param $attributesProvider + * @return Builders\Address|Builders\Admin|Builders\Customer|Builders\General|Builders\Order|Builders\Product + */ + private function prepareBuilder($attributesProvider) + { + // Fetch a matching builder instance + $builder = $this->getBuilder($attributesProvider->getResourceName()); + // Set the attributes for the builder to construct a model with + $builder->setAttributes($attributesProvider->readAttributes()); + return $builder; + } + + /** + * @param $resourceName + * @return mixed + */ + private function fetchDependency($resourceName) + { + if (is_array($this->fixtures[$resourceName])) { + return end($this->fixtures[$resourceName]); + } + return $this->fixtures[$resourceName]; + } + + /** + * @param $model + * @return $this + */ + public function setFixtureDependency($model) + { + if ($model instanceof \Mage_Core_Model_Abstract) { + $this->fixtures[$model->getResourceName()][] = $model; + } + return $this; + } + + public function setMultiplierId($model) + { + $this->multiplier[$model] = null; + return $this; } } diff --git a/tests/fixtures/address.php b/tests/fixtures/address.php index 3191ec5..784bb67 100644 --- a/tests/fixtures/address.php +++ b/tests/fixtures/address.php @@ -4,14 +4,13 @@ 'customer/address' => array( 'depends' => 'customer/customer', 'attributes' => array( - 'company' => 'Karlsson & Lord', - 'street' => 'Swedenborgsgatan 1', - 'city' => 'Stockholm', + 'company' => $this->faker->company, + 'street' => $this->faker->streetAddress, + 'city' => $this->faker->city, 'postcode' => '11450', - 'region' => 'Södertörn', 'country' => 'Sweden', 'country_id' => 'SE', - 'telephone' => '1234567890', + 'telephone' => $this->faker->phoneNumber, 'is_default_billing' => 1, 'is_default_shipping' => 1, 'save_in_address_book' => 1 From c3581ab939421cffd3684a9724043950a1349252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Wickstr=C3=B6m?= Date: Tue, 5 May 2015 21:54:28 +0200 Subject: [PATCH 2/3] Fix broken test --- .../Manager/Attributes/Provider/AttributesProviderSpec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/MageTest/Manager/Attributes/Provider/AttributesProviderSpec.php b/spec/MageTest/Manager/Attributes/Provider/AttributesProviderSpec.php index f420e92..d977810 100644 --- a/spec/MageTest/Manager/Attributes/Provider/AttributesProviderSpec.php +++ b/spec/MageTest/Manager/Attributes/Provider/AttributesProviderSpec.php @@ -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() @@ -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']); } From 7ea193438f6be39c4659ed3bec5256bf9c782cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Wickstr=C3=B6m?= Date: Wed, 6 May 2015 08:33:46 +0200 Subject: [PATCH 3/3] Fix the with method --- src/MageTest/Manager/Builders/Address.php | 2 +- src/MageTest/Manager/Factory.php | 2 +- src/MageTest/Manager/FixtureManager.php | 20 +++++++++++++++----- tests/MageTest/FactoryTest.php | 11 ++++++++++- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/MageTest/Manager/Builders/Address.php b/src/MageTest/Manager/Builders/Address.php index d28f39b..3d88cdf 100644 --- a/src/MageTest/Manager/Builders/Address.php +++ b/src/MageTest/Manager/Builders/Address.php @@ -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(); diff --git a/src/MageTest/Manager/Factory.php b/src/MageTest/Manager/Factory.php index 10bf465..9690f2c 100644 --- a/src/MageTest/Manager/Factory.php +++ b/src/MageTest/Manager/Factory.php @@ -84,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); } /** diff --git a/src/MageTest/Manager/FixtureManager.php b/src/MageTest/Manager/FixtureManager.php index 4831015..dcb67f4 100644 --- a/src/MageTest/Manager/FixtureManager.php +++ b/src/MageTest/Manager/FixtureManager.php @@ -91,21 +91,26 @@ public function loadFixture($resourceName, $providedFixtureFile = null, array $o } } } - return $this->create($attributesProvider->getResourceName(), $builder); + return $this->create($attributesProvider->getResourceName(), $builder, $providedFixtureFile, $overrides); } /** * @param $resourceName * @param BuilderInterface $builder + * @param $providedFixtureFile + * @param $overrides * @return mixed */ - private function create($resourceName, BuilderInterface $builder) + private function create($resourceName, BuilderInterface $builder, $providedFixtureFile, $overrides) { if ($this->multiplier[$resourceName] > 1) { $this->invokeBuild($resourceName, $builder); - return $this->loadFixture($resourceName); + return $this->loadFixture($resourceName, $providedFixtureFile, $overrides); } $this->invokeBuild($resourceName, $builder); + if (count($this->fixtures[$resourceName]) < 2) { + return reset($this->fixtures[$resourceName]); + } return $this->fixtures[$resourceName]; } @@ -335,11 +340,12 @@ private function fetchDependency($resourceName) return $this->fixtures[$resourceName]; } + /** - * @param $model + * @param \Mage_Core_Model_Abstract $model * @return $this */ - public function setFixtureDependency($model) + public function setFixtureDependency(\Mage_Core_Model_Abstract $model) { if ($model instanceof \Mage_Core_Model_Abstract) { $this->fixtures[$model->getResourceName()][] = $model; @@ -347,6 +353,10 @@ public function setFixtureDependency($model) return $this; } + /** + * @param $model + * @return $this + */ public function setMultiplierId($model) { $this->multiplier[$model] = null; diff --git a/tests/MageTest/FactoryTest.php b/tests/MageTest/FactoryTest.php index 40c61b6..3579e3c 100644 --- a/tests/MageTest/FactoryTest.php +++ b/tests/MageTest/FactoryTest.php @@ -29,9 +29,18 @@ public function testCreateSimpleProduct() } } + public function testSettingDependencyExplicitly() + { + $customer = Factory::make('customer/customer', ['firstname' => 'foobar']); + $addresses = Factory::with($customer)->times(2)->make('customer/address'); + $this->assertTrue(is_array($addresses)); + $this->assertCount(2, $addresses); + $this->assertEquals(end($addresses)->getFirstname(), 'foobar'); + } + public function testCreateAddress() { - $address = Factory::make('customer/address', ['city' => 'Stockholm']); + $address = Factory::make('customer/address', ['city' => 'Stockholm', 'company' => 'Karlsson & Lord']); $this->assertEquals('Karlsson & Lord', $address->getCompany()); $this->assertEquals('Stockholm', $address->getCity());