Skip to content

Commit

Permalink
Merge pull request #29 from Zizaco/cleanup-and-changes
Browse files Browse the repository at this point in the history
Cleanup and changes and adds support for #27
  • Loading branch information
scottrobertson committed Jun 5, 2014
2 parents 0f606ce + f840111 commit bf389b3
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: php

php:
php:
- 5.3
- 5.4
- 5.5
- hhvm

before_script:
- curl -s http://getcomposer.org/installer | php
Expand Down
108 changes: 82 additions & 26 deletions src/Zizaco/FactoryMuff/Kind.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,107 @@

abstract class Kind
{
/**
* The Kind classes that are available.
* @var array
*/
protected static $available_kinds = array(
'call',
'closure',
'date',
'email',
'factory',
'integer',
'none',
'string',
'text',
);

/**
* Holds the Kind we are working on
* @var Zizaco\FactoryMuff\Kind
*/
protected $kind = null;

/**
* Holds the model data
* @var array
*/
protected $model = null;

/**
* Initialise our Kind
* @param Zizaco\FactoryMuff\Kind $kind
* @param array $model
*/
public function __construct($kind, $model)
{
$this->kind = $kind;
$this->model = $model;
}

/**
* Detect the type of Kind we are processing
* @param string $kind
* @param array $model
* @return Zizaco\FactoryMuff\Kind
*/
public static function detect($kind, $model = null)
{
if($kind instanceof \Closure) {
return new Kind\Closure($kind, $model);
}
elseif (is_string($kind) && substr($kind, 0, 8) == 'factory|')
if ($kind instanceof \Closure)
{
return new Kind\Factory($kind, $model);
}
elseif (is_string($kind) && substr($kind, 0, 5) === 'call|')
{
return new Kind\Call($kind, $model);
return new Kind\Closure($kind, $model);
}
elseif (is_string($kind) && substr($kind, 0, 5) === 'date|')

$class = '\\Zizaco\\FactoryMuff\\Kind\\None';
foreach (static::$available_kinds as $available_kind)
{
return new Kind\Date($kind, $model);
if (substr($kind, 0, strlen($available_kind)) === $available_kind) {
$class = '\\Zizaco\\FactoryMuff\\Kind\\' . ucfirst($available_kind);
break;
}
}
elseif (is_string($kind) && substr($kind, 0, 8) === 'integer|')
{
return new Kind\Integer($kind, $model);

return new $class($kind, $model);

}

/**
* Returns an option passed to the Kind
* @param integer $index
* @param mixed $default
* @return mixed
*/
public function getOption($index, $default = null)
{
$options = $this->getOptions();
if (isset($options[$index])) {
return $options[$index];
}
else
{
switch ( $kind ) {
case 'email':
return new Kind\Email($kind, $model);
case 'text':
return new Kind\Text($kind, $model);
case 'string':
return new Kind\String($kind, $model);
default:
return new Kind\None($kind, $model);
}

return $default;
}

/**
* Return an array of all options passed to the Kind (after |)
* @return array
*/
public function getOptions()
{
$options = explode('|', $this->kind);
array_shift($options);

if (count($options) > 0) {
$options = explode(',', $options[0]);
}

return $options;
}

/**
* Abstract class used by individual Kind's to return
* generated data
* @return string|integer
*/
abstract public function generate();
}
2 changes: 1 addition & 1 deletion src/Zizaco/FactoryMuff/Kind/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Date extends Kind
{
public function generate()
{
$format = substr($this->kind, 5);
$format = $this->getOption(0, 'r');
$faker = \Faker\Factory::create();
return $faker->date($format);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Zizaco/FactoryMuff/Kind/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Integer extends Kind
{
public function generate()
{
$length = substr($this->kind, 8);
$length = (int) $this->getOption(0, 5);
$faker = \Faker\Factory::create();
return $faker->randomNumber($length);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Zizaco/FactoryMuff/Kind/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class String extends Kind
public function generate()
{
$faker = \Faker\Factory::create();
return $faker->word;
$length = $this->getOption(0, 10);

// Generate a large amount of text. The reason for this is that
// faker uses a maximum length, and not an exact length. We then substr this
$text = str_replace(' ', null, $faker->text($length * 3));
return substr($text, 0, $length);
}
}
7 changes: 6 additions & 1 deletion src/Zizaco/FactoryMuff/Kind/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Text extends Kind
public function generate()
{
$faker = \Faker\Factory::create();
return $faker->sentence();
$length = $this->getOption(0, 100);

// Generate a large amount of text. The reason for this is that
// faker uses a maximum length, and not an exact length. We then substr this
$text = $faker->text($length * 3);
return substr($text, 0, $length);
}
}
105 changes: 105 additions & 0 deletions tests/FactoryMuffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,53 @@ public function test_integer()
$this->assertEquals(9, strlen($obj->number));
}

public function test_string()
{
$this->factory->define('SampleModelA', array(
'string' => 'string|4',
));

$obj = $this->factory->create('SampleModelA');
$this->assertEquals(4, strlen($obj->string));
}

public function test_text()
{
$this->factory->define('SampleModelA', array(
'text' => 'text|4',
));

$obj = $this->factory->create('SampleModelA');
$this->assertEquals(4, strlen($obj->text));
}

public function test_text_default()
{
$this->factory->define('SampleModelA', array(
'text' => 'text',
));

$obj = $this->factory->create('SampleModelA');
$this->assertEquals(100, strlen($obj->text));
}

public function test_should_create()
{
$obj = $this->factory->create('SampleModelA');

$this->assertTrue( is_numeric($obj->id) );
}

public function test_get_ids()
{
$obj = $this->factory->create('SampleModelF');

$this->assertEquals(1, $obj->modelGetKey);
$this->assertEquals(1, $obj->modelPk);
$this->assertEquals(1, $obj->model_id);
$this->assertNull($obj->model_null);
}

public function test_should_throw_exception_on_model_save_failure()
{
$this->setExpectedException('\Zizaco\FactoryMuff\SaveException');
Expand Down Expand Up @@ -217,6 +257,71 @@ public function save()

class SampleModelE
{
public function save()
{
return true;
}
}

class SampleModelF
{

public static $factory = array(
'modelGetKey' => 'factory|SampleModelGetKey',
'modelPk' => 'factory|SampleModelPk',
'model_id' => 'factory|SampleModel_id',
'model_null' => 'factory|SampleModel_null',
);

public function save()
{
return true;
}
}

class SampleModelGetKey
{
public static $factory = array();
public function getKey()
{
return 1;
}

public function save()
{
return true;
}
}

class SampleModelPk
{
public static $factory = array();
public function pk()
{
return 1;
}

public function save()
{
return true;
}
}

class SampleModel_id
{
public static $factory = array();
public $_id = 1;

public function save()
{
return true;
}
}

class SampleModel_null
{
public static $factory = array();

public function save()
{
return true;
Expand Down

0 comments on commit bf389b3

Please sign in to comment.