-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance yii\behaviors\AttributeBehavior
#13586
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
|
||
namespace yiiunit\framework\behaviors; | ||
|
||
use Yii; | ||
use yiiunit\TestCase; | ||
use yii\db\Connection; | ||
use yii\db\ActiveRecord; | ||
use yii\behaviors\AttributeBehavior; | ||
|
||
/** | ||
* Unit test for [[\yii\behaviors\AttributeBehavior]]. | ||
* @see AttributeBehavior | ||
* | ||
* @group behaviors | ||
*/ | ||
class AttributeBehaviorTest extends TestCase | ||
{ | ||
/** | ||
* @var Connection test db connection | ||
*/ | ||
protected $dbConnection; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { | ||
static::markTestSkipped('PDO and SQLite extensions are required.'); | ||
} | ||
} | ||
|
||
public function setUp() | ||
{ | ||
$this->mockApplication([ | ||
'components' => [ | ||
'db' => [ | ||
'class' => '\yii\db\Connection', | ||
'dsn' => 'sqlite::memory:', | ||
] | ||
] | ||
]); | ||
|
||
$columns = [ | ||
'id' => 'pk', | ||
'name' => 'string', | ||
'alias' => 'string', | ||
]; | ||
Yii::$app->getDb()->createCommand()->createTable('test_attribute', $columns)->execute(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
Yii::$app->getDb()->close(); | ||
parent::tearDown(); | ||
} | ||
|
||
// Tests : | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you wrap the test methods into 1 test method with a dataprovider? This enables code-reuse and its easier to compare the data used for testing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
/** | ||
* @return array | ||
*/ | ||
public function preserveNonEmptyValuesDataProvider() | ||
{ | ||
return [ | ||
[ | ||
'John Doe', | ||
false, | ||
'John Doe', | ||
null, | ||
], | ||
[ | ||
'John Doe', | ||
false, | ||
'John Doe', | ||
'Johnny', | ||
], | ||
[ | ||
'John Doe', | ||
true, | ||
'John Doe', | ||
null, | ||
], | ||
[ | ||
'Johnny', | ||
true, | ||
'John Doe', | ||
'Johnny', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider preserveNonEmptyValuesDataProvider | ||
*/ | ||
public function testPreserveNonEmptyValues( | ||
$aliasExpected, | ||
$preserveNonEmptyValues, | ||
$name, | ||
$alias | ||
) | ||
{ | ||
$model = new ActiveRecordWithAttributeBehavior(); | ||
$model->attributeBehavior->preserveNonEmptyValues = $preserveNonEmptyValues; | ||
$model->name = $name; | ||
$model->alias = $alias; | ||
$model->validate(); | ||
|
||
$this->assertEquals($aliasExpected, $model->alias); | ||
} | ||
} | ||
|
||
/** | ||
* Test Active Record class with [[AttributeBehavior]] behavior attached. | ||
* | ||
* @property integer $id | ||
* @property string $name | ||
* @property string $alias | ||
* | ||
* @property AttributeBehavior $attributeBehavior | ||
*/ | ||
class ActiveRecordWithAttributeBehavior extends ActiveRecord | ||
{ | ||
public function behaviors() | ||
{ | ||
return [ | ||
'attribute' => [ | ||
'class' => AttributeBehavior::className(), | ||
'attributes' => [ | ||
self::EVENT_BEFORE_VALIDATE => 'alias', | ||
], | ||
'value' => function ($event) { | ||
return $event->sender->name; | ||
}, | ||
], | ||
]; | ||
} | ||
|
||
public static function tableName() | ||
{ | ||
return 'test_attribute'; | ||
} | ||
|
||
/** | ||
* @return AttributeBehavior | ||
*/ | ||
public function getAttributeBehavior() | ||
{ | ||
return $this->getBehavior('attribute'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
skipNotEmpty
be better?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@klimov-paul wouldn't
skipNonEmpty
be better? Note theNon
.It seems to me that is should sound like
skip non-empty variables
not theskip not empty variables
. It seems to me that the latter is not a well-formed English sentence. Please correct me if I'm wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Non
is better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@klimov-paul, @samdark, see #13586 (comment) for the motivation for $preserveNonEmptyValues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually yes,
preserveNonEmptyValues
is more clear despite being a bit longer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my POV,
preserve
is a bit more complex word thanomit
orskip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skip + not is a double negation which is not ideal. Positive formulation is better for readability. What about
keepSetValues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable may be set to empty value so it's not precise this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keepNonEmptyValues