Skip to content

Commit

Permalink
Allow callback to be a string for backwards compatibility & added tes…
Browse files Browse the repository at this point in the history
…t cases.
  • Loading branch information
Daniel Abramowitz committed Dec 24, 2013
1 parent b36adb5 commit e0fc71f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
7 changes: 2 additions & 5 deletions src/php/events/WhatsAppEventListenerLegacyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ class WhatsAppEventListenerLegacyAdapter extends WhatsAppEventListenerProxy {
protected $eventName;
/**
*
* @var callable The callback when the event is fired.
* @var string or callable: The callback when the event is fired.
*/
protected $callback;


/**
* Constructor.
*
* @param string $event
* To be removed.
*/
function __construct($eventName, callable $callback)
function __construct($eventName, $callback)
{
$this->eventName = $eventName;
$this->callback = $callback;
Expand Down
84 changes: 70 additions & 14 deletions tests/WhatAppEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
$w->eventManager()->addEventListener($listener);

// Old event bindings:
$old_function_called = array();
$old_function1_called = array();
$old_function2_called = array();

// Function bound to variable:
$oldFunction = function(
$phone,
$from,
Expand All @@ -20,8 +23,34 @@
$time,
$name,
$message
) use (&$old_function_called) {
array_push($old_function_called, array(
) use (&$old_function1_called) {
array_push($old_function1_called, array(
'onGetMessage',
array(
$phone,
$from,
$msgid,
$type,
$time,
$name,
$message
)
) );
};

// Named funciton:
function oldFunction2(
$phone,
$from,
$msgid,
$type,
$time,
$name,
$message
) {
global $old_function2_called;

array_push($old_function2_called, array(
'onGetMessage',
array(
$phone,
Expand All @@ -34,7 +63,11 @@
)
) );
};

// Callback approach:
$w->eventManager()->bind('onGetMessage', $oldFunction );
// String approach:
$w->eventManager()->bind('onGetMessage', 'oldFunction2' );

print( "Test #1: onGetMessage\n" );
/*
Expand Down Expand Up @@ -95,13 +128,33 @@

// Analyze the results:
if( $expected === $actual
&& $old_expected === $old_function_called ) {
print( "Test Passed.\n");
&& $old_expected === $old_function1_called
&& $old_expected === $old_function2_called ) {
print( "Test 1 Passed.\n");
} else {
print( "Test Failed!!!!!\n" );
print( "Test 1 Failed!!!!!\n" );
}
$old_function_called = array();
$old_function1_called = array();
$old_function2_called = array();

$expected = array(
//First event raised:
array(
// Event name:
'onGetMessage',
// Event Arguments:
array(
'$username',
'441234123456',
'1234567890-123',
'chat',
'1234567890',
'First LastName',
'TestMessage'
)
)
);
$old_expected = $expected;

print( "Test #2: Legacy Fire\n" );
$w->eventManager()->fire('onGetMessage', array(
Expand All @@ -113,15 +166,18 @@
'First LastName',
'TestMessage'
) );

// Analyze the results:
$actual = $listener->getAndResetCapture();
if( $old_expected === $actual
&& $old_expected === $old_function_called ) {
print( "Test Passed.\n");
if( $expected === $actual
&& $old_expected === $old_function1_called
&& $old_expected === $old_function2_called ) {
print( "Test 2 Passed.\n");
} else {
print( "Test Failed!!!!!\n" );
print( "Test 2 Failed!!!!!\n" );
}
$old_function_called = array();
$old_function1_called = array();
$old_function2_called = array();


print( "Test #3: onGetGroupMessage\n" );
Expand Down Expand Up @@ -179,9 +235,9 @@
unset($actual[0][1][1]); // Remove the time.

if( $expected === $actual ) {
print( "Test Passed.\n");
print( "Test 3 Passed.\n");
} else {
print( "Test Failed!!!!!\n" );
print( "Test 3 Failed!!!!!\n" );
}


Expand Down

0 comments on commit e0fc71f

Please sign in to comment.