/** * Various tests for OCI. * * @return void */ public function testVaria() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $page = $redbean->dispense("page"); try { $adapter->exec("an invalid query"); fail(); } catch (RedBean_Exception_SQL $e) { pass(); } asrt((int) $adapter->getCell("SELECT 123 FROM DUAL"), 123); $page->aname = "my page"; $id = (int) $redbean->store($page); asrt((int) $page->id, 1); asrt((int) $pdo->GetCell("SELECT count(*) FROM page"), 1); asrt($pdo->GetCell("SELECT aname FROM page WHERE ROWNUM<=1"), "my page"); asrt((int) $id, 1); $page = $redbean->load("page", 1); asrt($page->aname, "my page"); asrt((bool) $page->getMeta("type"), TRUE); asrt(isset($page->id), TRUE); asrt($page->getMeta("type"), "page"); asrt((int) $page->id, $id); R::nuke(); $rooms = R::dispense('room', 2); $rooms[0]->kind = 'suite'; $rooms[1]->kind = 'classic'; $rooms[0]->number = 6; $rooms[1]->number = 7; R::store($rooms[0]); R::store($rooms[1]); $rooms = R::getAssoc('SELECT ' . R::$writer->esc('number') . ', kind FROM room ORDER BY kind ASC'); foreach ($rooms as $key => $room) { asrt($key === 6 || $key === 7, TRUE); asrt($room == 'classic' || $room == 'suite', TRUE); } $rooms = R::$adapter->getAssoc('SELECT kind FROM room'); foreach ($rooms as $key => $room) { asrt($room == 'classic' || $room == 'suite', TRUE); asrt($room, $key); } $rooms = R::getAssoc('SELECT ' . R::$writer->esc('number') . ', kind FROM rooms2 ORDER BY kind ASC'); asrt(count($rooms), 0); asrt(is_array($rooms), TRUE); $date = R::dispense('mydate'); $date->date = '2012-12-12 20:50'; $date->time = '12:15'; $id = R::store($date); $ok = R::load('mydate', 1); }
exit('DB failed' . PHP_EOL); } R::freeze(true); try { $user_email = R::getCell('SELECT email' . ' FROM wxcsc_users' . ' WHERE openid = :openid' . ' LIMIT 1', [':openid' => $user_openid]); } catch (Exception $e) { header('Content-type:text/json;charset=utf-8'); echo json_encode(['result' => 'failed', 'error' => 'db error wechat', 'details' => $e->getMessage()]); die; } R::close(); _log(json_encode(['openid' => $user_openid, 'user_email' => $user_email])); // Query R::addDatabase('kayako', $GLOBALS['db_kayako_url'], $GLOBALS['db_kayako_user'], $GLOBALS['db_kayako_pass']); R::selectDatabase('kayako'); if (!R::testConnection()) { exit('DB failed' . PHP_EOL); } R::freeze(true); try { $user = R::getRow(' SELECT a.fullname AS name, ' . ' c.organizationname AS organization,' . ' a.phone AS phone,' . ' b.email AS email,' . ' d.com_century AS dc_id' . ' FROM kayako_fusion.swusers a' . ' INNER JOIN kayako_fusion.swuseremails b ' . ' ON a.userid = b.linktypeid' . ' INNER JOIN kayako_fusion.swuserorganizations c' . ' ON a.userorganizationid = c.userorganizationid' . ' LEFT JOIN supportsr.order_service_company d' . ' ON d.com_id = c.userorganizationid' . ' WHERE b.email = :email' . ' AND b.linktype = 1', [':email' => $user_email]); $user_dc_id_arr = explode(',', $user['dc_id']); $user_dc = R::getAssoc('SELECT name' . ' FROM supportsr.esr_companylist' . ' WHERE id IN (' . R::genSlots($user_dc_id_arr) . ')', $user_dc_id_arr); } catch (Exception $e) { header('Content-type:text/json;charset=utf-8'); echo json_encode(['result' => 'failed', 'error' => 'db error kayako', 'details' => $e->getMessage()]); die; } header('Content-type:text/json;charset=utf-8'); echo json_encode(['result' => 'true', 'user' => $user, 'user_dc' => $user_dc], JSON_UNESCAPED_UNICODE); die;
/** * Test selecting. * * @return void */ public function testSelects() { $rooms = R::dispense('room', 2); $rooms[0]->kind = 'suite'; $rooms[1]->kind = 'classic'; $rooms[0]->number = 6; $rooms[1]->number = 7; R::store($rooms[0]); R::store($rooms[1]); $rooms = R::getAssoc('SELECT * FROM room WHERE id < -999'); asrt(is_array($rooms), TRUE); asrt(count($rooms), 0); $rooms = R::getAssoc('SELECT ' . R::$writer->esc('number') . ', kind FROM room ORDER BY kind ASC'); foreach ($rooms as $key => $room) { asrt($key === 6 || $key === 7, TRUE); asrt($room == 'classic' || $room == 'suite', TRUE); } $rooms = R::$adapter->getAssoc('SELECT kind FROM room'); foreach ($rooms as $key => $room) { asrt($room == 'classic' || $room == 'suite', TRUE); asrt($room, $key); } $rooms = R::getAssoc('SELECT `number`, kind FROM rooms2 ORDER BY kind ASC'); asrt(count($rooms), 0); asrt(is_array($rooms), TRUE); // GetCell should return NULL in case of exception asrt(NULL, R::getCell('SELECT dream FROM fantasy')); }
/** * Test beautification of column names. * * @return void */ public function testBeautifulColumnNames() { testpack('Beautiful column names'); $town = R::dispense('town'); $town->isCapital = FALSE; $town->hasTrainStation = TRUE; $town->name = 'BeautyVille'; $houses = R::dispense('house', 2); $houses[0]->isForSale = TRUE; $town->ownHouse = $houses; R::store($town); $town = R::load('town', $town->id); asrt($town->isCapital == FALSE, TRUE); asrt($town->hasTrainStation == TRUE, TRUE); asrt($town->name == 'BeautyVille', TRUE); testpack('Accept datetime objects.'); $cal = R::dispense('calendar'); $cal->when = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); asrt($cal->when, '2000-01-01 00:00:00'); testpack('Affected rows test'); $currentDriver = $this->currentlyActiveDriverID; $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $bean = $redbean->dispense('bean'); $bean->prop = 3; //make test run with strict mode as well $redbean->store($bean); $adapter->exec('UPDATE bean SET prop = 2'); asrt($adapter->getAffectedRows(), 1); testpack('Testing Logger'); R::$adapter->getDatabase()->setLogger(new RedBean_Logger_Default()); asrt(R::$adapter->getDatabase()->getLogger() instanceof RedBean_Logger, TRUE); asrt(R::$adapter->getDatabase()->getLogger() instanceof RedBean_Logger_Default, TRUE); $bean = R::dispense('bean'); $bean->property = 1; $bean->unsetAll(array('property')); asrt($bean->property, NULL); asrt($bean->setAttr('property', 2) instanceof RedBean_OODBBean, TRUE); asrt($bean->property, 2); asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d/', R::isoDate()), 1); asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d/', R::isoDateTime()), 1); $redbean = R::getRedBean(); $adapter = R::getDatabaseAdapter(); $writer = R::getWriter(); asrt($redbean instanceof RedBean_OODB, TRUE); asrt($adapter instanceof RedBean_Adapter, TRUE); asrt($writer instanceof RedBean_QueryWriter, TRUE); R::setRedBean($redbean); pass(); //cant really test this R::setDatabaseAdapter($adapter); pass(); //cant really test this R::setWriter($writer); pass(); //cant really test this $u1 = R::dispense('user'); $u1->name = 'Gabor'; $u1->login = '******'; $u2 = R::dispense('user'); $u2->name = 'Eric'; $u2->login = '******'; R::store($u1); R::store($u2); $list = R::getAssoc('select login,' . R::$writer->esc('name') . ' from ' . R::$writer->esc('user') . ' '); asrt($list['e'], 'Eric'); asrt($list['g'], 'Gabor'); $painting = R::dispense('painting'); $painting->name = 'Nighthawks'; $id = R::store($painting); testpack('Testing Plugin Cooker'); $cooker = new RedBean_Plugin_Cooker(); $cooker->setToolbox($toolbox); try { asrt($cooker->graph('abc'), 'abc'); fail(); } catch (RedBean_Exception_Security $e) { pass(); } testpack('Testing SQL Error Types'); foreach ($writer->typeno_sqltype as $code => $text) { asrt(is_integer($code), TRUE); asrt(is_string($text), TRUE); } foreach ($writer->sqltype_typeno as $text => $code) { asrt(is_integer($code), TRUE); asrt(is_string($text), TRUE); } testpack('Testing Nowhere Pt. 1 (unfrozen)'); foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) { R::$method('select * from nowhere'); pass(); } testpack('Testing Nowhere Pt. 2 (frozen)'); R::freeze(TRUE); foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) { try { R::$method('select * from nowhere'); fail(); } catch (RedBean_Exception_SQL $e) { pass(); } } R::freeze(FALSE); }