assertEquals($mibew_api, MibewAPI::getAPI('base')); } /** * @depends testGetAPI */ public function testCheckFunction() { $api = MibewAPI::getAPI('base'); // Wrong function. Function name is absent $wrong_function = array(); // Try to catch MibewAPIException with // MibewAPIException::VARIABLE_IS_UNDEFINED_IN_RESULT code try { $api->checkFunction($wrong_function); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_FUNCTION_NAME, $e->getCode() ); } // Another wrong function. It have reserved name and second argument in // MibewAPI::checkFunction() will be true. $wrong_function = array( 'function' => 'result' ); // Try to catch MibewAPIException with MibewAPIException::FUNCTION_NAME_RESERVED // code try { $api->checkFunction($wrong_function, true); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::FUNCTION_NAME_RESERVED, $e->getCode() ); } // Another wrong function. It have reserved name, but second argument in // MibewAPI::checkFunction() will be false as default. Also it have no 'arguments' // element. $wrong_function = array( 'function' => 'result' ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_ARGUMENTS code try { $api->checkFunction($wrong_function); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_ARGUMENTS, $e->getCode() ); } // Another wrong function. 'arguments' element is an empty array. $wrong_function = array( 'function' => 'wrong_function', 'arguments' => array() ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_ARGUMENTS code try { $api->checkFunction($wrong_function); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_ARGUMENTS, $e->getCode() ); } // Another wrong function. 'arguments' element is not array. $wrong_function = array( 'function' => 'wrong_function', 'arguments' => 'not an array' ); // Try to catch MibewAPIException with MibewAPIException::WRONG_ARGUMENTS_TYPE code try { $api->checkFunction($wrong_function); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::WRONG_ARGUMENTS_TYPE, $e->getCode() ); } // Another wrong function. The obligatary arguments missed. $wrong_function = array( 'function' => 'wrong_function', 'arguments' => array( 'x' => 11 ) ); // Try to catch MibewAPIException with // MibewAPIException::OBLIGATORY_ARGUMENTS_MISSED code try { $api->checkFunction($wrong_function); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::OBLIGATORY_ARGUMENTS_MISSED, $e->getCode() ); } // Another wrong function. Some of the obligatary arguments missed. $wrong_function = array( 'function' => 'wrong_function', 'arguments' => array( 'x' => 11, 'return' => array() ) ); // Try to catch MibewAPIException with // MibewAPIException::OBLIGATORY_ARGUMENTS_MISSED code try { $api->checkFunction($wrong_function); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::OBLIGATORY_ARGUMENTS_MISSED, $e->getCode() ); } // Correct function $correct_function = array( 'function' => 'correct_function', 'arguments' => array( 'return' => array('name' => 'alias'), 'references' => array('x' => 1), 'x' => 'argument_from_first_function', 'argument_key' => 'argument_value' ) ); $api->checkFunction($correct_function); return $correct_function; } /** * @depends testCheckFunction */ public function testCheckRequest($correct_function) { $api = MibewAPI::getAPI('base'); // Wrong request. Its 'token' element is absent. $wrong_request = array(); // Try to catch MibewAPIException with MibewAPIException::EMPTY_TOKEN code try { $api->checkRequest($wrong_request); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_TOKEN, $e->getCode() ); } // Wrong request. Its 'token' element is empty. $wrong_request = array('token' => ''); // Try to catch MibewAPIException with MibewAPIException::EMPTY_TOKEN code try { $api->checkRequest($wrong_request); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_TOKEN, $e->getCode() ); } // Wrong request. Its 'function' element is absent. $wrong_request = array( 'token' => 'some_test_token' ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_FUNCTIONS code try { $api->checkRequest($wrong_request); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_FUNCTIONS, $e->getCode() ); } // Wrong request. Its 'function' element is empty. $wrong_request = array( 'token' => 'some_test_token', 'functions' => array() ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_FUNCTIONS code try { $api->checkRequest($wrong_request); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_FUNCTIONS, $e->getCode() ); } // Correct request $correct_request = array( 'token' => 'some_test_token', 'functions' => array($correct_function, $correct_function) ); $api->checkRequest($correct_request); return $correct_request; } /** * @depends testCheckRequest */ public function testCheckPackage($correct_request) { $api = MibewAPI::getAPI('base'); $trusted_signatures = array('some_trusted_signature'); // Wrong package. Signature element is absent. $wrong_package = array(); // Try to catch MibewAPIException with MibewAPIException::EMPTY_SIGNATURE code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_SIGNATURE, $e->getCode() ); } // Wrong package. Signature is wrong. $wrong_package = array('signature' => 'wrong_signature'); // Try to catch MibewAPIException with MibewAPIException::UNTRUSTED_SIGNATURE code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::UNTRUSTED_SIGNATURE, $e->getCode() ); } // Wrong package. Protocol is absent. $wrong_package = array( 'signature' => 'some_trusted_signature' ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_PROTOCOL code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_PROTOCOL, $e->getCode() ); } // Wrong package. Protocol is empty. $wrong_package = array( 'signature' => 'some_trusted_signature', 'proto' => '' ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_PROTOCOL code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_PROTOCOL, $e->getCode() ); } // Wrong package. Protocol is wrong. $wrong_package = array( 'signature' => 'some_trusted_signature', 'proto' => 'wrong_protocol' ); // Try to catch MibewAPIException with MibewAPIException::WRONG_PROTOCOL_VERSION // code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::WRONG_PROTOCOL_VERSION, $e->getCode() ); } // Wrong package. 'async' flag is absent. $wrong_package = array( 'signature' => 'some_trusted_signature', 'proto' => MibewAPI::PROTOCOL_VERSION ); // Try to catch MibewAPIException with MibewAPIException::ASYNC_FLAG_MISSED code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::ASYNC_FLAG_MISSED, $e->getCode() ); } // Wrong package. 'async' flag is wrong. $wrong_package = array( 'signature' => 'some_trusted_signature', 'proto' => MibewAPI::PROTOCOL_VERSION, 'async' => 'wrong_async_flag' ); // Try to catch MibewAPIException with MibewAPIException::WRONG_ASYNC_FLAG_VALUE // code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::WRONG_ASYNC_FLAG_VALUE, $e->getCode() ); } // Wrong package. Requests is absent. $wrong_package = array( 'signature' => 'some_trusted_signature', 'proto' => MibewAPI::PROTOCOL_VERSION, 'async' => false ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_REQUESTS code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_REQUESTS, $e->getCode() ); } // Wrong package. Requests is empty. $wrong_package = array( 'signature' => 'some_trusted_signature', 'proto' => MibewAPI::PROTOCOL_VERSION, 'async' => false, 'requests' => array() ); // Try to catch MibewAPIException with MibewAPIException::EMPTY_REQUESTS code try { $api->checkPackage($wrong_package, $trusted_signatures); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::EMPTY_REQUESTS, $e->getCode() ); } // Correct package. $correct_package = array( 'signature' => 'some_trusted_signature', 'proto' => MibewAPI::PROTOCOL_VERSION, 'async' => false, 'requests' => array($correct_request, $correct_request) ); $api->checkPackage($correct_package, $trusted_signatures); return $correct_package; } /** * @depends testCheckPackage */ public function testEncodePackage($correct_package) { $api = MibewAPI::getAPI('base'); // Get package values $requests = $correct_package['requests']; $signature = $correct_package['signature']; $async = $correct_package['async']; // Encode package $encoded_package = $api->encodePackage($requests, $signature, $async); $this->assertEquals( urlencode(json_encode($correct_package)), $encoded_package ); return array( 'package' => $correct_package, 'encoded_package' => $encoded_package ); } /** * @depends testEncodePackage */ public function testDecodePackage($vars) { $api = MibewAPI::getAPI('base'); // Try to catch MibewAPIException with MibewAPIException::NOT_VALID_JSON code try { $api->decodePackage( substr( $vars['encoded_package'], // Break package ceil(strlen($vars['encoded_package']) / 2) ), array('some_trusted_signature') ); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::NOT_VALID_JSON, $e->getCode() ); } $this->assertEquals( $vars['package'], $api->decodePackage( $vars['encoded_package'], array('some_trusted_signature') ) ); } /** * @depends testGetAPI */ public function testGetResultFunction() { $api = MibewAPI::getAPI('base'); // Wrong functions list. More than one result function. $functions_list = array( array( 'function' => 'result', 'num' => 1 ), array( 'function' => 'result', 'num' => 2 ) ); // Try to catch MibewAPIException with // MibewAPIException::RESULT_FUNCTION_ALREADY_EXISTS code try { $api->getResultFunction($functions_list, null); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::RESULT_FUNCTION_ALREADY_EXISTS, $e->getCode() ); } // Wrong functions list. Result function not exists, but getResultFunction's second // argument is true $functions_list = array(); // Try to catch MibewAPIException with // MibewAPIException::NO_RESULT_FUNCTION code try { $api->getResultFunction($functions_list, true); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::NO_RESULT_FUNCTION, $e->getCode() ); } // Wrong functions list. Result function exists, but getResultFunction's second // argument is false $functions_list = array( array( 'function' => 'result', 'num' => 1 ) ); // Try to catch MibewAPIException with // MibewAPIException::RESULT_FUNCTION_EXISTS code try { $api->getResultFunction($functions_list, false); $this->fail("Exception must be thrown"); } catch (MibewAPIException $e) { $this->assertEquals( MibewAPIException::RESULT_FUNCTION_EXISTS, $e->getCode() ); } // Correct functions list. Result function not exists and getResultFunction's second // argument is false $functions_list = array(); $this->assertEquals(null, $api->getResultFunction($functions_list, false)); // Correct functions list. Result function exists and getResultFunction's second // argument is true $functions_list = array( array( 'function' => 'result', 'num' => 1 ) ); $this->assertEquals( $functions_list[0], $api->getResultFunction($functions_list, true) ); // Correct functions list. Result function not exists and getResultFunction's second // argument is null $functions_list = array(); $this->assertEquals(null, $api->getResultFunction($functions_list, null)); // Correct functions list. Result function exists and getResultFunction's second // argument is null $functions_list = array( array( 'function' => 'result', 'num' => 1 ) ); $this->assertEquals( $functions_list[0], $api->getResultFunction($functions_list, null) ); } /** * @depends testGetAPI */ public function testBuildResult() { $api = MibewAPI::getAPI('base'); $token = 'some_test_token'; $package = array( 'token' => $token, 'functions' => array( array( 'function' => 'result', 'arguments' => array( 'references' => array(), 'return' => array(), 'test_argument' => 'test_value' ) ) ) ); $this->assertEquals( $package, $api->buildResult($token, array('test_argument' => 'test_value')) ); } } ?>