diff --git a/README.md b/README.md index 2287181..94c5676 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ $fm->define('Bar')->addDefinition('baz', Faker::date('Y-m-d')); ### Creating And Seeding -The `create` function will create and save your model, and will also save anything you generate with the `Factory` generator too. If you want to create multiple instances, check out the seed `seed` function, which accepts an additional argument at the start which is the number of models to generate in the process. The `seed` function will effectively be calling the `create` function over and over. It should be noted that you can set a custom save function before you get going with the `setSaveMethod` function. Also, a reminder that the `instance` function is still available if you don't want database persistence. +The `create` function will create and save your model, and will also save anything you generate with the `Factory` generator too. If you want to create multiple instances, check out the seed `seed` function, which accepts an additional argument at the start which is the number of models to generate in the process. The `seed` function will effectively be calling the `create` function over and over. Also, a reminder that the `instance` function is still available if you don't want database persistence. You may encounter the following exceptions: * `League\FactoryMuffin\Exceptions\ModelNotFoundException` will be thrown if the model class defined is not found. @@ -177,7 +177,7 @@ There are 5 other helper functions available: ### Deleting -You can delete all your saved models with the `deleteSaved` function. It should be noted that you can set a custom delete function before you get going with the `setDeleteMethod` function. Please note that your saved models will be deleted in the reverse order they were saved to ensure relationships behave correctly. +You can delete all your saved models with the `deleteSaved` function. Please note that your saved models will be deleted in the reverse order they were saved to ensure relationships behave correctly. If one or more models cannot be deleted, a `League\FactoryMuffin\Exceptions\DeletingFailedException` will be raised after we have attempted to delete all the saved models. You may access each underlying exception, in the order they were thrown during the whole process, with the `getExceptions` function which will return an array of exceptions. You may encounter the following exceptions: * `League\FactoryMuffin\Exceptions\DeleteFailedException` will be thrown if the delete function on your model returns false. @@ -241,9 +241,8 @@ class TestUserModel extends PHPUnit_Framework_TestCase // create a new factory muffin instance static::$fm = new FactoryMuffin(); - // note that method chaining is supported if - // you want to configure a few extra things - static::$fm->setSaveMethod('save')->setDeleteMethod('delete'); + // you can customize the save/delete methods + // new FactoryMuffin(new ModelStore('save', 'delete')); // load your model definitions static::$fm->loadFactories(__DIR__.'/factories'); diff --git a/src/FactoryMuffin.php b/src/FactoryMuffin.php index 6a8d50d..18b02f8 100644 --- a/src/FactoryMuffin.php +++ b/src/FactoryMuffin.php @@ -68,34 +68,6 @@ public function __construct(StoreInterface $store = null, GeneratorFactory $fact $this->factory = $factory ?: new GeneratorFactory(); } - /** - * Set the method we use when saving models. - * - * @param string $method The save method name. - * - * @return \League\FactoryMuffin\FactoryMuffin - */ - public function setSaveMethod($method) - { - $this->store->setSaveMethod($method); - - return $this; - } - - /** - * Set the method we use when deleting models. - * - * @param string $method The delete method name. - * - * @return \League\FactoryMuffin\FactoryMuffin - */ - public function setDeleteMethod($method) - { - $this->store->setDeleteMethod($method); - - return $this; - } - /** * Creates and saves multiple versions of a model. * diff --git a/src/Stores/AbstractStore.php b/src/Stores/AbstractStore.php index 580d48a..1a338f2 100644 --- a/src/Stores/AbstractStore.php +++ b/src/Stores/AbstractStore.php @@ -40,6 +40,13 @@ abstract class AbstractStore */ private $saved = []; + /** + * The underlying operational method names. + * + * @var string[] + */ + protected $methods = []; + /** * Save the model to the database. * diff --git a/src/Stores/StoreInterface.php b/src/Stores/StoreInterface.php index 96af7b5..928aa69 100644 --- a/src/Stores/StoreInterface.php +++ b/src/Stores/StoreInterface.php @@ -21,24 +21,6 @@ */ class StoreInterface { - /** - * Set the method we use when saving models. - * - * @param string $method The save method name. - * - * @return void - */ - public function setSaveMethod($method); - - /** - * Set the method we use when deleting models. - * - * @param string $method The delete method name. - * - * @return void - */ - public function setDeleteMethod($method); - /** * Save the model to the database. * diff --git a/tests/SaveAndDeleteTest.php b/tests/SaveAndDeleteTest.php index b4797de..878c734 100644 --- a/tests/SaveAndDeleteTest.php +++ b/tests/SaveAndDeleteTest.php @@ -13,6 +13,8 @@ use League\FactoryMuffin\Exceptions\DeletingFailedException; use League\FactoryMuffin\Exceptions\SaveFailedException; use League\FactoryMuffin\Exceptions\SaveMethodNotFoundException; +use League\FactoryMuffin\FactoryMuffin; +use League\FactoryMuffin\Stores\ModelStore; /** * This is save and delete test class. @@ -57,8 +59,9 @@ public function testShouldNotSave() */ public function testShouldThrowExceptionAfterSaveMethodRename() { - $return = static::$fm->setSaveMethod('foo'); - $this->assertInstanceOf('League\FactoryMuffin\FactoryMuffin', $return); + static::$fm = new FactoryMuffin(new ModelStore('foo')); + static::$fm->loadFactories(__DIR__.'/factories'); + try { static::$fm->create($model = 'ModelThatWillSaveStub'); } catch (SaveMethodNotFoundException $e) { @@ -77,9 +80,11 @@ public function testShouldThrowExceptionAfterSaveMethodRename() */ public function testShouldThrowExceptionAfterDeleteMethodRename() { - $return = static::$fm->setDeleteMethod('bar'); - $this->assertInstanceOf('League\FactoryMuffin\FactoryMuffin', $return); + static::$fm = new FactoryMuffin(new ModelStore(null, 'bar')); + static::$fm->loadFactories(__DIR__.'/factories'); + static::$fm->create($model = 'ModelThatWillSaveStub'); + try { static::$fm->deleteSaved(); } catch (DeletingFailedException $e) {