Created
July 17, 2014 16:18
-
-
Save ivanmorales/e451d95efc41ecd8f1c7 to your computer and use it in GitHub Desktop.
Yii command for creating PHPUnit Fixture from ActiveRecord Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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