diff --git a/.travis.yml b/.travis.yml index 0edb59c..9b2bbba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,10 @@ language: php -php: +php: - 5.3 - 5.4 + - 5.5 + - hhvm before_script: - curl -s http://getcomposer.org/installer | php diff --git a/src/Zizaco/FactoryMuff/Kind.php b/src/Zizaco/FactoryMuff/Kind.php index 2f014a8..ba5c433 100644 --- a/src/Zizaco/FactoryMuff/Kind.php +++ b/src/Zizaco/FactoryMuff/Kind.php @@ -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(); } \ No newline at end of file diff --git a/src/Zizaco/FactoryMuff/Kind/Date.php b/src/Zizaco/FactoryMuff/Kind/Date.php index 1ae10be..c4b9b08 100644 --- a/src/Zizaco/FactoryMuff/Kind/Date.php +++ b/src/Zizaco/FactoryMuff/Kind/Date.php @@ -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); } diff --git a/src/Zizaco/FactoryMuff/Kind/Integer.php b/src/Zizaco/FactoryMuff/Kind/Integer.php index 00dbbb3..b9f78cb 100644 --- a/src/Zizaco/FactoryMuff/Kind/Integer.php +++ b/src/Zizaco/FactoryMuff/Kind/Integer.php @@ -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); } diff --git a/src/Zizaco/FactoryMuff/Kind/String.php b/src/Zizaco/FactoryMuff/Kind/String.php index 6de4fc6..ea3a44f 100644 --- a/src/Zizaco/FactoryMuff/Kind/String.php +++ b/src/Zizaco/FactoryMuff/Kind/String.php @@ -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); } } \ No newline at end of file diff --git a/src/Zizaco/FactoryMuff/Kind/Text.php b/src/Zizaco/FactoryMuff/Kind/Text.php index 2ec071b..216d857 100644 --- a/src/Zizaco/FactoryMuff/Kind/Text.php +++ b/src/Zizaco/FactoryMuff/Kind/Text.php @@ -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); } } \ No newline at end of file diff --git a/tests/FactoryMuffTest.php b/tests/FactoryMuffTest.php index 361b620..92c5a8b 100644 --- a/tests/FactoryMuffTest.php +++ b/tests/FactoryMuffTest.php @@ -56,6 +56,36 @@ 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'); @@ -63,6 +93,16 @@ public function test_should_create() $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'); @@ -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;