Browse Source

The missing Component.php tests

tags/2.0.0-beta
Florian Fackler 11 years ago
parent
commit
c600572f34
  1. 4
      framework/base/Component.php
  2. 55
      tests/unit/framework/base/ComponentTest.php

4
framework/base/Component.php

@ -174,9 +174,7 @@ 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);
}
/**

55
tests/unit/framework/base/ComponentTest.php

@ -303,6 +303,57 @@ class ComponentTest extends TestCase
$this->assertNull($component->getBehavior('a'));
$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
@ -357,6 +408,10 @@ class NewComponent extends Component
{
$this->trigger('click', new Event);
}
public function setWriteOnly()
{
}
}
class NewBehavior extends Behavior

Loading…
Cancel
Save