trait CustomError { private $_errorMessages = []; /** * Use in method : return $this->setCustomErrorMessage(message); * * @param array $errorMessages * @return false */ public function setCustomErrorMessage($errorMessages) { if(!is_array($errorMessages)) $errorMessages = [$errorMessages]; $this->_errorMessages = $errorMessages; return false; } /** * @param string $errorMessage */ public function addCustomErrorMessage($errorMessage) { $this->_errorMessages[] = $errorMessage; } /** * @return array */ public function getCustomErrorMessages() { return $this->_errorMessages; } /** * @return mixed */ public function getCustomErrorMessageFirst() { return reset($this->_errorMessages); } /** * @return void */ public function clearCustomErrorMessages() { $this->_errorMessages = []; return; } }
class Blog extends CActiveRecord { use CustomError; // public function checkPrivacyCreate() { // , ... $parent_post = $this->getPost($this->parent_post_id); if (empty($parent_post)) return $this->setCustomErrorMessage(Yii::t('blog', 'post_not_found')); ... return true; } } // public function actionAddPost() { .... if (!$model->addPost()) Tools::jsonError($model->getCustomErrorMessages()); // JSON ... }
<?php Yii::import('system.cli.commands.MigrateCommand'); class MigratecomboCommand extends MigrateCommand { public $connections = array('db', 'db_test'); // public function actionUp($args) { if(($migrations=$this->getNewMigrations())===array()) { echo "No new migration found. Your system is up-to-date.\n"; return 0; } $total=count($migrations); $step=isset($args[0]) ? (int)$args[0] : 0; if($step>0) $migrations=array_slice($migrations,0,$step); $n=count($migrations); if($n===$total) echo "Total $n new ".($n===1 ? 'migration':'migrations')." to be applied:\n"; else echo "Total $n out of $total new ".($total===1 ? 'migration':'migrations')." to be applied:\n"; foreach($migrations as $migration) echo " $migration\n"; echo "\n"; if($this->confirm('Apply the above '.($n===1 ? 'migration':'migrations')."?")) { foreach($migrations as $migration) { foreach($this->connections as $connectionId) { // !!! , $this->connectionID = $connectionId; if($this->migrateUp($migration)===false) { echo "\nMigration failed. All later migrations are canceled.\n"; return 2; } } } echo "\nMigrated up successfully.\n"; } } public function actionDown($args) { $step=isset($args[0]) ? (int)$args[0] : 1; if($step<1) { echo "Error: The step parameter must be greater than 0.\n"; return 1; } if(($migrations=$this->getMigrationHistory($step))===array()) { echo "No migration has been done before.\n"; return 0; } $migrations=array_keys($migrations); $n=count($migrations); echo "Total $n ".($n===1 ? 'migration':'migrations')." to be reverted:\n"; foreach($migrations as $migration) echo " $migration\n"; echo "\n"; if($this->confirm('Revert the above '.($n===1 ? 'migration':'migrations')."?")) { foreach($migrations as $migration) { foreach($this->connections as $connectionId) { $this->connectionID = $connectionId; if($this->migrateDown($migration)===false) { echo "\nMigration failed. All later migrations are canceled.\n"; return 2; } } } echo "\nMigrated down successfully.\n"; } } private $_db; protected function getDbConnection() { if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection) return $this->_db; echo "Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n"; exit(1); } }
yiic migratecombo up(/down/create/...)
class m140317_060002_fill_search_column extends CDbMigration { public function up() { $goals = $this->getDbConnection() // , Yii::app()->db->createCommand... $this->getDbConnection() ->createCommand("SELECT id, `name` FROM goals WHERE `moderated` != 'deleted'") ->queryAll();
class WebTestCase extends CWebTestCase { public $loginRequired = false; protected function setUp() { parent::setUp(); $this->setBrowser('*firefox'); $this->setBrowserUrl(TEST_BASE_URL); $this->prepareTestSession(); if($this->loginRequired) { $this->login(); } } }
class MyTest extends CDbTestCase { public function newUserProvider() { // 3 $faker = \Faker\Factory::create('ru_RU'); $array = array(); for($i=0; $i<3; $i++) { $array[$i]['user']['name'] = $faker->name; $array[$i]['user']['address'] = $faker->address; $array[$i]['user']['country'] = $faker->country; } return $array; } /** * @param $user * @dataProvider newUserProvider */ public function testCreate($user) // 3 { $model = new User('signup'); $model->name = $user['name']; ... $model->save() } }
Source: https://habr.com/ru/post/217239/
All Articles