From d991f42cbdfc20b92a5e1c4e0ae08d95c3ad39f8 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 3 Dec 2011 15:04:17 -0500 Subject: [PATCH] Use namespace in unit tests. Fixed BehaviorTest.php --- framework/base/Component.php | 3 +- tests/unit/TestCase.php | 10 ++- tests/unit/bootstrap.php | 3 + tests/unit/framework/base/BehaviorTest.php | 14 ++-- tests/unit/framework/base/ComponentTest.php | 15 ++-- tests/unit/framework/base/DictionaryTest.php | 8 +- tests/unit/framework/base/ObjectTest.php | 109 ++++++++++++++++++++++++- tests/unit/framework/base/VectorTest.php | 4 +- tests/unit/framework/db/dao/CommandTest.php | 4 +- tests/unit/framework/db/dao/ConnectionTest.php | 6 +- 10 files changed, 146 insertions(+), 30 deletions(-) diff --git a/framework/base/Component.php b/framework/base/Component.php index 730e685..2ed8d64 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -209,7 +209,8 @@ class Component extends Object if (method_exists($this, $setter)) { // write property return $this->$setter(null); } elseif (method_exists($this, $name) && strncasecmp($name, 'on', 2) === 0) { // event - return unset($this->_e[strtolower($name)]); + unset($this->_e[strtolower($name)]); + return; } elseif (isset($this->_b[$name])) { // behavior return $this->detachBehavior($name); } elseif (is_array($this->_b)) { // behavior property diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index c7826c8..fa77eab 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -1,14 +1,16 @@ params === null) { - $this->params = require(__DIR__ . '/data/config.php'); + if (self::$params === null) { + self::$params = require(__DIR__ . '/data/config.php'); } - return isset($this->params[$name]) ? $this->params[$name] : null; + return isset(self::$params[$name]) ? self::$params[$name] : null; } } \ No newline at end of file diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 5a8aeaa..4dc7f33 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -7,4 +7,7 @@ $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; require_once(__DIR__ . '/../../framework/yii.php'); + +Yii::setAlias('@yiiunit', __DIR__); + require_once(__DIR__ . '/TestCase.php'); \ No newline at end of file diff --git a/tests/unit/framework/base/BehaviorTest.php b/tests/unit/framework/base/BehaviorTest.php index 519653e..b34a94e 100644 --- a/tests/unit/framework/base/BehaviorTest.php +++ b/tests/unit/framework/base/BehaviorTest.php @@ -1,4 +1,7 @@ attachBehavior('bar', $bar); + $bar->attachBehavior('bar', $behavior); $this->assertEquals('behavior property', $bar->behaviorProperty); - $this->assertEquals('behavior method', $bar->behaviorMethod); + $this->assertEquals('behavior method', $bar->behaviorMethod()); $this->assertEquals('behavior property', $bar->bar->behaviorProperty); - $this->assertEquals('behavior method', $bar->bar->behaviorMethod); + $this->assertEquals('behavior method', $bar->bar->behaviorMethod()); } } diff --git a/tests/unit/framework/base/ComponentTest.php b/tests/unit/framework/base/ComponentTest.php index 6327b15..686c097 100644 --- a/tests/unit/framework/base/ComponentTest.php +++ b/tests/unit/framework/base/ComponentTest.php @@ -1,5 +1,7 @@ sender->eventHandled=true; @@ -11,7 +13,7 @@ function globalEventHandler2($event) $event->handled=true; } -class ComponentTest extends TestCase +class ComponentTest extends \yiiunit\TestCase { protected $component; @@ -142,7 +144,7 @@ class ComponentTest extends TestCase { $component=new NewComponent; $this->assertEquals($component->onMyEvent->getCount(),0); - $component->onMyEvent='globalEventHandler'; + $component->onMyEvent='yiiunit\framework\base\globalEventHandler'; $component->onMyEvent=array($this->component,'myEventHandler'); $this->assertEquals($component->onMyEvent->getCount(),2); $this->assertFalse($component->eventHandled); @@ -155,7 +157,7 @@ class ComponentTest extends TestCase public function testStopEvent() { $component=new NewComponent; - $component->onMyEvent='globalEventHandler2'; + $component->onMyEvent='yiiunit\framework\base\globalEventHandler2'; $component->onMyEvent=array($this->component,'myEventHandler'); $component->onMyEvent(); $this->assertTrue($component->eventHandled); @@ -202,13 +204,6 @@ class ComponentTest extends TestCase $this->assertSame($behavior,$component->asa('a')); } - public function testEvaluateExpression() - { - $component = new NewComponent; - $this->assertEquals('Hello world',$component->evaluateExpression('"Hello $who"',array('who' => 'world'))); - $this->assertEquals('Hello world',$component->evaluateExpression(array($component,'exprEvaluator'),array('who' => 'world'))); - } - public function testCreate() { $component = NewComponent2::create(1, 2, array('a'=>3)); diff --git a/tests/unit/framework/base/DictionaryTest.php b/tests/unit/framework/base/DictionaryTest.php index a33e643..10df70c 100644 --- a/tests/unit/framework/base/DictionaryTest.php +++ b/tests/unit/framework/base/DictionaryTest.php @@ -1,5 +1,7 @@ $this->item1, 'k2' => $this->item2, - 'k3' => new ArrayObject(array( + 'k3' => new \ArrayObject(array( 'k4' => $this->item3, )) )); diff --git a/tests/unit/framework/base/ObjectTest.php b/tests/unit/framework/base/ObjectTest.php index d1b71af..7c820a3 100644 --- a/tests/unit/framework/base/ObjectTest.php +++ b/tests/unit/framework/base/ObjectTest.php @@ -1,4 +1,7 @@ object = new NewObject; + } + + public function tearDown() + { + $this->object = null; + } + public function testCreate() { $foo = Foo::create(array( @@ -19,4 +34,96 @@ class ObjectTest extends \yii\test\TestCase $this->assertEquals('test', $foo->prop['test']); } + + 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"); + } + + public function testCanGetProperty() + { + $this->assertTrue($this->object->canGetProperty('Text')); + $this->assertTrue($this->object->canGetProperty('text')); + $this->assertFalse($this->object->canGetProperty('Caption')); + } + + public function testCanSetProperty() + { + $this->assertTrue($this->object->canSetProperty('Text')); + $this->assertTrue($this->object->canSetProperty('text')); + $this->assertFalse($this->object->canSetProperty('Caption')); + } + + public function testGetProperty() + { + $this->assertTrue('default'===$this->object->Text); + $this->setExpectedException('yii\base\Exception'); + $value2=$this->object->Caption; + } + + public function testSetProperty() + { + $value='new value'; + $this->object->Text=$value; + $text=$this->object->Text; + $this->assertTrue($value===$this->object->Text); + $this->setExpectedException('yii\base\Exception'); + $this->object->NewMember=$value; + } + + 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->object->Text=''; + $this->assertTrue(isset($this->object->Text)); + $this->assertTrue(empty($this->object->Text)); + } + + + public function testEvaluateExpression() + { + $object = new NewObject; + $this->assertEquals('Hello world',$object->evaluateExpression('"Hello $who"',array('who' => 'world'))); + $this->assertEquals('Hello world',$object->evaluateExpression(array($object,'exprEvaluator'),array('who' => 'world'))); + } } + + +class NewObject extends \yii\base\Component +{ + private $_object = null; + private $_text = 'default'; + + public function getText() + { + return $this->_text; + } + + public function setText($value) + { + $this->_text=$value; + } + + public function getObject() + { + if(!$this->_object) + { + $this->_object=new self; + $this->_object->_text='object text'; + } + return $this->_object; + } + + public function exprEvaluator($p1,$comp) + { + return "Hello $p1"; + } +} \ No newline at end of file diff --git a/tests/unit/framework/base/VectorTest.php b/tests/unit/framework/base/VectorTest.php index b8bf1a5..6dbbcfa 100644 --- a/tests/unit/framework/base/VectorTest.php +++ b/tests/unit/framework/base/VectorTest.php @@ -1,5 +1,7 @@ open(); $this->assertTrue($connection->active); - $this->assertTrue($connection->pdo instanceof PDO); + $this->assertTrue($connection->pdo instanceof \PDO); $connection->close(); $this->assertFalse($connection->active);