Skip to content

Commit

Permalink
Merge pull request #48 from Zizaco/default-to-faker
Browse files Browse the repository at this point in the history
Default to faker
  • Loading branch information
scottrobertson committed Jul 4, 2014
2 parents dcfab58 + 6cc2b9a commit 8a2a0ff
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Zizaco/FactoryMuff/Kind.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Kind
'factory',
'integer',
'name',
'none',
'generic',
'string',
'text',
);
Expand Down Expand Up @@ -57,7 +57,7 @@ public static function detect($kind, $model = null)
return new Kind\Closure($kind, $model);
}

$class = '\\Zizaco\\FactoryMuff\\Kind\\None';
$class = '\\Zizaco\\FactoryMuff\\Kind\\Generic';
foreach (static::$available_kinds as $available_kind)
{
if (substr($kind, 0, strlen($available_kind)) === $available_kind) {
Expand Down
26 changes: 26 additions & 0 deletions src/Zizaco/FactoryMuff/Kind/Generic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Zizaco\FactoryMuff\Kind;

use Zizaco\FactoryMuff\Kind;

class Generic extends Kind
{
/**
* We attempt to use Faker for any string passed in, if a Faker property does
* not exist, then we just return the original string
* @return mixed
*/
public function generate()
{
$faker = \Faker\Factory::create();

// Only try and use Faker when there are no spaces in the string
if (strpos($this->kind, ' ') === false)
{
return $faker->{$this->kind} ?: $this->kind;
}

return $this->kind;
}
}
13 changes: 0 additions & 13 deletions src/Zizaco/FactoryMuff/Kind/None.php

This file was deleted.

13 changes: 12 additions & 1 deletion tests/FactoryMuffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ public function setUp()
$this->factory = new FactoryMuff();
}

public function test_defauling_to_faker()
{
$obj = $this->factory->create('SampleModelB');
$this->assertInternalType('array', $obj->card);
$this->assertArrayHasKey('type', $obj->card);
$this->assertArrayHasKey('number', $obj->card);
$this->assertArrayHasKey('name', $obj->card);
$this->assertArrayHasKey('expirationDate', $obj->card);
}

public function test_should_get_attributes_for()
{
$attr = $this->factory->attributesFor('SampleModelA');
Expand Down Expand Up @@ -204,7 +214,7 @@ class SampleModelA
'modelb_id' => 'factory|SampleModelB',
'name' => 'string',
'email' => 'email',
'message' => 'text',
'message' => 'text'
);

public function save()
Expand All @@ -227,6 +237,7 @@ class SampleModelB extends SampleModelA
'title' => 'string',
'email' => 'email',
'content' => 'text',
'card' => 'creditCardDetails'
);
}

Expand Down

0 comments on commit 8a2a0ff

Please sign in to comment.