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

Attach behavior via "as behaviorName" with Closure #20248

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions framework/base/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
$name = trim(substr($name, 3));
if ($value instanceof Behavior) {
$this->attachBehavior($name, $value);
} elseif ($value instanceof \Closure) {
$this->attachBehavior($name, call_user_func($value));

Check warning on line 194 in framework/base/Component.php

View check run for this annotation

Codecov / codecov/patch

framework/base/Component.php#L193-L194

Added lines #L193 - L194 were not covered by tests
} elseif ((isset($value['class']) && is_subclass_of($value['class'], Behavior::class)) || (isset($value['__class']) && is_subclass_of($value['__class'], Behavior::class))) {
$this->attachBehavior($name, Yii::createObject($value));
} elseif (is_string($value) && is_subclass_of($value, Behavior::class, true)) {
Expand Down
2 changes: 1 addition & 1 deletion framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ private function isValueDifferent($newValue, $oldValue)
{
if (is_array($newValue) && is_array($oldValue)) {
// Only sort associative arrays
$sorter = function(&$array) {
$sorter = function (&$array) {
if (ArrayHelper::isAssociative($array)) {
ksort($array);
}
Expand Down
1 change: 0 additions & 1 deletion framework/db/mssql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,5 +823,4 @@ public function createColumnSchemaBuilder($type, $length = null)
{
return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]);
}

}
23 changes: 15 additions & 8 deletions tests/framework/base/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,22 +328,26 @@ public function testAttachBehavior()
$this->assertTrue($component->hasProperty('p'));
$component->test();
$this->assertTrue($component->behaviorCalled);
}

$this->assertSame($behavior, $component->detachBehavior('a'));
$this->assertFalse($component->hasProperty('p'));
$this->expectException('yii\base\UnknownMethodException');
$component->test();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test was passing but the assertions following this were never run.


$p = 'as b';
public function testAs()
{
$component = new NewComponent();
$component->$p = ['class' => 'NewBehavior'];
$this->assertSame($behavior, $component->getBehavior('a'));
$component->{'as a'} = new NewBehavior();
$this->assertTrue($component->hasProperty('p'));
$component->test();
$this->assertTrue($component->behaviorCalled);

$component->{'as b'} = ['class' => NewBehavior::class];
$this->assertNotNull($component->getBehavior('b'));

$component->{'as c'} = ['__class' => NewBehavior::class];
$this->assertNotNull($component->getBehavior('c'));

$component->{'as d'} = function () {
return new NewBehavior();
};
$this->assertNotNull($component->getBehavior('d'));
}

public function testAttachBehaviors()
Expand Down Expand Up @@ -376,6 +380,9 @@ public function testDetachBehavior()

$detachedBehavior = $component->detachBehavior('z');
$this->assertNull($detachedBehavior);

$this->expectException('yii\base\UnknownMethodException');
$component->test();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This made more sense to test here than in attachBehaviors where it lived and was causing the test to end prematurely.

}

public function testDetachBehaviors()
Expand Down