Skip to content

Commit

Permalink
feature(events): Adds static methods for returning common values
Browse files Browse the repository at this point in the history
These functions are particularly useful as handlers for events/hooks.
  • Loading branch information
mrclay committed Jul 2, 2015
1 parent 2dde839 commit f080fed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
48 changes: 48 additions & 0 deletions engine/classes/Elgg/Values.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Elgg;

/**
* Functions for use as plugin hook/event handlers or other situations where you need a
* globally accessible callable.
*/
class Values {

/**
* Return true
*
* @return true
* @since 1.12.0
*/
public static function getTrue() {
return true;
}

/**
* Return false
*
* @return false
* @since 1.12.0
*/
public static function getFalse() {
return false;
}

/**
* Return null
*
* @return null
* @since 1.12.0
*/
public static function getNull() {
}

/**
* Return empty array
*
* @return array
* @since 1.12.0
*/
public static function getArray() {
return [];
}
}
18 changes: 3 additions & 15 deletions engine/tests/phpunit/Elgg/EventsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testTriggerCallsRegisteredHandlersAndReturnsTrue() {
public function testFalseStopsPropagationAndReturnsFalse() {
$events = new \Elgg\EventsService();

$events->registerHandler('foo', 'bar', array('\Elgg\EventsServiceTest', 'returnFalse'));
$events->registerHandler('foo', 'bar', 'Elgg\Values::getFalse');
$events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));

$this->assertFalse($events->trigger('foo', 'bar'));
Expand All @@ -33,7 +33,7 @@ public function testFalseStopsPropagationAndReturnsFalse() {
public function testNullDoesNotStopPropagation() {
$events = new \Elgg\EventsService();

$events->registerHandler('foo', 'bar', array('\Elgg\EventsServiceTest', 'returnNull'));
$events->registerHandler('foo', 'bar', 'Elgg\Values::getNull');
$events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));

$this->assertTrue($events->trigger('foo', 'bar'));
Expand All @@ -43,7 +43,7 @@ public function testNullDoesNotStopPropagation() {
public function testUnstoppableEventsCantBeStoppedAndReturnTrue() {
$events = new \Elgg\EventsService();

$events->registerHandler('foo', 'bar', array('\Elgg\EventsServiceTest', 'returnFalse'));
$events->registerHandler('foo', 'bar', 'Elgg\Values::getFalse');
$events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));

$this->assertTrue($events->trigger('foo', 'bar', null, array(
Expand All @@ -69,17 +69,5 @@ public function incrementCounter() {
$this->counter++;
return true;
}

public static function returnTrue() {
return true;
}

public static function returnFalse() {
return false;
}

public static function returnNull() {
return;
}
}

10 changes: 1 addition & 9 deletions engine/tests/phpunit/Elgg/PluginHooksServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testCanPassParamsAndChangeReturnValue() {

public function testNullReturnDoesntChangeValue() {
$hooks = new \Elgg\PluginHooksService();
$hooks->registerHandler('foo', 'bar', array('\Elgg\PluginHooksServiceTest', 'returnNull'));
$hooks->registerHandler('foo', 'bar', 'Elgg\Values::getNull');

$returnval = $hooks->trigger('foo', 'bar', array(), 1);

Expand All @@ -46,10 +46,6 @@ public function testUncallableHandlersAreLogged() {

$hooks->trigger('foo', 'bar');
}

public static function returnTwo() {
return 2;
}

public static function changeReturn($foo, $bar, $returnval, $params) {
$testCase = $params['testCase'];
Expand All @@ -59,10 +55,6 @@ public static function changeReturn($foo, $bar, $returnval, $params) {
return 2;
}

public static function returnNull() {
return;
}

public static function throwInvalidArg() {
throw new \InvalidArgumentException();
}
Expand Down

0 comments on commit f080fed

Please sign in to comment.