Browse Source

Use namespace in unit tests.

Fixed BehaviorTest.php
tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
d991f42cbd
  1. 3
      framework/base/Component.php
  2. 10
      tests/unit/TestCase.php
  3. 3
      tests/unit/bootstrap.php
  4. 14
      tests/unit/framework/base/BehaviorTest.php
  5. 15
      tests/unit/framework/base/ComponentTest.php
  6. 8
      tests/unit/framework/base/DictionaryTest.php
  7. 109
      tests/unit/framework/base/ObjectTest.php
  8. 4
      tests/unit/framework/base/VectorTest.php
  9. 4
      tests/unit/framework/db/dao/CommandTest.php
  10. 6
      tests/unit/framework/db/dao/ConnectionTest.php

3
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

10
tests/unit/TestCase.php

@ -1,14 +1,16 @@
<?php
namespace yiiunit;
class TestCase extends \yii\test\TestCase
{
public $params;
public static $params;
function getParam($name)
{
if ($this->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;
}
}

3
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');

14
tests/unit/framework/base/BehaviorTest.php

@ -1,4 +1,7 @@
<?php
namespace yiiunit\framework\base;
class BarClass extends \yii\base\Component
{
@ -14,19 +17,16 @@ class BarBehavior extends \yii\base\Behavior
}
}
/**
* BehaviorTest
*/
class BehaviorTest extends \yii\test\TestCase
class BehaviorTest extends \yiiunit\TestCase
{
public function testAttachAndAccessing()
{
$bar = BarClass::create();
$behavior = new BarBehavior();
$bar->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());
}
}

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

@ -1,5 +1,7 @@
<?php
namespace yiiunit\framework\base;
function globalEventHandler($event)
{
$event->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));

8
tests/unit/framework/base/DictionaryTest.php

@ -1,5 +1,7 @@
<?php
namespace yiiunit\framework\base;
use yii\base\Dictionary;
class MapItem
@ -7,7 +9,7 @@ class MapItem
public $data='data';
}
class DictionaryTest extends TestCase
class DictionaryTest extends \yiiunit\TestCase
{
protected $dictionary;
protected $item1,$item2,$item3;
@ -115,10 +117,10 @@ class DictionaryTest extends TestCase
public function testRecursiveMergeWithTraversable(){
$dictionary = new Dictionary();
$obj = new ArrayObject(array(
$obj = new \ArrayObject(array(
'k1' => $this->item1,
'k2' => $this->item2,
'k3' => new ArrayObject(array(
'k3' => new \ArrayObject(array(
'k4' => $this->item3,
))
));

109
tests/unit/framework/base/ObjectTest.php

@ -1,4 +1,7 @@
<?php
namespace yiiunit\framework\base;
class Foo extends \yii\base\Object
{
public $prop;
@ -7,8 +10,20 @@ class Foo extends \yii\base\Object
/**
* ObjectTest
*/
class ObjectTest extends \yii\test\TestCase
class ObjectTest extends \yiiunit\TestCase
{
protected $object;
public function setUp()
{
$this->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";
}
}

4
tests/unit/framework/base/VectorTest.php

@ -1,5 +1,7 @@
<?php
namespace yiiunit\framework\base;
use yii\base\Vector;
class ListItem
@ -7,7 +9,7 @@ class ListItem
public $data='data';
}
class VectorTest extends TestCase
class VectorTest extends \yiiunit\TestCase
{
protected $vector;
protected $item1, $item2, $item3;

4
tests/unit/framework/db/dao/CommandTest.php

@ -1,10 +1,12 @@
<?php
namespace yiiunit\framework\db\dao;
use yii\db\dao\Connection;
use yii\db\dao\Command;
use yii\db\dao\Query;
class CommandTest extends TestCase
class CommandTest extends \yiiunit\TestCase
{
private $connection;

6
tests/unit/framework/db/dao/ConnectionTest.php

@ -1,8 +1,10 @@
<?php
namespace yiiunit\framework\db\dao;
use yii\db\dao\Connection;
class ConnectionTest extends TestCase
class ConnectionTest extends \yiiunit\TestCase
{
function setUp()
{
@ -29,7 +31,7 @@ class ConnectionTest extends TestCase
$connection->open();
$this->assertTrue($connection->active);
$this->assertTrue($connection->pdo instanceof PDO);
$this->assertTrue($connection->pdo instanceof \PDO);
$connection->close();
$this->assertFalse($connection->active);

Loading…
Cancel
Save