mirror of
https://github.com/Mibew/java.git
synced 2025-01-22 17:40:35 +03:00
Create tests for RequestProcessor class
This commit is contained in:
parent
24ecf02e7b
commit
7b6a433ae6
@ -0,0 +1,462 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
class TestProcessor extends RequestProcessor {
|
||||
|
||||
public $callList = array();
|
||||
|
||||
public $responses = array();
|
||||
|
||||
public function __construct($config = array()) {
|
||||
$config += array(
|
||||
'event_prefix' => 'test'
|
||||
);
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
protected function getMibewAPIInstance() {
|
||||
return MibewAPI::getAPI('MibewAPITestInteraction');
|
||||
}
|
||||
|
||||
protected function processRequest($request, $result_function = null) {
|
||||
array_push($this->callList, 'processRequest');
|
||||
return parent::processRequest($request, $result_function);
|
||||
}
|
||||
|
||||
protected function processFunction($function, MibewAPIExecutionContext &$context) {
|
||||
array_push($this->callList, 'processFunction');
|
||||
return parent::processFunction($function, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Think about callbacks saving
|
||||
*/
|
||||
protected function saveCallback($token, $callback) {
|
||||
array_push($this->callList, 'saveCallback');
|
||||
}
|
||||
|
||||
protected function loadCallback($token) {
|
||||
array_push($this->callList, 'loadCallback');
|
||||
if ($token == 'callback_only_test') {
|
||||
return array('function' => 'time', 'arguments' => array());
|
||||
} elseif($token == 'callback_and_result_test') {
|
||||
return array('function' => 'request_processor_callback', 'arguments' => array('second' => true));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function sendSyncRequest($request) {
|
||||
array_push($this->callList, 'sendSyncRequest');
|
||||
$return_result = false;
|
||||
foreach ($request['functions'] as $function) {
|
||||
if ($function['function'] == 'return_result') {
|
||||
$return_result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($return_result) {
|
||||
return array(
|
||||
'requests' => array(
|
||||
array(
|
||||
'token' => $request['token'],
|
||||
'functions' => array(
|
||||
array(
|
||||
'function' => 'result',
|
||||
'arguments' => array(
|
||||
'some_argument' => 'some_value',
|
||||
'return' => array(),
|
||||
'references' => array()
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
return array(
|
||||
'requests' => array(
|
||||
array(
|
||||
'token' => $request['token'],
|
||||
'functions' => array(
|
||||
array(
|
||||
'function' => 'not_a_result',
|
||||
'arguments' => array(
|
||||
'some_argument' => 'some_value',
|
||||
'return' => array(),
|
||||
'references' => array()
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function sendAsyncRequest($request) {
|
||||
array_push($this->callList, 'sendAsyncRequest');
|
||||
}
|
||||
|
||||
protected function sendSyncResponses($responses) {
|
||||
array_push($this->callList, 'sendSyncResponses');
|
||||
$this->responses = $responses;
|
||||
}
|
||||
|
||||
protected function sendAsyncResponses($responses) {
|
||||
array_push($this->callList, 'sendAsyncResponses');
|
||||
}
|
||||
|
||||
protected function processorCall(&$func) {
|
||||
array_push($this->callList, 'processorCall');
|
||||
if ($func['function'] == 'call_func') {
|
||||
$func['results']['processorCall'] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
function request_processor_callback($arguments) {
|
||||
$dispatcher = EventDispatcher::getInstance();
|
||||
$dispatcher->triggerEvent('testRequestProcessorCallback', $arguments);
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Test plugin for PHPUnit tests
|
||||
*/
|
||||
Class Request_processor_testPlugin extends Plugin{
|
||||
|
||||
public $callList = array();
|
||||
|
||||
public $errorCode = 0;
|
||||
|
||||
public $callbackArguments = array();
|
||||
|
||||
public function __construct() {
|
||||
$this->initialized = true;
|
||||
}
|
||||
|
||||
public function getWeight() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public function registerEvents() {
|
||||
$dispatcher = EventDispatcher::getInstance();
|
||||
$dispatcher->registerEvent('testRequestProcessorCallback');
|
||||
}
|
||||
|
||||
public function registerListeners() {
|
||||
$registered_events = array(
|
||||
'testRequestReceived',
|
||||
'testRequestError',
|
||||
'testResponseReceived',
|
||||
'testCallError',
|
||||
'testFunctionCall',
|
||||
'testRequestProcessorCallback'
|
||||
);
|
||||
$dispatcher = EventDispatcher::getInstance();
|
||||
foreach ($registered_events as $event) {
|
||||
$dispatcher->attachListener($event, $this, $event);
|
||||
}
|
||||
}
|
||||
|
||||
public function testRequestReceived(&$arguments) {
|
||||
array_push($this->callList, 'testRequestReceived');
|
||||
}
|
||||
|
||||
public function testRequestError(&$arguments) {
|
||||
array_push($this->callList, 'testRequestError');
|
||||
$this->errorCode = $arguments['exception']->getCode();
|
||||
}
|
||||
|
||||
public function testResponseReceived(&$arguments) {
|
||||
array_push($this->callList, 'testResponseReceived');
|
||||
}
|
||||
|
||||
public function testCallError(&$arguments) {
|
||||
array_push($this->callList, 'testCallError');
|
||||
$this->errorCode = $arguments['exception']->getCode();
|
||||
}
|
||||
|
||||
public function testFunctionCall(&$arguments) {
|
||||
array_push($this->callList, 'testFunctionCall');
|
||||
if ($arguments['function'] == 'call_func') {
|
||||
$arguments['results']['pluginCall'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function testRequestProcessorCallback(&$arguments) {
|
||||
array_push($this->callList, 'testRequestProcessorCallback');
|
||||
$this->callbackArguments = $arguments;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user