From 7b6a433ae62177fb8504ea87026f1045908ed113 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 27 Sep 2012 12:04:40 +0000 Subject: [PATCH] Create tests for RequestProcessor class --- .../libs/classes/RequestProcessorTest.php | 462 ++++++++++++++++++ .../webim/libs/classes/test_processor.php | 115 +++++ .../webim/libs/request_processor_test.php | 8 + .../request_processor_test_plugin.php | 73 +++ 4 files changed, 658 insertions(+) create mode 100644 src/messenger/tests/server_side/webim/libs/classes/RequestProcessorTest.php create mode 100644 src/messenger/tests/server_side/webim/libs/classes/test_processor.php create mode 100644 src/messenger/tests/server_side/webim/libs/request_processor_test.php create mode 100644 src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php diff --git a/src/messenger/tests/server_side/webim/libs/classes/RequestProcessorTest.php b/src/messenger/tests/server_side/webim/libs/classes/RequestProcessorTest.php new file mode 100644 index 00000000..b0829c56 --- /dev/null +++ b/src/messenger/tests/server_side/webim/libs/classes/RequestProcessorTest.php @@ -0,0 +1,462 @@ + '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(); + } + +} + +?> diff --git a/src/messenger/tests/server_side/webim/libs/classes/test_processor.php b/src/messenger/tests/server_side/webim/libs/classes/test_processor.php new file mode 100644 index 00000000..f20398d7 --- /dev/null +++ b/src/messenger/tests/server_side/webim/libs/classes/test_processor.php @@ -0,0 +1,115 @@ + '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; + } + } +} + +?> diff --git a/src/messenger/tests/server_side/webim/libs/request_processor_test.php b/src/messenger/tests/server_side/webim/libs/request_processor_test.php new file mode 100644 index 00000000..06feeef0 --- /dev/null +++ b/src/messenger/tests/server_side/webim/libs/request_processor_test.php @@ -0,0 +1,8 @@ +triggerEvent('testRequestProcessorCallback', $arguments); +} + +?> \ No newline at end of file diff --git a/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php b/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php new file mode 100644 index 00000000..b0e31f98 --- /dev/null +++ b/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php @@ -0,0 +1,73 @@ +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; + } +} + +?> \ No newline at end of file