Skip to content

Commit

Permalink
Even more flexibility when defining group definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Dec 12, 2014
1 parent 430809e commit 9e903d4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 49 deletions.
54 changes: 22 additions & 32 deletions src/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
*/
class Definition
{
/**
* The model group.
*
* @var string|null
*/
protected $group;

/**
* The model class name.
*
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand Down
24 changes: 14 additions & 10 deletions src/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];
}

/**
Expand Down
28 changes: 26 additions & 2 deletions tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function testBasicDefinitionFunctions()

$this->assertNull($definition->getGroup());
$this->assertSame('AttributeDefinitionsStub', $definition->getClass());
$this->assertSame('AttributeDefinitionsStub', $definition->getModel());
}

public function testAttributeDefinitionFunctions()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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
*/
Expand All @@ -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());
Expand Down
9 changes: 4 additions & 5 deletions tests/factories/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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';
Expand Down

0 comments on commit 9e903d4

Please sign in to comment.