Browse Source

test

tags/2.0.0-beta
Qiang Xue 14 years ago
parent
commit
7106337421
  1. 14
      framework/base/Component.php
  2. 2
      framework/test/TestCase.php
  3. 56
      tests/unit/framework/base/ComponentTest.php

14
framework/base/Component.php

@ -135,7 +135,7 @@ class Component
elseif (method_exists($this, $name) && strncasecmp($name, 'on', 2) === 0) { // event, e.g. onClick()
$name = strtolower($name);
if (!isset($this->_e[$name])) {
$this->_e[$name] = new \yii\collections\Vector;
$this->_e[$name] = new Vector;
}
return $this->_e[$name];
}
@ -149,7 +149,7 @@ class Component
}
}
}
throw new Exception('Getting unknown property: ' . get_class($this) . '.' $name);
throw new Exception('Getting unknown property: ' . get_class($this) . '.' . $name);
}
/**
@ -176,7 +176,7 @@ class Component
elseif (method_exists($this, $name) && strncasecmp($name, 'on', 2) === 0) {
$name = strtolower($name);
if (!isset($this->_e[$name])) {
$this->_e[$name] = new \yii\collections\Vector;
$this->_e[$name] = new Vector;
}
return $this->_e[$name]->add($value);
}
@ -188,10 +188,10 @@ class Component
}
}
if (method_exists($this, 'get' . $name)) {
throw new Exception('Setting read-only property: ' . get_class($this) . '.' $name);
throw new Exception('Setting read-only property: ' . get_class($this) . '.' . $name);
}
else {
throw new Exception('Setting unknown property: ' . get_class($this) . '.' $name);
throw new Exception('Setting unknown property: ' . get_class($this) . '.' . $name);
}
}
@ -269,7 +269,7 @@ class Component
}
}
elseif (method_exists($this, 'get' . $name)) {
throw new Exception('Unsetting read-only property: ' . get_class($this) . '.' $name);
throw new Exception('Unsetting read-only property: ' . get_class($this) . '.' . $name);
}
}
@ -304,7 +304,7 @@ class Component
}
}
}
throw new Exception('Unknown method: ' . get_class($this) . '::' $name . '()');
throw new Exception('Unknown method: ' . get_class($this) . "::$name()");
}
/**

2
framework/test/TestCase.php

@ -7,7 +7,7 @@
* @license http://www.yiiframework.com/license/
*/
namespace yii\test\TestCase;
namespace yii\test;
require_once('PHPUnit/Runner/Version.php');
require_once('PHPUnit/Autoload.php');

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

@ -1,6 +1,4 @@
<?php
require_once dirname(__FILE__) . '/NewComponent.php';
require_once dirname(__FILE__) . '/NewBehavior.php';
function globalEventHandler($event)
{
@ -13,7 +11,7 @@ function globalEventHandler2($event)
$event->handled=true;
}
class ComponentTest extends CTestCase
class ComponentTest extends \yii\test\TestCase
{
protected $component;
@ -133,7 +131,7 @@ class ComponentTest extends CTestCase
{
$this->component->attachEventHandler('OnMyEvent',array($this->component,'myEventHandler'));
$this->assertFalse($this->component->eventHandled);
$this->component->raiseEvent('OnMyEvent',new CEvent($this));
$this->component->raiseEvent('OnMyEvent',new \yii\base\Event($this));
$this->assertTrue($this->component->eventHandled);
//$this->setExpectedException('CException');
@ -221,3 +219,53 @@ class ComponentTest extends CTestCase
$this->assertEquals('Hello world',$component->evaluateExpression(array($component,'exprEvaluator'),array('who' => 'world')));
}
}
class NewComponent extends \yii\base\Component
{
private $_object = null;
private $_text = 'default';
public $eventHandled = false;
public $behaviorCalled = false;
public function getText()
{
return $this->_text;
}
public function setText($value)
{
$this->_text=$value;
}
public function getObject()
{
if(!$this->_object)
{
$this->_object=new NewComponent;
$this->_object->_text='object text';
}
return $this->_object;
}
public function onMyEvent()
{
$this->raiseEvent('OnMyEvent',new \yii\base\Event($this));
}
public function myEventHandler($event)
{
$this->eventHandled=true;
}
public function exprEvaluator($p1,$comp) {
return "Hello $p1";
}
}
class NewBehavior extends \yii\base\Behavior
{
public function test()
{
$this->owner->behaviorCalled=true;
return 2;
}
}

Loading…
Cancel
Save