tray/src/messenger/tests/server_side/webim/libs/classes/RequestProcessorTest.php
2013-03-13 15:32:41 +00:00

463 lines
13 KiB
PHP

<?php
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/event_dispatcher.php';
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/plugin.php';
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/plugin_manager.php';
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/request_processor.php';
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/mibew_api.php';
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/mibew_api_interaction.php';
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/mibew_api_execution_context.php';
require_once dirname(__FILE__) . '/../request_processor_test.php';
require_once dirname(__FILE__) . '/mibew_api_test_interaction.php';
require_once dirname(__FILE__) . '/test_processor.php';
/**
* Test class for RequestProcessor.
* Generated by PHPUnit on 2012-09-21 at 19:05:21.
*/
class RequestProcessorTest extends PHPUnit_Framework_TestCase {
/**
* @var RequestProcessor
*/
protected static $object;
/**
* @var MibewAPI
*/
protected static $mibewAPI;
/**
* @var Request_processor_testPlugin
*/
protected static $plugin;
public static function setUpBeforeClass() {
// Load Mibew API
self::$mibewAPI = MibewAPI::getAPI('MibewAPITestInteraction');
// Initialize TestProcessor object
self::$object = new TestProcessor(array(
'signature' => 'valid_signature',
'trusted_signatures' => array('trusted_signature')
));
// Initialize events listener plugin
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/../../plugins/'));
PluginManager::loadPlugins(
array(
array('name' => 'request_processor_test')
)
);
self::$plugin = PluginManager::getPlugin('request_processor_test');
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {}
public function test__construct() {
// Try to get error with undefined signature
try {
new TestProcessor(
array(
'trusted_signatures' => array('')
)
);
$this->fail('Error must be thrown');
} catch(PHPUnit_Framework_Error $e) {}
// Try to get error with undefined trusted signatures
try {
new TestProcessor(
array('signature' => '')
);
$this->fail('Error must be thrown');
} catch(PHPUnit_Framework_Error $e) {}
// Try to create object
$processor = new TestProcessor(
array(
'signature' => '',
'trusted_signatures' => array(''),
'event_prefix' => '_testProcessor'
)
);
unset($processor);
}
public function testReceiveRequest() {
// Test signature check
// Create request
$package = self::$mibewAPI->encodePackage(
array(
array(
'token' => 'test',
'functions' => array(
array(
'function' => 'test',
'arguments' => array(
'return' => array(),
'references' => array()
)
)
)
)
),
'wrong signature',
false
);
// Process request and check result
$this->assertFalse(self::$object->receiveRequest($package));
// Check plugin call list
$this->assertEquals(array('testRequestError'), self::$plugin->callList);
self::$plugin->callList = array();
// Check error code
$this->assertEquals(MibewAPIException::UNTRUSTED_SIGNATURE, self::$plugin->errorCode);
// Check TestProcessor call list
$this->assertEquals(array(), self::$object->callList);
// Test synchronous request
// Create request
$package = self::$mibewAPI->encodePackage(
array(
array(
'token' => 'sync_call_test',
'functions' => array(
array(
'function' => 'call_func',
'arguments' => array(
'return' => array('processorCall' => 'processorCall', 'pluginCall' => 'pluginCall'),
'references' => array()
)
)
)
)
),
'trusted_signature',
false
);
// Process request and check result
$this->assertTrue(self::$object->receiveRequest($package));
// Check plugin call list
$this->assertEquals(array('testRequestReceived', 'testFunctionCall'), self::$plugin->callList);
self::$plugin->callList = array();
// Check TestProcessor call list
$this->assertEquals(
array(
'processRequest',
'processFunction',
'processorCall',
'sendSyncResponses'
),
self::$object->callList
);
self::$object->callList = array();
// Check response
$this->assertEquals(
array(
array(
'token' => 'sync_call_test',
'functions' => array(
array(
'function' => 'result',
'arguments' => array(
'processorCall' => true,
'pluginCall' => true,
'return' => array(),
'references' => array()
)
)
)
)
),
self::$object->responses
);
self::$object->responses = array();
// Test first asynchronous request (no callbacks and no result function)
// Create request
$package = self::$mibewAPI->encodePackage(
array(
array(
'token' => 'async_call_test',
'functions' => array(
array(
'function' => 'call_func',
'arguments' => array(
'return' => array('processorCall' => 'processorCall', 'pluginCall' => 'pluginCall'),
'references' => array()
)
)
)
)
),
'trusted_signature',
true
);
// Process request and check result
$this->assertTrue(self::$object->receiveRequest($package));
// Check plugin call list
$this->assertEquals(array('testRequestReceived', 'testFunctionCall'), self::$plugin->callList);
self::$plugin->callList = array();
// Check TestProcessor call list
$this->assertEquals(
array(
'loadCallback',
'processRequest',
'processFunction',
'processorCall',
'sendAsyncResponses'
),
self::$object->callList
);
self::$object->callList = array();
// Check response
$this->assertEquals(
array(
array(
'token' => 'async_call_test',
'functions' => array(
array(
'function' => 'result',
'arguments' => array(
'processorCall' => true,
'pluginCall' => true,
'return' => array(),
'references' => array()
)
)
)
)
),
self::$object->responses
);
self::$object->responses = array();
// Test asynchronous request with result function and no callback
// Create request
$package = self::$mibewAPI->encodePackage(
array(
array(
'token' => 'result_only_test',
'functions' => array(
array(
'function' => 'result',
'arguments' => array(
'test_argument' => 'test_value',
'return' => array(),
'references' => array()
)
)
)
)
),
'trusted_signature',
true
);
// Process request and check result
$this->assertTrue(self::$object->receiveRequest($package));
// Check plugin call list
$this->assertEquals(array('testRequestReceived'), self::$plugin->callList);
self::$plugin->callList = array();
// Check TestProcessor call list
$this->assertEquals(array('loadCallback'), self::$object->callList);
self::$object->callList = array();
// Check response
$this->assertEmpty(self::$object->responses);
// Test asynchronous request with callback and no result function
// Create request
$package = self::$mibewAPI->encodePackage(
array(
array(
'token' => 'callback_only_test',
'functions' => array(
array(
'function' => 'not_result',
'arguments' => array(
'test_argument' => 'test_value',
'return' => array(),
'references' => array()
)
)
)
)
),
'trusted_signature',
true
);
// Process request and check result
$this->assertFalse(self::$object->receiveRequest($package));
// Check plugin call list
$this->assertEquals(array('testRequestReceived', 'testRequestError'), self::$plugin->callList);
self::$plugin->callList = array();
// Check error code
$this->assertEquals(MibewAPIException::NO_RESULT_FUNCTION, self::$plugin->errorCode);
// Check TestProcessor call list
$this->assertEquals(array('loadCallback', 'processRequest'), self::$object->callList);
self::$object->callList = array();
// Check response
$this->assertEmpty(self::$object->responses);
// Test asynchronous request with callback and result function
// Create request
$package = self::$mibewAPI->encodePackage(
array(
array(
'token' => 'callback_and_result_test',
'functions' => array(
array(
'function' => 'result',
'arguments' => array(
'first' => true,
'return' => array(),
'references' => array()
)
)
)
)
),
'trusted_signature',
true
);
// Process request and check result
$this->assertTrue(self::$object->receiveRequest($package));
// Check plugin call list
$this->assertEquals(array('testRequestReceived', 'testRequestProcessorCallback'), self::$plugin->callList);
self::$plugin->callList = array();
// Check callback arguments
$this->assertEquals(
array(
'first' => true,
'second' => true, // Set in TestProcessor::loadCallback method
'return' => array(),
'references' => array()
),
self::$plugin->callbackArguments
);
self::$plugin->callbackArguments = array();
// Check TestProcessor call list
$this->assertEquals(array('loadCallback', 'processRequest'), self::$object->callList);
self::$object->callList = array();
// Check response
$this->assertEmpty(self::$object->responses);
}
public function testCall() {
// Check call to not a function
// Call function
$this->assertFalse(self::$object->call('', true));
// Check plugin call list
$this->assertEquals(array('testCallError'), self::$plugin->callList);
self::$plugin->callList = array();
// Check error code
$this->assertEquals(RequestProcessorException::WRONG_ARGUMENTS, self::$plugin->errorCode);
// Check TestProcessor call list
$this->assertEmpty(self::$object->callList);
// Check call to wrong function array. Only empty function's arguments checked. Other errors tested with
// MibewAPI class.
// Specify function
$function = array(
'function' => 'not a function'
);
// Call function
$this->assertFalse(self::$object->call(array($function), true));
// Check plugin call list
$this->assertEquals(array('testCallError'), self::$plugin->callList);
self::$plugin->callList = array();
// Check error code
$this->assertEquals(MibewAPIException::EMPTY_ARGUMENTS, self::$plugin->errorCode);
// Check TestProcessor call list
$this->assertEmpty(self::$object->callList);
// Check asynchronous request
// Specify function
$function = array(
'function' => 'test_func',
'arguments' => array(
'return' => array(),
'references' => array()
)
);
// Call function
$this->assertTrue(
self::$object->call(
array($function),
true,
array('function' => 'callback', 'arguments' => array())
)
);
// Check plugin call list
$this->assertEquals(array(), self::$plugin->callList);
self::$plugin->callList = array();
// Check TestProcessor call list
$this->assertEquals(array('saveCallback', 'sendAsyncRequest'), self::$object->callList);
self::$object->callList = array();
// Check synchronous request with function that have result in response package
// Specify function
$function = array(
'function' => 'return_result',
'arguments' => array(
'return' => array(),
'references' => array()
)
);
// Call function
$this->assertNotEquals(false, self::$object->call(array($function), false));
// Check plugin call list
$this->assertEquals(array('testResponseReceived'), self::$plugin->callList);
self::$plugin->callList = array();
// Check TestProcessor call list
$this->assertEquals(
array('sendSyncRequest', 'processRequest'),
self::$object->callList
);
self::$object->callList = array();
// Check synchronous request with function that have no result in response package
// Specify function
$function = array(
'function' => 'some_test_func',
'arguments' => array(
'return' => array(),
'references' => array()
)
);
// Call function
$this->assertFalse(self::$object->call(array($function), false));
// Check plugin call list
$this->assertEquals(array('testResponseReceived', 'testCallError'), self::$plugin->callList);
self::$plugin->callList = array();
// Check error code
$this->assertEquals(MibewAPIException::NO_RESULT_FUNCTION, self::$plugin->errorCode);
// Check TestProcessor call list
$this->assertEquals(
array('sendSyncRequest', 'processRequest'),
self::$object->callList
);
self::$object->callList = array();
}
}
?>