Skip to content

Commit

Permalink
Updated two exceptions to make more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Dec 12, 2014
1 parent a2f5349 commit 2f0fcbc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ $fm->define('Bar')->addDefinition('baz', Faker::date('Y-m-d'));
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.

You may encounter the following exceptions:
* `League\FactoryMuffin\Exceptions\ModelNotFoundException` will be thrown if the model class defined is not found.
* `League\FactoryMuffin\Exceptions\NoDefinedFactoryException` will be thrown if you try to create a model and you haven't defined a model definition for it earlier.
* `League\FactoryMuffin\Exceptions\MissingModelException` will be thrown if the model class defined is not found.
* `League\FactoryMuffin\Exceptions\MissingDefinitionException` will be thrown if you try to create a model and you haven't defined a model definition for it earlier.
* `League\FactoryMuffin\Exceptions\SaveFailedException` will be thrown if the save function on your model returns false.
* `League\FactoryMuffin\Exceptions\SaveMethodNotFoundException` will be thrown if the save function on your model does not exist.
* Any other exception thrown by your model while trying to create or save it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
namespace League\FactoryMuffin\Exceptions;

/**
* This is the no defined factory exception class.
* This is the missing definition exception class.
*
* This is thrown when you try to create a model without defining its factory.
* This class extends ModelException, so you may want to try to catch that
* exception instead, if you want to be more general.
* This is thrown when you try to create a model without registering its
* definitions first. This class extends ModelException, so you may want to try
* to catch that exception instead, if you want to be more general.
*
* @author Scott Robertson <[email protected]>
* @author Graham Campbell <[email protected]>
* @license <https://github.com/thephpleague/factory-muffin/blob/master/LICENSE> MIT
*/
class NoDefinedFactoryException extends ModelException
class MissingDefinitionException extends ModelException
{
/**
* Create a new instance.
Expand All @@ -38,7 +38,7 @@ class NoDefinedFactoryException extends ModelException
public function __construct($model, $message = null)
{
if (!$message) {
$message = "No model definition was defined for the model: '$model'.";
$message = "A model definition for '$model' has not been registered.";
}

parent::__construct($model, $message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
namespace League\FactoryMuffin\Exceptions;

/**
* This is the model not found exception class.
* This is the missing model exception not found exception class.
*
* This is thrown when we try to create an object, but the model class defined
* is not found. This class extends ModelException, so you may want to try to
* was not found. This class extends ModelException, so you may want to try to
* catch that exception instead, if you want to be more general.
*
* @author Scott Robertson <[email protected]>
* @author Graham Campbell <[email protected]>
* @license <https://github.com/thephpleague/factory-muffin/blob/master/LICENSE> MIT
*/
class ModelNotFoundException extends ModelException
class MissingModelException extends ModelException
{
/**
* Create a new instance.
Expand All @@ -38,7 +38,7 @@ class ModelNotFoundException extends ModelException
public function __construct($model, $message = null)
{
if (!$message) {
$message = "No class was defined for the model: '$model'.";
$message = "The model class '$model' is undefined.";
}

parent::__construct($model, $message);
Expand Down
12 changes: 6 additions & 6 deletions src/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use League\FactoryMuffin\Exceptions\DeleteMethodNotFoundException;
use League\FactoryMuffin\Exceptions\DeletingFailedException;
use League\FactoryMuffin\Exceptions\DirectoryNotFoundException;
use League\FactoryMuffin\Exceptions\ModelNotFoundException;
use League\FactoryMuffin\Exceptions\NoDefinedFactoryException;
use League\FactoryMuffin\Exceptions\MissingDefinitionException;
use League\FactoryMuffin\Exceptions\MissingModelException;
use League\FactoryMuffin\Exceptions\SaveFailedException;
use League\FactoryMuffin\Exceptions\SaveMethodNotFoundException;
use League\FactoryMuffin\Generators\GeneratorFactory;
Expand Down Expand Up @@ -231,14 +231,14 @@ protected function make($model, array $attr, $save)
* @param string $class The class name.
* @param \Closure|null $maker The maker closure.
*
* @throws \League\FactoryMuffin\Exceptions\ModelNotFoundException
* @throws \League\FactoryMuffin\Exceptions\MissingModelException
*
* @return object
*/
protected function makeClass($class, Closure $maker = null)
{
if (!class_exists($class)) {
throw new ModelNotFoundException($class);
throw new MissingModelException($class);
}

if ($maker) {
Expand Down Expand Up @@ -424,7 +424,7 @@ protected function generate($object, array $attr = [])
*
* @param string $model The full model name.
*
* @throws \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
* @throws \League\FactoryMuffin\Exceptions\MissingDefinitionException
*
* @return \League\FactoryMuffin\Definition
*/
Expand All @@ -434,7 +434,7 @@ public function getDefinition($model)
return $this->definitions[$model];
}

throw new NoDefinedFactoryException($model);
throw new MissingDefinitionException($model);
}

/**
Expand Down
26 changes: 13 additions & 13 deletions tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/

use League\FactoryMuffin\Exceptions\DirectoryNotFoundException;
use League\FactoryMuffin\Exceptions\ModelNotFoundException;
use League\FactoryMuffin\Exceptions\NoDefinedFactoryException;
use League\FactoryMuffin\Exceptions\MissingDefinitionException;
use League\FactoryMuffin\Exceptions\MissingModelException;
use League\FactoryMuffin\Faker\Facade as Faker;

/**
Expand Down Expand Up @@ -101,14 +101,14 @@ public function testDefineWithReplacementGenerators()
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\ModelNotFoundException
* @expectedException \League\FactoryMuffin\Exceptions\MissingModelException
*/
public function testModelNotFound()
{
try {
static::$fm->create($model = 'NotAClass');
} catch (ModelNotFoundException $e) {
$this->assertSame("No class was defined for the model: '$model'.", $e->getMessage());
} catch (MissingModelException $e) {
$this->assertSame("The model class '$model' is undefined.", $e->getMessage());
$this->assertSame($model, $e->getModel());
throw $e;
}
Expand Down Expand Up @@ -170,21 +170,21 @@ public function testGroupClearAttributes()
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
* @expectedException \League\FactoryMuffin\Exceptions\MissingDefinitionException
*/
public function testShouldThrowExceptionWhenLoadingANonExistentGroup()
{
try {
static::$fm->create('error:UserModelStub');
} catch (NoDefinedFactoryException $e) {
$this->assertSame("No model definition was defined for the model: 'error:UserModelStub'.", $e->getMessage());
$this->assertSame('error:UserModelStub', $e->getModel());
static::$fm->create($model = 'error:UserModelStub');
} catch (MissingDefinitionException $e) {
$this->assertSame("A model definition for '$model' has not been registered.", $e->getMessage());
$this->assertSame($model, $e->getModel());
throw $e;
}
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
* @expectedException \League\FactoryMuffin\Exceptions\MissingDefinitionException
*/
public function testGroupDefineNoBaseModel()
{
Expand All @@ -193,8 +193,8 @@ public function testGroupDefineNoBaseModel()
'name' => Faker::firstNameMale(),
'age' => Faker::numberBetween(1, 15),
]);
} catch (NoDefinedFactoryException $e) {
$this->assertSame("No model definition was defined for the model: 'DogModelStub'.", $e->getMessage());
} catch (MissingDefinitionException $e) {
$this->assertSame("A model definition for 'DogModelStub' has not been registered.", $e->getMessage());
$this->assertSame('DogModelStub', $e->getModel());
throw $e;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/FactoryMuffinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* THE SOFTWARE.
*/

use League\FactoryMuffin\Exceptions\NoDefinedFactoryException;
use League\FactoryMuffin\Exceptions\MissingDefinitionException;

/**
* This is factory muffin test class.
Expand Down Expand Up @@ -82,14 +82,14 @@ public function testFakerDefaultLongitude()
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
* @expectedException \League\FactoryMuffin\Exceptions\MissingDefinitionException
*/
public function testShouldThrowExceptionWhenNoDefinedFactoryException()
public function testShouldThrowExceptionWhenMissingDefinitionException()
{
try {
static::$fm->instance($model = 'ModelWithNoFactoryClassStub');
} catch (NoDefinedFactoryException $e) {
$this->assertSame("No model definition was defined for the model: '$model'.", $e->getMessage());
} catch (MissingDefinitionException $e) {
$this->assertSame("A model definition for '$model' has not been registered.", $e->getMessage());
$this->assertSame($model, $e->getModel());
throw $e;
}
Expand Down

0 comments on commit 2f0fcbc

Please sign in to comment.