getMessage(); } return null; } function countInserts() { $n = 0; foreach ($GLOBALS['db_calls'] as $call) { if (stripos($call[0], 'INSERT') !== false) $n++; } return $n; } // ---- Part 1: newDevice with no fields ---- $msg = runDevice($root, array('action' => 'device', 'newDevice' => array())); check("empty newDevice does not INSERT", countInserts(), 0); check("empty newDevice reaches ajaxError, not ajaxResponse", $msg !== 'ok' && !is_null($msg), true); // ---- Part 2: newDevice with a Name but no KeyString ---- $msg = runDevice($root, array('action' => 'device', 'newDevice' => array('Name' => 'a'))); check("newDevice without KeyString does not INSERT", countInserts(), 0); check("newDevice without KeyString reaches ajaxError, not ajaxResponse", $msg !== 'ok' && !is_null($msg), true); // ---- Part 3: valid newDevice ---- $msg = runDevice($root, array('action' => 'device', 'newDevice' => array('Name' => 'a', 'KeyString' => 'b'))); check("valid newDevice reaches ajaxResponse", $msg, 'ok'); check("valid newDevice INSERTs once", countInserts(), 1); check("valid newDevice INSERT carries Name and KeyString", isset($GLOBALS['db_calls'][0]) ? $GLOBALS['db_calls'][0][1] : null, array('a', 'b')); echo "\n".($failures ? "FAILURES: $failures\n" : "All passed\n"); exit($failures ? 1 : 0); }