[limb-svn] r6805 - in 3.x/trunk/limb/core: src tests/cases
svn at limb-project.com
svn at limb-project.com
Tue Feb 26 12:05:08 MSK 2008
Author: cmz
Date: 2008-02-26 12:05:08 +0300 (Tue, 26 Feb 2008)
New Revision: 6805
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6805
Added:
3.x/trunk/limb/core/src/lmbEvent.class.php
3.x/trunk/limb/core/tests/cases/lmbEventTest.class.php
Modified:
3.x/trunk/limb/core/src/lmbDelegate.class.php
3.x/trunk/limb/core/tests/cases/lmbDelegateTest.class.php
Log:
-- added lmbEvent class to 'core' package
-- added method lmbDelegate::equal
Modified: 3.x/trunk/limb/core/src/lmbDelegate.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbDelegate.class.php 2008-02-22 14:31:16 UTC (rev 6804)
+++ 3.x/trunk/limb/core/src/lmbDelegate.class.php 2008-02-26 09:05:08 UTC (rev 6805)
@@ -77,6 +77,30 @@
return $this->is_valid;
$this->is_valid = is_callable($this->php_callback);
return $this->is_valid;
+ }
+
+ function equal($delegate)
+ {
+ $delegate = self::objectify($delegate);
+ if(!$this->isValid() || !$delegate->isValid())
+ return false;
+
+ $callback1 = $this->getCallback();
+ $callback2 = $delegate->getCallback();
+
+ $array_cb1 = is_array($callback1);
+ $array_cb2 = is_array($callback2);
+ if($array_cb1 != $array_cb2)
+ return false;
+
+ if($array_cb1)
+ {
+ return $callback1[0] === $callback2[0] && $callback1[1] == $callback2[1];
+ }
+ else
+ {
+ return $callback1 == $callback2;
+ }
}
static function objectify($delegate)
Property changes on: 3.x/trunk/limb/core/src/lmbDelegate.class.php
___________________________________________________________________
Name: svn:keywords
+ Id
Added: 3.x/trunk/limb/core/src/lmbEvent.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbEvent.class.php (rev 0)
+++ 3.x/trunk/limb/core/src/lmbEvent.class.php 2008-02-26 09:05:08 UTC (rev 6805)
@@ -0,0 +1,95 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2008 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/core/src/lmbDelegate.class.php');
+
+/**
+ * Event object. In fact it's a collection of delegates
+ *
+ * @package core
+ * @version $Id$
+ */
+class lmbEvent
+{
+
+ /**
+ * Array of delegates
+ *
+ * @var array
+ */
+ protected $delegates = array();
+
+ /**
+ * Find a delegate added to event
+ *
+ * @param mixed $delegate finding delegate
+ * @return mixed number of delegate or false if delegate is not found
+ */
+ function find($delegate)
+ {
+ $delegate = lmbDelegate::objectify($delegate);
+ foreach($this->delegates as $n => $dlg)
+ {
+ if($delegate->equal($dlg))
+ return $n;
+ }
+ return false;
+ }
+
+ /**
+ * Return true if delegate was added to the event already
+ *
+ * @param mixed $delegete
+ * @return boolean
+ */
+ function exists($delegete)
+ {
+ return $this->find($delegete) !== false;
+ }
+
+ /**
+ * Add a delegate to be invoked
+ *
+ * @param mixed $delegete
+ */
+ function add($delegete)
+ {
+ $this->delegates[] = lmbDelegate::objectify($delegete);
+ }
+
+ /**
+ * Remove delegate from the event object
+ *
+ * @param mixed $delegete
+ */
+ function remove($delegete)
+ {
+ if(($num = $this->find($delegete)) !== false)
+ unset($this->delegates[$num]);
+ }
+
+ /**
+ * Invoke all delegates containing in the event object.
+ * Stops invoking if delegate return a not null result
+ *
+ */
+ function invokeAll()
+ {
+ $args = func_get_args();
+ lmbDelegate::invokeAll($this->delegates, $args);
+ }
+
+ function invokeChain()
+ {
+ $args = func_get_args();
+ return lmbDelegate::invokeChain($this->delegates, $args);
+ }
+
+}
+?>
\ No newline at end of file
Property changes on: 3.x/trunk/limb/core/src/lmbEvent.class.php
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: 3.x/trunk/limb/core/tests/cases/lmbDelegateTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbDelegateTest.class.php 2008-02-22 14:31:16 UTC (rev 6804)
+++ 3.x/trunk/limb/core/tests/cases/lmbDelegateTest.class.php 2008-02-26 09:05:08 UTC (rev 6805)
@@ -181,6 +181,25 @@
$this->assertTrue($s2->instance_called);
$this->assertFalse($s3->instance_called);
$this->assertNull($s3->instance_arg);
+ }
+
+ function testEqual()
+ {
+ $s1 = new DelegateTestingStub();
+ $s2 = new DelegateTestingStub();
+
+ $d1 = new lmbDelegate($s1, 'instanceMethod');
+ $d2 = new lmbDelegate($s2, 'instanceReturningMethod');
+ $d3 = new lmbDelegate($s1, 'instanceMethod');
+ $d4 = new lmbDelegate($s1, 'instanceReturningMethod');
+ $d5 = new lmbDelegate('DelegateTestingStubFunction');
+ $d6 = new lmbDelegate('DelegateTestingStubFunction');
+
+ $this->assertFalse($d1->equal($d2));
+ $this->assertTrue($d1->equal($d3));
+ $this->assertFalse($d1->equal($d4));
+ $this->assertFalse($d1->equal($d6));
+ $this->assertTrue($d5->equal($d6));
}
}
Property changes on: 3.x/trunk/limb/core/tests/cases/lmbDelegateTest.class.php
___________________________________________________________________
Name: svn:keywords
+ Id
Added: 3.x/trunk/limb/core/tests/cases/lmbEventTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbEventTest.class.php (rev 0)
+++ 3.x/trunk/limb/core/tests/cases/lmbEventTest.class.php 2008-02-26 09:05:08 UTC (rev 6805)
@@ -0,0 +1,135 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2008 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/core/src/lmbEvent.class.php');
+
+class EventTestingStubObject
+{
+
+ public $invoked = 0;
+ public $last_arg = null;
+ public $last_arg2 = null;
+
+ protected $return;
+
+ function __construct($return = 'invoked')
+ {
+ $this->return = $return;
+ }
+
+ function invokable($arg = null, $arg2 = null)
+ {
+ $this->invoked ++;
+ $this->last_arg = $arg;
+ $this->last_arg2 = $arg2;
+ return $this->return;
+ }
+
+}
+
+class lmbEventTest extends UnitTestCase {
+
+ function testInvokeAll()
+ {
+ $obj1 = new EventTestingStubObject();
+ $obj2 = new EventTestingStubObject();
+
+ $event = new lmbEvent();
+ $event->add(new lmbDelegate($obj1, 'invokable'));
+ $event->add(new lmbDelegate($obj2, 'invokable'));
+
+ $event->invokeAll('invoked');
+ $this->assertEqual($obj1->invoked, 1);
+ $this->assertEqual($obj2->invoked, 1);
+ $this->assertEqual($obj1->last_arg, 'invoked');
+ $this->assertEqual($obj2->last_arg, 'invoked');
+ }
+
+ function testInvokeChain()
+ {
+ $obj1 = new EventTestingStubObject(null);
+ $obj2 = new EventTestingStubObject('result');
+ $obj3 = new EventTestingStubObject();
+
+ $event = new lmbEvent();
+ $event->add(new lmbDelegate($obj1, 'invokable'));
+ $event->add(new lmbDelegate($obj2, 'invokable'));
+ $event->add(new lmbDelegate($obj3, 'invokable'));
+
+ $result = $event->invokeChain('invoked');
+ $this->assertEqual($result, 'result');
+ $this->assertEqual($obj1->invoked, 1);
+ $this->assertEqual($obj2->invoked, 1);
+ $this->assertEqual($obj3->invoked, 0);
+ $this->assertEqual($obj1->last_arg, 'invoked');
+ $this->assertEqual($obj2->last_arg, 'invoked');
+ }
+
+ function testFind()
+ {
+ $obj1 = new EventTestingStubObject();
+ $obj2 = new EventTestingStubObject();
+
+ $event = new lmbEvent();
+ $event->add(new lmbDelegate($obj1, 'invokable'));
+ $event->add(new lmbDelegate($obj2, 'invokable'));
+
+ $num = $event->find(array($obj2, 'invokable'));
+ $this->assertEqual($num, 1);
+
+ $num = $event->find(array(new EventTestingStubObject(), 'invokable'));
+ $this->assertFalse($num);
+ }
+
+ function testExists()
+ {
+ $obj1 = new EventTestingStubObject();
+ $obj2 = new EventTestingStubObject();
+
+ $event = new lmbEvent();
+ $event->add(new lmbDelegate($obj1, 'invokable'));
+ $event->add(new lmbDelegate($obj2, 'invokable'));
+
+ $result = $event->exists(array($obj2, 'invokable'));
+ $this->assertTrue($result);
+
+ $result = $event->exists(array(new EventTestingStubObject(), 'invokable'));
+ $this->assertFalse($result);
+ }
+
+ function testRemove()
+ {
+ $obj1 = new EventTestingStubObject();
+ $obj2 = new EventTestingStubObject();
+
+ $event = new lmbEvent();
+ $event->add(new lmbDelegate($obj1, 'invokable'));
+ $event->add(new lmbDelegate($obj2, 'invokable'));
+
+ $event->invokeAll();
+ $event->remove(new lmbDelegate($obj2, 'invokable'));
+ $event->invokeAll();
+ $this->assertEqual($obj1->invoked, 2);
+ $this->assertEqual($obj2->invoked, 1);
+ }
+
+ function testPassingInvokeArgs()
+ {
+ $obj = new EventTestingStubObject();
+
+ $event = new lmbEvent();
+ $event->add(new lmbDelegate($obj, 'invokable'));
+
+ $event->invokeAll('arg1', 'arg2');
+ $this->assertEqual($obj->last_arg, 'arg1');
+ $this->assertEqual($obj->last_arg2, 'arg2');
+ }
+
+}
+?>
\ No newline at end of file
Property changes on: 3.x/trunk/limb/core/tests/cases/lmbEventTest.class.php
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the limb-svn
mailing list