Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Feb 14, 2016
1 parent c89633c commit bf3e5b1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 55 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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');
Expand Down
28 changes: 0 additions & 28 deletions src/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Stores/AbstractStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ abstract class AbstractStore
*/
private $saved = [];

/**
* The underlying operational method names.
*
* @var string[]
*/
protected $methods = [];

/**
* Save the model to the database.
*
Expand Down
18 changes: 0 additions & 18 deletions src/Stores/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 9 additions & 4 deletions tests/SaveAndDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit bf3e5b1

Please sign in to comment.