Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Yii Framework 2 Change Log
- Bug #11404: `yii\base\Model::loadMultiple()` returns true even if `yii\base\Model::load()` returns false (zvook)
- Bug #13513: Fixed RBAC migration to work correctly on Oracle DBMS (silverfire)
- Enh #13550: Refactored unset call order in `yii\di\ServiceLocator::set()` (Lanrik)
- Enh #13586: Added `$preserveNonEmptyValues` property to the `yii\behaviors\AttributeBehavior` (Kolyunya)


2.0.11.2 February 08, 2017
Expand Down
8 changes: 8 additions & 0 deletions framework/behaviors/AttributeBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class AttributeBehavior extends Behavior
* @since 2.0.8
*/
public $skipUpdateOnClean = true;
/**
* @var bool whether to preserve non-empty attribute values.
* @since 2.0.12
*/
public $preserveNonEmptyValues = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would skipNotEmpty be better?

Copy link
Contributor Author

@Kolyunya Kolyunya Feb 20, 2017

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 the Non.
It seems to me that is should sound like skip non-empty variables not the skip not empty variables. It seems to me that the latter is not a well-formed English sentence. Please correct me if I'm wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Non is better.

Copy link
Member

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

Copy link
Member

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.

Copy link
Member

@SilverFire SilverFire Feb 22, 2017

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 than omit or skip

Copy link
Member

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

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keepNonEmptyValues



/**
Expand Down Expand Up @@ -117,6 +122,9 @@ public function evaluateAttributes($event)
foreach ($attributes as $attribute) {
// ignore attribute names which are not string (e.g. when set by TimestampBehavior::updatedAtAttribute)
if (is_string($attribute)) {
if ($this->preserveNonEmptyValues && !empty($this->owner->$attribute)) {
continue;
}
$this->owner->$attribute = $value;
}
}
Expand Down
149 changes: 149 additions & 0 deletions tests/framework/behaviors/AttributeBehaviorTest.php
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 :

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@Kolyunya Kolyunya Feb 17, 2017

Choose a reason for hiding this comment

The 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');
}
}