diff --git a/src/Zizaco/FactoryMuff/Kind.php b/src/Zizaco/FactoryMuff/Kind.php index 09bf69c..ca84ae1 100644 --- a/src/Zizaco/FactoryMuff/Kind.php +++ b/src/Zizaco/FactoryMuff/Kind.php @@ -15,9 +15,9 @@ abstract class Kind 'closure', 'date', 'factory', + 'generic', 'integer', 'name', - 'none', 'string', 'text', ); @@ -61,7 +61,7 @@ public static function detect($kind, $model = null) return new Kind\Closure($kind, $model, $faker); } - $class = '\\Zizaco\\FactoryMuff\\Kind\\None'; + $class = '\\Zizaco\\FactoryMuff\\Kind\\Generic'; foreach (static::$availableKinds as $availableKind) { if (substr($kind, 0, strlen($availableKind)) === $availableKind) { $class = '\\Zizaco\\FactoryMuff\\Kind\\' . ucfirst($availableKind); diff --git a/src/Zizaco/FactoryMuff/Kind/Generic.php b/src/Zizaco/FactoryMuff/Kind/Generic.php new file mode 100644 index 0000000..9de3d45 --- /dev/null +++ b/src/Zizaco/FactoryMuff/Kind/Generic.php @@ -0,0 +1,31 @@ +kind) or strpos($this->kind, ' ') !== false) { + return $this->kind; + } + + // If it fails to call it, it must not be a real thing + try { + return call_user_func(array($this->faker, $this->kind)); + } catch (InvalidArgumentException $e) { + + } + + // Just return the literal string + return $this->kind; + } +} diff --git a/src/Zizaco/FactoryMuff/Kind/None.php b/src/Zizaco/FactoryMuff/Kind/None.php deleted file mode 100644 index 446d2a1..0000000 --- a/src/Zizaco/FactoryMuff/Kind/None.php +++ /dev/null @@ -1,18 +0,0 @@ -faker, $this->kind)); - } catch (InvalidArgumentException $e) { - return $this->kind; - } - } -} diff --git a/tests/FactoryMuffTest.php b/tests/FactoryMuffTest.php index 7151aa6..8ded4d2 100644 --- a/tests/FactoryMuffTest.php +++ b/tests/FactoryMuffTest.php @@ -2,8 +2,8 @@ use Zizaco\FactoryMuff\FactoryMuff; -class FactoryMuffTest extends PHPUnit_Framework_TestCase { - +class FactoryMuffTest extends PHPUnit_Framework_TestCase +{ protected $factory; public function setUp() @@ -11,15 +11,25 @@ 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'); foreach ($attr as $value) { - $this->assertEquals( 'string' ,gettype($value) ); + $this->assertInternalType('string', $value); } - $this->assertTrue( is_numeric($attr['modelb_id']) ); + $this->assertTrue(is_numeric($attr['modelb_id']) ); } public function test_date_kind() @@ -107,7 +117,7 @@ public function test_should_create() { $obj = $this->factory->create('SampleModelA'); - $this->assertTrue( is_numeric($obj->id) ); + $this->assertTrue(is_numeric($obj->id)); } public function test_get_ids() @@ -167,7 +177,7 @@ public function test_faker_default_boolean() $obj = $this->factory->create('SampleModelA'); - $this->assertTrue(is_bool($obj->something), "Asserting {$obj->something} is a boolean"); + $this->assertInternalType('boolean', $obj->something, "Asserting {$obj->something} is a boolean"); } public function test_faker_default_latitude() @@ -205,7 +215,7 @@ public function test_should_throw_exception_when_no_defined_factory() public function test_should_accept_closure_as_attribute_factory() { $this->factory->define('SampleModelA', array( - 'text' => function() { + 'text' => function () { return 'just a string'; }, )); @@ -239,17 +249,17 @@ class SampleModelA 'modelb_id' => 'factory|SampleModelB', 'name' => 'string', 'email' => 'email', - 'message' => 'text', + 'message' => 'text' ); public function save() { $this->id = date('U'); + return true; } } - /** * Testing only * @@ -262,10 +272,10 @@ class SampleModelB extends SampleModelA 'title' => 'string', 'email' => 'email', 'content' => 'text', + 'card' => 'creditCardDetails' ); } - /** * Testing only * @@ -309,6 +319,7 @@ public static function makeSlug($text) public static function mungeModel($model) { $bits = explode('@', strtolower($model->email)); + return $bits[0]; } public function save() @@ -396,7 +407,7 @@ public static function factory() { return array( 'string' => 'just a string', - 'four' => function() { + 'four' => function () { return 2 + 2; } );