Skip to content

Instantly share code, notes, and snippets.

@ivanmorales
Created July 17, 2014 16:18
Show Gist options
  • Save ivanmorales/e451d95efc41ecd8f1c7 to your computer and use it in GitHub Desktop.
Save ivanmorales/e451d95efc41ecd8f1c7 to your computer and use it in GitHub Desktop.
Yii command for creating PHPUnit Fixture from ActiveRecord Model
<?php
class CreateFixtureCommand extends CConsoleCommand
{
public function actionIndex($type, $key_column, $ids)
{
$ids = explode(',', $ids);
if (!@class_exists($type))
{
throw new CException("Type: $type does not exist");
}
$model = new $type;
if (!array_key_exists($key_column, $model->attributes))
{
throw new CException("Column: $key_column does not exist on type $type");
}
$items = $model->findAllByPk($ids);
if (empty($items))
{
throw new CException("No data found for specified parameters.");
}
echo "
--------------- COPY FIXTURE BELOW ---------------\n\n";
echo "
<?php
return array(";
foreach($items as $item) {
echo "
'{$item->$key_column}' => array(";
foreach($item->attributes as $key => $value) {
echo "
'$key' => '$value',";
}
echo "
),";
}
echo "
);";
echo "
--------------- COPY FIXTURE ABOVE ---------------";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment