summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/Collections/TListTest.php271
-rw-r--r--tests/unit/TComponentTest.php178
-rw-r--r--tests/unit/phpunit2.php9
3 files changed, 458 insertions, 0 deletions
diff --git a/tests/unit/Collections/TListTest.php b/tests/unit/Collections/TListTest.php
new file mode 100644
index 00000000..3dacb701
--- /dev/null
+++ b/tests/unit/Collections/TListTest.php
@@ -0,0 +1,271 @@
+<?php
+require_once dirname(__FILE__).'/../phpunit2.php';
+
+class ListItem
+{
+ public $data='data';
+}
+
+class NewList extends TList
+{
+ private $_canAddItem=true;
+ private $_canRemoveItem=true;
+ private $_itemAdded=false;
+ private $_itemRemoved=false;
+
+ protected function addedItem($item)
+ {
+ $this->_itemAdded=true;
+ }
+
+ protected function removedItem($item)
+ {
+ $this->_itemRemoved=true;
+ }
+
+ protected function canAddItem($item)
+ {
+ return $this->_canAddItem;
+ }
+
+ protected function canRemoveItem($item)
+ {
+ return $this->_canRemoveItem;
+ }
+
+ public function setCanAddItem($value)
+ {
+ $this->_canAddItem=$value;
+ }
+
+ public function setCanRemoveItem($value)
+ {
+ $this->_canRemoveItem=$value;
+ }
+
+ public function isItemAdded()
+ {
+ return $this->_itemAdded;
+ }
+
+ public function isItemRemoved()
+ {
+ return $this->_itemRemoved;
+ }
+}
+
+/**
+ * @package System.Collections
+ */
+class TListTest extends PHPUnit2_Framework_TestCase {
+ protected $list;
+ protected $item1,$item2,$item3;
+
+ public function setUp() {
+ $this->list=new TList;
+ $this->item1=new ListItem;
+ $this->item2=new ListItem;
+ $this->item3=new ListItem;
+ $this->list->add($this->item1);
+ $this->list->add($this->item2);
+ }
+
+ public function tearDown() {
+ $this->list=null;
+ $this->item1=null;
+ $this->item2=null;
+ $this->item3=null;
+ }
+
+ public function testConstruct() {
+ $a=array(1,2,3);
+ $list=new TList($a);
+ $this->assertEquals(3,$list->getCount());
+ $list2=new TList($this->list);
+ $this->assertEquals(2,$list2->getCount());
+ }
+
+ public function testGetCount() {
+ $this->assertEquals(2,$this->list->getCount());
+ $this->assertEquals(2,$this->list->Count);
+ }
+
+ public function testAdd() {
+ $this->list->add(null);
+ $this->list->add($this->item3);
+ $this->assertEquals(4,$this->list->getCount());
+ $this->assertEquals(3,$this->list->indexOf($this->item3));
+ }
+
+ public function testInsert() {
+ $this->list->insert(0,$this->item3);
+ $this->assertEquals(3,$this->list->getCount());
+ $this->assertEquals(2,$this->list->indexOf($this->item2));
+ $this->assertEquals(0,$this->list->indexOf($this->item3));
+ $this->assertEquals(1,$this->list->indexOf($this->item1));
+ try {
+ $this->list->insert(4,$this->item3);
+ $this->fail('exception not raised when adding item at an out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+
+ }
+ }
+
+ public function testRemove() {
+ $this->list->remove($this->item1);
+ $this->assertEquals(1,$this->list->getCount());
+ $this->assertEquals(-1,$this->list->indexOf($this->item1));
+ $this->assertEquals(0,$this->list->indexOf($this->item2));
+ try {
+ $this->list->remove($this->item1);
+ $this->fail('exception not raised when removing nonexisting item');
+ } catch(Exception $e) {
+
+ }
+ }
+
+ public function testRemoveAt() {
+ $this->list->add($this->item3);
+ $this->list->removeAt(1);
+ $this->assertEquals(-1,$this->list->indexOf($this->item2));
+ $this->assertEquals(1,$this->list->indexOf($this->item3));
+ $this->assertEquals(0,$this->list->indexOf($this->item1));
+ try {
+ $this->list->removeAt(2);
+ $this->fail('exception not raised when removing item with invalid index');
+ } catch(TInvalidDataValueException $e) {
+
+ }
+ }
+
+ public function testClear() {
+ $this->list->clear();
+ $this->assertEquals(0,$this->list->getCount());
+ $this->assertEquals(-1,$this->list->indexOf($this->item1));
+ $this->assertEquals(-1,$this->list->indexOf($this->item2));
+ }
+
+ public function testContains() {
+ $this->assertTrue($this->list->contains($this->item1));
+ $this->assertTrue($this->list->contains($this->item2));
+ $this->assertFalse($this->list->contains($this->item3));
+ }
+
+ public function testIndexOf() {
+ $this->assertEquals(0,$this->list->indexOf($this->item1));
+ $this->assertEquals(1,$this->list->indexOf($this->item2));
+ $this->assertEquals(-1,$this->list->indexOf($this->item3));
+ }
+
+ public function testCopyFrom() {
+ $array=array($this->item3,$this->item1);
+ $this->list->copyFrom($array);
+ $this->assertTrue(count($array)==2 && $this->list[0]===$this->item3 && $this->list[1]===$this->item1);
+ try {
+ $this->list->copyFrom($this);
+ $this->fail('exception not raised when copying from non-traversable object');
+ } catch(TInvalidDataTypeException $e) {
+
+ }
+ }
+
+ public function testMergeWith() {
+ $array=array($this->item3,$this->item1);
+ $this->list->mergeWith($array);
+ $this->assertTrue($this->list->getCount()==4 && $this->list[0]===$this->item1 && $this->list[3]===$this->item1);
+ try {
+ $this->list->mergeWith($this);
+ $this->fail('exception not raised when copying from non-traversable object');
+ } catch(TInvalidDataTypeException $e) {
+
+ }
+ }
+
+ public function testToArray() {
+ $array=$this->list->toArray();
+ $this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
+ }
+
+ public function testArrayRead() {
+ $this->assertTrue($this->list[0]===$this->item1);
+ $this->assertTrue($this->list[1]===$this->item2);
+ try {
+ $a=$this->list[2];
+ $this->fail('exception not raised when accessing item with out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+
+ }
+ }
+
+ /*public function testArrayWrite() {
+ $this->list[]=$this->item3;
+ $this->assertTrue($this->list[2]===$this->item3 && $this->list->getCount()===3);
+ $this->list[0]=$this->item3;
+ $this->assertTrue($this->list[0]===$this->item3 && $this->list->getCount()===3 && $this->list->indexOf($this->item1)===-1);
+ unset($this->list[1]);
+ $this->assertTrue($this->list->getCount()===2 && $this->list->indexOf($this->item2)===-1);
+ try {
+ $this->list[5]=$this->item3;
+ $this->fail('exception not raised when setting item at an out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+
+ }
+ try {
+ unset($this->list[5]);
+ $this->fail('exception not raised when unsetting item at an out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+
+ }
+ }*/
+
+ public function testGetIterator() {
+ $n=0;
+ $found=0;
+ foreach($this->list as $index=>$item) {
+ foreach($this->list as $a=>$b); // test of iterator
+ $n++;
+ if($index===0 && $item===$this->item1)
+ $found++;
+ if($index===1 && $item===$this->item2)
+ $found++;
+ }
+ $this->assertTrue($n==2 && $found==2);
+ }
+
+ public function testArrayMisc() {
+ $this->assertEquals(1,count($this->list));
+ $this->assertTrue(isset($this->list[1]));
+ $this->assertFalse(isset($this->list[2]));
+ }
+
+ public function testDerivedClasses() {
+ $newList=new NewList;
+ $this->assertFalse($newList->isItemAdded());
+ $newList->add($this->item1);
+ $this->assertTrue($newList->isItemAdded());
+ $newList->add($this->item2);
+
+ $newList->setCanAddItem(false);
+ try {
+ $newList->add($this->item3);
+ $this->fail('no exception raised when adding an item that is disallowed');
+ } catch(TInvalidOperationException $e) {
+ $this->assertEquals(2,$newList->getCount());
+ }
+
+ $this->assertFalse($newList->isItemRemoved());
+ $newList->remove($this->item1);
+ $this->assertTrue($newList->isItemRemoved());
+
+ $newList->setCanRemoveItem(false);
+ try {
+ $newList->remove($this->item2);
+ $this->fail('no exception raised when removing an item that is disallowed');
+ } catch(TInvalidOperationException $e) {
+ $this->assertEquals(1,$newList->getCount());
+ }
+ }
+}
+
+
+?> \ No newline at end of file
diff --git a/tests/unit/TComponentTest.php b/tests/unit/TComponentTest.php
new file mode 100644
index 00000000..e62017a7
--- /dev/null
+++ b/tests/unit/TComponentTest.php
@@ -0,0 +1,178 @@
+<?php
+require_once dirname(__FILE__).'/phpunit2.php';
+
+class NewComponent extends TComponent {
+ private $_object = null;
+ private $_text = 'default';
+ private $_eventHandled = 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($param) {
+ $this->raiseEvent('OnMyEvent',$this,$param);
+ }
+
+ public function myEventHandler($sender,$param) {
+ $this->_eventHandled=true;
+ }
+
+ public function isEventHandled() {
+ return $this->_eventHandled;
+ }
+}
+
+/**
+ * @package System
+ */
+class TComponentTest extends PHPUnit2_Framework_TestCase {
+
+ protected $component;
+
+ public function setUp() {
+ $this->component = new NewComponent();
+ }
+
+ public function tearDown() {
+ $this->component = null;
+ }
+
+ public function testHasProperty() {
+ $this->assertTrue($this->component->hasProperty('Text'), "Component hasn't property Text");
+ $this->assertTrue($this->component->hasProperty('text'), "Component hasn't property text");
+ $this->assertFalse($this->component->hasProperty('Caption'), "Component as property Caption");
+ }
+
+ public function testCanGetProperty() {
+ $this->assertTrue($this->component->canGetProperty('Text'));
+ $this->assertTrue($this->component->canGetProperty('text'));
+ $this->assertFalse($this->component->canGetProperty('Caption'));
+ }
+
+ public function testCanSetProperty() {
+ $this->assertTrue($this->component->canSetProperty('Text'));
+ $this->assertTrue($this->component->canSetProperty('text'));
+ $this->assertFalse($this->component->canSetProperty('Caption'));
+ }
+
+ public function testGetProperty() {
+ $this->assertTrue('default'===$this->component->Text);
+ try {
+ $value2=$this->component->Caption;
+ $this->fail('exception not raised when getting undefined property');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testSetProperty() {
+ $value='new value';
+ $this->component->Text=$value;
+ $text=$this->component->Text;
+ $this->assertTrue($value===$this->component->Text);
+ try {
+ $this->component->NewMember=$value;
+ $this->fail('exception not raised when setting undefined property');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testGetSubProperty() {
+ $this->assertTrue('object text'===$this->component->getSubProperty('Object.Text'));
+ }
+
+ public function testSetSubProperty() {
+ $this->component->setSubProperty('Object.Text','new object text');
+ $this->assertEquals('new object text',$this->component->getSubProperty('Object.Text'));
+ }
+
+ public function testHasEvent() {
+ $this->assertTrue($this->component->hasEvent('OnMyEvent'));
+ $this->assertTrue($this->component->hasEvent('onmyevent'));
+ $this->assertFalse($this->component->hasEvent('onYourEvent'));
+ }
+
+ public function testHasEventHandler() {
+ $this->assertFalse($this->component->hasEventHandler('OnMyEvent'));
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->assertTrue($this->component->hasEventHandler('OnMyEvent'));
+ }
+
+ public function testGetEventHandlers() {
+ $list=$this->component->getEventHandlers('OnMyEvent');
+ $this->assertTrue(($list instanceof TList) && ($list->getCount()===0));
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->assertTrue(($list instanceof TList) && ($list->getCount()===1));
+ try {
+ $list=$this->component->getEventHandlers('YourEvent');
+ $this->fail('exception not raised when getting event handlers for undefined event');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testAttachEventHandler() {
+ $this->component->attachEventHandler('OnMyEvent','foo');
+ $this->assertTrue($this->component->getEventHandlers('OnMyEvent')->getCount()===1);
+ try {
+ $this->component->attachEventHandler('YourEvent','foo');
+ $this->fail('exception not raised when attaching event handlers for undefined event');
+ } catch(TInvalidOperationException $e) {
+ }
+ /*$this->component->MyEvent[]='foo2';
+ $this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===2);
+ $this->component->getEventHandlers('MyEvent')->add('foo3');
+ $this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===3);
+ $this->component->MyEvent[0]='foo4';
+ $this->assertTrue($this->component->getEventHandlers('MyEvent')->getCount()===3);
+ $this->component->getEventHandlers('MyEvent')->insert(0,'foo5');
+ $this->assertTrue($this->component->MyEvent->Count===4 && $this->component->MyEvent[0]==='foo5');
+ $this->component->MyEvent='foo6';
+ $this->assertTrue($this->component->MyEvent->Count===5 && $this->component->MyEvent[4]==='foo6');*/
+ }
+
+ public function testRaiseEvent() {
+ $this->component->attachEventHandler('OnMyEvent',array($this->component,'myEventHandler'));
+ $this->assertFalse($this->component->isEventHandled());
+ $this->component->raiseEvent('OnMyEvent',$this,null);
+ $this->assertTrue($this->component->isEventHandled());
+ $this->component->attachEventHandler('OnMyEvent',array($this->component,'Object.myEventHandler'));
+ $this->assertFalse($this->component->Object->isEventHandled());
+ $this->component->raiseEvent('OnMyEvent',$this,null);
+ $this->assertTrue($this->component->Object->isEventHandled());
+ }
+
+ public function testEvaluateExpression() {
+ $expression="1+2";
+ $this->assertTrue(3===$this->component->evaluateExpression($expression));
+ try {
+ $button=$this->component->evaluateExpression('$this->button');
+ $this->fail('exception not raised when evaluating an invalid exception');
+ } catch(Exception $e) {
+ }
+ }
+
+ public function testEvaluateStatements() {
+ $statements='$a="test string"; echo $a;';
+ $this->assertEquals('test string',$this->component->evaluateStatements($statements));
+ try {
+ $statements='$a=new NewComponent; echo $a->button;';
+ $button=$this->component->evaluateStatements($statements);
+ $this->fail('exception not raised when evaluating an invalid statement');
+ } catch(Exception $e) {
+ }
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/unit/phpunit2.php b/tests/unit/phpunit2.php
new file mode 100644
index 00000000..81e85057
--- /dev/null
+++ b/tests/unit/phpunit2.php
@@ -0,0 +1,9 @@
+<?php
+/**
+ * A few common settings for all unit tests.
+ */
+define('PRADO_FRAMEWORK_DIR', dirname(__FILE__).'/../../framework');
+set_include_path(PRADO_FRAMEWORK_DIR.':'.get_include_path());
+require_once PRADO_FRAMEWORK_DIR.'/prado.php';
+require_once 'PHPUnit2/Framework/TestCase.php';
+?> \ No newline at end of file