Commit faef457f by Qiang Xue

Fixed Object class.

parent ac9b3159
...@@ -107,7 +107,6 @@ class Object ...@@ -107,7 +107,6 @@ class Object
{ {
$getter = 'get' . $name; $getter = 'get' . $name;
if (method_exists($this, $getter)) { if (method_exists($this, $getter)) {
// property is not null
return $this->$getter() !== null; return $this->$getter() !== null;
} else { } else {
return false; return false;
...@@ -129,7 +128,6 @@ class Object ...@@ -129,7 +128,6 @@ class Object
{ {
$setter = 'set' . $name; $setter = 'set' . $name;
if (method_exists($this, $setter)) { if (method_exists($this, $setter)) {
// write property
$this->$setter(null); $this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) { } elseif (method_exists($this, 'get' . $name)) {
throw new BadPropertyException('Unsetting read-only property: ' . get_class($this) . '.' . $name); throw new BadPropertyException('Unsetting read-only property: ' . get_class($this) . '.' . $name);
...@@ -150,9 +148,9 @@ class Object ...@@ -150,9 +148,9 @@ class Object
*/ */
public function __call($name, $params) public function __call($name, $params)
{ {
if ($this->canGetProperty($name, false)) {
$getter = 'get' . $name; $getter = 'get' . $name;
$func = $this->$getter; if (method_exists($this, $getter)) {
$func = $this->$getter();
if ($func instanceof \Closure) { if ($func instanceof \Closure) {
return call_user_func_array($func, $params); return call_user_func_array($func, $params);
} }
......
...@@ -2,33 +2,14 @@ ...@@ -2,33 +2,14 @@
namespace yiiunit\framework\base; 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 * ObjectTest
*/ */
class ObjectTest extends \yiiunit\TestCase class ObjectTest extends \yiiunit\TestCase
{ {
/**
* @var NewObject
*/
protected $object; protected $object;
public function setUp() public function setUp()
...@@ -43,9 +24,12 @@ class ObjectTest extends \yiiunit\TestCase ...@@ -43,9 +24,12 @@ class ObjectTest extends \yiiunit\TestCase
public function testHasProperty() public function testHasProperty()
{ {
$this->assertTrue($this->object->hasProperty('Text'), "Component hasn't property Text"); $this->assertTrue($this->object->hasProperty('Text'));
$this->assertTrue($this->object->hasProperty('text'), "Component hasn't property text"); $this->assertTrue($this->object->hasProperty('text'));
$this->assertFalse($this->object->hasProperty('Caption'), "Component as property Caption"); $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() public function testCanGetProperty()
...@@ -53,13 +37,20 @@ class ObjectTest extends \yiiunit\TestCase ...@@ -53,13 +37,20 @@ class ObjectTest extends \yiiunit\TestCase
$this->assertTrue($this->object->canGetProperty('Text')); $this->assertTrue($this->object->canGetProperty('Text'));
$this->assertTrue($this->object->canGetProperty('text')); $this->assertTrue($this->object->canGetProperty('text'));
$this->assertFalse($this->object->canGetProperty('Caption')); $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() public function testCanSetProperty()
{ {
$this->assertTrue($this->object->canSetProperty('Text')); $this->assertTrue($this->object->canSetProperty('Text'));
$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->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() public function testGetProperty()
...@@ -73,8 +64,7 @@ class ObjectTest extends \yiiunit\TestCase ...@@ -73,8 +64,7 @@ class ObjectTest extends \yiiunit\TestCase
{ {
$value = 'new value'; $value = 'new value';
$this->object->Text = $value; $this->object->Text = $value;
$text = $this->object->Text; $this->assertEquals($value, $this->object->Text);
$this->assertTrue($value === $this->object->Text);
$this->setExpectedException('yii\base\BadPropertyException'); $this->setExpectedException('yii\base\BadPropertyException');
$this->object->NewMember = $value; $this->object->NewMember = $value;
} }
...@@ -82,15 +72,45 @@ class ObjectTest extends \yiiunit\TestCase ...@@ -82,15 +72,45 @@ class ObjectTest extends \yiiunit\TestCase
public function testIsset() public function testIsset()
{ {
$this->assertTrue(isset($this->object->Text)); $this->assertTrue(isset($this->object->Text));
$this->assertTrue(!empty($this->object->Text)); $this->assertFalse(empty($this->object->Text));
unset($this->object->Text);
$this->assertFalse(isset($this->object->Text));
$this->assertFalse(!empty($this->object->Text));
$this->object->Text = ''; $this->object->Text = '';
$this->assertTrue(isset($this->object->Text)); $this->assertTrue(isset($this->object->Text));
$this->assertTrue(empty($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 ...@@ -99,6 +119,8 @@ class NewObject extends \yii\base\Component
{ {
private $_object = null; private $_object = null;
private $_text = 'default'; private $_text = 'default';
private $_items = array();
public $content;
public function getText() public function getText()
{ {
...@@ -119,8 +141,15 @@ class NewObject extends \yii\base\Component ...@@ -119,8 +141,15 @@ class NewObject extends \yii\base\Component
return $this->_object; return $this->_object;
} }
public function exprEvaluator($p1, $comp) public function getExecute()
{
return function($param) {
return $param * 2;
};
}
public function getItems()
{ {
return "Hello $p1"; return $this->_items;
} }
} }
\ No newline at end of file
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