From 71ca799f5fd33cf5d2525a67488e9f1018135436 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Thu, 11 Dec 2014 23:34:21 +0000 Subject: [PATCH] Implemented the new way to define model definitions --- README.md | 50 +++---- src/Definition.php | 125 +++++++++++++----- src/Exceptions/DeleteFailedException.php | 2 +- .../DeleteMethodNotFoundException.php | 2 +- src/Exceptions/DirectoryNotFoundException.php | 2 +- src/Exceptions/ModelNotFoundException.php | 2 +- src/Exceptions/NoDefinedFactoryException.php | 2 +- src/Exceptions/SaveFailedException.php | 4 +- .../SaveMethodNotFoundException.php | 2 +- src/FactoryMuffin.php | 12 +- tests/DefinitionTest.php | 73 +++++++++- tests/FactoryMuffinTest.php | 2 +- tests/SaveAndDeleteTest.php | 16 +-- tests/factories/definition.php | 26 ++-- tests/factories/eloquent.php | 4 +- tests/factories/main.php | 20 ++- tests/factories/savedelete.php | 25 ++-- tests/stubs/example.php | 2 +- 18 files changed, 243 insertions(+), 128 deletions(-) diff --git a/README.md b/README.md index 7e8b203..b954bd0 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,11 @@ It maybe be useful for existing users to check out the [upgrade guide](UPGRADING This is the usage guide for Factory Muffin 3.0. Within this guide, you will see "the `xyz` function can be called". You should assume that these functions should be called on an instance of `League\FactoryMuffin\FactoryMuffin`; you should keep track of this instance yourself, and you can of course have multiple instances of this class for maximum flexibility. For simplicities sake, many of our examples include a `$fm` variable. This variable will actually be made available when files are required using the `loadFactories` function. -### Factory Definitions +### Model Definitions -You can define model factories using the `define` function. You may call it like this: `$fm->define('Fully\Qualifed\ModelName', array('foo' => 'bar'))`, where `foo` is the name of the attribute you want set on your model, and `bar` describes how you wish to generate the attribute. Please see the generators section for more information on how this works. +You can define model factories using the `define` function. You may call it like this: `$fm->define('Fully\Qualifed\ModelName')->addDefinitions('foo', 'bar')`, where `foo` is the name of the attribute you want set on your model, and `bar` describes how you wish to generate the attribute. You may also define multiple attributes at once like this: `$fm->define('Fully\Qualifed\ModelName')->setDefinitions('foo', 'bar')`. Note that both functions append to the internal attributes definition array rather than replacing it. Please see the generators section for more information on how this works. -You can also define multiple different factory definitions for your models. You can do this by prefixing the model class name with your "group" followed by a colon. This results in you defining your model like this: `$fm->define('myGroup:Fully\Qualifed\ModelName', array('foo' => 'bar'))`. You don't have to entirely define your model here because we will first look for a definition without the group prefix, then apply your group definition on top of that definition, overriding attribute definitions where required. +You can also define multiple different model definitions for your models. You can do this by prefixing the model class name with your "group" followed by a colon. This results in you defining your model like this: `$fm->define('myGroup:Fully\Qualifed\ModelName')->addDefinitions('bar', 'baz')`. You don't have to entirely define your model here because we will first look for a definition without the group prefix, then apply your group definition on top of that definition, overriding attribute definitions where required. We have provided a nifty way for you to do this in your tests. PHPUnit provides a `setupBeforeClass` function. Within that function you can call `$fm->loadFactories(__DIR__ . '/factories');`, and it will include all files in the factories folder. Within those php files, you can put your definitions (all your code that calls the define function). The `loadFactories` function will throw a `League\FactoryMuffin\Exceptions\DirectoryNotFoundException` exception if the directory you're loading is not found. @@ -70,7 +70,7 @@ The callable generator can be used if you want a more custom solution. Whatever As you can see from this example, the ability to use a closure to generate attributes can be so useful and flexible. Here we use it to generate a slug based on the initially randomly generated 5 word long title. ```php -$fm->define('MyModel', array( +$fm->define('MyModel')->setDefinitions([ 'title' => Faker::sentence(5), 'slug' => function ($object, $saved) { $slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $object->title); @@ -79,16 +79,16 @@ $fm->define('MyModel', array( return $slug; }, -)); +]); ``` ##### Example 2 This will set the `foo` attribute to whatever calling `MyModel::exampleMethod($object, $saved)` returns. ```php -$fm->define('MyModel', array( +$fm->define('MyModel'->setDefinitions([ 'foo' => 'MyModel::exampleMethod', -)); +]); ``` ##### Example 3 @@ -97,13 +97,13 @@ There is a simple example of setting a few different attributes using our faker ```php use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('MyModel', array( +$fm->define('MyModel')->setDefinitions([ 'foo' => Faker::word(), // Set the foo attribute to a random word 'name' => Faker::firstNameMale(), // Set the name attribute to a random male first name 'email' => Faker::email(), // Set the email attribute to a random email address 'body' => Faker::text(), // Set the body attribute to a random string of text 'slogan' => Faker::sentence(), // Set the slogan attribute to a random sentence -)); +]); ``` ##### Example 4 @@ -112,9 +112,9 @@ This will set the `age` attribute to a random number between 20 and 40. ```php use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('MyModel', array( +$fm->define('MyModel')->setDefinitions([ 'age' => Faker::numberBetween(20, 40), -)); +]); ``` ##### Example 5 @@ -123,9 +123,7 @@ This will set the `name` attribute to a random female first name. Because we've ```php use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('MyModel', array( - 'name' => Faker::unique()->firstNameFemale(), -)); +$fm->define('MyModel')->addDefinition('name', Faker::unique()->firstNameFemale()); ``` ##### Example 6 @@ -134,9 +132,7 @@ This will set the `profile_pic` attribute to a random image url of dimensions 40 ```php use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('MyModel', array( - 'profile_pic' => Faker::optional()->imageUrl(400, 400), -)); +$fm->define('MyModel')->addDefinition('profile_pic', Faker::optional()->imageUrl(400, 400)); ``` ##### More @@ -151,13 +147,9 @@ The factory generator can be useful for setting up relationships between models. When we create a Foo object, we will find that the Bar object will been generated and saved too, and it's id will be assigned to the `bar_id` attribute of the Foo model. ```php -$fm->define('Foo', array( - 'bar_id' => 'factory|Bar', -)); +$fm->define('Foo')->addDefinition('bar_id', 'factory|Bar'); -$fm->define('Bar', array( - 'baz' => Faker::date('Y-m-d'), -)); +$fm->define('Bar')->addDefinition('baz', Faker::date('Y-m-d')); ``` ### Creating And Seeding @@ -166,7 +158,7 @@ The `create` function will create and save your model, and will also save anythi 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 factory definition for it earlier. +* `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\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. @@ -205,19 +197,19 @@ To start with, we need to create some definitions: use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('Message', array( +$fm->define('Message')->setDefinitions([ 'user_id' => 'factory|User', 'subject' => Faker::sentence(), 'message' => Faker::text(), 'phone_number' => Faker::randomNumber(8), 'created' => Faker::date('Ymd h:s'), 'slug' => 'Message::makeSlug', -), function ($object, $saved) { +])->setCallback(function ($object, $saved) { // we're taking advantage of the callback functionality here $object->message .= '!'; }); -$fm->define('User', array( +$fm->define('User')->setDefinitions([ 'username' => Faker::firstNameMale(), 'email' => Faker::email(), 'avatar' => Faker::imageUrl(400, 600), @@ -225,7 +217,7 @@ $fm->define('User', array( 'four' => function() { return 2 + 2; }, -)); +]); ``` You can then use these factories in your tests: @@ -247,7 +239,7 @@ class TestUserModel extends PHPUnit_Framework_TestCase // you want to configure a few extra things static::$fm->setSaveMethod('save')->setDeleteMethod('delete'); - // load your factory definitions + // load your model definitions static::$fm->loadFactories(__DIR__.'/factories'); // you can optionally set the faker locale diff --git a/src/Definition.php b/src/Definition.php index 7aa9e83..38042d1 100644 --- a/src/Definition.php +++ b/src/Definition.php @@ -25,13 +25,6 @@ */ class Definition { - /** - * The full model name. - * - * @var string - */ - protected $model; - /** * The model group. * @@ -47,11 +40,11 @@ class Definition protected $class; /** - * The attribute definitions. + * The full model name. * - * @var array + * @var string */ - protected $definitions; + protected $model; /** * The closure callback. @@ -60,37 +53,30 @@ class Definition */ protected $callback; + /** + * The attribute definitions. + * + * @var array + */ + protected $definitions = []; + /** * Create a new model definition. * - * @param string $model The full model name. - * @param array $definitions The attribute definitions. - * @param \Closure|null $callback The closure callback. + * @param string $model The full model name. * * @return void */ - public function __construct($model, array $definitions = [], Closure $callback = null) + public function __construct($model) { - $this->model = $model; - $this->definitions = $definitions; - $this->callback = $callback; - if (strpos($model, ':') !== false) { $this->group = current(explode(':', $model)); $this->class = str_replace($this->group.':', '', $model); } else { $this->class = $model; } - } - /** - * Get the full model name including group prefixes. - * - * @return string - */ - public function getModel() - { - return $this->model; + $this->model = $model; } /** @@ -114,13 +100,39 @@ public function getClass() } /** - * Get the attribute definitions. + * Get the full model name including group prefixes. * - * @return array + * @return string */ - public function getDefinitions() + public function getModel() + { + return $this->model; + } + + /** + * Set the closure callback. + * + * @param \Closure|null $callback + * + * @return $this + */ + public function setCallback(Closure $callback = null) { - return $this->definitions; + $this->callback = $callback; + + return $this; + } + + /** + * Clear the closure callback. + * + * @return $this + */ + public function clearCallback() + { + $this->callback = null; + + return $this; } /** @@ -132,4 +144,55 @@ public function getCallback() { return $this->callback; } + + /** + * Add an attribute definitions. + * + * @param string $attribute + * @param string|callable $definition + * + * @return $this + */ + public function addDefinition($attribute, $definition) + { + $this->definitions[$attribute] = $definition; + + return $this; + } + + /** + * Set the attribute definitions. + * + * @param array $definitions + * + * @return $this + */ + public function setDefinitions(array $definitions = []) + { + $this->definitions = array_merge($this->definitions, $definitions); + + return $this; + } + + /** + * Clear the attribute definitions. + * + * @return $this + */ + public function clearDefinitions() + { + $this->definitions = []; + + return $this; + } + + /** + * Get the attribute definitions. + * + * @return array + */ + public function getDefinitions() + { + return (array) $this->definitions; + } } diff --git a/src/Exceptions/DeleteFailedException.php b/src/Exceptions/DeleteFailedException.php index a5d336d..0b54115 100644 --- a/src/Exceptions/DeleteFailedException.php +++ b/src/Exceptions/DeleteFailedException.php @@ -38,7 +38,7 @@ class DeleteFailedException extends ModelException public function __construct($model, $message = null) { if (!$message) { - $message = "We could not delete the model of type: '$model'."; + $message = "We could not delete the model: '$model'."; } parent::__construct($model, $message); diff --git a/src/Exceptions/DeleteMethodNotFoundException.php b/src/Exceptions/DeleteMethodNotFoundException.php index 3cf1ec7..ddae6c1 100644 --- a/src/Exceptions/DeleteMethodNotFoundException.php +++ b/src/Exceptions/DeleteMethodNotFoundException.php @@ -50,7 +50,7 @@ public function __construct($object, $method, $message = null) $model = get_class($object); if (!$message) { - $message = "The delete method '$method' was not found on the model of type: '$model'."; + $message = "The delete method '$method' was not found on the model: '$model'."; } parent::__construct($model, $method, $message); diff --git a/src/Exceptions/DirectoryNotFoundException.php b/src/Exceptions/DirectoryNotFoundException.php index 7cafe5f..655c956 100644 --- a/src/Exceptions/DirectoryNotFoundException.php +++ b/src/Exceptions/DirectoryNotFoundException.php @@ -19,7 +19,7 @@ /** * This is the directory not found exception class. * - * This is thrown if you try to load factory definitions from a directory that + * This is thrown if you try to load model definitions from a directory that * doesn't exit. * * @author Scott Robertson diff --git a/src/Exceptions/ModelNotFoundException.php b/src/Exceptions/ModelNotFoundException.php index 3528091..ee8484f 100644 --- a/src/Exceptions/ModelNotFoundException.php +++ b/src/Exceptions/ModelNotFoundException.php @@ -38,7 +38,7 @@ class ModelNotFoundException extends ModelException public function __construct($model, $message = null) { if (!$message) { - $message = "No class was defined for the model of type: '$model'."; + $message = "No class was defined for the model: '$model'."; } parent::__construct($model, $message); diff --git a/src/Exceptions/NoDefinedFactoryException.php b/src/Exceptions/NoDefinedFactoryException.php index 727389d..cb0f238 100644 --- a/src/Exceptions/NoDefinedFactoryException.php +++ b/src/Exceptions/NoDefinedFactoryException.php @@ -38,7 +38,7 @@ class NoDefinedFactoryException extends ModelException public function __construct($model, $message = null) { if (!$message) { - $message = "No factory definition(s) were defined for the model of type: '$model'."; + $message = "No model definition was defined for the model: '$model'."; } parent::__construct($model, $message); diff --git a/src/Exceptions/SaveFailedException.php b/src/Exceptions/SaveFailedException.php index 2094565..56e7b31 100644 --- a/src/Exceptions/SaveFailedException.php +++ b/src/Exceptions/SaveFailedException.php @@ -49,9 +49,9 @@ public function __construct($model, $errors = null, $message = null) if (!$message) { if ($errors) { - $message = "$errors We could not save the model of type: '$model'."; + $message = "$errors We could not save the model: '$model'."; } else { - $message = "We could not save the model of type: '$model'."; + $message = "We could not save the model: '$model'."; } } diff --git a/src/Exceptions/SaveMethodNotFoundException.php b/src/Exceptions/SaveMethodNotFoundException.php index 0b52919..5a6d82d 100644 --- a/src/Exceptions/SaveMethodNotFoundException.php +++ b/src/Exceptions/SaveMethodNotFoundException.php @@ -50,7 +50,7 @@ public function __construct($object, $method, $message = null) $model = get_class($object); if (!$message) { - $message = "The save method '$method' was not found on the model of type: '$model'."; + $message = "The save method '$method' was not found on the model: '$model'."; } parent::__construct($model, $method, $message); diff --git a/src/FactoryMuffin.php b/src/FactoryMuffin.php index 1429c2d..bb5262e 100644 --- a/src/FactoryMuffin.php +++ b/src/FactoryMuffin.php @@ -428,7 +428,7 @@ protected function generate($object, array $attr = []) * * @return \League\FactoryMuffin\Definition */ - protected function getDefinition($model) + public function getDefinition($model) { if (isset($this->definitions[$model])) { return $this->definitions[$model]; @@ -440,17 +440,13 @@ protected function getDefinition($model) /** * Define a new model factory. * - * @param string $model The full model name. - * @param array $definitions The attribute definitions. - * @param \Closure|null $callback The closure callback. + * @param string $model The full model name. * * @return $this */ - public function define($model, array $definitions = [], Closure $callback = null) + public function define($model) { - $this->definitions[$model] = new Definition($model, $definitions, $callback); - - return $this; + return $this->definitions[$model] = new Definition($model); } /** diff --git a/tests/DefinitionTest.php b/tests/DefinitionTest.php index 50fa41d..df08f33 100644 --- a/tests/DefinitionTest.php +++ b/tests/DefinitionTest.php @@ -38,6 +38,60 @@ public function testDefine() $this->assertContains('@', $user->email); } + public function testBasicDefinitionFunctions() + { + $definition = static::$fm->getDefinition('AttributeDefinitionsStub'); + + $this->assertNull($definition->getGroup()); + $this->assertSame('AttributeDefinitionsStub', $definition->getClass()); + $this->assertSame('AttributeDefinitionsStub', $definition->getModel()); + } + + public function testAttributeDefinitionFunctions() + { + $definition = static::$fm->getDefinition('AttributeDefinitionsStub'); + + $this->assertSame([], $definition->getDefinitions()); + + $definition->addDefinition('foo', 'bar'); + $this->assertSame(['foo' => 'bar'], $definition->getDefinitions()); + + $definition->setDefinitions(['bar' => 'baz']); + $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $definition->getDefinitions()); + + $definition->setDefinitions([]); + $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $definition->getDefinitions()); + + $definition->clearDefinitions(); + $this->assertSame([], $definition->getDefinitions()); + + $definition->setDefinitions(['bar' => 'baz', 'baz' => 'foo']); + $this->assertSame(['bar' => 'baz', 'baz' => 'foo'], $definition->getDefinitions()); + } + + public function testCallbackDefinitionFunctions() + { + $definition = static::$fm->getDefinition('AttributeDefinitionsStub'); + + $this->assertNull($definition->getCallback()); + + $callback = function () { + return $foo; + }; + + $definition->setCallback($callback); + $this->assertSame($callback, $definition->getCallback()); + + $definition->setCallback(null); + $this->assertNull($definition->getCallback()); + + $definition->setCallback($callback); + $this->assertSame($callback, $definition->getCallback()); + + $definition->clearCallback(); + $this->assertNull($definition->getCallback()); + } + public function testDefineWithReplacementGenerators() { $user = static::$fm->create('UserModelStub', [ @@ -60,7 +114,7 @@ public function testModelNotFound() try { static::$fm->create($model = 'NotAClass'); } catch (ModelNotFoundException $e) { - $this->assertSame("No class was defined for the model of type: '$model'.", $e->getMessage()); + $this->assertSame("No class was defined for the model: '$model'.", $e->getMessage()); $this->assertSame($model, $e->getModel()); throw $e; } @@ -108,7 +162,7 @@ public function testShouldThrowExceptionWhenLoadingANonExistentGroup() try { static::$fm->create('error:UserModelStub'); } catch (NoDefinedFactoryException $e) { - $this->assertSame("No factory definition(s) were defined for the model of type: 'error:UserModelStub'.", $e->getMessage()); + $this->assertSame("No model definition was defined for the model: 'error:UserModelStub'.", $e->getMessage()); $this->assertSame('error:UserModelStub', $e->getModel()); throw $e; } @@ -122,7 +176,7 @@ public function testGroupDefineNoBaseModel() try { static::$fm->create('foo:DogModelStub'); } catch (NoDefinedFactoryException $e) { - $this->assertSame("No factory definition(s) were defined for the model of type: 'DogModelStub'.", $e->getMessage()); + $this->assertSame("No model definition was defined for the model: 'DogModelStub'.", $e->getMessage()); $this->assertSame('DogModelStub', $e->getModel()); throw $e; } @@ -215,6 +269,19 @@ public function testFactoryIsBoundToClosure() } } +class AttributeDefinitionsStub +{ + public function save() + { + return true; + } + + public function delete() + { + return true; + } +} + class UserModelStub { public function save() diff --git a/tests/FactoryMuffinTest.php b/tests/FactoryMuffinTest.php index 530d5ef..3bd4085 100644 --- a/tests/FactoryMuffinTest.php +++ b/tests/FactoryMuffinTest.php @@ -89,7 +89,7 @@ public function testShouldThrowExceptionWhenNoDefinedFactoryException() try { static::$fm->instance($model = 'ModelWithNoFactoryClassStub'); } catch (NoDefinedFactoryException $e) { - $this->assertSame("No factory definition(s) were defined for the model of type: '$model'.", $e->getMessage()); + $this->assertSame("No model definition was defined for the model: '$model'.", $e->getMessage()); $this->assertSame($model, $e->getModel()); throw $e; } diff --git a/tests/SaveAndDeleteTest.php b/tests/SaveAndDeleteTest.php index 5fb29f0..dfe613f 100644 --- a/tests/SaveAndDeleteTest.php +++ b/tests/SaveAndDeleteTest.php @@ -57,7 +57,7 @@ public function testShouldThrowExceptionAfterSaveMethodRename() try { static::$fm->create($model = 'ModelThatWillSaveStub'); } catch (SaveMethodNotFoundException $e) { - $this->assertSame("The save method 'foo' was not found on the model of type: '$model'.", $e->getMessage()); + $this->assertSame("The save method 'foo' was not found on the model: '$model'.", $e->getMessage()); $this->assertSame($model, $e->getModel()); $this->assertSame('foo', $e->getMethod()); $this->assertInstanceOf($model, $e->getObject()); @@ -81,7 +81,7 @@ public function testShouldThrowExceptionAfterDeleteMethodRename() } catch (DeletingFailedException $e) { $exceptions = $e->getExceptions(); $this->assertSame("We encountered 1 problem(s) while trying to delete the saved models.", $e->getMessage()); - $this->assertSame("The delete method 'bar' was not found on the model of type: '$model'.", $exceptions[0]->getMessage()); + $this->assertSame("The delete method 'bar' was not found on the model: '$model'.", $exceptions[0]->getMessage()); $this->assertSame($model, $exceptions[0]->getModel()); $this->assertSame('bar', $exceptions[0]->getMethod()); $this->assertInstanceOf($model, $exceptions[0]->getObject()); @@ -100,7 +100,7 @@ public function testShouldThrowExceptionOnModelSaveFailure() try { static::$fm->create($model = 'ModelThatFailsToSaveStub'); } catch (SaveFailedException $e) { - $this->assertSame("We could not save the model of type: '$model'.", $e->getMessage()); + $this->assertSame("We could not save the model: '$model'.", $e->getMessage()); $this->assertSame($model, $e->getModel()); $this->assertNull($e->getErrors()); throw $e; @@ -118,7 +118,7 @@ public function testShouldThrowExceptionOnModelDeleteFailure() } catch (DeletingFailedException $e) { $exceptions = $e->getExceptions(); $this->assertSame("We encountered 1 problem(s) while trying to delete the saved models.", $e->getMessage()); - $this->assertSame("We could not delete the model of type: '$model'.", $exceptions[0]->getMessage()); + $this->assertSame("We could not delete the model: '$model'.", $exceptions[0]->getMessage()); throw $e; } } @@ -147,7 +147,7 @@ public function testShouldThrowExceptionWithoutSaveMethod() try { static::$fm->create($model = 'ModelWithNoSaveMethodStub'); } catch (SaveMethodNotFoundException $e) { - $this->assertSame("The save method 'save' was not found on the model of type: '$model'.", $e->getMessage()); + $this->assertSame("The save method 'save' was not found on the model: '$model'.", $e->getMessage()); $this->assertSame($model, $e->getModel()); $this->assertSame('save', $e->getMethod()); $this->assertInstanceOf($model, $e->getObject()); @@ -166,7 +166,7 @@ public function testShouldThrowExceptionWithoutDeleteMethod() } catch (DeletingFailedException $e) { $exceptions = $e->getExceptions(); $this->assertSame("We encountered 1 problem(s) while trying to delete the saved models.", $e->getMessage()); - $this->assertSame("The delete method 'delete' was not found on the model of type: '$model'.", $exceptions[0]->getMessage()); + $this->assertSame("The delete method 'delete' was not found on the model: '$model'.", $exceptions[0]->getMessage()); $this->assertSame($model, $exceptions[0]->getModel()); $this->assertSame('delete', $exceptions[0]->getMethod()); $this->assertInstanceOf($model, $exceptions[0]->getObject()); @@ -182,7 +182,7 @@ public function testShouldThrowExceptionWithValidationErrors() try { static::$fm->create($model = 'ModelWithValidationErrorsStub'); } catch (SaveFailedException $e) { - $this->assertSame("Failed to save. We could not save the model of type: '$model'.", $e->getMessage()); + $this->assertSame("Failed to save. We could not save the model: '$model'.", $e->getMessage()); $this->assertSame($model, $e->getModel()); $this->assertSame('Failed to save.', $e->getErrors()); throw $e; @@ -202,7 +202,7 @@ public function testShouldThrowMultipleDeletionExceptions() $exceptions = $e->getExceptions(); $this->assertSame("We encountered 2 problem(s) while trying to delete the saved models.", $e->getMessage()); $this->assertSame("OH NOES!", $exceptions[0]->getMessage()); - $this->assertSame("The delete method 'delete' was not found on the model of type: '$model'.", $exceptions[1]->getMessage()); + $this->assertSame("The delete method 'delete' was not found on the model: '$model'.", $exceptions[1]->getMessage()); $this->assertSame($model, $exceptions[1]->getModel()); $this->assertSame('delete', $exceptions[1]->getMethod()); $this->assertInstanceOf($model, $exceptions[1]->getObject()); diff --git a/tests/factories/definition.php b/tests/factories/definition.php index cc1f49a..ee610e6 100644 --- a/tests/factories/definition.php +++ b/tests/factories/definition.php @@ -14,45 +14,41 @@ use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('ProfileModelStub', [ - 'profile' => Faker::text(), -]); +$fm->define('ProfileModelStub')->addDefinition('profile', Faker::text()); + +$fm->define('AttributeDefinitionsStub'); -$fm->define('NotAClass', []); +$fm->define('NotAClass'); -$fm->define('UserModelStub', [ +$fm->define('UserModelStub')->setDefinitions([ 'name' => Faker::word(), 'active' => Faker::boolean(), 'email' => Faker::email(), 'profile' => 'factory|ProfileModelStub', ]); -$fm->define('group:UserModelStub', [ - 'address' => Faker::address(), -]); +$fm->define('group:UserModelStub')->addDefinition('address', Faker::address()); -$fm->define('anothergroup:UserModelStub', [ +$fm->define('anothergroup:UserModelStub')->setDefinitions([ 'address' => Faker::address(), 'active' => 'custom', ]); -$fm->define('callbackgroup:UserModelStub', [], function ($obj) { +$fm->define('callbackgroup:UserModelStub')->setCallback(function ($obj) { $obj->test = 'bar'; }); -$fm->define('foo:DogModelStub', [ +$fm->define('foo:DogModelStub')->setDefinitions([ 'name' => Faker::firstNameMale(), 'age' => Faker::numberBetween(1, 15), ]); -$fm->define('ExampleCallbackStub', [], function ($obj, $saved) { +$fm->define('ExampleCallbackStub')->setCallback(function ($obj, $saved) { $obj->callback = 'yaycalled'; $obj->saved = $saved; }); -$fm->define('AnotherCallbackStub', [ - 'foo' => Faker::email(), -], function ($obj, $saved) { +$fm->define('AnotherCallbackStub')->addDefinition('foo', Faker::email())->setCallback(function ($obj, $saved) { $obj->foo = 'hello there'; $obj->saved = $saved; }); diff --git a/tests/factories/eloquent.php b/tests/factories/eloquent.php index b76624b..8c565a3 100644 --- a/tests/factories/eloquent.php +++ b/tests/factories/eloquent.php @@ -14,12 +14,12 @@ use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('User', [ +$fm->define('User')->setDefinitions([ 'name' => Faker::firstNameMale(), 'email' => Faker::email(), ]); -$fm->define('Cat', [ +$fm->define('Cat')->setDefinitions([ 'name' => Faker::firstNameFemale(), 'user_id' => Faker::numberBetween(1, 5), ]); diff --git a/tests/factories/main.php b/tests/factories/main.php index 010c59b..d85a846 100644 --- a/tests/factories/main.php +++ b/tests/factories/main.php @@ -14,19 +14,19 @@ use League\FactoryMuffin\Faker\Facade as Faker; -$fm->define('IdTestModelGetKeyStub', []); -$fm->define('IdTestModelPkStub', []); -$fm->define('IdTestModelIdStub', []); -$fm->define('IdTestModelNullStub', []); +$fm->define('IdTestModelGetKeyStub'); +$fm->define('IdTestModelPkStub'); +$fm->define('IdTestModelIdStub'); +$fm->define('IdTestModelNullStub'); -$fm->define('IdTestModelStub', [ +$fm->define('IdTestModelStub')->setDefinitions([ 'modelGetKey' => 'factory|IdTestModelGetKeyStub', 'modelPk' => 'factory|IdTestModelPkStub', 'model_id' => 'factory|IdTestModelIdStub', 'model_null' => 'factory|IdTestModelNullStub', ]); -$fm->define('FakerDefaultingModelStub', [ +$fm->define('FakerDefaultingModelStub')->setDefinitions([ 'title' => Faker::word(), 'email' => Faker::email(), 'content' => Faker::text(), @@ -36,7 +36,7 @@ 'optional_text' => Faker::optional()->text(), ]); -$fm->define('MainModelStub', [ +$fm->define('MainModelStub')->setDefinitions([ 'modelb_id' => 'factory|FakerDefaultingModelStub', 'name' => Faker::word(), 'email' => Faker::email(), @@ -57,11 +57,9 @@ }, ]); -$fm->define('ComplexModelStub', [ - 'future' => 'ComplexModelStub::fortyDaysFromNow', -]); +$fm->define('ComplexModelStub')->addDefinition('future', 'ComplexModelStub::fortyDaysFromNow'); -$fm->define('ModelWithStaticMethodFactory', [ +$fm->define('ModelWithStaticMethodFactory')->setDefinitions([ 'string' => 'just a string', 'data' => function ($object, $saved) { return compact('object', 'saved'); diff --git a/tests/factories/savedelete.php b/tests/factories/savedelete.php index f4101b5..6a266ce 100644 --- a/tests/factories/savedelete.php +++ b/tests/factories/savedelete.php @@ -12,20 +12,23 @@ * THE SOFTWARE. */ -$fm->define('ModelThatWillSaveStub', []); -$fm->define('ModelThatFailsToSaveStub', []); -$fm->define('ModelThatFailsToDeleteStub', []); -$fm->define('ModelThatAlsoFailsToDeleteStub', []); -$fm->define('ModelWithNoSaveMethodStub', []); -$fm->define('ModelWithNoDeleteMethodStub', []); -$fm->define('ModelWithValidationErrorsStub', []); -$fm->define('ModelWithTrackedSaves', []); -$fm->define('no return:ModelWithTrackedSaves', [], function () { +$fm->define('ModelThatWillSaveStub'); +$fm->define('ModelThatFailsToSaveStub'); +$fm->define('ModelThatFailsToDeleteStub'); +$fm->define('ModelThatAlsoFailsToDeleteStub'); +$fm->define('ModelWithNoSaveMethodStub'); +$fm->define('ModelWithNoDeleteMethodStub'); +$fm->define('ModelWithValidationErrorsStub'); +$fm->define('ModelWithTrackedSaves'); + +$fm->define('no return:ModelWithTrackedSaves')->setCallback(function () { // No return is treated as true }); -$fm->define('return true:ModelWithTrackedSaves', [], function () { + +$fm->define('return true:ModelWithTrackedSaves')->setCallback(function () { return true; }); -$fm->define('return false:ModelWithTrackedSaves', [], function () { + +$fm->define('return false:ModelWithTrackedSaves')->setCallback(function () { return false; }); diff --git a/tests/stubs/example.php b/tests/stubs/example.php index c78b435..333f3d3 100644 --- a/tests/stubs/example.php +++ b/tests/stubs/example.php @@ -12,4 +12,4 @@ * THE SOFTWARE. */ -$fm->define('ExampleDefinedModelStub', []); +$fm->define('ExampleDefinedModelStub');