Skip to content

Commit

Permalink
Fixed #16247
Browse files Browse the repository at this point in the history
  • Loading branch information
MakeilaLundy committed May 16, 2018
1 parent 2468282 commit ac69e33
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
11 changes: 9 additions & 2 deletions framework/base/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,15 @@ public function __call($name, $params)
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
$this-> _eventWildcards = [];

if ($this->_behaviors !== null) {
$behaviors = $this->_behaviors;
$this->_behaviors = null;
foreach ($behaviors as $name => $behavior) {
$this->attachBehavior($name, clone $behavior);
}
}
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/framework/behaviors/CloningBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yiiunit\framework\behaviors;

use yii\base\Behavior;
use yii\base\Model;
use yiiunit\TestCase;

/**
* Unit test for cloning behaviors when objects are cloned.
*
* @group behaviors
*/
class CloningBehaviorTest extends TestCase
{
public function testCloningObjectsClonesBehaviors()
{
$model1 = new ModelWithBehavior();
$model1->myBehaviorProperty = 'foo';
$model2 = clone $model1;

$this->assertEquals($model1->myBehaviorProperty, $model2->myBehaviorProperty);
}
}

/**
* Test Model class with a behavior attached.
*
* @mixin BehaviorWithProperty
*/
class ModelWithBehavior extends Model
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'myBehavior' => BehaviorWithProperty::class
];
}
}

/**
* Test Behavior class with property.
*
*/
class BehaviorWithProperty extends Behavior
{
public $myBehaviorProperty;
}

0 comments on commit ac69e33

Please sign in to comment.