Fixes #13508: Fixed duplicate attachment of behavior BC break

Fixes BC break introduced in
954c771fdb (diff-d45d5e14cbb9739c2e1c63b53ba5e363L669)
This commit is contained in:
Carsten Brandt
2017-02-04 13:47:04 +01:00
committed by Alexander Makarov
parent 5eded299e0
commit 25b78aa615
3 changed files with 52 additions and 9 deletions

View File

@ -1,10 +1,10 @@
Yii Framework 2 Change Log
==========================
2.0.12 under development
2.0.11.2 under development
--------------------------
- no changes in this release.
- Bug #13508: Fixed duplicate attachment of behavior BC break (cebe)
2.0.11.1 February 02, 2017

View File

@ -669,13 +669,13 @@ class Component extends Object
if (is_int($name)) {
$behavior->attach($this);
$this->_behaviors[] = $behavior;
}
} else {
if (isset($this->_behaviors[$name])) {
$this->_behaviors[$name]->detach();
}
$behavior->attach($this);
$this->_behaviors[$name] = $behavior;
}
return $behavior;
}

View File

@ -22,6 +22,9 @@ class FooClass extends Component
class BarBehavior extends Behavior
{
public static $attachCount = 0;
public static $detachCount = 0;
public $behaviorProperty = 'behavior property';
public function behaviorMethod()
@ -46,6 +49,18 @@ class BarBehavior extends Behavior
return parent::hasMethod($name);
}
public function attach($owner)
{
self::$attachCount++;
parent::attach($owner);
}
public function detach()
{
self::$detachCount++;
parent::detach();
}
}
/**
@ -59,11 +74,16 @@ class BehaviorTest extends TestCase
$this->mockApplication();
}
public function testAttachAndAccessing()
public function testAttachAndAccessingWithName()
{
BarBehavior::$attachCount = 0;
BarBehavior::$detachCount = 0;
$bar = new BarClass();
$behavior = new BarBehavior();
$bar->attachBehavior('bar', $behavior);
$this->assertEquals(1, BarBehavior::$attachCount);
$this->assertEquals(0, BarBehavior::$detachCount);
$this->assertEquals('behavior property', $bar->behaviorProperty);
$this->assertEquals('behavior method', $bar->behaviorMethod());
$this->assertEquals('behavior property', $bar->getBehavior('bar')->behaviorProperty);
@ -71,14 +91,37 @@ class BehaviorTest extends TestCase
$behavior = new BarBehavior(['behaviorProperty' => 'reattached']);
$bar->attachBehavior('bar', $behavior);
$this->assertEquals(2, BarBehavior::$attachCount);
$this->assertEquals(1, BarBehavior::$detachCount);
$this->assertEquals('reattached', $bar->behaviorProperty);
}
public function testAttachAndAccessingAnonymous()
{
BarBehavior::$attachCount = 0;
BarBehavior::$detachCount = 0;
$bar = new BarClass();
$behavior = new BarBehavior();
$bar->attachBehaviors([$behavior]);
$this->assertEquals(1, BarBehavior::$attachCount);
$this->assertEquals(0, BarBehavior::$detachCount);
$this->assertEquals('behavior property', $bar->behaviorProperty);
$this->assertEquals('behavior method', $bar->behaviorMethod());
}
public function testAutomaticAttach()
{
BarBehavior::$attachCount = 0;
BarBehavior::$detachCount = 0;
$foo = new FooClass();
$this->assertEquals(0, BarBehavior::$attachCount);
$this->assertEquals(0, BarBehavior::$detachCount);
$this->assertEquals('behavior property', $foo->behaviorProperty);
$this->assertEquals('behavior method', $foo->behaviorMethod());
$this->assertEquals(1, BarBehavior::$attachCount);
$this->assertEquals(0, BarBehavior::$detachCount);
}
public function testMagicMethods()