Permalink
Browse files

Merge pull request #416 from elvan/test-ccomponent

Test ccomponent
  • Loading branch information...
2 parents 6d3ce85 + 167080d commit 1cf440fd7eaef33ce3d8a9ffdd4e7724f72baf85 @samdark samdark committed Feb 29, 2012
Showing with 156 additions and 79 deletions.
  1. +156 −79 tests/framework/base/CComponentTest.php
@@ -41,6 +41,8 @@ public function testCanGetProperty()
$this->assertTrue($this->component->canGetProperty('Text'));
$this->assertTrue($this->component->canGetProperty('text'));
$this->assertFalse($this->component->canGetProperty('Caption'));
+ $this->assertTrue($this->component->canGetProperty('Object'));
+ $this->assertTrue($this->component->canGetProperty('object'));
}
public function testCanSetProperty()
@@ -54,48 +56,75 @@ public function testCanSetProperty()
public function testGetProperty()
{
- $this->assertEquals('default', $this->component->Text);
- $this->assertEquals('default', $this->component->text);
+ $this->assertSame('default', $this->component->Text);
+ $this->assertSame('default', $this->component->text);
$this->assertInstanceOf('NewComponent', $this->component->Object);
$this->assertInstanceOf('NewComponent', $this->component->object);
- }
- /**
- * @expectedException CException
- * @expectedExceptionMessage Property "NewComponent.caption" is not defined.
- */
- public function testGetException()
- {
- $this->component->caption;
+ try {
+ $this->component->caption;
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Property "NewComponent.caption" is not defined.', $e->getMessage());
+ }
}
public function testSetProperty()
{
$this->component->text = 'new value';
- $this->assertEquals('new value', $this->component->text);
- }
+ $this->assertSame('new value', $this->component->text);
- /**
- * @expectedException CException
- * @expectedExceptionMessage Property "NewComponent.newMember" is not defined.
- */
- public function testSetException()
- {
- $this->component->newMember = 'new value';
+ try {
+ $this->component->object = new NewComponent();
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Property "NewComponent.object" is read only.', $e->getMessage());
+ }
+
+ try {
+ $this->component->newMember = 'new value';
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Property "NewComponent.newMember" is not defined.', $e->getMessage());
+ }
}
public function testIsset()
{
$this->assertTrue(isset($this->component->Text));
- $this->assertTrue(!empty($this->component->Text));
+ $this->assertFalse(empty($this->component->Text));
+ $this->assertTrue(isset($this->component->object));
+ }
+ public function testUnset()
+ {
unset($this->component->Text);
$this->assertFalse(isset($this->component->Text));
- $this->assertFalse(!empty($this->component->Text));
+ $this->assertTrue(empty($this->component->Text));
- $this->component->Text='';
+ $this->component->Text='new text';
$this->assertTrue(isset($this->component->Text));
- $this->assertTrue(empty($this->component->Text));
+ $this->assertFalse(empty($this->component->Text));
+
+ try {
+ unset($this->component->object);
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Property "NewComponent.object" is read only.', $e->getMessage());
+ }
+ }
+
+ public function testCallMethodFromBehavior()
+ {
+ $this->component->attachBehavior('newBehavior', new NewBehavior);
+ $this->assertSame(2, $this->component->test());
+
+ try {
+ $this->component->otherMethod();
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('NewComponent and its behaviors do not have a method or closure named "otherMethod".', $e->getMessage());
+ }
}
public function testHasEvent()
@@ -115,35 +144,46 @@ public function testHasEventHandler()
public function testGetEventHandlers()
{
$list=$this->component->getEventHandlers('OnMyEvent');
- $this->assertEquals($list->getCount(),0);
+ $this->assertSame($list->getCount(),0);
$this->component->attachEventHandler('OnMyEvent','foo');
- $this->assertEquals($list->getCount(),1);
- $this->setExpectedException('CException');
- $list=$this->component->getEventHandlers('YourEvent');
+ $this->assertSame($list->getCount(),1);
+
+ try {
+ $this->component->getEventHandlers('YourEvent');
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Event "NewComponent.YourEvent" is not defined.', $e->getMessage());
+ }
}
public function testAttachEventHandler()
{
$this->component->attachEventHandler('OnMyEvent','foo');
- $this->assertTrue($this->component->getEventHandlers('OnMyEvent')->getCount()===1);
- $this->setExpectedException('CException');
- $this->component->attachEventHandler('YourEvent','foo');
+ $this->assertInstanceOf('CList', $this->component->getEventHandlers('onMyEvent'));
+ $this->assertSame(1, $this->component->getEventHandlers('onMyEvent')->getCount());
+
+ try {
+ $this->component->attachEventHandler('onYourEvent', 'foo');
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Event "NewComponent.onYourEvent" is not defined.', $e->getMessage());
+ }
}
public function testDettachEventHandler()
{
$this->component->attachEventHandler('OnMyEvent','foo');
$this->component->attachEventHandler('OnMyEvent',array($this->component,'myEventHandler'));
- $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),2);
+ $this->assertSame($this->component->getEventHandlers('OnMyEvent')->getCount(),2);
$this->assertTrue($this->component->detachEventHandler('OnMyEvent','foo'));
- $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),1);
+ $this->assertSame($this->component->getEventHandlers('OnMyEvent')->getCount(),1);
$this->assertFalse($this->component->detachEventHandler('OnMyEvent','foo'));
- $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),1);
+ $this->assertSame($this->component->getEventHandlers('OnMyEvent')->getCount(),1);
$this->assertTrue($this->component->detachEventHandler('OnMyEvent',array($this->component,'myEventHandler')));
- $this->assertEquals($this->component->getEventHandlers('OnMyEvent')->getCount(),0);
+ $this->assertSame($this->component->getEventHandlers('OnMyEvent')->getCount(),0);
$this->assertFalse($this->component->detachEventHandler('OnMyEvent','foo'));
}
@@ -152,20 +192,24 @@ public function testRaiseEvent()
{
$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 CEvent($this->component));
$this->assertTrue($this->component->eventHandled);
- //$this->setExpectedException('CException');
- //$this->component->raiseEvent('OnUnknown',new CEvent($this));
+ try {
+ $this->component->raiseEvent('OnUnknown', new CEvent($this->component));
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Event "NewComponent.onunknown" is not defined.', $e->getMessage());
+ }
}
public function testEventAccessor()
{
$component=new NewComponent;
- $this->assertEquals($component->onMyEvent->getCount(),0);
+ $this->assertSame($component->onMyEvent->getCount(),0);
$component->onMyEvent='globalEventHandler';
$component->onMyEvent=array($this->component,'myEventHandler');
- $this->assertEquals($component->onMyEvent->getCount(),2);
+ $this->assertSame($component->onMyEvent->getCount(),2);
$this->assertFalse($component->eventHandled);
$this->assertFalse($this->component->eventHandled);
$component->onMyEvent();
@@ -185,58 +229,91 @@ public function testStopEvent()
public function testInvalidHandler1()
{
- $this->component->onMyEvent=array(1,2,3);
- $this->setExpectedException('CException');
- $this->component->onMyEvent();
+ $this->component->onMyEvent = array(1, 2, 3);
+
+ try {
+ $this->component->onMyEvent();
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Event "NewComponent.onmyevent" is attached with an invalid handler "array".', $e->getMessage());
+ }
}
public function testInvalidHandler2()
{
- $this->component->onMyEvent=array($this->component,'nullHandler');
- $this->setExpectedException('CException');
- $this->component->onMyEvent();
+ $this->component->onMyEvent = array($this->component, 'nullHandler');
+
+ try {
+ $this->component->onMyEvent();
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('Event "NewComponent.onmyevent" is attached with an invalid handler "nullHandler".', $e->getMessage());
+ }
}
- public function testDetachBehavior() {
- $component=new NewComponent;
- $behavior = new NewBehavior;
- $component->attachBehavior('a',$behavior);
- $this->assertSame($behavior,$component->detachBehavior('a'));
+
+ public function testAttachingBehavior()
+ {
+ $this->component->attachBehavior('newBehavior', 'NewBehavior');
+ $this->assertInstanceOf('NewBehavior', $this->component->newBehavior);
}
- public function testDetachingBehaviors() {
- $component=new NewComponent;
- $behavior = new NewBehavior;
- $component->attachBehavior('a',$behavior);
- $component->detachBehaviors();
- $this->setExpectedException('CException');
- $component->test();
+
+ public function testDetachingBehavior()
+ {
+ $behavior = new NewBehavior;
+ $this->component->attachBehavior('newBehavior', $behavior);
+ $this->assertSame($behavior, $this->component->detachBehavior('newBehavior'));
+ $this->assertFalse(isset($this->component->newBehavior));
}
- public function testEnablingBehavior() {
- $component=new NewComponent;
- $behavior = new NewBehavior;
- $component->attachBehavior('a',$behavior);
- $component->disableBehavior('a');
+
+ public function testDetachingBehaviors()
+ {
+ $behavior = new NewBehavior;
+
+ $this->component->attachBehavior('newBehavior', $behavior);
+ $this->component->detachBehaviors();
+
+ try {
+ $this->component->test();
+ } catch (Exception $e) {
+ $this->assertInstanceOf('CException', $e);
+ $this->assertSame('NewComponent and its behaviors do not have a method or closure named "test".', $e->getMessage());
+ }
+ }
+
+ public function testEnablingBehavior()
+ {
+ $behavior = new NewBehavior;
+
+ $this->component->attachBehavior('newBehavior', $behavior);
+ $this->component->disableBehavior('newBehavior');
$this->assertFalse($behavior->getEnabled());
- $component->enableBehavior('a');
+
+ $this->component->enableBehavior('newBehavior');
$this->assertTrue($behavior->getEnabled());
}
- public function testEnablingBehaviors() {
- $component=new NewComponent;
- $behavior = new NewBehavior;
- $component->attachBehavior('a',$behavior);
- $component->disableBehaviors();
+
+ public function testEnablingBehaviors()
+ {
+ $behavior = new NewBehavior;
+
+ $this->component->attachBehavior('newBehavior', $behavior);
+ $this->component->disableBehaviors();
$this->assertFalse($behavior->getEnabled());
- $component->enableBehaviors();
+
+ $this->component->enableBehaviors();
$this->assertTrue($behavior->getEnabled());
}
- public function testAsa() {
- $component=new NewComponent;
- $behavior = new NewBehavior;
- $component->attachBehavior('a',$behavior);
- $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 testAsa()
+ {
+ $behavior = new NewBehavior;
+ $this->component->attachBehavior('newBehavior', $behavior);
+ $this->assertSame($behavior, $this->component->asa('newBehavior'));
+ }
+
+ public function testEvaluateExpression()
+ {
+ $this->assertSame('Hello world', $this->component->evaluateExpression('"Hello $who"', array('who' => 'world')));
+ $this->assertSame('Hello world', $this->component->evaluateExpression(array($this->component, 'exprEvaluator'), array('who' => 'world')));
}
}

0 comments on commit 1cf440f

Please sign in to comment.