From 2f0fcbce13bdb224c7ec7bff60db009ce5a6a3b8 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 12 Dec 2014 19:34:31 +0000 Subject: [PATCH] Updated two exceptions to make more sense --- README.md | 4 +-- ...ion.php => MissingDefinitionException.php} | 12 ++++----- ...xception.php => MissingModelException.php} | 8 +++--- src/FactoryMuffin.php | 12 ++++----- tests/DefinitionTest.php | 26 +++++++++---------- tests/FactoryMuffinTest.php | 10 +++---- 6 files changed, 36 insertions(+), 36 deletions(-) rename src/Exceptions/{NoDefinedFactoryException.php => MissingDefinitionException.php} (72%) rename src/Exceptions/{ModelNotFoundException.php => MissingModelException.php} (82%) diff --git a/README.md b/README.md index b954bd0..6b119c7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Exceptions/NoDefinedFactoryException.php b/src/Exceptions/MissingDefinitionException.php similarity index 72% rename from src/Exceptions/NoDefinedFactoryException.php rename to src/Exceptions/MissingDefinitionException.php index cb0f238..6bdc8a9 100644 --- a/src/Exceptions/NoDefinedFactoryException.php +++ b/src/Exceptions/MissingDefinitionException.php @@ -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 * @author Graham Campbell * @license MIT */ -class NoDefinedFactoryException extends ModelException +class MissingDefinitionException extends ModelException { /** * Create a new instance. @@ -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); diff --git a/src/Exceptions/ModelNotFoundException.php b/src/Exceptions/MissingModelException.php similarity index 82% rename from src/Exceptions/ModelNotFoundException.php rename to src/Exceptions/MissingModelException.php index ee8484f..b96f3db 100644 --- a/src/Exceptions/ModelNotFoundException.php +++ b/src/Exceptions/MissingModelException.php @@ -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 * @author Graham Campbell * @license MIT */ -class ModelNotFoundException extends ModelException +class MissingModelException extends ModelException { /** * Create a new instance. @@ -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); diff --git a/src/FactoryMuffin.php b/src/FactoryMuffin.php index 077e2a0..c6116e1 100644 --- a/src/FactoryMuffin.php +++ b/src/FactoryMuffin.php @@ -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; @@ -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) { @@ -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 */ @@ -434,7 +434,7 @@ public function getDefinition($model) return $this->definitions[$model]; } - throw new NoDefinedFactoryException($model); + throw new MissingDefinitionException($model); } /** diff --git a/tests/DefinitionTest.php b/tests/DefinitionTest.php index 110a032..20a0c7c 100644 --- a/tests/DefinitionTest.php +++ b/tests/DefinitionTest.php @@ -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; /** @@ -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; } @@ -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() { @@ -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; } diff --git a/tests/FactoryMuffinTest.php b/tests/FactoryMuffinTest.php index 3bd4085..74fd6d8 100644 --- a/tests/FactoryMuffinTest.php +++ b/tests/FactoryMuffinTest.php @@ -12,7 +12,7 @@ * THE SOFTWARE. */ -use League\FactoryMuffin\Exceptions\NoDefinedFactoryException; +use League\FactoryMuffin\Exceptions\MissingDefinitionException; /** * This is factory muffin test class. @@ -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; }