From 34cc79cafbf31ec2b9f2f6eed88a5c0c8e4d05ce Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Fri, 4 Jul 2014 14:38:14 +0100 Subject: [PATCH] My own approach to #46 with a PSR-2 cleanup --- src/Zizaco/FactoryMuff/Facade/FactoryMuff.php | 12 +- src/Zizaco/FactoryMuff/FactoryMuff.php | 62 ++---- src/Zizaco/FactoryMuff/Kind.php | 189 +++++++++--------- src/Zizaco/FactoryMuff/Kind/Call.php | 44 ++-- src/Zizaco/FactoryMuff/Kind/Closure.php | 3 +- src/Zizaco/FactoryMuff/Kind/Date.php | 14 +- src/Zizaco/FactoryMuff/Kind/Email.php | 14 -- src/Zizaco/FactoryMuff/Kind/Factory.php | 24 +-- src/Zizaco/FactoryMuff/Kind/Integer.php | 27 +-- src/Zizaco/FactoryMuff/Kind/Name.php | 6 +- src/Zizaco/FactoryMuff/Kind/None.php | 15 +- src/Zizaco/FactoryMuff/Kind/String.php | 20 +- src/Zizaco/FactoryMuff/Kind/Text.php | 6 +- .../FactoryMuff/NoDefinedFactoryException.php | 2 +- src/Zizaco/FactoryMuff/SaveException.php | 2 +- tests/FactoryMuffTest.php | 35 ++++ 16 files changed, 240 insertions(+), 235 deletions(-) delete mode 100644 src/Zizaco/FactoryMuff/Kind/Email.php diff --git a/src/Zizaco/FactoryMuff/Facade/FactoryMuff.php b/src/Zizaco/FactoryMuff/Facade/FactoryMuff.php index e769527..78438e1 100644 --- a/src/Zizaco/FactoryMuff/Facade/FactoryMuff.php +++ b/src/Zizaco/FactoryMuff/Facade/FactoryMuff.php @@ -17,11 +17,10 @@ class FactoryMuff * @static */ private static $fmInstance; - /** * Get or stance FactoryMuff obj - * + * * @access private * @static * @@ -29,8 +28,7 @@ class FactoryMuff */ private static function fmInstance() { - if (! static::$fmInstance) - { + if (! static::$fmInstance) { static::$fmInstance = new \Zizaco\FactoryMuff\FactoryMuff; } @@ -40,7 +38,7 @@ private static function fmInstance() /** * Creates and saves in db an instance * of Model with mock attributes - * + * * @param string $model Model class name. * @param array $attr Model attributes. * @@ -57,7 +55,7 @@ public static function create( $model, $attr = array() ) /** * Return an instance of the model, which is * not saved in the database - * + * * @param string $model Model class name. * @param array $attr Model attributes. * @@ -73,7 +71,7 @@ public static function instance( $model, $attr = array() ) /** * Returns an array of mock attributes * for the especified model - * + * * @param string $model Model class name. * @param array $attr Model attributes. * diff --git a/src/Zizaco/FactoryMuff/FactoryMuff.php b/src/Zizaco/FactoryMuff/FactoryMuff.php index 2e24e05..1e1a61e 100644 --- a/src/Zizaco/FactoryMuff/FactoryMuff.php +++ b/src/Zizaco/FactoryMuff/FactoryMuff.php @@ -2,8 +2,6 @@ namespace Zizaco\FactoryMuff; -use Zizaco\FactoryMuff\Kind; - /** * Creates models with random attributes * @@ -14,13 +12,10 @@ */ class FactoryMuff { - /** * $factories * * @var array - * - * @access private */ private $factories = array(); @@ -31,23 +26,19 @@ class FactoryMuff * @param string $model Model class name. * @param array $attr Model attributes. * - * @access public - * * @return mixed Returns the model instance. */ - public function create( $model, $attr = array() ) + public function create($model, $attr = array()) { - $obj = $this->instance( $model, $attr ); + $obj = $this->instance($model, $attr); $result = $obj->save(); - if ( !$result ) { + if (!$result) { $message = ''; - if(isset($obj->validationErrors)) - { - if($obj->validationErrors) - { + if (isset($obj->validationErrors)) { + if ($obj->validationErrors) { $message = $obj->validationErrors.' - '; } } @@ -65,14 +56,12 @@ public function create( $model, $attr = array() ) * @param string $model Model class name. * @param array $attr Model attributes. * - * @access public - * * @return mixed Returns the model instance. */ - public function instance( $model, $attr = array() ) + public function instance($model, $attr = array()) { // Get the factory attributes for that model - $attr_array = $this->attributesFor( $model, $attr ); + $attr_array = $this->attributesFor($model, $attr); // Create, set, save and return instance $obj = new $model(); @@ -91,18 +80,16 @@ public function instance( $model, $attr = array() ) * @param string $model Model class name. * @param array $attr Model attributes. * - * @access public - * * @return array Returns an attributes array. */ - public function attributesFor( $model, $attr = array() ) + public function attributesFor($model, $attr = array()) { $factory_attrs = $this->getFactoryAttrs($model); // Prepare attributes - foreach ( $factory_attrs as $key => $kind ) { - if ( ! isset($attr[$key]) ){ - $attr[$key] = $this->generateAttr( $kind, $model ); + foreach ($factory_attrs as $key => $kind) { + if (! isset($attr[$key])) { + $attr[$key] = $this->generateAttr($kind, $model); } } @@ -112,10 +99,8 @@ public function attributesFor( $model, $attr = array() ) /** * Define a new model factory * - * @param string $model Model class name. - * @param array $definition Array with definition of attributes. - * - * @access public + * @param string $model Model class name. + * @param array $definition Array with definition of attributes. * * @return void */ @@ -129,25 +114,21 @@ public function define($model, array $definition = array()) * * @param string $model Model class name. * - * @access private - * * @return array Returns an factory definition array. */ private function getFactoryAttrs($model) { - if(isset($this->factories[$model])) { + if (isset($this->factories[$model])) { return $this->factories[$model]; } - if(method_exists($model, 'factory')) - { + if (method_exists($model, 'factory')) { return $model::factory(); - } - else { + } else { // Get the $factory static and check for errors - $static_vars = get_class_vars( $model ); + $static_vars = get_class_vars($model); - if (isset( $static_vars['factory'] ) ) { + if (isset($static_vars['factory'])) { return $static_vars['factory']; } } @@ -158,16 +139,15 @@ private function getFactoryAttrs($model) /** * Generate an attribute based in the wordlist * - * @param string $kind The kind of attribute that will be generate. + * @param string $kind The kind of attribute that will be generate. * @param string $model The name of the model class * - * @access private - * * @return mixed String or an instance of related model. */ - public function generateAttr( $kind, $model = NULL ) + public function generateAttr($kind, $model = NULL) { $kind = Kind::detect($kind, $model); + return $kind->generate(); } } diff --git a/src/Zizaco/FactoryMuff/Kind.php b/src/Zizaco/FactoryMuff/Kind.php index cbfd379..09bf69c 100644 --- a/src/Zizaco/FactoryMuff/Kind.php +++ b/src/Zizaco/FactoryMuff/Kind.php @@ -2,110 +2,113 @@ namespace Zizaco\FactoryMuff; +use Faker\Factory as Faker; + abstract class Kind { - /** - * The Kind classes that are available. - * @var array - */ - protected static $available_kinds = array( - 'call', - 'closure', - 'date', - 'email', - 'factory', - 'integer', - 'name', - '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) + /** + * The Kind classes that are available. + * @var array + */ + protected static $availableKinds = array( + 'call', + 'closure', + 'date', + 'factory', + 'integer', + 'name', + '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, $faker) { - return new Kind\Closure($kind, $model); + $this->kind = $kind; + $this->model = $model; + $this->faker = $faker; } - $class = '\\Zizaco\\FactoryMuff\\Kind\\None'; - foreach (static::$available_kinds as $available_kind) + /** + * 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 (substr($kind, 0, strlen($available_kind)) === $available_kind) { - $class = '\\Zizaco\\FactoryMuff\\Kind\\' . ucfirst($available_kind); - break; - } + // TODO Move this somewhere where its only instantiated once + $faker = new Faker; + + if ($kind instanceof \Closure) { + return new Kind\Closure($kind, $model, $faker); + } + + $class = '\\Zizaco\\FactoryMuff\\Kind\\None'; + foreach (static::$availableKinds as $availableKind) { + if (substr($kind, 0, strlen($availableKind)) === $availableKind) { + $class = '\\Zizaco\\FactoryMuff\\Kind\\' . ucfirst($availableKind); + break; + } + } + + return new $class($kind, $model, $faker->create()); + } - 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]; + /** + * 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]; + } + + return $default; } - 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); - /** - * 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]); + } - if (count($options) > 0) { - $options = explode(',', $options[0]); + return $options; } - 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 + /** + * Abstract class used by individual Kind's to return + * generated data + * @return string|integer + */ + abstract public function generate(); +} diff --git a/src/Zizaco/FactoryMuff/Kind/Call.php b/src/Zizaco/FactoryMuff/Kind/Call.php index f49aba9..86487d8 100644 --- a/src/Zizaco/FactoryMuff/Kind/Call.php +++ b/src/Zizaco/FactoryMuff/Kind/Call.php @@ -2,32 +2,34 @@ namespace Zizaco\FactoryMuff\Kind; +use Exception; +use Zizaco\FactoryMuff\FactoryMuff; use Zizaco\FactoryMuff\Kind; class Call extends Kind { - public function generate() - { - $factory = new \Zizaco\FactoryMuff\FactoryMuff; - $callable = substr($this->kind, 5); - $params = array(); + public function generate() + { + $factory = new FactoryMuff; + $callable = substr($this->kind, 5); + $params = array(); - if (strstr($callable, '|')) { - $parts = explode('|', $callable); - $callable = array_shift($parts); + if (strstr($callable, '|')) { + $parts = explode('|', $callable); + $callable = array_shift($parts); - if ($parts[0] === 'factory' && count($parts) > 1) { - $params[] = $factory->create($parts[1]); - } else { - $attr = implode('|', $parts); - $params[] = $factory->generateAttr($attr, $this->model); - } - } + if ($parts[0] === 'factory' && count($parts) > 1) { + $params[] = $factory->create($parts[1]); + } else { + $attr = implode('|', $parts); + $params[] = $factory->generateAttr($attr, $this->model); + } + } + + if (! method_exists($this->model, $callable)) { + throw new Exception("$this->model does not have a static $callable method"); + } - if (method_exists($this->model, $callable)) { - return call_user_func_array("$this->model::$callable", $params); - } else { - throw new \Exception("$this->model does not have a static $callable method"); + return call_user_func_array("$this->model::$callable", $params); } - } -} \ No newline at end of file +} diff --git a/src/Zizaco/FactoryMuff/Kind/Closure.php b/src/Zizaco/FactoryMuff/Kind/Closure.php index 6a6aba1..2a7b1b9 100644 --- a/src/Zizaco/FactoryMuff/Kind/Closure.php +++ b/src/Zizaco/FactoryMuff/Kind/Closure.php @@ -9,6 +9,7 @@ class Closure extends Kind public function generate() { $kind = $this->kind; + return $kind(); } -} \ No newline at end of file +} diff --git a/src/Zizaco/FactoryMuff/Kind/Date.php b/src/Zizaco/FactoryMuff/Kind/Date.php index c4b9b08..42dfd4b 100644 --- a/src/Zizaco/FactoryMuff/Kind/Date.php +++ b/src/Zizaco/FactoryMuff/Kind/Date.php @@ -6,10 +6,10 @@ class Date extends Kind { - public function generate() - { - $format = $this->getOption(0, 'r'); - $faker = \Faker\Factory::create(); - return $faker->date($format); - } -} \ No newline at end of file + public function generate() + { + $format = $this->getOption(0, 'r'); + + return $this->faker->date($format); + } +} diff --git a/src/Zizaco/FactoryMuff/Kind/Email.php b/src/Zizaco/FactoryMuff/Kind/Email.php deleted file mode 100644 index 7ad143c..0000000 --- a/src/Zizaco/FactoryMuff/Kind/Email.php +++ /dev/null @@ -1,14 +0,0 @@ -email; - } -} \ No newline at end of file diff --git a/src/Zizaco/FactoryMuff/Kind/Factory.php b/src/Zizaco/FactoryMuff/Kind/Factory.php index 4288ed9..df778ab 100644 --- a/src/Zizaco/FactoryMuff/Kind/Factory.php +++ b/src/Zizaco/FactoryMuff/Kind/Factory.php @@ -11,25 +11,19 @@ public function generate() $factory = new \Zizaco\FactoryMuff\FactoryMuff; $related = $factory->create(substr($this->kind, 8)); - if (method_exists($related, 'getKey')) - { + if (method_exists($related, 'getKey')) { return $related->getKey(); - } - elseif (method_exists($related, 'pk')) // Kohana Primary Key - { + } elseif (method_exists($related, 'pk')) { // Kohana Primary Key + return $related->pk(); - } - elseif(isset($related->id)) // id Attribute - { + } elseif (isset($related->id)) { // id Attribute + return $related->id; - } - elseif(isset($related->_id)) // Mongo _id attribute - { + } elseif (isset($related->_id)) { // Mongo _id attribute + return $related->_id; - } - else - { + } else { return null; } } -} \ No newline at end of file +} diff --git a/src/Zizaco/FactoryMuff/Kind/Integer.php b/src/Zizaco/FactoryMuff/Kind/Integer.php index 23a183a..4de0b0f 100644 --- a/src/Zizaco/FactoryMuff/Kind/Integer.php +++ b/src/Zizaco/FactoryMuff/Kind/Integer.php @@ -6,19 +6,20 @@ class Integer extends Kind { - public function generate() - { - $length = (int) $this->getOption(0, 5); - return $this->randomNumber($length); - } + public function generate() + { + $length = (int) $this->getOption(0, 5); - private function randomNumber($length) - { - $integer = null; - for($i = 0; $i < $length; $i++) { - $integer .= mt_rand(1, 9); + return $this->randomNumber($length); } - return (int) $integer; - } -} \ No newline at end of file + private function randomNumber($length) + { + $integer = null; + for ($i = 0; $i < $length; $i++) { + $integer .= mt_rand(1, 9); + } + + return (int) $integer; + } +} diff --git a/src/Zizaco/FactoryMuff/Kind/Name.php b/src/Zizaco/FactoryMuff/Kind/Name.php index 57b37e8..51189c3 100644 --- a/src/Zizaco/FactoryMuff/Kind/Name.php +++ b/src/Zizaco/FactoryMuff/Kind/Name.php @@ -9,7 +9,7 @@ class Name extends Kind public function generate() { $gender = $this->getOption(0, null); - $faker = \Faker\Factory::create(); - return $faker->name($gender); + + return $this->faker->name($gender); } -} \ No newline at end of file +} diff --git a/src/Zizaco/FactoryMuff/Kind/None.php b/src/Zizaco/FactoryMuff/Kind/None.php index 2dbe89f..446d2a1 100644 --- a/src/Zizaco/FactoryMuff/Kind/None.php +++ b/src/Zizaco/FactoryMuff/Kind/None.php @@ -2,12 +2,17 @@ namespace Zizaco\FactoryMuff\Kind; +use InvalidArgumentException; use Zizaco\FactoryMuff\Kind; class None extends Kind { - public function generate() - { - return $this->kind; - } -} \ No newline at end of file + public function generate() + { + try { + return call_user_func(array($this->faker, $this->kind)); + } catch (InvalidArgumentException $e) { + return $this->kind; + } + } +} diff --git a/src/Zizaco/FactoryMuff/Kind/String.php b/src/Zizaco/FactoryMuff/Kind/String.php index ea3a44f..0ea9c50 100644 --- a/src/Zizaco/FactoryMuff/Kind/String.php +++ b/src/Zizaco/FactoryMuff/Kind/String.php @@ -6,14 +6,14 @@ class String extends Kind { - public function generate() - { - $faker = \Faker\Factory::create(); - $length = $this->getOption(0, 10); + public function generate() + { + $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 + // 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, $this->faker->text($length * 3)); + + return substr($text, 0, $length); + } +} diff --git a/src/Zizaco/FactoryMuff/Kind/Text.php b/src/Zizaco/FactoryMuff/Kind/Text.php index 216d857..e77b9b8 100644 --- a/src/Zizaco/FactoryMuff/Kind/Text.php +++ b/src/Zizaco/FactoryMuff/Kind/Text.php @@ -8,12 +8,12 @@ class Text extends Kind { public function generate() { - $faker = \Faker\Factory::create(); $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); + $text = $this->faker->text($length * 3); + return substr($text, 0, $length); } -} \ No newline at end of file +} diff --git a/src/Zizaco/FactoryMuff/NoDefinedFactoryException.php b/src/Zizaco/FactoryMuff/NoDefinedFactoryException.php index 53c2f66..2a68e0d 100644 --- a/src/Zizaco/FactoryMuff/NoDefinedFactoryException.php +++ b/src/Zizaco/FactoryMuff/NoDefinedFactoryException.php @@ -3,4 +3,4 @@ class NoDefinedFactoryException extends \Exception { -} \ No newline at end of file +} diff --git a/src/Zizaco/FactoryMuff/SaveException.php b/src/Zizaco/FactoryMuff/SaveException.php index 4686f3d..97d6629 100644 --- a/src/Zizaco/FactoryMuff/SaveException.php +++ b/src/Zizaco/FactoryMuff/SaveException.php @@ -3,4 +3,4 @@ class SaveException extends \Exception { -} \ No newline at end of file +} diff --git a/tests/FactoryMuffTest.php b/tests/FactoryMuffTest.php index fc81cac..7151aa6 100644 --- a/tests/FactoryMuffTest.php +++ b/tests/FactoryMuffTest.php @@ -159,6 +159,41 @@ public function test_should_create_based_on_define_declaration() $this->assertEquals('just a string', $obj->text); } + public function test_faker_default_boolean() + { + $this->factory->define('SampleModelA', array( + 'something' => 'boolean', + )); + + $obj = $this->factory->create('SampleModelA'); + + $this->assertTrue(is_bool($obj->something), "Asserting {$obj->something} is a boolean"); + } + + public function test_faker_default_latitude() + { + $this->factory->define('SampleModelA', array( + 'lat' => 'latitude', + )); + + $obj = $this->factory->create('SampleModelA'); + + $this->assertGreaterThanOrEqual(-90, $obj->lat); + $this->assertLessThanOrEqual(90, $obj->lat); + } + + public function test_faker_default_longitude() + { + $this->factory->define('SampleModelA', array( + 'lon' => 'longitude', + )); + + $obj = $this->factory->create('SampleModelA'); + + $this->assertGreaterThanOrEqual(-180, $obj->lon); + $this->assertLessThanOrEqual(180, $obj->lon); + } + /** * @expectedException Zizaco\FactoryMuff\NoDefinedFactoryException */