diff --git a/framework/base/Object.php b/framework/base/Object.php index 1811986..a26a3b9 100644 --- a/framework/base/Object.php +++ b/framework/base/Object.php @@ -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); } diff --git a/tests/unit/framework/base/ObjectTest.php b/tests/unit/framework/base/ObjectTest.php index 99e5384..b7731bc 100644 --- a/tests/unit/framework/base/ObjectTest.php +++ b/tests/unit/framework/base/ObjectTest.php @@ -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 function($param) { + return $param * 2; + }; + } + + public function getItems() { - return "Hello $p1"; + return $this->_items; } } \ No newline at end of file