private function getResultForMethod($method, $data = array()) { $rawPostData = json_encode(array('action' => 'DebugException', 'method' => $method, 'tid' => 1, 'type' => 'rpc', 'data' => $data)); $dispatcher = new BanchaDispatcher(); $responses = json_decode($dispatcher->dispatch(new BanchaRequestCollection($rawPostData), array('return' => true))); return $responses; }
/** * Tests the exception handling. * */ public function testExceptions() { Configure::write('debug', 2); // Create some requests. $rawPostData = json_encode(array(array('action' => 'ThisControllerDoesNotExist', 'method' => 'index', 'tid' => 1, 'type' => 'rpc', 'data' => array()), array('action' => 'ArticlesException', 'method' => 'throwNotFoundExceptionMethod', 'tid' => 2, 'type' => 'rpc', 'data' => array()), array('action' => 'ArticlesException', 'method' => 'throwExceptionMethod', 'tid' => 3, 'type' => 'rpc', 'data' => array()))); // Create dispatcher and dispatch requests. $dispatcher = new BanchaDispatcher(); $responses = json_decode($dispatcher->dispatch(new BanchaRequestCollection($rawPostData), array('return' => true))); // set debug level back to normal Configure::write('debug', $this->standardDebugLevel); $this->assertEquals(3, count($responses), 'Three requests result in three responses.'); // controller class not found $this->assertEquals('exception', $responses[0]->type); $this->assertEquals('MissingControllerException', $responses[0]->exceptionType); $this->assertEquals('Controller class ThisControllerDoesNotExistsController could not be found.', $responses[0]->message); $this->assertTrue(!empty($responses[0]->where), 'message'); // application controller method exception catched $this->assertEquals('exception', $responses[1]->type); $this->assertEquals('NotFoundException', $responses[1]->exceptionType); $this->assertEquals('Invalid article', $responses[1]->message); // another application level error $this->assertEquals('exception', $responses[2]->type); $this->assertEquals('Exception', $responses[2]->exceptionType); $this->assertEquals('Method specific error message, see bottom of this test', $responses[2]->message); $this->assertEquals('In file "' . __FILE__ . '" on line ' . $GLOBALS['EXCEPTION_LINE'] . '.', $responses[2]->where, 'message'); }
/** * Tests the dispatch() method of BanchaDispatcher without the 'return'-option. Thus dispatch() sends the response * directly to the browser. We need to capture the output to test it. * */ public function testDispatchWithoutReturn() { $this->markTestSkipped("When executing the AllTests TestSuite the PHPUnit output buffers break this test."); $rawPostData = json_encode(array(array('action' => 'Test', 'method' => 'testaction1', 'data' => null, 'type' => 'rpc', 'tid' => 1), array('action' => 'Test', 'method' => 'testaction2', 'data' => null, 'type' => 'rpc', 'tid' => 2))); $collection = new BanchaRequestCollection($rawPostData); $dispatcher = new BanchaDispatcher(); // if there is already a buffer, save the current result $cakeTestBuffer = ob_get_clean(); ob_start(); // capture output, because we want to test without return $dispatcher->dispatch($collection); $responses = json_decode(ob_get_clean()); // ob_end_clean() does not restore the Content-Type, but we do not want to send the header in CLI mode. if (isset($_SERVER['HTTP_HOST'])) { header("Content-Type: text/html; charset=utf-8"); } // if there was a buffer, refill it as before if ($cakeTestBuffer != FALSE) { ob_start(); echo $cakeTestBuffer; } $this->assertEquals('Hello World!', $responses[0]->result->text); $this->assertEquals('foobar', $responses[1]->result->text); }
/** * This tests ensures that two requests, which are sent in the same batch request are executed in order of their * transaction IDs. Therefore the order is wrong in the input data and the test ensures that their order is correct * in the output data. * */ public function testEditEditOneRequest() { $this->markTestSkipped("Consistancy is not yet implemented."); // Preparation: create article $article = new Article(); $article->create(); $article->save(array('title' => 'foo')); $dispatcher = new BanchaDispatcher(); $client_id = uniqid(); $article_id = $article->id; // test $rawPostData = json_encode(array(array('action' => 'Article', 'method' => 'update', 'tid' => 2, 'type' => 'rpc', 'data' => array(array('data' => array('__bcid' => $client_id, 'id' => $article_id, 'title' => 'foobar', 'published' => true)))), array('action' => 'Article', 'method' => 'update', 'tid' => 1, 'type' => 'rpc', 'data' => array(array('data' => array('__bcid' => $client_id, 'id' => $article_id, 'title' => 'barfoo', 'published' => true)))))); $responses = json_decode($dispatcher->dispatch(new BanchaRequestCollection($rawPostData), array('return' => true))); // check that both requests got executed successfully $this->assertEquals('barfoo', $responses[0]->result->data->title); $this->assertEquals('foobar', $responses[1]->result->data->title); // check that record was changed correctly $article = ClassRegistry::init('Article'); $data = $article->read(null, $article_id); $this->assertEquals('foobar', $data['Article']['title']); ClassRegistry::flush(); // clean data $article->delete($article_id); }
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib'); define('WWW_ROOT', ROOT . DS . APP_DIR . 'webroot'); define('WEBROOT_DIR', basename(WWW_ROOT)); define('APP_PATH', ROOT . DS . APP_DIR . DS); define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS); include CORE_PATH . 'Cake' . DS . 'bootstrap.php'; include ROOT . DS . 'plugins' . DS . 'Bancha' . DS . 'Config' . DS . 'bootstrap.php'; Configure::write('debug', 1); App::uses('BanchaDispatcher', 'Bancha.Bancha/Routing'); App::uses('BanchaRequestCollection', 'Bancha.Bancha/Network'); App::uses('AppModel', 'Model'); App::uses('Article', 'Model'); // require_once dirname(__FILE__) . '/ArticlesController.php'; if (isset($_SERVER['argv'][1])) { $client_id = $_SERVER['argv'][1]; } if (isset($_SERVER['argv'][2])) { $article_id = $_SERVER['argv'][2]; } if (isset($_SERVER['argv'][3])) { $tid = $_SERVER['argv'][3]; } if (isset($_SERVER['argv'][4])) { $title = $_SERVER['argv'][4]; } if (isset($_SERVER['argv'][5])) { define('SLEEP_TIME', $_SERVER['argv'][5]); } $dispatcher = new BanchaDispatcher(); $rawPostData = json_encode(array(array('action' => 'Articles', 'method' => 'update', 'tid' => $tid, 'type' => 'rpc', 'data' => array(array('data' => array('__bcid' => $client_id, 'id' => $article_id, 'title' => $title, 'published' => true)))))); $responses = json_decode($dispatcher->dispatch(new BanchaRequestCollection($rawPostData), array('return' => true)));
/** * Test the bancha stack, especially the dispatching with a multi-request * */ public function testMultiRequest() { // used fixture: // array('id' => 988, 'title' => 'Title 1', 'published' => true, ...) // Build a request like it looks in Ext JS. $rawPostData = json_encode(array(array('action' => 'Article', 'method' => 'create', 'tid' => 1, 'type' => 'rpc', 'data' => array(array('data' => array('title' => 'Hello World', 'body' => 'foobar', 'published' => false, 'user_id' => 1)))), array('action' => 'Article', 'method' => 'update', 'tid' => 2, 'type' => 'rpc', 'data' => array(array('data' => array('id' => 988, 'title' => 'foobar', 'published' => false)))))); $dispatcher = new BanchaDispatcher(); $responses = json_decode($dispatcher->dispatch(new BanchaRequestCollection($rawPostData), array('return' => true))); // general response checks (check dispatcher, collections and transformers) $this->assertEquals('Article', $responses[0]->action); $this->assertEquals('create', $responses[0]->method); $this->assertEquals('rpc', $responses[0]->type); $this->assertEquals(1, $responses[0]->tid); $this->assertEquals('Article', $responses[1]->action); $this->assertEquals('update', $responses[1]->method); $this->assertEquals('rpc', $responses[1]->type); $this->assertEquals(2, $responses[1]->tid); $this->assertEquals(2, count($responses)); // test data for first request $this->assertNotNull($responses[0]->result->data->id); $this->assertEquals('Hello World', $responses[0]->result->data->title); $this->assertEquals(false, $responses[0]->result->data->published); $this->assertEquals(1, $responses[0]->result->data->user_id); // test data for second request $this->assertEquals(988, $responses[1]->result->data->id); $this->assertEquals('foobar', $responses[1]->result->data->title); $this->assertEquals(0, $responses[1]->result->data->published); $this->assertEquals('Text 1', $responses[1]->result->data->body); // should be a full record }
define('APP_PATH', null); define('CORE_PATH', null); } else {*/ define('APP_PATH', ROOT . DS . APP_DIR . DS); define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS); //} } if (!(include CORE_PATH . 'Cake' . DS . 'bootstrap.php')) { trigger_error("CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR); } // load bootstrap of bancha after cake and app bootstrap is loaded above $failedLoadingBootstrap = true; if (file_exists(ROOT . DS . 'plugins' . DS . 'Bancha' . DS . 'Config' . DS . 'bancha-dispatcher-bootstrap.php')) { // Bancha is in the general plugins folder $failedLoadingBootstrap = !(include ROOT . DS . 'plugins' . DS . 'Bancha' . DS . 'Config' . DS . 'bancha-dispatcher-bootstrap.php'); } else { if (file_exists(APP_PATH . 'Plugin' . DS . 'Bancha' . DS . 'Config' . DS . 'bancha-dispatcher-bootstrap.php')) { // Bancha is in the app Plugin folder $failedLoadingBootstrap = !(include APP_PATH . 'Plugin' . DS . 'Bancha' . DS . 'Config' . DS . 'bancha-dispatcher-bootstrap.php'); } } if ($failedLoadingBootstrap) { trigger_error("Bancha bootstrap could not be loaded. \n\t\tCheck the value of ROOT in APP/webroot/index.php. \n\t\tIt should point to the directory containing your " . DS . " ROOT directory and your " . DS . "vendors root directory.", E_USER_ERROR); } if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') { return; } else { $Dispatcher = new BanchaDispatcher(); $raw_post_data = file_get_contents("php://input"); $Dispatcher->dispatch(new BanchaRequestCollection($raw_post_data ? $raw_post_data : '', isset($_POST) ? $_POST : array())); }