diff --git a/src/Definition.php b/src/Definition.php index 5041154..a9c0849 100644 --- a/src/Definition.php +++ b/src/Definition.php @@ -25,13 +25,6 @@ */ class Definition { - /** - * The model group. - * - * @var string|null - */ - protected $group; - /** * The model class name. * @@ -40,11 +33,11 @@ class Definition protected $class; /** - * The full model name. + * The model group. * - * @var string + * @var string|null */ - protected $model; + protected $group; /** * The closure callback. @@ -63,50 +56,47 @@ class Definition /** * Create a new model definition. * - * @param string $model The full model name. + * @param string $class The model class name. * * @return void */ - public function __construct($model) + public function __construct($class) { - if (strpos($model, ':') !== false) { - $this->group = current(explode(':', $model)); - $this->class = str_replace($this->group.':', '', $model); - } else { - $this->class = $model; - } - - $this->model = $model; + $this->class = $class; } /** - * Get the definition group. + * Returns the real model class without the group prefix. * - * @return string|null + * @return string */ - public function getGroup() + public function getClass() { - return $this->group; + return $this->class; } /** - * Returns the real model class without the group prefix. + * Set the model group. * - * @return string + * @param string|null $group + * + * @return $this */ - public function getClass() + public function setGroup($group) { - return $this->class; + $this->group = $group; + + return $this; } /** - * Get the full model name including group prefixes. + * Get the model group. * - * @return string + * @return string|null */ - public function getModel() + public function getGroup() { - return $this->model; + return $this->group; } /** diff --git a/src/FactoryMuffin.php b/src/FactoryMuffin.php index a6400e9..670f560 100644 --- a/src/FactoryMuffin.php +++ b/src/FactoryMuffin.php @@ -215,13 +215,11 @@ protected function make($model, array $attr, $save) Arr::add($this->pending, $object); } - // Get the group specific attribute definitions - if ($definition->getGroup()) { - $attr = array_merge($attr, $this->getDefinition($model)->getDefinitions()); - } + // Get the attribute definitions + $attributes = array_merge($this->getDefinition($model)->getDefinitions(), $attr); // Generate and save each attribute for the model - $this->generate($object, $attr); + $this->generate($object, $attributes); return $object; } @@ -408,11 +406,8 @@ public function instance($model, array $attr = []) */ protected function generate($object, array $attr = []) { - $definitions = $this->getDefinition(get_class($object))->getDefinitions(); - $attributes = array_merge($definitions, $attr); - // Generate and save each attribute - foreach ($attributes as $key => $kind) { + foreach ($attr as $key => $kind) { $generated = $this->getGeneratorFactory()->generate($kind, $object); $this->setAttribute($object, $key, $generated); } @@ -445,7 +440,16 @@ public function getDefinition($model) */ public function define($model) { - return $this->definitions[$model] = new Definition($model); + if (strpos($model, ':') !== false) { + $group = current(explode(':', $model)); + $class = str_replace($group.':', '', $model); + $this->definitions[$model] = clone $this->getDefinition($class); + $this->definitions[$model]->setGroup($group); + } else { + $this->definitions[$model] = new Definition($model); + } + + return $this->definitions[$model]; } /** diff --git a/tests/DefinitionTest.php b/tests/DefinitionTest.php index c08bad6..01a7ed1 100644 --- a/tests/DefinitionTest.php +++ b/tests/DefinitionTest.php @@ -44,7 +44,6 @@ public function testBasicDefinitionFunctions() $this->assertNull($definition->getGroup()); $this->assertSame('AttributeDefinitionsStub', $definition->getClass()); - $this->assertSame('AttributeDefinitionsStub', $definition->getModel()); } public function testAttributeDefinitionFunctions() @@ -93,6 +92,7 @@ public function testDefineWithReplacementGenerators() ]); $this->assertInstanceOf('UserModelStub', $user); + $this->assertSame('foo', $user->test); $this->assertInternalType('string', $user->name); $this->assertInternalType('string', $user->fullName); $this->assertNotEquals('name', $user->fullName); @@ -119,6 +119,7 @@ public function testGroupDefine() $user = static::$fm->create('group:UserModelStub'); $this->assertInstanceOf('UserModelStub', $user); + $this->assertSame('foo', $user->test); $this->assertInternalType('string', $user->address); $this->assertNotEquals('address', $user->address); $this->assertInternalType('string', $user->name); @@ -131,12 +132,21 @@ public function testGroupDefineOverwrite() $user = static::$fm->create('anothergroup:UserModelStub'); $this->assertInstanceOf('UserModelStub', $user); + $this->assertSame('foo', $user->test); $this->assertInternalType('string', $user->address); $this->assertInternalType('string', $user->name); $this->assertSame('custom', $user->active); $this->assertContains('@', $user->email); } + public function testGroupKeepCallback() + { + $user = static::$fm->create('UserModelStub'); + + $this->assertInstanceOf('UserModelStub', $user); + $this->assertSame('foo', $user->test); + } + public function testGroupCallback() { $user = static::$fm->create('callbackgroup:UserModelStub'); @@ -148,6 +158,17 @@ public function testGroupCallback() $this->assertContains('@', $user->email); } + public function testGroupClearAttributes() + { + $user = static::$fm->create('noattributes:UserModelStub'); + + $this->assertInstanceOf('UserModelStub', $user); + $this->assertSame('foo', $user->test); + $this->assertFalse(isset($user->name)); + $this->assertFalse(isset($user->active)); + $this->assertFalse(isset($user->email)); + } + /** * @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException */ @@ -168,7 +189,10 @@ public function testShouldThrowExceptionWhenLoadingANonExistentGroup() public function testGroupDefineNoBaseModel() { try { - static::$fm->create('foo:DogModelStub'); + static::$fm->define('foo:DogModelStub')->setDefinitions([ + 'name' => Faker::firstNameMale(), + 'age' => Faker::numberBetween(1, 15), + ]); } catch (NoDefinedFactoryException $e) { $this->assertSame("No model definition was defined for the model: 'DogModelStub'.", $e->getMessage()); $this->assertSame('DogModelStub', $e->getModel()); diff --git a/tests/factories/definition.php b/tests/factories/definition.php index ee610e6..1d2dd43 100644 --- a/tests/factories/definition.php +++ b/tests/factories/definition.php @@ -25,7 +25,9 @@ 'active' => Faker::boolean(), 'email' => Faker::email(), 'profile' => 'factory|ProfileModelStub', -]); +])->setCallback(function ($obj) { + $obj->test = 'foo'; +}); $fm->define('group:UserModelStub')->addDefinition('address', Faker::address()); @@ -38,10 +40,7 @@ $obj->test = 'bar'; }); -$fm->define('foo:DogModelStub')->setDefinitions([ - 'name' => Faker::firstNameMale(), - 'age' => Faker::numberBetween(1, 15), -]); +$fm->define('noattributes:UserModelStub')->clearDefinitions(); $fm->define('ExampleCallbackStub')->setCallback(function ($obj, $saved) { $obj->callback = 'yaycalled';