Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Zizaco/factory-muff
Browse files Browse the repository at this point in the history
Conflicts:
	src/Zizaco/FactoryMuff/Kind.php
	src/Zizaco/FactoryMuff/Kind/None.php
  • Loading branch information
Phil Sturgeon committed Jul 4, 2014
2 parents 34cc79c + 8a2a0ff commit 1d2fbf4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/Zizaco/FactoryMuff/Kind.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ abstract class Kind
'closure',
'date',
'factory',
'generic',
'integer',
'name',
'none',
'string',
'text',
);
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions src/Zizaco/FactoryMuff/Kind/Generic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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()
{
// Only try and use Faker when there are no spaces in the string
if (! is_string($this->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;
}
}
18 changes: 0 additions & 18 deletions src/Zizaco/FactoryMuff/Kind/None.php

This file was deleted.

33 changes: 22 additions & 11 deletions tests/FactoryMuffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@

use Zizaco\FactoryMuff\FactoryMuff;

class FactoryMuffTest extends PHPUnit_Framework_TestCase {

class FactoryMuffTest extends PHPUnit_Framework_TestCase
{
protected $factory;

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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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';
},
));
Expand Down Expand Up @@ -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
*
Expand All @@ -262,10 +272,10 @@ class SampleModelB extends SampleModelA
'title' => 'string',
'email' => 'email',
'content' => 'text',
'card' => 'creditCardDetails'
);
}


/**
* Testing only
*
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -396,7 +407,7 @@ public static function factory()
{
return array(
'string' => 'just a string',
'four' => function() {
'four' => function () {
return 2 + 2;
}
);
Expand Down

0 comments on commit 1d2fbf4

Please sign in to comment.