Commit c600572f by Florian Fackler

The missing Component.php tests

parent 009d47db
...@@ -174,10 +174,8 @@ class Component extends Object ...@@ -174,10 +174,8 @@ class Component extends Object
} }
} }
} }
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '.' . $name); throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '.' . $name);
} }
}
/** /**
* Calls the named method which is not a class method. * Calls the named method which is not a class method.
......
...@@ -303,6 +303,57 @@ class ComponentTest extends TestCase ...@@ -303,6 +303,57 @@ class ComponentTest extends TestCase
$this->assertNull($component->getBehavior('a')); $this->assertNull($component->getBehavior('a'));
$this->assertNull($component->getBehavior('b')); $this->assertNull($component->getBehavior('b'));
} }
public function testSetReadOnlyProperty()
{
$this->setExpectedException(
'\yii\base\InvalidCallException',
'Setting read-only property: yiiunit\framework\base\NewComponent::object'
);
$this->component->object = 'z';
}
public function testSetPropertyOfBehavior()
{
$this->assertNull($this->component->getBehavior('a'));
$behavior = new NewBehavior;
$this->component->attachBehaviors([
'a' => $behavior,
]);
$this->component->p = 'Yii is cool.';
$this->assertSame('Yii is cool.', $this->component->getBehavior('a')->p);
}
public function testSettingBehaviorWithSetter()
{
$behaviorName = 'foo';
$this->assertNull($this->component->getBehavior($behaviorName));
$p = 'as ' . $behaviorName;
$this->component->$p = __NAMESPACE__ . '\NewBehavior';
$this->assertSame(__NAMESPACE__ . '\NewBehavior', get_class($this->component->getBehavior($behaviorName)));
}
public function testWriteOnlyProperty()
{
$this->setExpectedException(
'\yii\base\InvalidCallException',
'Getting write-only property: yiiunit\framework\base\NewComponent::writeOnly'
);
$this->component->writeOnly;
}
public function testSuccessfulMethodCheck()
{
$this->assertTrue($this->component->hasMethod('hasProperty'));
}
public function testTurningOffNonExistingBehavior()
{
$this->assertFalse($this->component->hasEventHandlers('foo'));
$this->assertFalse($this->component->off('foo'));
}
} }
class NewComponent extends Component class NewComponent extends Component
...@@ -357,6 +408,10 @@ class NewComponent extends Component ...@@ -357,6 +408,10 @@ class NewComponent extends Component
{ {
$this->trigger('click', new Event); $this->trigger('click', new Event);
} }
public function setWriteOnly()
{
}
} }
class NewBehavior extends Behavior class NewBehavior extends Behavior
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment