mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 06:11:35 +08:00
Fixed Object class.
This commit is contained in:
@ -107,7 +107,6 @@ class Object
|
||||
{
|
||||
$getter = 'get' . $name;
|
||||
if (method_exists($this, $getter)) {
|
||||
// property is not null
|
||||
return $this->$getter() !== null;
|
||||
} else {
|
||||
return false;
|
||||
@ -129,7 +128,6 @@ class Object
|
||||
{
|
||||
$setter = 'set' . $name;
|
||||
if (method_exists($this, $setter)) {
|
||||
// write property
|
||||
$this->$setter(null);
|
||||
} elseif (method_exists($this, 'get' . $name)) {
|
||||
throw new BadPropertyException('Unsetting read-only property: ' . get_class($this) . '.' . $name);
|
||||
@ -150,9 +148,9 @@ class Object
|
||||
*/
|
||||
public function __call($name, $params)
|
||||
{
|
||||
if ($this->canGetProperty($name, false)) {
|
||||
$getter = 'get' . $name;
|
||||
$func = $this->$getter;
|
||||
$getter = 'get' . $name;
|
||||
if (method_exists($this, $getter)) {
|
||||
$func = $this->$getter();
|
||||
if ($func instanceof \Closure) {
|
||||
return call_user_func_array($func, $params);
|
||||
}
|
||||
|
@ -2,33 +2,14 @@
|
||||
|
||||
namespace yiiunit\framework\base;
|
||||
|
||||
class Foo extends \yii\base\Object
|
||||
{
|
||||
public $prop;
|
||||
}
|
||||
|
||||
class Bar extends \yii\base\Component
|
||||
{
|
||||
public $prop1;
|
||||
public $prop2;
|
||||
public $prop3;
|
||||
|
||||
public function __construct($a, $b)
|
||||
{
|
||||
$this->prop1 = $a + $b;
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->prop3 = 3;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ObjectTest
|
||||
*/
|
||||
class ObjectTest extends \yiiunit\TestCase
|
||||
{
|
||||
/**
|
||||
* @var NewObject
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
public function setUp()
|
||||
@ -43,9 +24,12 @@ class ObjectTest extends \yiiunit\TestCase
|
||||
|
||||
public function testHasProperty()
|
||||
{
|
||||
$this->assertTrue($this->object->hasProperty('Text'), "Component hasn't property Text");
|
||||
$this->assertTrue($this->object->hasProperty('text'), "Component hasn't property text");
|
||||
$this->assertFalse($this->object->hasProperty('Caption'), "Component as property Caption");
|
||||
$this->assertTrue($this->object->hasProperty('Text'));
|
||||
$this->assertTrue($this->object->hasProperty('text'));
|
||||
$this->assertFalse($this->object->hasProperty('Caption'));
|
||||
$this->assertTrue($this->object->hasProperty('content'));
|
||||
$this->assertFalse($this->object->hasProperty('content', false));
|
||||
$this->assertFalse($this->object->hasProperty('Content'));
|
||||
}
|
||||
|
||||
public function testCanGetProperty()
|
||||
@ -53,13 +37,20 @@ class ObjectTest extends \yiiunit\TestCase
|
||||
$this->assertTrue($this->object->canGetProperty('Text'));
|
||||
$this->assertTrue($this->object->canGetProperty('text'));
|
||||
$this->assertFalse($this->object->canGetProperty('Caption'));
|
||||
$this->assertTrue($this->object->canGetProperty('content'));
|
||||
$this->assertFalse($this->object->canGetProperty('content', false));
|
||||
$this->assertFalse($this->object->canGetProperty('Content'));
|
||||
}
|
||||
|
||||
public function testCanSetProperty()
|
||||
{
|
||||
$this->assertTrue($this->object->canSetProperty('Text'));
|
||||
$this->assertTrue($this->object->canSetProperty('text'));
|
||||
$this->assertFalse($this->object->canSetProperty('Object'));
|
||||
$this->assertFalse($this->object->canSetProperty('Caption'));
|
||||
$this->assertTrue($this->object->canSetProperty('content'));
|
||||
$this->assertFalse($this->object->canSetProperty('content', false));
|
||||
$this->assertFalse($this->object->canSetProperty('Content'));
|
||||
}
|
||||
|
||||
public function testGetProperty()
|
||||
@ -73,8 +64,7 @@ class ObjectTest extends \yiiunit\TestCase
|
||||
{
|
||||
$value = 'new value';
|
||||
$this->object->Text = $value;
|
||||
$text = $this->object->Text;
|
||||
$this->assertTrue($value === $this->object->Text);
|
||||
$this->assertEquals($value, $this->object->Text);
|
||||
$this->setExpectedException('yii\base\BadPropertyException');
|
||||
$this->object->NewMember = $value;
|
||||
}
|
||||
@ -82,15 +72,45 @@ class ObjectTest extends \yiiunit\TestCase
|
||||
public function testIsset()
|
||||
{
|
||||
$this->assertTrue(isset($this->object->Text));
|
||||
$this->assertTrue(!empty($this->object->Text));
|
||||
|
||||
unset($this->object->Text);
|
||||
$this->assertFalse(isset($this->object->Text));
|
||||
$this->assertFalse(!empty($this->object->Text));
|
||||
$this->assertFalse(empty($this->object->Text));
|
||||
|
||||
$this->object->Text = '';
|
||||
$this->assertTrue(isset($this->object->Text));
|
||||
$this->assertTrue(empty($this->object->Text));
|
||||
|
||||
$this->object->Text = null;
|
||||
$this->assertFalse(isset($this->object->Text));
|
||||
$this->assertTrue(empty($this->object->Text));
|
||||
}
|
||||
|
||||
public function testUnset()
|
||||
{
|
||||
unset($this->object->Text);
|
||||
$this->assertFalse(isset($this->object->Text));
|
||||
$this->assertTrue(empty($this->object->Text));
|
||||
}
|
||||
|
||||
public function testArrayProperty()
|
||||
{
|
||||
$this->assertEquals(array(), $this->object->items);
|
||||
// the following won't work
|
||||
/*
|
||||
$this->object->items[] = 1;
|
||||
$this->assertEquals(array(1), $this->object->items);
|
||||
*/
|
||||
}
|
||||
|
||||
public function testObjectProperty()
|
||||
{
|
||||
$this->assertTrue($this->object->object instanceof NewObject);
|
||||
$this->assertEquals('object text', $this->object->object->text);
|
||||
$this->object->object->text = 'new text';
|
||||
$this->assertEquals('new text', $this->object->object->text);
|
||||
}
|
||||
|
||||
public function testAnonymousFunctionProperty()
|
||||
{
|
||||
$this->assertEquals(2, $this->object->execute(1));
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,6 +119,8 @@ class NewObject extends \yii\base\Component
|
||||
{
|
||||
private $_object = null;
|
||||
private $_text = 'default';
|
||||
private $_items = array();
|
||||
public $content;
|
||||
|
||||
public function getText()
|
||||
{
|
||||
@ -119,8 +141,15 @@ class NewObject extends \yii\base\Component
|
||||
return $this->_object;
|
||||
}
|
||||
|
||||
public function exprEvaluator($p1, $comp)
|
||||
public function getExecute()
|
||||
{
|
||||
return "Hello $p1";
|
||||
return function($param) {
|
||||
return $param * 2;
|
||||
};
|
||||
}
|
||||
|
||||
public function getItems()
|
||||
{
|
||||
return $this->_items;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user